optimize demod, allow for channel count changes

This commit is contained in:
Charles J. Cliffe 2014-12-21 17:37:41 -05:00
parent 2b18f03cb5
commit 24c86bc9e4
8 changed files with 82 additions and 49 deletions

View File

@ -11,7 +11,7 @@ std::map<int, std::thread *> 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<AudioThread *>;
#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

View File

@ -22,6 +22,15 @@ class AudioThreadInput {
public:
int frequency;
int sampleRate;
int channels;
AudioThreadInput(): frequency(0), sampleRate(0), channels(0) {
}
~AudioThreadInput() {
}
std::vector<float> data;
};

View File

@ -79,7 +79,7 @@ class DemodulatorThreadPostIQData {
public:
std::vector<liquid_float_complex> data;
float audio_resample_ratio;
msresamp_crcf audio_resampler;
msresamp_rrrf audio_resampler;
float resample_ratio;
msresamp_crcf resampler;

View File

@ -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;

View File

@ -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;

View File

@ -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<float> 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);

View File

@ -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;

View File

@ -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;