2014-11-16 16:50:37 -05:00
|
|
|
#include "AudioThread.h"
|
|
|
|
#include "CubicSDRDefs.h"
|
|
|
|
#include <vector>
|
2014-12-18 20:11:25 -05:00
|
|
|
#include <algorithm>
|
2014-12-11 19:07:21 -05:00
|
|
|
#include "DemodulatorThread.h"
|
2014-11-16 16:50:37 -05:00
|
|
|
|
2014-12-18 20:11:25 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
std::map<int, AudioThread *> AudioThread::deviceController;
|
|
|
|
std::map<int, std::thread *> AudioThread::deviceThread;
|
|
|
|
#endif
|
2014-11-22 22:33:32 -05:00
|
|
|
|
2014-12-18 20:11:25 -05:00
|
|
|
AudioThread::AudioThread(AudioThreadInputQueue *inputQueue, DemodulatorThreadCommandQueue* threadQueueNotify) :
|
|
|
|
inputQueue(inputQueue), terminated(false), audio_queue_ptr(0), underflow_count(0), threadQueueNotify(threadQueueNotify), gain(1.0), active(
|
2014-12-21 17:37:41 -05:00
|
|
|
false) {
|
2014-12-18 20:11:25 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
boundThreads = new std::vector<AudioThread *>;
|
|
|
|
#endif
|
2014-11-22 22:33:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioThread::~AudioThread() {
|
2014-12-18 20:11:25 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
delete boundThreads.load();
|
|
|
|
#endif
|
|
|
|
}
|
2014-11-22 22:33:32 -05:00
|
|
|
|
2014-12-18 20:11:25 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
void AudioThread::bindThread(AudioThread *other) {
|
|
|
|
boundThreads.load()->push_back(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioThread::removeThread(AudioThread *other) {
|
|
|
|
std::vector<AudioThread *>::iterator i;
|
|
|
|
i = std::find(boundThreads.load()->begin(), boundThreads.load()->end(), other);
|
|
|
|
if (i != boundThreads.load()->end()) {
|
|
|
|
boundThreads.load()->erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioThread::deviceCleanup() {
|
|
|
|
std::map<int,AudioThread *>::iterator i;
|
|
|
|
|
|
|
|
for (i = deviceController.begin(); i != deviceController.end(); i++) {
|
|
|
|
i->second->terminate();
|
|
|
|
}
|
2014-11-22 22:33:32 -05:00
|
|
|
}
|
|
|
|
|
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;
|
2014-12-18 20:11:25 -05:00
|
|
|
memset(out, 0, nBufferFrames * 2 * sizeof(float));
|
2014-12-04 19:44:49 -05:00
|
|
|
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-18 20:11:25 -05:00
|
|
|
if (!src->boundThreads.load()->empty()) {
|
|
|
|
src->gain = 1.0 / src->boundThreads.load()->size();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < src->boundThreads.load()->size(); j++) {
|
|
|
|
AudioThread *srcmix = (*(src->boundThreads.load()))[j];
|
|
|
|
if (srcmix->terminated || !srcmix->inputQueue || srcmix->inputQueue->empty() || !srcmix->isActive()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:43:56 -05:00
|
|
|
if (srcmix->currentInput.channels == 0 || !srcmix->currentInput.data) {
|
2014-12-21 17:37:41 -05:00
|
|
|
if (!srcmix->inputQueue->empty()) {
|
2014-12-22 21:12:13 -05:00
|
|
|
if (srcmix->currentInput.data) {
|
|
|
|
delete srcmix->currentInput.data;
|
|
|
|
}
|
2014-12-18 20:11:25 -05:00
|
|
|
srcmix->inputQueue->pop(srcmix->currentInput);
|
2014-12-22 21:12:13 -05:00
|
|
|
srcmix->audio_queue_ptr = 0;
|
2014-12-18 20:11:25 -05:00
|
|
|
}
|
2014-12-22 21:12:13 -05:00
|
|
|
return 0;
|
2014-12-21 17:37:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (srcmix->currentInput.channels == 1) {
|
|
|
|
for (int i = 0; i < nBufferFrames; i++) {
|
2014-12-22 21:12:13 -05:00
|
|
|
if (srcmix->audio_queue_ptr >= srcmix->currentInput.data->size()) {
|
|
|
|
if (srcmix->currentInput.data) {
|
|
|
|
delete srcmix->currentInput.data;
|
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
srcmix->inputQueue->pop(srcmix->currentInput);
|
|
|
|
srcmix->audio_queue_ptr = 0;
|
|
|
|
}
|
2014-12-22 21:12:13 -05:00
|
|
|
if (srcmix->currentInput.data && srcmix->currentInput.data->size()) {
|
|
|
|
float v = (*srcmix->currentInput.data)[srcmix->audio_queue_ptr] * src->gain;
|
|
|
|
out[i * 2] += v;
|
|
|
|
out[i * 2 + 1] += v;
|
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
srcmix->audio_queue_ptr++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) {
|
2014-12-22 23:27:52 -05:00
|
|
|
if (srcmix->audio_queue_ptr >= srcmix->currentInput.data->size()) {
|
2014-12-22 21:12:13 -05:00
|
|
|
if (srcmix->currentInput.data) {
|
|
|
|
delete srcmix->currentInput.data;
|
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
srcmix->inputQueue->pop(srcmix->currentInput);
|
|
|
|
srcmix->audio_queue_ptr = 0;
|
|
|
|
}
|
2014-12-22 21:12:13 -05:00
|
|
|
if (srcmix->currentInput.data && srcmix->currentInput.data->size()) {
|
|
|
|
out[i] = out[i] + (*srcmix->currentInput.data)[srcmix->audio_queue_ptr] * src->gain;
|
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
srcmix->audio_queue_ptr++;
|
|
|
|
}
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
|
2014-12-18 20:11:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status,
|
|
|
|
void *userData) {
|
|
|
|
AudioThread *src = (AudioThread *) userData;
|
|
|
|
float *out = (float*) outputBuffer;
|
|
|
|
memset(out, 0, nBufferFrames * 2 * sizeof(float));
|
|
|
|
if (status) {
|
|
|
|
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-22 19:43:56 -05:00
|
|
|
if (src->currentInput.channels == 0 || !src->currentInput.data) {
|
2014-12-21 17:37:41 -05:00
|
|
|
if (!src->inputQueue->empty()) {
|
2014-12-22 19:43:56 -05:00
|
|
|
if (src->currentInput.data) {
|
|
|
|
delete src->currentInput.data;
|
|
|
|
}
|
2014-12-15 20:47:46 -05:00
|
|
|
src->inputQueue->pop(src->currentInput);
|
2014-12-22 21:12:13 -05:00
|
|
|
src->audio_queue_ptr = 0;
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
return 0;
|
2014-12-04 19:44:49 -05:00
|
|
|
}
|
2014-12-15 20:47:46 -05:00
|
|
|
|
2014-12-21 17:37:41 -05:00
|
|
|
if (src->currentInput.channels == 1) {
|
|
|
|
for (int i = 0; i < nBufferFrames; i++) {
|
2014-12-22 19:43:56 -05:00
|
|
|
if (src->audio_queue_ptr >= src->currentInput.data->size()) {
|
|
|
|
if (src->currentInput.data) {
|
|
|
|
delete src->currentInput.data;
|
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
if (src->terminated) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
src->inputQueue->pop(src->currentInput);
|
|
|
|
src->audio_queue_ptr = 0;
|
|
|
|
}
|
2014-12-22 21:12:13 -05:00
|
|
|
if (src->currentInput.data && src->currentInput.data->size()) {
|
|
|
|
out[i * 2] = out[i * 2 + 1] = (*src->currentInput.data)[src->audio_queue_ptr] * src->gain;
|
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
src->audio_queue_ptr++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) {
|
2014-12-22 19:43:56 -05:00
|
|
|
if (src->audio_queue_ptr >= src->currentInput.data->size()) {
|
2014-12-21 17:37:41 -05:00
|
|
|
if (src->terminated) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
src->inputQueue->pop(src->currentInput);
|
|
|
|
src->audio_queue_ptr = 0;
|
|
|
|
}
|
2014-12-22 21:12:13 -05:00
|
|
|
if (src->currentInput.data && src->currentInput.data->size()) {
|
|
|
|
out[i] = (*src->currentInput.data)[src->audio_queue_ptr] * src->gain;
|
|
|
|
}
|
2014-12-21 17:37:41 -05:00
|
|
|
src->audio_queue_ptr++;
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 19:44:49 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-18 20:11:25 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void AudioThread::enumerateDevices() {
|
|
|
|
int numDevices = dac.getDeviceCount();
|
|
|
|
|
|
|
|
for (int i = 0; i < numDevices; i++) {
|
|
|
|
RtAudio::DeviceInfo info = dac.getDeviceInfo(i);
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::cout << "Audio Device #" << i << " " << info.name << std::endl;
|
|
|
|
std::cout << "\tDefault Output? " << (info.isDefaultOutput ? "Yes" : "No") << std::endl;
|
|
|
|
std::cout << "\tDefault Input? " << (info.isDefaultOutput ? "Yes" : "No") << std::endl;
|
|
|
|
std::cout << "\tInput channels: " << info.inputChannels << std::endl;
|
|
|
|
std::cout << "\tOutput channels: " << info.outputChannels << std::endl;
|
|
|
|
std::cout << "\tDuplex channels: " << info.duplexChannels << std::endl;
|
|
|
|
|
|
|
|
std::cout << "\t" << "Native formats:" << std::endl;
|
|
|
|
RtAudioFormat nFormats = info.nativeFormats;
|
|
|
|
if (nFormats & RTAUDIO_SINT8) {
|
|
|
|
std::cout << "\t\t8-bit signed integer." << std::endl;
|
|
|
|
}
|
|
|
|
if (nFormats & RTAUDIO_SINT16) {
|
|
|
|
std::cout << "\t\t16-bit signed integer." << std::endl;
|
|
|
|
}
|
|
|
|
if (nFormats & RTAUDIO_SINT24) {
|
|
|
|
std::cout << "\t\t24-bit signed integer." << std::endl;
|
|
|
|
}
|
|
|
|
if (nFormats & RTAUDIO_SINT32) {
|
|
|
|
std::cout << "\t\t32-bit signed integer." << std::endl;
|
|
|
|
}
|
|
|
|
if (nFormats & RTAUDIO_FLOAT32) {
|
|
|
|
std::cout << "\t\t32-bit float normalized between plus/minus 1.0." << std::endl;
|
|
|
|
}
|
|
|
|
if (nFormats & RTAUDIO_FLOAT64) {
|
|
|
|
std::cout << "\t\t32-bit float normalized between plus/minus 1.0." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned int>::iterator srate;
|
|
|
|
|
|
|
|
std::cout << "\t" << "Supported sample rates:" << std::endl;
|
|
|
|
|
|
|
|
for (srate = info.sampleRates.begin(); srate != info.sampleRates.end(); srate++) {
|
|
|
|
std::cout << "\t\t" << (*srate) << "hz" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 19:44:49 -05:00
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
void AudioThread::threadMain() {
|
2014-12-16 20:33:44 -05:00
|
|
|
#ifdef __APPLE__
|
2014-12-16 21:30:03 -05:00
|
|
|
pthread_t tID = pthread_self(); // ID of this thread
|
2014-12-18 20:11:25 -05:00
|
|
|
int priority = sched_get_priority_max( SCHED_RR) - 1;
|
2014-12-21 17:37:41 -05:00
|
|
|
sched_param prio = {priority}; // scheduling priority of thread
|
2014-12-18 20:11:25 -05:00
|
|
|
pthread_setschedparam(tID, SCHED_RR, &prio);
|
2014-12-16 20:33:44 -05:00
|
|
|
#endif
|
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
opts.streamName = "CubicSDR Audio Output";
|
2014-12-05 20:13:45 -05:00
|
|
|
|
2014-12-04 19:44:49 -05:00
|
|
|
try {
|
2014-12-18 20:11:25 -05:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2014-12-18 20:13:29 -05:00
|
|
|
opts.priority = sched_get_priority_max(SCHED_FIFO);
|
|
|
|
// opts.flags = RTAUDIO_MINIMIZE_LATENCY;
|
|
|
|
opts.flags = RTAUDIO_SCHEDULE_REALTIME;
|
|
|
|
|
2014-12-18 20:11:25 -05:00
|
|
|
if (deviceController.find(parameters.deviceId) == deviceController.end()) {
|
|
|
|
deviceController[parameters.deviceId] = new AudioThread(NULL, NULL);
|
|
|
|
deviceController[parameters.deviceId]->bindThread(this);
|
|
|
|
deviceThread[parameters.deviceId] = new std::thread(&AudioThread::threadMain, deviceController[parameters.deviceId]);
|
|
|
|
} else if (deviceController[parameters.deviceId] == this) {
|
|
|
|
dac.openStream(¶meters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &audioCallback, (void *) this, &opts);
|
|
|
|
dac.startStream();
|
|
|
|
} else {
|
|
|
|
deviceController[parameters.deviceId]->bindThread(this);
|
|
|
|
}
|
|
|
|
active = true;
|
|
|
|
#else
|
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();
|
2014-12-18 20:11:25 -05:00
|
|
|
|
|
|
|
#endif
|
2014-12-04 19:44:49 -05:00
|
|
|
} 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-18 20:11:25 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
if (deviceController[parameters.deviceId] != this) {
|
|
|
|
deviceController[parameters.deviceId]->removeThread(this);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
dac.stopStream();
|
|
|
|
dac.closeStream();
|
|
|
|
} catch (RtAudioError& e) {
|
|
|
|
e.printMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2014-12-04 19:44:49 -05:00
|
|
|
try {
|
|
|
|
// Stop the stream
|
|
|
|
dac.stopStream();
|
2014-12-18 20:11:25 -05:00
|
|
|
dac.closeStream();
|
2014-12-04 19:44:49 -05:00
|
|
|
} 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-12-18 20:11:25 -05:00
|
|
|
#endif
|
2014-11-30 17:11:29 -05:00
|
|
|
|
|
|
|
std::cout << "Audio thread done." << std::endl;
|
2014-12-11 19:07:21 -05:00
|
|
|
|
2014-12-18 20:11:25 -05:00
|
|
|
if (threadQueueNotify != NULL) {
|
|
|
|
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
|
|
|
}
|
2014-12-18 20:11:25 -05:00
|
|
|
|
|
|
|
bool AudioThread::isActive() {
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioThread::setActive(bool state) {
|
|
|
|
#ifdef __APPLE__
|
|
|
|
AudioThreadInput dummy;
|
|
|
|
if (state && !active) {
|
|
|
|
while (!inputQueue->empty()) { // flush queue
|
|
|
|
inputQueue->pop(dummy);
|
2014-12-22 19:43:56 -05:00
|
|
|
if (dummy.data) {
|
|
|
|
delete dummy.data;
|
|
|
|
}
|
2014-12-18 20:11:25 -05:00
|
|
|
}
|
2014-12-22 21:12:13 -05:00
|
|
|
deviceController[parameters.deviceId]->bindThread(this);
|
2014-12-18 20:11:25 -05:00
|
|
|
} else if (!state && active) {
|
|
|
|
deviceController[parameters.deviceId]->removeThread(this);
|
|
|
|
while (!inputQueue->empty()) { // flush queue
|
|
|
|
inputQueue->pop(dummy);
|
2014-12-22 19:43:56 -05:00
|
|
|
if (dummy.data) {
|
|
|
|
delete dummy.data;
|
|
|
|
}
|
2014-12-18 20:11:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
active = state;
|
|
|
|
}
|