2014-11-16 16:50:37 -05:00
|
|
|
#include "AudioThread.h"
|
|
|
|
#include "CubicSDRDefs.h"
|
|
|
|
#include <vector>
|
2014-12-11 19:07:21 -05:00
|
|
|
#include "DemodulatorThread.h"
|
2014-11-16 16:50:37 -05:00
|
|
|
|
2014-12-11 19:07:21 -05:00
|
|
|
AudioThread::AudioThread(AudioThreadInputQueue *inputQueue, DemodulatorThreadCommandQueue* threadQueueNotify) :
|
|
|
|
inputQueue(inputQueue), terminated(false), audio_queue_ptr(0), underflow_count(0), threadQueueNotify(threadQueueNotify) {
|
2014-11-22 22:33:32 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioThread::~AudioThread() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status,
|
|
|
|
void *userData) {
|
|
|
|
AudioThread *src = (AudioThread *) userData;
|
|
|
|
float *out = (float*) outputBuffer;
|
|
|
|
if (status) {
|
2014-12-05 20:13:45 -05:00
|
|
|
std::cout << "Audio buffer underflow.." << (src->underflow_count++) << std::endl;
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
2014-12-05 20:13:45 -05:00
|
|
|
|
2014-12-15 20:47:46 -05:00
|
|
|
if (src->audio_queue_ptr == src->currentInput.data.size()) {
|
|
|
|
if (src->terminated) {
|
|
|
|
return 1;
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
2014-12-15 20:47:46 -05:00
|
|
|
src->inputQueue->pop(src->currentInput);
|
|
|
|
src->audio_queue_ptr = 0;
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
2014-12-05 20:13:45 -05:00
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
for (int i = 0; i < nBufferFrames * 2; i++) {
|
2014-12-15 20:47:46 -05:00
|
|
|
out[i] = src->currentInput.data[src->audio_queue_ptr];
|
2014-12-04 19:44:49 -05:00
|
|
|
src->audio_queue_ptr++;
|
2014-12-15 20:47:46 -05:00
|
|
|
if (src->audio_queue_ptr == src->currentInput.data.size()) {
|
|
|
|
if (src->terminated) {
|
|
|
|
return 1;
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
2014-12-15 20:47:46 -05:00
|
|
|
src->inputQueue->pop(src->currentInput);
|
|
|
|
src->audio_queue_ptr = 0;
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-15 20:47:46 -05:00
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
void AudioThread::threadMain() {
|
2014-12-16 20:33:44 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
pthread_t tID = pthread_self(); // ID of this thread
|
|
|
|
int priority = sched_get_priority_min( SCHED_RR );
|
|
|
|
sched_param prio = { priority }; // scheduling priority of thread
|
|
|
|
pthread_setschedparam( tID, SCHED_RR, &prio );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::cout << "Audio thread initializing.." << std::endl;
|
2014-11-30 17:11:29 -05:00
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
if (dac.getDeviceCount() < 1) {
|
|
|
|
std::cout << "No audio devices found!" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtAudio::StreamParameters parameters;
|
|
|
|
parameters.deviceId = dac.getDefaultOutputDevice();
|
|
|
|
parameters.nChannels = 2;
|
|
|
|
parameters.firstChannel = 0;
|
|
|
|
unsigned int sampleRate = AUDIO_FREQUENCY;
|
2014-12-05 21:16:43 -05:00
|
|
|
unsigned int bufferFrames = 256;
|
2014-12-04 19:44:49 -05:00
|
|
|
|
2014-12-05 18:20:28 -05:00
|
|
|
RtAudio::StreamOptions opts;
|
2014-12-16 20:33:44 -05:00
|
|
|
opts.flags = RTAUDIO_SCHEDULE_REALTIME;
|
|
|
|
// | RTAUDIO_MINIMIZE_LATENCY;
|
2014-12-05 20:13:45 -05:00
|
|
|
// opts.flags = RTAUDIO_MINIMIZE_LATENCY;
|
2014-12-05 18:20:28 -05:00
|
|
|
opts.streamName = "CubicSDR Audio Output";
|
2014-12-16 00:22:33 -05:00
|
|
|
// opts.priority = sched_get_priority_max(SCHED_FIFO);
|
2014-12-05 20:13:45 -05:00
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
try {
|
2014-12-05 18:20:28 -05:00
|
|
|
dac.openStream(¶meters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &audioCallback, (void *) this, &opts);
|
2014-12-04 19:44:49 -05:00
|
|
|
dac.startStream();
|
|
|
|
} catch (RtAudioError& e) {
|
|
|
|
e.printMessage();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-04 19:03:02 -05:00
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
while (!terminated) {
|
2014-12-15 20:47:46 -05:00
|
|
|
AudioThreadCommand command;
|
|
|
|
cmdQueue.pop(command);
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
2014-11-22 22:56:33 -05:00
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
try {
|
|
|
|
// Stop the stream
|
|
|
|
dac.stopStream();
|
|
|
|
} catch (RtAudioError& e) {
|
|
|
|
e.printMessage();
|
|
|
|
}
|
2014-11-30 17:11:29 -05:00
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
if (dac.isStreamOpen()) {
|
|
|
|
dac.closeStream();
|
|
|
|
}
|
2014-11-30 17:11:29 -05:00
|
|
|
|
|
|
|
std::cout << "Audio thread done." << std::endl;
|
2014-12-11 19:07:21 -05:00
|
|
|
|
|
|
|
DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_AUDIO_TERMINATED);
|
|
|
|
tCmd.context = this;
|
|
|
|
threadQueueNotify->push(tCmd);
|
2014-11-16 16:50:37 -05:00
|
|
|
}
|
|
|
|
|
2014-11-23 19:39:27 -05:00
|
|
|
void AudioThread::terminate() {
|
|
|
|
terminated = true;
|
2014-12-15 20:47:46 -05:00
|
|
|
AudioThreadCommand endCond; // push an empty input to bump the queue
|
|
|
|
cmdQueue.push(endCond);
|
2014-11-23 19:39:27 -05:00
|
|
|
}
|