public static unsafe int Callback(void* input, void* output, int buflen,
            PA19.PaStreamCallbackTimeInfo* timeInfo, int statusFlags, void* userData)
        {
            try
            {
                if (audio_paused || !audio_running)
                    return callback_return;

                audio_run.WaitOne(100);

                if (SDRmode)
                {
            #if(WIN64)
                Int64* array_ptr = (Int64*)input;
                float* in_l_ptr1 = (float*)array_ptr[0];
                float* in_r_ptr1 = (float*)array_ptr[1];
                double* VAC_in = (double*)input;
                array_ptr = (Int64*)output;
                float* out_l_ptr1 = (float*)array_ptr[1];
                float* out_r_ptr1 = (float*)array_ptr[0];
                array_ptr = (Int64*)input;
                in_l_ptr1 = (float*)array_ptr[0];
                in_r_ptr1 = (float*)array_ptr[1];
            #endif

            #if(WIN32)
                    int* array_ptr = (int*)input;
                    float* in_r_ptr1 = (float*)array_ptr[0];
                    float* in_l_ptr1 = (float*)array_ptr[1];
                    array_ptr = (int*)output;
                    float* out_l_ptr1 = (float*)array_ptr[1];
                    float* out_r_ptr1 = (float*)array_ptr[0];
                    array_ptr = (int*)input;
                    in_l_ptr1 = (float*)array_ptr[0];
                    in_r_ptr1 = (float*)array_ptr[1];
            #endif
                    #region MOX

                    if (mox)
                    {
                        if (mox_switch_time >= 5)
                        {
                            Mode new_mode = MainForm.OpModeVFOA;

                            if (MainForm.TXSplit)
                                new_mode = MainForm.OpModeVFOB;

                            switch (new_mode)
                            {
                                case Mode.CW:
                                    {
                                        MainForm.cwEncoder.Exchange_samples(ref buffer_mox, ref monitor_buffer, buflen);

                                        if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                            (rb_monOUT_r.WriteSpace() >= buflen))
                                        {
                                            fixed (float* rptr = &mox_bufferF_l[0])
                                            fixed (float* lptr = &mox_bufferF_r[0])
                                            {
                                                for (int i = 0; i < buflen; i++)
                                                {
                                                    mox_bufferF_l[i] = monitor_buffer[i].Re;
                                                    mox_bufferF_r[i] = monitor_buffer[i].Im;
                                                    CH1_buffer[i] = monitor_buffer[i].Re;
                                                }

                                                EnterCriticalSection(cs_mon);
                                                rb_monOUT_l.WritePtr(lptr, buflen);
                                                rb_monOUT_r.WritePtr(rptr, buflen);
                                                LeaveCriticalSection(cs_mon);
                                            }
                                        }

                                        if (buffer_ptr_A >= 2048)
                                            buffer_ptr_A = 0;

                                        if (!TXswap)
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_r_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                                out_l_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                                CH1_buffer[buffer_ptr_A] = monitor_buffer[i].Re;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                                buffer_ptr_A++;
                                            }
                                        }
                                        else
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_r_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                                out_l_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                                CH1_buffer[buffer_ptr_A] = monitor_buffer[i].Re;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                                buffer_ptr_A++;
                                            }
                                        }

                                        if (buffer_ptr_A == 2048)
                                        {
                                            //Array.Copy(CH1_buffer, MainForm.cwDecoder.fft_buff_ch5, 2048);
                                            //MainForm.cwDecoder.AudioEvent1.Set();
                                            buffer_ptr_A = 0;
                                        }
                                    }
                                    break;

                                case Mode.RTTY:
                                    {
                                        MainForm.rtty.Exchange_samples(0, ref buffer_mox, ref monitor_buffer, buflen);

                                        if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                            (rb_monOUT_r.WriteSpace() >= buflen))
                                        {
                                            fixed (float* rptr = &mox_bufferF_l[0])
                                            fixed (float* lptr = &mox_bufferF_r[0])
                                            {
                                                for (int i = 0; i < buflen; i++)
                                                {
                                                    mox_bufferF_l[i] = monitor_buffer[i].Re;
                                                    mox_bufferF_r[i] = monitor_buffer[i].Im;
                                                }

                                                EnterCriticalSection(cs_mon);
                                                rb_monOUT_l.WritePtr(lptr, buflen);
                                                rb_monOUT_r.WritePtr(rptr, buflen);
                                                LeaveCriticalSection(cs_mon);
                                            }
                                        }

                                        if (buffer_ptr_A >= 2048)
                                            buffer_ptr_A = 0;

                                        if (!TXswap)
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_r_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                                out_l_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                                CH1_buffer[buffer_ptr_A] = monitor_buffer[i].Re;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                                buffer_ptr_A++;
                                            }
                                        }
                                        else
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_l_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                                out_r_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                                CH1_buffer[buffer_ptr_A] = monitor_buffer[i].Re;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                                buffer_ptr_A++;
                                            }
                                        }

                                        if (buffer_ptr_A == 2048)
                                            buffer_ptr_A = 0;
                                    }
                                    break;

                                case Mode.BPSK31:
                                case Mode.BPSK63:
                                case Mode.BPSK125:
                                case Mode.BPSK250:
                                case Mode.QPSK31:
                                case Mode.QPSK63:
                                case Mode.QPSK125:
                                case Mode.QPSK250:
                                    {
                                        MainForm.psk.Exchange_samples(0, ref buffer_mox, ref monitor_buffer, buflen);

                                        if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                            (rb_monOUT_r.WriteSpace() >= buflen))
                                        {
                                            fixed (float* rptr = &mox_bufferF_l[0])
                                            fixed (float* lptr = &mox_bufferF_r[0])
                                            {
                                                for (int i = 0; i < buflen; i++)
                                                {
                                                    mox_bufferF_l[i] = monitor_buffer[i].Re;
                                                    mox_bufferF_r[i] = monitor_buffer[i].Im;
                                                }

                                                EnterCriticalSection(cs_mon);
                                                rb_monOUT_l.WritePtr(lptr, buflen);
                                                rb_monOUT_r.WritePtr(rptr, buflen);
                                                LeaveCriticalSection(cs_mon);
                                            }
                                        }

                                        if (buffer_ptr_A >= 2048)
                                            buffer_ptr_A = 0;

                                        if (!TXswap)
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_r_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                                out_l_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                                CH1_buffer[buffer_ptr_A] = monitor_buffer[i].Re;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                                buffer_ptr_A++;
                                            }
                                        }
                                        else
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_r_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                                out_l_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                                CH1_buffer[buffer_ptr_A] = monitor_buffer[i].Re;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                                buffer_ptr_A++;
                                            }
                                        }

                                        if (buffer_ptr_A == 2048)
                                            buffer_ptr_A = 0;
                                    }
                                    break;
                            }
                        }
                        else
                        {
                            mox_switch_time++;

                            switch (MainForm.OpModeVFOA)
                            {
                                case Mode.CW:
                                    {

                                        for (int i = 0; i < buflen; i++)
                                        {
                                            out_r_ptr1[0] = 0.0f;
                                            out_l_ptr1[0] = 0.0f;
                                            out_l_ptr1++;
                                            out_r_ptr1++;
                                        }

                                        //Array.Copy(zero_bufferF, MainForm.cwDecoder.fft_buff_ch5, 2048);
                                        //Array.Copy(zero_bufferF, MainForm.cwDecoder.fft_buff_ch6, 2048);
                                        //MainForm.cwDecoder.AudioEvent1.Set();
                                    }
                                    break;

                                case Mode.RTTY:
                                    {
                                        for (int i = 0; i < buflen; i++)
                                        {
                                            out_r_ptr1[0] = 0.0f;
                                            out_l_ptr1[0] = 0.0f;
                                            out_l_ptr1++;
                                            out_r_ptr1++;
                                        }

                                        Array.Copy(zero_bufferF, MainForm.rtty.ch1_buffer, 2048);
                                        Array.Copy(zero_bufferF, MainForm.rtty.ch2_buffer, 2048);
                                        MainForm.rtty.AudioEventRX1.Set();
                                    }
                                    break;

                                case Mode.BPSK31:
                                case Mode.BPSK63:
                                case Mode.BPSK125:
                                case Mode.BPSK250:
                                case Mode.QPSK31:
                                case Mode.QPSK63:
                                case Mode.QPSK125:
                                case Mode.QPSK250:
                                    {
                                        for (int i = 0; i < buflen; i++)
                                        {
                                            out_r_ptr1[0] = 0.0f;
                                            out_l_ptr1[0] = 0.0f;
                                            out_l_ptr1++;
                                            out_r_ptr1++;
                                        }

                                        Array.Copy(zero_bufferF, MainForm.psk.ch1_buffer, 2048);
                                        Array.Copy(zero_bufferF, MainForm.psk.ch2_buffer, 2048);
                                        MainForm.psk.AudioEvent1.Set();
                                    }
                                    break;
                            }

                            if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                (rb_monOUT_r.WriteSpace() >= buflen))
                            {
                                fixed (float* rptr = &mox_bufferF_l[0])
                                fixed (float* lptr = &mox_bufferF_r[0])
                                {
                                    for (int i = 0; i < buflen; i++)
                                    {
                                        mox_bufferF_l[i] = 0.0f;
                                        mox_bufferF_r[i] = 0.0f;
                                    }

                                    EnterCriticalSection(cs_mon);
                                    rb_monOUT_l.WritePtr(lptr, buflen);
                                    rb_monOUT_r.WritePtr(rptr, buflen);
                                    LeaveCriticalSection(cs_mon);
                                }
                            }
                        }
                    }

                    #endregion

                    #region RX

                    else
                    {
                        float* in_l = null, in_r = null, out_l = null, out_r = null;
                        out_l = out_l_ptr1;
                        out_r = out_r_ptr1;

                        if (!RXswap)
                        {
                            in_l = in_l_ptr1;
                            in_r = in_r_ptr1;
                        }
                        else
                        {
                            in_l = in_r_ptr1;
                            in_r = in_l_ptr1;
                        }

                        if (record)
                        {
                            if (rb_mon_l.WriteSpace() < 2 * buflen)
                            {
                                MainForm.recorder.Recording = false;
                                record = false;
                            }

                            rb_mon_l.WritePtr(in_l, buflen, true);
                            rb_mon_r.WritePtr(in_r, buflen, true);
                            rec_progress++;

                            if (rec_progress >= 50)
                            {
                                MainForm.recorder.Invoke(new CrossThreadCommand(MainForm.recorder.CommandCallback),
                                    "Set Recording progress", rb_mon_l.wptr);
                                rec_progress = 0;
                            }
                        }

                        if (loopback)
                        {
                            if (rb_mon_l.ReadSpace() < buflen)
                            {
                                rb_mon_l.ResetReadPtr();
                                rb_mon_r.ResetReadPtr();
                            }

                            if (!RXswap)
                            {
                                rb_mon_l.ReadPtr(in_l, buflen);
                                rb_mon_r.ReadPtr(in_r, buflen);
                            }
                            else
                            {
                                rb_mon_r.ReadPtr(in_l, buflen);
                                rb_mon_l.ReadPtr(in_r, buflen);
                            }

                            play_progress++;

                            if (play_progress >= 30)
                            {
                                MainForm.recorder.Invoke(new CrossThreadCommand(MainForm.recorder.CommandCallback),
                                    "Set Play progress", rb_mon_l.rptr);
                                play_progress = 0;
                            }
                        }

                        switch (MainForm.IQcorrection)
                        {
                            case IQ_correction.WBIR:
                            case IQ_correction.FIXED:
                                {
                                    if (iq_progress >= 50)
                                    {
                                        for (int i = 0; i < 2048; i++)
                                        {
                                            iq_buffer[i].Re = in_l[i];
                                            iq_buffer[i].Im = in_r[i];
                                        }

                                        for (int i = 0; i < 2048; i++)
                                        {
                                            in_l[i] = iq_buffer[i].Re;
                                            in_r[i] = iq_buffer[i].Im;
                                        }
                                    }
                                    else
                                        iq_progress++;
                                }
                                break;
                        }

                        fixed (float* output_l = &tmp_buffer_ch1[0])
                        fixed (float* output_r = &tmp_buffer_ch2[0])
                            ExchangeSamples(0, in_l, in_r, output_l, output_r, buflen);

                        if (RX2)
                        {
                            fixed (float* output_l = &tmp_buffer_ch3[0])
                            fixed (float* output_r = &tmp_buffer_ch4[0])
                            {
                                ExchangeSamplesSubRX(0, 0, output_l, output_r, buflen);
                            }
                        }

                        if (buffer_ptr_A >= 2048 || buffer_ptr_B >= 2048)
                        {
                            buffer_ptr_A = 0;
                            buffer_ptr_B = 0;
                        }

                        for (int i = 0; i < buflen; i++)
                        {
                            CH1_buffer[buffer_ptr_A] = tmp_buffer_ch1[i];
                            CH2_buffer[buffer_ptr_A] = tmp_buffer_ch2[i];
                            CH3_buffer[buffer_ptr_B] = tmp_buffer_ch3[i];
                            CH4_buffer[buffer_ptr_B] = tmp_buffer_ch4[i];
                            buffer_ptr_A++;
                            buffer_ptr_B++;
                        }

                        if (!monitor_paused)
                        {
                            switch (MainForm.OpModeVFOA)
                            {
                                case Mode.CW:
                                    {
                                        if (mox_switch_time >= 5)
                                        {
                                            if (buffer_ptr_A >= 2048)
                                            {
                                                Array.Copy(CH1_buffer, MainForm.cwDecoder.fft_buff_ch5, 2048);
                                                MainForm.cwDecoder.AudioEvent1.Set();
                                                buffer_ptr_A = 0;
                                            }

                                        }
                                        else
                                        {
                                            mox_switch_time++;

                                            if (buffer_ptr_A >= 2048)
                                            {
                                                Array.Copy(zero_bufferF, MainForm.cwDecoder.fft_buff_ch5, 2048);       // mute
                                                MainForm.cwDecoder.AudioEvent1.Set();
            #if(WIN32)
                                                array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                array_ptr = (Int64*)output;
            #endif
                                                out_l_ptr1 = (float*)array_ptr[1];
                                                out_r_ptr1 = (float*)array_ptr[0];
                                                buffer_ptr_A = 0;
                                            }
                                        }
                                    }
                                    break;

                                case Mode.RTTY:
                                    {
                                        if (MainForm.rtty.run_thread)
                                        {
                                            if (mox_switch_time >= 5)
                                            {
                                                if (buffer_ptr_A >= 2048)
                                                {
                                                    Array.Copy(CH1_buffer, MainForm.rtty.ch1_buffer, 2048);
                                                    Array.Copy(CH2_buffer, MainForm.rtty.ch2_buffer, 2048);
                                                    MainForm.rtty.AudioEventRX1.Set();
                                                    buffer_ptr_A = 0;
                                                }
                                            }
                                            else
                                            {
                                                mox_switch_time++;

                                                if (buffer_ptr_A >= 2048)
                                                {
                                                    Array.Copy(zero_bufferF, MainForm.rtty.ch1_buffer, 2048);
                                                    Array.Copy(zero_bufferF, MainForm.rtty.ch2_buffer, 2048);
                                                    MainForm.rtty.AudioEventRX1.Set();

            #if(WIN32)
                                                    array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                    array_ptr = (Int64*)output;
            #endif

                                                    out_l_ptr1 = (float*)array_ptr[1];
                                                    out_r_ptr1 = (float*)array_ptr[0];
                                                    buffer_ptr_A = 0;
                                                }
                                            }
                                        }
                                    }
                                    break;

                                case Mode.BPSK31:
                                case Mode.BPSK63:
                                case Mode.BPSK125:
                                case Mode.BPSK250:
                                case Mode.QPSK31:
                                case Mode.QPSK63:
                                case Mode.QPSK125:
                                case Mode.QPSK250:
                                    {
                                        if (MainForm.psk.run_thread)
                                        {
                                            if (mox_switch_time >= 5)
                                            {
                                                if (buffer_ptr_A >= 2048)
                                                {
                                                    Array.Copy(CH1_buffer, MainForm.psk.ch1_buffer, 2048);
                                                    MainForm.psk.AudioEvent1.Set();
                                                    buffer_ptr_A = 0;
                                                }
                                            }
                                            else
                                            {
                                                mox_switch_time++;

                                                if (buffer_ptr_A >= 2048)
                                                {
                                                    Array.Copy(zero_bufferF, MainForm.psk.ch1_buffer, 2048);    // mute
                                                    MainForm.psk.AudioEvent1.Set();

            #if(WIN32)
                                                    array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                    array_ptr = (Int64*)output;
            #endif

                                                    out_l_ptr1 = (float*)array_ptr[1];
                                                    out_r_ptr1 = (float*)array_ptr[0];
                                                    buffer_ptr_A = 0;
                                                }
                                            }
                                        }
                                    }
                                    break;
                            }

                            if (RX2)
                            {
                                switch (MainForm.OpModeVFOB)
                                {
                                    case Mode.CW:
                                        {
                                            if (mox_switch_time >= 5)
                                            {
                                                if (buffer_ptr_B >= 2048)
                                                {
                                                    switch (MainForm.OpModeVFOA)
                                                    {
                                                        case Mode.CW:
                                                        case Mode.BPSK31:
                                                        case Mode.BPSK63:
                                                        case Mode.BPSK125:
                                                        case Mode.BPSK250:
                                                        case Mode.QPSK31:
                                                        case Mode.QPSK63:
                                                        case Mode.QPSK125:
                                                        case Mode.QPSK250:
                                                            Array.Copy(CH2_buffer, MainForm.cwDecoder.fft_buff_ch6, 2048);
                                                            break;

                                                        case Mode.RTTY:
                                                            Array.Copy(CH3_buffer, MainForm.cwDecoder.fft_buff_ch6, 2048);
                                                            break;
                                                    }

                                                    MainForm.cwDecoder.AudioEvent2.Set();
                                                    buffer_ptr_B = 0;
                                                }

                                            }
                                            else
                                            {
                                                mox_switch_time++;

                                                if (buffer_ptr_B >= 2048)
                                                {
                                                    Array.Copy(zero_bufferF, MainForm.cwDecoder.fft_buff_ch6, 2048);    // mute
                                                    MainForm.cwDecoder.AudioEvent2.Set();

            #if(WIN32)
                                                    array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                    array_ptr = (Int64*)output;
            #endif

                                                    out_l_ptr1 = (float*)array_ptr[1];
                                                    out_r_ptr1 = (float*)array_ptr[0];
                                                    buffer_ptr_B = 0;
                                                }
                                            }
                                        }
                                        break;

                                    case Mode.RTTY:
                                        {
                                            if (MainForm.rtty.run_thread)
                                            {
                                                if (mox_switch_time >= 5)
                                                {
                                                    if (buffer_ptr_B >= 2048)
                                                    {

                                                        Array.Copy(CH3_buffer, MainForm.rtty.ch3_buffer, 2048);
                                                        Array.Copy(CH4_buffer, MainForm.rtty.ch4_buffer, 2048);
                                                        MainForm.rtty.AudioEventRX2.Set();
                                                        buffer_ptr_B = 0;
                                                    }
                                                }
                                                else
                                                {
                                                    mox_switch_time++;

                                                    if (buffer_ptr_B >= 2048)
                                                    {
                                                        Array.Copy(zero_bufferF, MainForm.rtty.ch3_buffer, 2048);
                                                        Array.Copy(zero_bufferF, MainForm.rtty.ch4_buffer, 2048);
                                                        MainForm.rtty.AudioEventRX2.Set();

            #if(WIN32)
                                                        array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                        array_ptr = (Int64*)output;
            #endif

                                                        out_l_ptr1 = (float*)array_ptr[1];
                                                        out_r_ptr1 = (float*)array_ptr[0];
                                                        buffer_ptr_B = 0;
                                                    }
                                                }
                                            }
                                        }
                                        break;

                                    case Mode.BPSK31:
                                    case Mode.BPSK63:
                                    case Mode.BPSK125:
                                    case Mode.BPSK250:
                                    case Mode.QPSK31:
                                    case Mode.QPSK63:
                                    case Mode.QPSK125:
                                    case Mode.QPSK250:
                                        {
                                            if (MainForm.psk.run_thread)
                                            {
                                                if (mox_switch_time >= 5)
                                                {
                                                    if (buffer_ptr_B >= 2048)
                                                    {
                                                        switch (MainForm.OpModeVFOA)
                                                        {
                                                            case Mode.CW:
                                                            case Mode.BPSK31:
                                                            case Mode.BPSK63:
                                                            case Mode.BPSK125:
                                                            case Mode.BPSK250:
                                                            case Mode.QPSK31:
                                                            case Mode.QPSK63:
                                                            case Mode.QPSK125:
                                                            case Mode.QPSK250:
                                                                Array.Copy(CH2_buffer, MainForm.psk.ch2_buffer, 2048);
                                                                break;

                                                            case Mode.RTTY:
                                                                Array.Copy(CH3_buffer, MainForm.psk.ch2_buffer, 2048);
                                                                break;
                                                        }

                                                        MainForm.psk.AudioEvent2.Set();
                                                        buffer_ptr_B = 0;
                                                    }
                                                }
                                                else
                                                {
                                                    mox_switch_time++;

                                                    if (buffer_ptr_B >= 2048)
                                                    {
                                                        Array.Copy(zero_bufferF, MainForm.psk.ch2_buffer, 2048);    // mute
                                                        MainForm.psk.AudioEvent2.Set();

            #if(WIN32)
                                                        array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                        array_ptr = (Int64*)output;
            #endif

                                                        out_l_ptr1 = (float*)array_ptr[1];
                                                        out_r_ptr1 = (float*)array_ptr[0];
                                                        buffer_ptr_B = 0;
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                }
                            }
                        }

                        if (channel == 5)
                        {
                            switch (MainForm.OpModeVFOA)
                            {
                                case Mode.RTTY:
                                    {
                                        if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                            (rb_monOUT_r.WriteSpace() >= buflen))
                                        {
                                            fixed (float* output_l = &tmp_buffer_ch1[0])
                                            fixed (float* output_r = &tmp_buffer_ch2[0])
                                            {
                                                EnterCriticalSection(cs_mon);
                                                rb_monOUT_l.WritePtr(output_l, buflen);
                                                rb_monOUT_r.WritePtr(output_r, buflen);
                                                LeaveCriticalSection(cs_mon);
                                            }
                                        }

                                        if (mox_switch_time >= 5)
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_l_ptr1[0] = tmp_buffer_ch1[i] * (float)volume;
                                                out_r_ptr1[0] = tmp_buffer_ch2[i] * (float)volume;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                            }
                                        }
                                        else
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_l_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                out_r_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                            }
                                        }
                                    }
                                    break;

                                default:
                                    {
                                        if (monitor_enabled)
                                        {
                                            if (rb_monOUT_l.WriteSpace() >= buflen &&
                                            rb_monOUT_r.WriteSpace() >= buflen)
                                            {
                                                fixed (float* output_l = &tmp_buffer_ch1[0])
                                                {
                                                    EnterCriticalSection(cs_mon);
                                                    rb_monOUT_l.WritePtr(output_l, buflen);
                                                    rb_monOUT_r.WritePtr(output_l, buflen);
                                                    LeaveCriticalSection(cs_mon);
                                                }
                                            }
                                            else
                                            {
                                                rb_monOUT_l.Reset();
                                                rb_monOUT_r.Reset();
                                            }
                                        }

                                        if (mox_switch_time >= 5)
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_l_ptr1[0] = tmp_buffer_ch1[i] * (float)volume;
                                                out_r_ptr1[0] = tmp_buffer_ch1[i] * (float)volume;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                            }
                                        }
                                        else
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_l_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                out_r_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                            }
                                        }
                                    }
                                    break;
                            }
                        }
                        else if (channel == 6)
                        {
                            switch (MainForm.OpModeVFOA)
                            {
                                case Mode.RTTY:
                                    {
                                        switch (MainForm.OpModeVFOB)
                                        {
                                            case Mode.RTTY:
                                                {
                                                    if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                                        (rb_monOUT_r.WriteSpace() >= buflen))
                                                    {
                                                        fixed (float* output_l = &tmp_buffer_ch3[0])
                                                        fixed (float* output_r = &tmp_buffer_ch4[0])
                                                        {
                                                            EnterCriticalSection(cs_mon);
                                                            rb_monOUT_l.WritePtr(output_l, buflen);
                                                            rb_monOUT_r.WritePtr(output_r, buflen);
                                                            LeaveCriticalSection(cs_mon);
                                                        }
                                                    }

                                                    if (mox_switch_time >= 5)
                                                    {
                                                        for (int i = 0; i < buflen; i++)
                                                        {
                                                            out_l_ptr1[0] = tmp_buffer_ch3[i] * (float)volume;
                                                            out_r_ptr1[0] = tmp_buffer_ch4[i] * (float)volume;
                                                            out_l_ptr1++;
                                                            out_r_ptr1++;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        for (int i = 0; i < buflen; i++)
                                                        {
                                                            out_l_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                            out_r_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                            out_l_ptr1++;
                                                            out_r_ptr1++;
                                                        }
                                                    }
                                                }
                                                break;

                                            default:
                                                {
                                                    if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                                        (rb_monOUT_r.WriteSpace() >= buflen))
                                                    {
                                                        fixed (float* output_l = &tmp_buffer_ch3[0])
                                                        {
                                                            EnterCriticalSection(cs_mon);
                                                            rb_monOUT_l.WritePtr(output_l, buflen);
                                                            rb_monOUT_r.WritePtr(output_l, buflen);
                                                            LeaveCriticalSection(cs_mon);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        rb_monOUT_l.Reset();
                                                        rb_monOUT_r.Reset();
                                                    }

                                                    if (mox_switch_time >= 5)
                                                    {
                                                        for (int i = 0; i < buflen; i++)
                                                        {
                                                            out_l_ptr1[0] = tmp_buffer_ch3[i] * (float)volume;
                                                            out_r_ptr1[0] = tmp_buffer_ch3[i] * (float)volume;
                                                            out_l_ptr1++;
                                                            out_r_ptr1++;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        for (int i = 0; i < buflen; i++)
                                                        {
                                                            out_l_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                            out_r_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                            out_l_ptr1++;
                                                            out_r_ptr1++;
                                                        }
                                                    }
                                                }
                                                break;
                                        }
                                    }
                                    break;

                                default:
                                    {
                                        switch (MainForm.OpModeVFOA)
                                        {
                                            case Mode.RTTY:
                                                {
                                                    if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                                        (rb_monOUT_r.WriteSpace() >= buflen))
                                                    {
                                                        fixed (float* output_l = &tmp_buffer_ch3[0])
                                                        fixed (float* output_r = &tmp_buffer_ch4[0])
                                                        {
                                                            EnterCriticalSection(cs_mon);
                                                            rb_monOUT_l.WritePtr(output_l, buflen);
                                                            rb_monOUT_r.WritePtr(output_r, buflen);
                                                            LeaveCriticalSection(cs_mon);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        rb_monOUT_l.Reset();
                                                        rb_monOUT_r.Reset();
                                                    }

                                                    if (mox_switch_time >= 5)
                                                    {
                                                        for (int i = 0; i < buflen; i++)
                                                        {
                                                            out_l_ptr1[0] = tmp_buffer_ch3[i] * (float)volume;
                                                            out_r_ptr1[0] = tmp_buffer_ch4[i] * (float)volume;
                                                            out_l_ptr1++;
                                                            out_r_ptr1++;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        for (int i = 0; i < buflen; i++)
                                                        {
                                                            out_l_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                            out_r_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                            out_l_ptr1++;
                                                            out_r_ptr1++;
                                                        }
                                                    }
                                                }
                                                break;

                                            default:
                                                {
                                                    switch (MainForm.OpModeVFOB)
                                                    {
                                                        case Mode.RTTY:
                                                            {
                                                                if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                                                    (rb_monOUT_r.WriteSpace() >= buflen))
                                                                {
                                                                    fixed (float* output_l = &tmp_buffer_ch3[0])
                                                                    fixed (float* output_r = &tmp_buffer_ch4[0])
                                                                    {
                                                                        EnterCriticalSection(cs_mon);
                                                                        rb_monOUT_l.WritePtr(output_l, buflen);
                                                                        rb_monOUT_r.WritePtr(output_r, buflen);
                                                                        LeaveCriticalSection(cs_mon);
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    rb_monOUT_l.Reset();
                                                                    rb_monOUT_r.Reset();
                                                                }

                                                                if (mox_switch_time >= 5)
                                                                {
                                                                    for (int i = 0; i < buflen; i++)
                                                                    {
                                                                        out_l_ptr1[0] = tmp_buffer_ch3[i] * (float)volume;
                                                                        out_r_ptr1[0] = tmp_buffer_ch4[i] * (float)volume;
                                                                        out_l_ptr1++;
                                                                        out_r_ptr1++;
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    for (int i = 0; i < buflen; i++)
                                                                    {
                                                                        out_l_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                                        out_r_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                                        out_l_ptr1++;
                                                                        out_r_ptr1++;
                                                                    }
                                                                }
                                                            }
                                                            break;

                                                        default:
                                                            {
                                                                if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                                                    (rb_monOUT_r.WriteSpace() >= buflen))
                                                                {
                                                                    fixed (float* output_l = &tmp_buffer_ch2[0])
                                                                    {
                                                                        EnterCriticalSection(cs_mon);
                                                                        rb_monOUT_l.WritePtr(output_l, buflen);
                                                                        rb_monOUT_r.WritePtr(output_l, buflen);
                                                                        LeaveCriticalSection(cs_mon);
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    rb_monOUT_l.Reset();
                                                                    rb_monOUT_r.Reset();
                                                                }

                                                                if (mox_switch_time >= 5)
                                                                {
                                                                    for (int i = 0; i < buflen; i++)
                                                                    {
                                                                        out_l_ptr1[0] = tmp_buffer_ch2[i] * (float)volume;
                                                                        out_r_ptr1[0] = tmp_buffer_ch2[i] * (float)volume;
                                                                        out_l_ptr1++;
                                                                        out_r_ptr1++;
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    for (int i = 0; i < buflen; i++)
                                                                    {
                                                                        out_l_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                                        out_r_ptr1[0] = zero_bufferF[i] * (float)volume;
                                                                        out_l_ptr1++;
                                                                        out_r_ptr1++;
                                                                    }
                                                                }
                                                            }
                                                            break;
                                                    }
                                                }
                                                break;
                                        }
                                    }
                                    break;
                            }
                        }
                    }

                    #endregion
                }

                #region Morse runner

                else
                {                                           // Morse runner
            #if(WIN32)
                    int* array_ptr = (int*)input;
                    float* in_ptr_l = (float*)array_ptr[0];
                    float* in_ptr_r = (float*)array_ptr[1];

                    int* out_array_ptr = (int*)output;
                    float* out_l_ptr1 = (float*)out_array_ptr[0];
                    float* out_r_ptr1 = (float*)out_array_ptr[1];
            #endif

            #if(WIN64)
                    Int64* array_ptr = (Int64*)input;
                    float* in_ptr_l = (float*)array_ptr[0];
                    float* in_ptr_r = (float*)array_ptr[1];
                    ushort[] buffer_l = new ushort[buflen];
                    ushort[] buffer_r = new ushort[buflen];

                    Int64* out_array_ptr = (Int64*)output;
                    float* out_l_ptr1 = (float*)out_array_ptr[0];
                    float* out_r_ptr1 = (float*)out_array_ptr[1];
            #endif
                    fixed (float* output_l = &tmp_buffer_ch1[0])
                    fixed (float* output_r = &tmp_buffer_ch2[0])
                        ExchangeSamples(0, in_ptr_l, in_ptr_r, output_l, output_r, buflen);

                    for (int i = 0; i < buflen; i++)
                    {
                        tmp_buffer[i] = in_ptr_l[i];
                        out_l_ptr1[i] = (float)(in_ptr_l[i] * volume);
                        out_r_ptr1[i] = (float)(in_ptr_r[i] * volume);
                        in_ptr_l[i] *= (float)input_level;
                        in_ptr_r[i] *= (float)input_level;
                    }

                    for (int i = 0; i < buflen; i++)
                    {
                        if (wptr >= 2048)
                        {
                            if (MainForm.cwDecoder.audio_buffer != null &&
                                MainForm.cwDecoder.audio_buffer.Length == 2048)
                            {
                                Array.Copy(MRbuffer, MainForm.cwDecoder.audio_buffer, 2048);
                                MainForm.cwDecoder.AudioEvent1.Set();

                            }

                            wptr = 0;
                        }
                        else
                        {
                            MRbuffer[wptr] = in_ptr_r[i];
                            i += decimation - 1;
                            wptr++;
                        }
                    }
                }

                #endregion

                #region Scope

                if ((MainForm.DisplayMode == DisplayMode.PANASCOPE || MainForm.DisplayMode == DisplayMode.PANASCOPE_INV ||
                    MainForm.MonitorMode == DisplayMode.SCOPE) && !pause_scope && buffer_ptr_A == 0)
                {
                    Mode new_mode = MainForm.OpModeVFOA;

                    if (channel == 6)
                        new_mode = MainForm.OpModeVFOB;

                    switch (new_mode)
                    {
                        case Mode.RTTY:
                            if (channel == 5)
                            {
                                DoScopeSpace(CH1_buffer, 2048);
                                DoScopeMark(CH2_buffer, 2048);
                            }
                            else if (channel == 6)
                            {
                                DoScopeSpace(CH3_buffer, 2048);
                                DoScopeMark(CH4_buffer, 2048);
                            }
                            break;

                        default:
                            {
                                if (channel == 5)
                                {
                                    DoScope(CH1_buffer, 2048);
                                }
                                else if (channel == 6)
                                {
                                    DoScope(CH2_buffer, 2048);
                                }
                            }
                            break;
                    }
                }

                #endregion

                audio_run.ReleaseMutex();

                return callback_return;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
                return 0;
            }
        }
        public static unsafe bool StartAudio(ref PA19.PaStreamCallback callback,
            uint block_size, double sample_rate, int host_api_index, int input_dev_index,
            int output_dev_index, int num_channels, int callback_num, int latency_ms)
        {
            try
            {
                int in_dev = PA19.PA_HostApiDeviceIndexToDeviceIndex(host_api_index, input_dev_index);
                int out_dev = PA19.PA_HostApiDeviceIndexToDeviceIndex(host_api_index, output_dev_index);

                PA19.PaStreamParameters inparam = new PA19.PaStreamParameters();
                PA19.PaStreamParameters outparam = new PA19.PaStreamParameters();

                inparam.device = in_dev;
                inparam.channelCount = num_channels;

                    inparam.sampleFormat = PA19.paFloat32 | PA19.paNonInterleaved;
                    inparam.suggestedLatency = ((float)latency_ms / 1000);
                    outparam.device = out_dev;
                    outparam.channelCount = num_channels;
                    outparam.sampleFormat = PA19.paFloat32 | PA19.paNonInterleaved;
                    outparam.suggestedLatency = ((float)latency_ms / 1000);

                    if (host_api_index == PA19.PA_HostApiTypeIdToHostApiIndex(PA19.PaHostApiTypeId.paWASAPI))
                    {
                        PA19.PaWasapiStreamInfo stream_info = new PA19.PaWasapiStreamInfo();
                        stream_info.hostApiType = PA19.PaHostApiTypeId.paWASAPI;
                        stream_info.version = 1;
                        stream_info.size = (UInt32)sizeof(PA19.PaWasapiStreamInfo);
                        inparam.hostApiSpecificStreamInfo = &stream_info;
                        outparam.hostApiSpecificStreamInfo = &stream_info;

                        if (QSK && callback_num != 2)
                        {
                            stream_info.flags = (UInt32)PA19.PaWasapiFlags.paWinWasapiExclusive |
                                PA19.AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
                        }
                    }

                int error = 0;

                if (SDRmode)
                {
                    if (QSK)
                    {
                        if (callback_num == 0)
                            error = PA19.PA_OpenStream(out stream1, &inparam, null, sample_rate, block_size, 0, callback, 0, 0);
                        else if (callback_num == 1)
                            error = PA19.PA_OpenStream(out stream2, null, &outparam, sample_rate, block_size, 0, callback, 0, 1);
                        else if (callback_num == 2)
                            error = PA19.PA_OpenStream(out stream3, null, &outparam, sample_rate, block_size, 0, callback, 0, 2);
                    }
                    else
                    {
                        if (callback_num == 0)
                            error = PA19.PA_OpenStream(out stream1, &inparam, &outparam, sample_rate, block_size, 0, callback, 0, 0);
                        else if (callback_num == 2)
                            error = PA19.PA_OpenStream(out stream3, null, &outparam, sample_rate, block_size, 0, callback, 0, 2);
                    }

                    if (error != 0)
                    {
                        MessageBox.Show(PA19.PA_GetErrorText(error), "PortAudio Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return false;
                    }
                }
                else
                {                                           // MorseRunner mode
                    error = 0;

                    if (callback_num == 0)
                        error = PA19.PA_OpenStream(out stream1, &inparam, &outparam, sample_rate, block_size, 0, callback, 0, 0);
                    else if (callback_num == 2)
                        error = PA19.PA_OpenStream(out stream3, null, &outparam, sample_rate, block_size, 0, callback, 0, 2);
                }

                if (callback_num == 0)
                    error = PA19.PA_StartStream(stream1);
                else if (callback_num == 1)
                    error = PA19.PA_StartStream(stream2);
                else if (callback_num == 2)
                    error = PA19.PA_StartStream(stream3);

                if (error != 0)
                {
                    MessageBox.Show(PA19.PA_GetErrorText(error), "PortAudio Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in StartAudio function!\n" + ex.ToString());
                return false;
            }
        }
        public static unsafe int MonitorCallback(void* input, void* output, int buflen,
    PA19.PaStreamCallbackTimeInfo* timeInfo, int statusFlags, void* userData)
        {
            try
            {
            #if(WIN64)
                Int64* array_ptr = (Int64*)input;
                float* in_l_ptr1 = (float*)array_ptr[0];
                float* in_r_ptr1 = (float*)array_ptr[1];
                double* VAC_in = (double*)input;
                array_ptr = (Int64*)output;
                float* out_l_ptr1 = (float*)array_ptr[1];
                float* out_r_ptr1 = (float*)array_ptr[0];
                array_ptr = (Int64*)input;
                in_l_ptr1 = (float*)array_ptr[0];
                in_r_ptr1 = (float*)array_ptr[1];
            #endif

            #if(WIN32)
                int* array_ptr = (int*)output;
                float* out_l_ptr1 = (float*)array_ptr[1];
                float* out_r_ptr1 = (float*)array_ptr[0];
            #endif

                if ((rb_monOUT_l.ReadSpace() >= buflen) && (rb_monOUT_r.ReadSpace() >= buflen))
                {
                    EnterCriticalSection(cs_mon);
                    rb_monOUT_l.ReadPtr(out_l_ptr1, buflen);
                    rb_monOUT_r.ReadPtr(out_r_ptr1, buflen);
                    LeaveCriticalSection(cs_mon);
                }

                for (int i = 0; i < buflen; i++)
                {
                    out_l_ptr1[i] *= (float)volume;
                    out_r_ptr1[i] *= (float)volume;
                }

                return callback_return;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
                return 0;
            }
        }
        public static unsafe int OutputCallback(void* input, void* output, int buflen,
            PA19.PaStreamCallbackTimeInfo* timeInfo, int statusFlags, void* userData)
        {
            try
            {
                if (audio_paused)
                    return callback_return;
            #if(WIN64)
                Int64* array_ptr = (Int64*)output;
                float* out_l_ptr1 = (float*)array_ptr[1];
                float* out_r_ptr1 = (float*)array_ptr[0];
            #endif

            #if(WIN32)
                int* array_ptr = (int*)output;
                float* out_l_ptr1 = (float*)array_ptr[1];
                float* out_r_ptr1 = (float*)array_ptr[0];
            #endif

                if (SDRmode)
                {
                    #region MOX

                    if (mox)
                    {
                        switch (MainForm.OpModeVFOA)
                        {
                            case Mode.CW:
                                {
                                    /*if (mox_switch_time < 1)
                                    {
                                        mox_switch_time++;

                                        for (int i = 0; i < buflen; i++)
                                        {
                                            out_r_ptr1[0] = zero_buffer[i].Re;
                                            out_l_ptr1[0] = zero_buffer[i].Im;
                                            out_l_ptr1++;
                                            out_r_ptr1++;
                                        }

                                        Array.Copy(zero_bufferF, MainForm.cwDecoder.fft_buff_ch5, buflen);
                                        Array.Copy(zero_bufferF, MainForm.cwDecoder.fft_buff_ch6, buflen);
                                        MainForm.cwDecoder.AudioEvent.Set();

                                        if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                            (rb_monOUT_r.WriteSpace() >= buflen))
                                        {
                                            fixed (float* rptr = &mox_bufferF_l[0])
                                            fixed (float* lptr = &mox_bufferF_r[0])
                                            {
                                                for (int i = 0; i < buflen; i++)
                                                {
                                                    mox_bufferF_l[i] = 0.0f;
                                                    mox_bufferF_r[i] = 0.0f;
                                                }

                                                EnterCriticalSection(cs_mon);
                                                rb_monOUT_l.WritePtr(lptr, buflen);
                                                rb_monOUT_r.WritePtr(rptr, buflen);
                                                LeaveCriticalSection(cs_mon);
                                            }
                                        }
                                    }
                                    else*/
                                    {
                                        MainForm.cwEncoder.Exchange_samples(ref buffer_mox, ref monitor_buffer, buflen);

                                        if (!TXswap)
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_r_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                                out_l_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                                CH1_buffer[buffer_ptr_A] = monitor_buffer[i].Re;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                                buffer_ptr_A++;
                                            }
                                        }
                                        else
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                out_r_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                                out_l_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                                CH1_buffer[buffer_ptr_A] = monitor_buffer[i].Re;
                                                out_l_ptr1++;
                                                out_r_ptr1++;
                                                buffer_ptr_A++;
                                            }
                                        }

                                        if (buffer_ptr_A == 2048)
                                        {
                                            Array.Copy(CH1_buffer, MainForm.cwDecoder.fft_buff_ch5, 2048);
                                            MainForm.cwDecoder.AudioEvent1.Set();
                                            buffer_ptr_A = 0;
                                        }

                                        if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                            (rb_monOUT_r.WriteSpace() >= buflen))
                                        {
                                            fixed (float* rptr = &mox_bufferF_l[0])
                                            fixed (float* lptr = &mox_bufferF_r[0])
                                            {
                                                for (int i = 0; i < buflen; i++)
                                                {
                                                    mox_bufferF_l[i] = monitor_buffer[i].Re;
                                                    mox_bufferF_r[i] = monitor_buffer[i].Im;
                                                }

                                                EnterCriticalSection(cs_mon);
                                                rb_monOUT_l.WritePtr(lptr, buflen);
                                                rb_monOUT_r.WritePtr(rptr, buflen);
                                                LeaveCriticalSection(cs_mon);
                                            }
                                        }
                                    }
                                }
                                break;

                            case Mode.RTTY:
                                {
                                    MainForm.rtty.Exchange_samples(1, ref buffer_mox, ref monitor_buffer, buflen);

                                    if (!TXswap)
                                    {
                                        for (int i = 0; i < buflen; i++)
                                        {
                                            out_r_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                            out_l_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                            CH1_buffer[i] = monitor_buffer[i].Re;
                                            out_l_ptr1++;
                                            out_r_ptr1++;
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0; i < buflen; i++)
                                        {
                                            out_l_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                            out_r_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                            CH1_buffer[i] = monitor_buffer[i].Re;
                                            out_l_ptr1++;
                                            out_r_ptr1++;
                                        }
                                    }

                                    if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                        (rb_monOUT_r.WriteSpace() >= buflen))
                                    {
                                        fixed (float* rptr = &mox_bufferF_l[0])
                                        fixed (float* lptr = &mox_bufferF_r[0])
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                mox_bufferF_l[i] = monitor_buffer[i].Re;
                                                mox_bufferF_r[i] = monitor_buffer[i].Im;
                                            }

                                            EnterCriticalSection(cs_mon);
                                            rb_monOUT_l.WritePtr(lptr, buflen);
                                            rb_monOUT_r.WritePtr(rptr, buflen);
                                            LeaveCriticalSection(cs_mon);
                                        }
                                    }
                                }
                                break;

                            case Mode.BPSK31:
                            case Mode.BPSK63:
                            case Mode.BPSK125:
                            case Mode.BPSK250:
                            case Mode.QPSK31:
                            case Mode.QPSK63:
                            case Mode.QPSK125:
                            case Mode.QPSK250:
                                {
                                    MainForm.psk.Exchange_samples(0, ref buffer_mox, ref monitor_buffer, buflen);

                                    if (!TXswap)
                                    {
                                        for (int i = 0; i < buflen; i++)
                                        {
                                            out_r_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                            out_l_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                            CH1_buffer[i] = monitor_buffer[i].Re;
                                            out_l_ptr1++;
                                            out_r_ptr1++;
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0; i < buflen; i++)
                                        {
                                            out_r_ptr1[0] = buffer_mox[i].Re * (float)pwr;
                                            out_l_ptr1[0] = buffer_mox[i].Im * (float)pwr;
                                            CH1_buffer[i] = monitor_buffer[i].Re;
                                            out_l_ptr1++;
                                            out_r_ptr1++;
                                        }
                                    }

                                    if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                        (rb_monOUT_r.WriteSpace() >= buflen))
                                    {
                                        fixed (float* rptr = &mox_bufferF_l[0])
                                        fixed (float* lptr = &mox_bufferF_r[0])
                                        {
                                            for (int i = 0; i < buflen; i++)
                                            {
                                                mox_bufferF_l[i] = monitor_buffer[i].Re;
                                                mox_bufferF_r[i] = monitor_buffer[i].Im;
                                            }

                                            EnterCriticalSection(cs_mon);
                                            rb_monOUT_l.WritePtr(lptr, buflen);
                                            rb_monOUT_r.WritePtr(rptr, buflen);
                                            LeaveCriticalSection(cs_mon);
                                        }
                                    }
                                }
                                break;
                        }
                    }

                    #endregion

                    #region RX

                    else
                    {
                        float* out_l = null, out_r = null;
                        out_l = out_l_ptr1;
                        out_r = out_r_ptr1;

                        if (mox_switch_time > 2)
                        {
                            fixed (float* output_l = &tmp_buffer_ch1[0])
                            fixed (float* output_r = &tmp_buffer_ch2[0])
                                ExchangeOutputSamples(0, output_l, output_r, buflen);

                                if (RX2)
                                {
                                    fixed (float* output_l = &tmp_buffer_ch3[0])
                                    fixed (float* output_r = &tmp_buffer_ch4[0])
                                    {
                                        ExchangeSamplesSubRX(0, 0, output_l, output_r, buflen);
                                    }
                                }

                                if (buffer_ptr_A >= 2048 || buffer_ptr_B >= 2048)
                                {
                                    buffer_ptr_A = 0;
                                    buffer_ptr_B = 0;
                                }

                                for (int i = 0; i < buflen; i++)
                                {
                                    CH1_buffer[buffer_ptr_A] = tmp_buffer_ch1[i];
                                    CH2_buffer[buffer_ptr_A] = tmp_buffer_ch2[i];
                                    CH3_buffer[buffer_ptr_B] = tmp_buffer_ch3[i];
                                    CH4_buffer[buffer_ptr_B] = tmp_buffer_ch4[i];
                                    buffer_ptr_A++;
                                    buffer_ptr_B++;
                                }

                                if (channel == 5)
                                {
                                    if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                        (rb_monOUT_r.WriteSpace() >= buflen))
                                    {
                                        fixed (float* output_l = &tmp_buffer_ch1[0])
                                        fixed (float* output_r = &tmp_buffer_ch2[0])
                                        {
                                            EnterCriticalSection(cs_mon);
                                            rb_monOUT_l.WritePtr(output_l, buflen);
                                            rb_monOUT_r.WritePtr(output_l, buflen);
                                            LeaveCriticalSection(cs_mon);
                                        }
                                    }

                                    /*for (int i = 0; i < buflen; i++)
                                    {
                                        output_l[i] *= (float)volume;
                                        output_r[i] = output_l[i];
                                    }*/
                                }
                                else if (channel == 6)
                                {
                                    if (monitor_enabled && (rb_monOUT_l.WriteSpace() >= buflen) &&
                                        (rb_monOUT_r.WriteSpace() >= buflen))
                                    {
                                        fixed (float* output_l = &tmp_buffer_ch1[0])
                                        fixed (float* output_r = &tmp_buffer_ch2[0])
                                        {
                                            EnterCriticalSection(cs_mon);
                                            rb_monOUT_l.WritePtr(output_r, buflen);
                                            rb_monOUT_r.WritePtr(output_r, buflen);
                                            LeaveCriticalSection(cs_mon);
                                        }
                                    }

                                    /*for (int i = 0; i < buflen; i++)
                                    {
                                        output_l[i] *= (float)volume;
                                        output_r[i] = output_l[i];
                                    }*/
                                }

                            switch (MainForm.OpModeVFOA)
                            {
                                case Mode.CW:
                                    {
                                        if (buffer_ptr_A == 2048)
                                        {
                                            Array.Copy(CH1_buffer, MainForm.cwDecoder.fft_buff_ch5, 2048);
                                            MainForm.cwDecoder.AudioEvent1.Set();
                                            buffer_ptr_A = 0;
                                        }
                                    }
                                    break;

                                case Mode.RTTY:
                                    {
                                        if (MainForm.rtty.run_thread)
                                        {
                                            if (buffer_ptr_A == 2048)
                                            {
                                                Array.Copy(CH1_buffer, MainForm.rtty.ch1_buffer, 2048);
                                                Array.Copy(CH2_buffer, MainForm.rtty.ch2_buffer, 2048);
                                                MainForm.rtty.AudioEventRX1.Set();
                                                buffer_ptr_A = 0;
                                            }
                                        }
                                    }
                                    break;

                                case Mode.BPSK31:
                                case Mode.BPSK63:
                                case Mode.BPSK125:
                                case Mode.BPSK250:
                                case Mode.QPSK31:
                                case Mode.QPSK63:
                                case Mode.QPSK125:
                                case Mode.QPSK250:
                                    {
                                        if (MainForm.psk.run_thread)
                                        {
                                            if (buffer_ptr_A == 2048)
                                            {
                                                Array.Copy(CH1_buffer, MainForm.psk.ch1_buffer, 2048);
                                                Array.Copy(CH2_buffer, MainForm.psk.ch2_buffer, 2048);
                                                MainForm.psk.AudioEvent1.Set();
                                                buffer_ptr_A = 0;
                                            }
                                        }
                                    }
                                    break;
                            }

                            if (RX2)
                            {
                                switch (MainForm.OpModeVFOB)
                                {
                                    case Mode.CW:
                                        {
                                            if (mox_switch_time >= 5)
                                            {
                                                if (buffer_ptr_B >= 2048)
                                                {
                                                    switch (MainForm.OpModeVFOA)
                                                    {
                                                        case Mode.CW:
                                                        case Mode.BPSK31:
                                                        case Mode.BPSK63:
                                                        case Mode.BPSK125:
                                                        case Mode.BPSK250:
                                                        case Mode.QPSK31:
                                                        case Mode.QPSK63:
                                                        case Mode.QPSK125:
                                                        case Mode.QPSK250:
                                                            Array.Copy(CH2_buffer, MainForm.cwDecoder.fft_buff_ch6, 2048);
                                                            break;

                                                        case Mode.RTTY:
                                                            Array.Copy(CH3_buffer, MainForm.cwDecoder.fft_buff_ch6, 2048);
                                                            break;
                                                    }

                                                    MainForm.cwDecoder.AudioEvent2.Set();
                                                    buffer_ptr_B = 0;
                                                }

                                            }
                                            else
                                            {
                                                mox_switch_time++;

                                                if (buffer_ptr_B >= 2048)
                                                {
                                                    Array.Copy(zero_bufferF, MainForm.cwDecoder.fft_buff_ch6, 2048);    // mute
                                                    MainForm.cwDecoder.AudioEvent2.Set();

            #if(WIN32)
                                                    array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                    array_ptr = (Int64*)output;
            #endif

                                                    out_l_ptr1 = (float*)array_ptr[1];
                                                    out_r_ptr1 = (float*)array_ptr[0];
                                                    buffer_ptr_B = 0;
                                                }
                                            }
                                        }
                                        break;

                                    case Mode.RTTY:
                                        {
                                            if (MainForm.rtty.run_thread)
                                            {
                                                if (mox_switch_time >= 5)
                                                {
                                                    if (buffer_ptr_B >= 2048)
                                                    {

                                                        Array.Copy(CH3_buffer, MainForm.rtty.ch3_buffer, 2048);
                                                        Array.Copy(CH4_buffer, MainForm.rtty.ch4_buffer, 2048);
                                                        MainForm.rtty.AudioEventRX2.Set();
                                                        buffer_ptr_B = 0;
                                                    }
                                                }
                                                else
                                                {
                                                    mox_switch_time++;

                                                    if (buffer_ptr_B >= 2048)
                                                    {
                                                        Array.Copy(zero_bufferF, MainForm.rtty.ch3_buffer, 2048);
                                                        Array.Copy(zero_bufferF, MainForm.rtty.ch4_buffer, 2048);
                                                        MainForm.rtty.AudioEventRX2.Set();

            #if(WIN32)
                                                        array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                        array_ptr = (Int64*)output;
            #endif

                                                        out_l_ptr1 = (float*)array_ptr[1];
                                                        out_r_ptr1 = (float*)array_ptr[0];
                                                        buffer_ptr_B = 0;
                                                    }
                                                }
                                            }
                                        }
                                        break;

                                    case Mode.BPSK31:
                                    case Mode.BPSK63:
                                    case Mode.BPSK125:
                                    case Mode.BPSK250:
                                    case Mode.QPSK31:
                                    case Mode.QPSK63:
                                    case Mode.QPSK125:
                                    case Mode.QPSK250:
                                        {
                                            if (MainForm.psk.run_thread)
                                            {
                                                if (mox_switch_time >= 5)
                                                {
                                                    if (buffer_ptr_B >= 2048)
                                                    {
                                                        switch (MainForm.OpModeVFOA)
                                                        {
                                                            case Mode.CW:
                                                            case Mode.BPSK31:
                                                            case Mode.BPSK63:
                                                            case Mode.BPSK125:
                                                            case Mode.BPSK250:
                                                            case Mode.QPSK31:
                                                            case Mode.QPSK63:
                                                            case Mode.QPSK125:
                                                            case Mode.QPSK250:
                                                                Array.Copy(CH2_buffer, MainForm.psk.ch2_buffer, 2048);
                                                                break;

                                                            case Mode.RTTY:
                                                                Array.Copy(CH3_buffer, MainForm.psk.ch2_buffer, 2048);
                                                                break;
                                                        }

                                                        MainForm.psk.AudioEvent2.Set();
                                                        buffer_ptr_B = 0;
                                                    }
                                                }
                                                else
                                                {
                                                    mox_switch_time++;

                                                    if (buffer_ptr_B >= 2048)
                                                    {
                                                        Array.Copy(zero_bufferF, MainForm.psk.ch2_buffer, 2048);    // mute
                                                        MainForm.psk.AudioEvent2.Set();

            #if(WIN32)
                                                        array_ptr = (int*)output;
            #endif

            #if(WIN64)
                                                        array_ptr = (Int64*)output;
            #endif

                                                        out_l_ptr1 = (float*)array_ptr[1];
                                                        out_r_ptr1 = (float*)array_ptr[0];
                                                        buffer_ptr_B = 0;
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                }
                            }
                        }
                        else
                            mox_switch_time++;
                    }

                    #endregion
                }

                #region Scope

                if ((MainForm.DisplayMode == DisplayMode.PANASCOPE || MainForm.DisplayMode == DisplayMode.PANASCOPE_INV)
                    && !pause_scope && buffer_ptr_A==0)
                {

                    if (channel == 5)
                    {
                        DoScope(CH1_buffer, 2048);
                    }
                    else if (channel == 6)
                    {
                        DoScope(CH2_buffer, 2048);
                    }
                }

                #endregion

                return callback_return;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
                return 0;
            }
        }
        public static unsafe int InputCallback(void* input, void* output, int buflen,
    PA19.PaStreamCallbackTimeInfo* timeInfo, int statusFlags, void* userData)
        {
            try
            {
                if (audio_paused)
                    return callback_return;
            #if(WIN64)
                Int64* array_ptr = (Int64*)input;
                float* in_l_ptr1 = (float*)array_ptr[0];
                float* in_r_ptr1 = (float*)array_ptr[1];
                array_ptr = (Int64*)input;
                in_l_ptr1 = (float*)array_ptr[0];
                in_r_ptr1 = (float*)array_ptr[1];
            #endif

            #if(WIN32)
                int* array_ptr = (int*)input;
                float* in_r_ptr1 = (float*)array_ptr[0];
                float* in_l_ptr1 = (float*)array_ptr[1];
                array_ptr = (int*)input;
                in_l_ptr1 = (float*)array_ptr[0];
                in_r_ptr1 = (float*)array_ptr[1];
            #endif

                float* in_l = null, in_r = null;

                if (!RXswap)
                {
                    in_l = in_l_ptr1;
                    in_r = in_r_ptr1;
                }
                else
                {
                    in_l = in_r_ptr1;
                    in_r = in_l_ptr1;
                }

                if (SDRmode)
                {
                    if (!mox)
                    {
                        if (mox_switch_time >= 2)
                            ExchangeInputSamples(0, in_l, in_r, buflen);
                        else
                        {
                            mox_switch_time++;

                            /*fixed (float* rptr = &tmp_bufferF_l[0])
                            fixed (float* lptr = &tmp_bufferF_r[0])
                            {
                                ExchangeInputSamples(0, lptr, rptr, buflen);
                            }*/
                        }
                    }
                    else
                    {
                        /*if (mox_switch_time >= 2)
                        {
                            fixed (float* rptr = &tmp_bufferF_l[0])
                            fixed (float* lptr = &tmp_bufferF_r[0])
                            {
                                ExchangeInputSamples(0, lptr, rptr, buflen);
                            }
                        }
                        else
                        {
                            mox_switch_time++;

                            fixed (float* rptr = &tmp_bufferF_l[0])
                            fixed (float* lptr = &tmp_bufferF_r[0])
                            {
                                for (int i = 0; i < buflen; i++)
                                {
                                    tmp_bufferF_l[i] = in_l[i];
                                    tmp_bufferF_r[i] = in_r[i];
                                }

                                ExchangeInputSamples(0, lptr, rptr, buflen);
                            }
                        }*/
                    }
                }

                return callback_return;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
                return 0;
            }
        }