OSX audio may be stable, works on 2010 macbook pro

This commit is contained in:
Charles J. Cliffe 2014-12-05 20:13:45 -05:00
parent 7c6d2e1131
commit ad800056d5
4 changed files with 67 additions and 24 deletions

View File

@ -51,8 +51,8 @@ bool CubicSDR::OnInit() {
int main_policy;
struct sched_param main_param;
main_policy = SCHED_OTHER;
main_param.sched_priority = sched_get_priority_min(SCHED_OTHER);
main_policy = SCHED_RR;
main_param.sched_priority = sched_get_priority_min(SCHED_RR)+2;
pthread_setschedparam(pthread_self(), main_policy, &main_param);
#endif

View File

@ -1,6 +1,10 @@
#pragma once
#ifdef __APPLE__
#define BUF_SIZE (16384*3)
#else
#define BUF_SIZE (16384*4)
#endif
#define SRATE 2000000
#define FFT_SIZE 2048

View File

@ -3,7 +3,7 @@
#include <vector>
AudioThread::AudioThread(AudioThreadInputQueue *inputQueue) :
inputQueue(inputQueue), terminated(false), audio_queue_ptr(0) {
inputQueue(inputQueue), terminated(false), audio_queue_ptr(0), underflow_count(0) {
}
@ -16,29 +16,63 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu
AudioThread *src = (AudioThread *) userData;
float *out = (float*) outputBuffer;
if (status) {
std::cout << "Audio buffer underflow.." << std::endl;
std::cout << "Audio buffer underflow.." << (src->underflow_count++) << std::endl;
}
if (!src->audio_queue.size()) {
std::queue<std::vector<float> > *audio_queue = src->audio_queue.load();
#ifdef __APPLE__ // Crude re-sync
int wait_for_it = 0;
if (audio_queue->empty()) {
while (wait_for_it++ < 50) { // Can we wait it out?
std::this_thread::sleep_for(std::chrono::microseconds(10000));
// std::this_thread::yield();
if (!audio_queue->empty()) {
std::cout << "Buffer recovery.." << std::endl;
break;
}
}
}
#endif
if (audio_queue->empty()) {
for (int i = 0; i < nBufferFrames * 2; i++) {
out[i] = 0;
}
return 0;
}
std::vector<float> nextBuffer = src->audio_queue.front();
wait_for_it = 0;
std::vector<float> nextBuffer = audio_queue->front();
int nextBufferSize = nextBuffer.size();
for (int i = 0; i < nBufferFrames * 2; i++) {
out[i] = nextBuffer[src->audio_queue_ptr];
src->audio_queue_ptr++;
if (src->audio_queue_ptr == nextBuffer.size()) {
src->audio_queue.pop();
if (src->audio_queue_ptr == nextBufferSize) {
audio_queue->pop();
src->audio_queue_ptr = 0;
if (!src->audio_queue.size()) {
for (int j = i; j < nBufferFrames * 2; j++) {
std::cout << "Audio buffer underflow.." << std::endl;
out[i] = 0;
}
return 0;
if (audio_queue->empty()) {
#ifdef __APPLE__
while (wait_for_it++ < 50) { // Can we wait it out?
std::this_thread::sleep_for(std::chrono::microseconds(10000));
if (!audio_queue->empty()) {
std::cout << "Buffer recovery.." << std::endl;
break;
}
}
#endif
if (audio_queue->empty()) {
for (int j = i; j < nBufferFrames * 2; j++) {
std::cout << "Audio buffer underflow mid request.." << (src->underflow_count++) << std::endl;
out[i] = 0;
}
return 0;
}
}
nextBuffer = src->audio_queue.front();
nextBuffer = audio_queue->front();
}
}
return 0;
@ -57,12 +91,15 @@ void AudioThread::threadMain() {
parameters.nChannels = 2;
parameters.firstChannel = 0;
unsigned int sampleRate = AUDIO_FREQUENCY;
unsigned int bufferFrames = 256;
unsigned int bufferFrames = 0;
RtAudio::StreamOptions opts;
opts.flags = RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_SCHEDULE_REALTIME;
// opts.flags = RTAUDIO_SCHEDULE_REALTIME | RTAUDIO_MINIMIZE_LATENCY;
// opts.flags = RTAUDIO_MINIMIZE_LATENCY;
opts.streamName = "CubicSDR Audio Output";
opts.priority = 0;
opts.priority = sched_get_priority_max(SCHED_FIFO);
audio_queue = new std::queue<std::vector<float> >;
try {
dac.openStream(&parameters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &audioCallback, (void *) this, &opts);
@ -74,10 +111,11 @@ void AudioThread::threadMain() {
while (!terminated) {
AudioThreadInput inp;
inputQueue->pop(inp);
if (inp.data.size()) {
audio_queue.push(inp.data);
}
inputQueue->pop(inp);
if (inp.data.size()) {
audio_queue.load()->push(inp.data);
}
// std::this_thread::yield();
}
try {

View File

@ -28,8 +28,9 @@ typedef ThreadQueue<AudioThreadInput> AudioThreadInputQueue;
class AudioThread {
public:
std::queue<std::vector<float> > audio_queue;
unsigned int audio_queue_ptr;
std::atomic< std::queue<std::vector<float> > *> audio_queue;
std::atomic<unsigned int> audio_queue_ptr;
std::atomic<unsigned int> underflow_count;
AudioThread(AudioThreadInputQueue *inputQueue);
~AudioThread();