diff --git a/src/audio/AudioThread.cpp b/src/audio/AudioThread.cpp index 53a186f..b429ec3 100644 --- a/src/audio/AudioThread.cpp +++ b/src/audio/AudioThread.cpp @@ -11,7 +11,7 @@ std::map AudioThread::deviceThread; AudioThread::AudioThread(AudioThreadInputQueue *inputQueue, DemodulatorThreadCommandQueue* threadQueueNotify) : inputQueue(inputQueue), terminated(false), audio_queue_ptr(0), underflow_count(0), threadQueueNotify(threadQueueNotify), gain(1.0), active( - false) { + false) { #ifdef __APPLE__ boundThreads = new std::vector; #endif @@ -65,14 +65,35 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu continue; } - for (int i = 0; i < nBufferFrames * 2; i++) { - if (srcmix->audio_queue_ptr >= srcmix->currentInput.data.size()) { + if (srcmix->currentInput.channels == 0) { + if (!srcmix->inputQueue->empty()) { srcmix->inputQueue->pop(srcmix->currentInput); - srcmix->audio_queue_ptr = 0; } - out[i] = out[i] + srcmix->currentInput.data[srcmix->audio_queue_ptr] * src->gain; - srcmix->audio_queue_ptr++; + continue; } + + if (srcmix->currentInput.channels == 1) { + for (int i = 0; i < nBufferFrames; i++) { + if (srcmix->audio_queue_ptr >= srcmix->currentInput.data.size()) { + srcmix->inputQueue->pop(srcmix->currentInput); + srcmix->audio_queue_ptr = 0; + } + float v = srcmix->currentInput.data[srcmix->audio_queue_ptr] * src->gain; + out[i * 2] += v; + out[i * 2 + 1] += v; + srcmix->audio_queue_ptr++; + } + } else { + for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) { + if (srcmix->audio_queue_ptr >= srcmix->currentInput.data.size()) { + srcmix->inputQueue->pop(srcmix->currentInput); + srcmix->audio_queue_ptr = 0; + } + out[i] = out[i] + srcmix->currentInput.data[srcmix->audio_queue_ptr] * src->gain; + srcmix->audio_queue_ptr++; + } + } + } return 0; @@ -89,23 +110,42 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu std::cout << "Audio buffer underflow.." << (src->underflow_count++) << std::endl; } - for (int i = 0; i < nBufferFrames * 2; i++) { - if (src->audio_queue_ptr >= src->currentInput.data.size()) { - if (src->terminated) { - break; - } + if (src->currentInput.channels == 0) { + if (!src->inputQueue->empty()) { src->inputQueue->pop(src->currentInput); - src->audio_queue_ptr = 0; } - out[i] = src->currentInput.data[src->audio_queue_ptr] * src->gain; - src->audio_queue_ptr++; + return 0; } + if (src->currentInput.channels == 1) { + for (int i = 0; i < nBufferFrames; i++) { + if (src->audio_queue_ptr >= src->currentInput.data.size()) { + if (src->terminated) { + break; + } + src->inputQueue->pop(src->currentInput); + src->audio_queue_ptr = 0; + } + out[i * 2] = out[i * 2 + 1] = src->currentInput.data[src->audio_queue_ptr] * src->gain; + src->audio_queue_ptr++; + } + } else { + for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) { + if (src->audio_queue_ptr >= src->currentInput.data.size()) { + if (src->terminated) { + break; + } + src->inputQueue->pop(src->currentInput); + src->audio_queue_ptr = 0; + } + out[i] = src->currentInput.data[src->audio_queue_ptr] * src->gain; + src->audio_queue_ptr++; + } + } return 0; } #endif - void AudioThread::enumerateDevices() { int numDevices = dac.getDeviceCount(); @@ -158,7 +198,7 @@ void AudioThread::threadMain() { #ifdef __APPLE__ pthread_t tID = pthread_self(); // ID of this thread int priority = sched_get_priority_max( SCHED_RR) - 1; - sched_param prio = { priority }; // scheduling priority of thread + sched_param prio = {priority}; // scheduling priority of thread pthread_setschedparam(tID, SCHED_RR, &prio); #endif diff --git a/src/audio/AudioThread.h b/src/audio/AudioThread.h index e295555..9ea135a 100644 --- a/src/audio/AudioThread.h +++ b/src/audio/AudioThread.h @@ -22,6 +22,15 @@ class AudioThreadInput { public: int frequency; int sampleRate; + int channels; + + AudioThreadInput(): frequency(0), sampleRate(0), channels(0) { + + } + + ~AudioThreadInput() { + + } std::vector data; }; diff --git a/src/demod/DemodDefs.h b/src/demod/DemodDefs.h index edcfae2..e5822c1 100644 --- a/src/demod/DemodDefs.h +++ b/src/demod/DemodDefs.h @@ -79,7 +79,7 @@ class DemodulatorThreadPostIQData { public: std::vector data; float audio_resample_ratio; - msresamp_crcf audio_resampler; + msresamp_rrrf audio_resampler; float resample_ratio; msresamp_crcf resampler; diff --git a/src/demod/DemodulatorPreThread.cpp b/src/demod/DemodulatorPreThread.cpp index 01e4644..19775ec 100644 --- a/src/demod/DemodulatorPreThread.cpp +++ b/src/demod/DemodulatorPreThread.cpp @@ -65,9 +65,9 @@ void DemodulatorPreThread::initialize() { // msresamp_crcf_print(resampler); if (audio_resampler) { - msresamp_crcf_destroy(audio_resampler); + msresamp_rrrf_destroy(audio_resampler); } - audio_resampler = msresamp_crcf_create(audio_resample_ratio, As); + audio_resampler = msresamp_rrrf_create(audio_resample_ratio, As); // msresamp_crcf_print(audio_resampler); initialized = true; diff --git a/src/demod/DemodulatorPreThread.h b/src/demod/DemodulatorPreThread.h index 4e5b577..0295984 100644 --- a/src/demod/DemodulatorPreThread.h +++ b/src/demod/DemodulatorPreThread.h @@ -57,7 +57,7 @@ protected: msresamp_crcf resampler; float resample_ratio; - msresamp_crcf audio_resampler; + msresamp_rrrf audio_resampler; float audio_resample_ratio; DemodulatorThreadParameters params; diff --git a/src/demod/DemodulatorThread.cpp b/src/demod/DemodulatorThread.cpp index 50dd31c..f9befc5 100644 --- a/src/demod/DemodulatorThread.cpp +++ b/src/demod/DemodulatorThread.cpp @@ -28,7 +28,7 @@ void DemodulatorThread::threadMain() { pthread_setschedparam(tID, SCHED_FIFO, &prio); #endif - msresamp_crcf audio_resampler = NULL; + msresamp_rrrf audio_resampler = NULL; msresamp_crcf resampler = NULL; agc = agc_crcf_create(); @@ -50,7 +50,7 @@ void DemodulatorThread::threadMain() { audio_resampler = inp.audio_resampler; } else if (resampler != inp.resampler) { msresamp_crcf_destroy(resampler); - msresamp_crcf_destroy(audio_resampler); + msresamp_rrrf_destroy(audio_resampler); resampler = inp.resampler; audio_resampler = inp.audio_resampler; } @@ -65,35 +65,19 @@ void DemodulatorThread::threadMain() { agc_crcf_execute_block(agc, resampled_data, num_written, agc_data); float audio_resample_ratio = inp.audio_resample_ratio; - float demod_output[num_written]; freqdem_demodulate_block(fdem, agc_data, num_written, demod_output); - liquid_float_complex demod_audio_data[num_written]; - - for (int i = 0; i < num_written; i++) { - demod_audio_data[i].real = demod_output[i]; - demod_audio_data[i].imag = 0; - } - int audio_out_size = ceil((float) (num_written) * audio_resample_ratio); - liquid_float_complex resampled_audio_output[audio_out_size]; + float resampled_audio_output[audio_out_size]; unsigned int num_audio_written; - msresamp_crcf_execute(audio_resampler, demod_audio_data, num_written, resampled_audio_output, &num_audio_written); - - std::vector newBuffer; - newBuffer.resize(num_audio_written * 2); - for (int i = 0; i < num_audio_written; i++) { - liquid_float_complex y = resampled_audio_output[i]; - - newBuffer[i * 2] = y.real; - newBuffer[i * 2 + 1] = y.real; - } + msresamp_rrrf_execute(audio_resampler, demod_output, num_written, resampled_audio_output, &num_audio_written); AudioThreadInput ati; - ati.data = newBuffer; + ati.channels = 1; + ati.data.assign(resampled_audio_output,resampled_audio_output+num_audio_written); if (audioInputQueue != NULL) { if (!squelch_enabled || ((agc_crcf_get_signal_level(agc)) >= 0.1)) { @@ -103,21 +87,21 @@ void DemodulatorThread::threadMain() { if (visOutQueue != NULL && visOutQueue->empty()) { AudioThreadInput ati_vis; + ati_vis.channels = ati.channels; + int num_vis = DEMOD_VIS_SIZE; if (num_audio_written > num_written) { if (num_vis > num_audio_written) { num_vis = num_audio_written; } - ati_vis.data.resize(num_vis); - for (int i = 0; i < num_vis; i++) { - ati_vis.data[i] = resampled_audio_output[i].real; - } + ati_vis.data.assign(ati.data.begin(), ati.data.begin()+num_vis); } else { if (num_vis > num_written) { num_vis = num_written; } ati_vis.data.assign(demod_output, demod_output + num_vis); } + visOutQueue->push(ati_vis); // std::cout << "Signal: " << agc_crcf_get_signal_level(agc) << " -- " << agc_crcf_get_rssi(agc) << "dB " << std::endl; } @@ -150,7 +134,7 @@ void DemodulatorThread::threadMain() { msresamp_crcf_destroy(resampler); } if (audio_resampler != NULL) { - msresamp_crcf_destroy(audio_resampler); + msresamp_rrrf_destroy(audio_resampler); } agc_crcf_destroy(agc); diff --git a/src/demod/DemodulatorWorkerThread.cpp b/src/demod/DemodulatorWorkerThread.cpp index f002737..8cadf90 100644 --- a/src/demod/DemodulatorWorkerThread.cpp +++ b/src/demod/DemodulatorWorkerThread.cpp @@ -58,7 +58,7 @@ void DemodulatorWorkerThread::threadMain() { result.fir_filter = firfilt_crcf_create(h, h_len); result.resampler = msresamp_crcf_create(result.resample_ratio, As); - result.audio_resampler = msresamp_crcf_create(result.audio_resample_ratio, As); + result.audio_resampler = msresamp_rrrf_create(result.audio_resample_ratio, As); result.audioSampleRate = filterCommand.audioSampleRate; result.bandwidth = filterCommand.bandwidth; diff --git a/src/demod/DemodulatorWorkerThread.h b/src/demod/DemodulatorWorkerThread.h index 9911972..950b5f1 100644 --- a/src/demod/DemodulatorWorkerThread.h +++ b/src/demod/DemodulatorWorkerThread.h @@ -36,7 +36,7 @@ public: firfilt_crcf fir_filter; msresamp_crcf resampler; float resample_ratio; - msresamp_crcf audio_resampler; + msresamp_rrrf audio_resampler; float audio_resample_ratio; unsigned int inputRate;