Switch from portaudio callback to stream write

It’s already in a thread, not sure if callback was helping?
This commit is contained in:
Charles J. Cliffe 2014-11-22 22:56:33 -05:00
parent 39ba38b82a
commit fa521ac30d
2 changed files with 44 additions and 83 deletions

View File

@ -2,50 +2,8 @@
#include "CubicSDRDefs.h"
#include <vector>
static int audioCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) {
AudioThread *src = (AudioThread *) userData;
float *out = (float*) outputBuffer;
if (statusFlags & paOutputOverflow) {
std::cout << "Audio buffer overflow.." << std::endl;
}
if (statusFlags & paOutputUnderflow) {
std::cout << "Audio buffer underflow.." << std::endl;
}
if ((statusFlags & paPrimingOutput) || (statusFlags & paOutputOverflow) || !src->audio_queue.size()) {
for (int i = 0; i < framesPerBuffer * 2; i++) {
out[i] = 0;
}
return paContinue;
}
std::vector<float> nextBuffer = src->audio_queue.front();
for (int i = 0; i < framesPerBuffer * 2; i++) {
out[i] = nextBuffer[src->audio_queue_ptr];
src->audio_queue_ptr++;
if (src->audio_queue_ptr == nextBuffer.size()) {
src->audio_queue.pop();
src->audio_queue_ptr = 0;
if (!src->audio_queue.size()) {
break;
}
nextBuffer = src->audio_queue.front();
}
}
return paContinue;
}
AudioThread::AudioThread(AudioThreadInputQueue *inputQueue) :
inputQueue(inputQueue), stream(NULL), audio_queue_ptr(0) {
inputQueue(inputQueue), stream(NULL) {
}
@ -68,31 +26,40 @@ void AudioThread::threadMain() {
int preferred_device = -1;
outputParameters.device = (preferred_device != -1) ? preferred_device : Pa_GetDefaultOutputDevice();
outputParameters.device =
(preferred_device != -1) ?
preferred_device : Pa_GetDefaultOutputDevice();
if (outputParameters.device == paNoDevice) {
std::cout << "Error: No default output device.\n";
}
outputParameters.channelCount = 2;
outputParameters.sampleFormat = paFloat32;
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.suggestedLatency = Pa_GetDeviceInfo(
outputParameters.device)->defaultHighOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
stream = NULL;
err = Pa_OpenStream(&stream, NULL, &outputParameters, AUDIO_FREQUENCY, paFramesPerBufferUnspecified,
paPrimeOutputBuffersUsingStreamCallback | paClipOff, &audioCallback, this);
// err = Pa_OpenStream(&stream, NULL, &outputParameters, AUDIO_FREQUENCY,
// paFramesPerBufferUnspecified,
// paPrimeOutputBuffersUsingStreamCallback | paClipOff, &audioCallback,
// this);
err = Pa_OpenStream(&stream, NULL, &outputParameters, AUDIO_FREQUENCY,
paFramesPerBufferUnspecified, paClipOff, NULL, NULL);
err = Pa_StartStream(stream);
if (err != paNoError) {
std::cout << "Error starting stream: " << Pa_GetErrorText(err) << std::endl;
std::cout << "Error starting stream: " << Pa_GetErrorText(err)
<< std::endl;
std::cout << "\tPortAudio error: " << Pa_GetErrorText(err) << std::endl;
}
while (1) {
AudioThreadInput inp;
inputQueue->pop(inp);
audio_queue.push(inp.data);
Pa_WriteStream(stream, &inp.data[0], inp.data.size()/2);
}
}

View File

@ -15,9 +15,6 @@
#include "ThreadQueue.h"
#include "portaudio.h"
static int audioCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData);
class AudioThreadInput {
public:
int frequency;
@ -30,9 +27,6 @@ typedef ThreadQueue<AudioThreadInput> AudioThreadInputQueue;
class AudioThread {
public:
std::queue<std::vector<float> > audio_queue;
unsigned int audio_queue_ptr;
AudioThread(AudioThreadInputQueue *inputQueue);
~AudioThread();