mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2026-06-01 21:54:39 -04:00
Merge pull request #14 from cjcliffe/integrate_rtaudio
Integrate RtAudio
This commit is contained in:
@@ -47,6 +47,16 @@ bool CubicSDR::OnInit() {
|
||||
|
||||
AppFrame *appframe = new AppFrame();
|
||||
|
||||
#ifdef __APPLE__
|
||||
int main_policy;
|
||||
struct sched_param main_param;
|
||||
|
||||
main_policy = SCHED_OTHER;
|
||||
main_param.sched_priority = sched_get_priority_min(SCHED_OTHER);
|
||||
|
||||
pthread_setschedparam(pthread_self(), main_policy, &main_param);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#define BUF_SIZE (16384*6)
|
||||
#define BUF_SIZE (16384*4)
|
||||
#define SRATE 2000000
|
||||
#define FFT_SIZE 2048
|
||||
|
||||
|
||||
+71
-40
@@ -3,7 +3,7 @@
|
||||
#include <vector>
|
||||
|
||||
AudioThread::AudioThread(AudioThreadInputQueue *inputQueue) :
|
||||
inputQueue(inputQueue), stream(NULL), terminated(false) {
|
||||
inputQueue(inputQueue), terminated(false), audio_queue_ptr(0) {
|
||||
|
||||
}
|
||||
|
||||
@@ -11,54 +11,85 @@ AudioThread::~AudioThread() {
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
std::cout << "Audio buffer underflow.." << std::endl;
|
||||
}
|
||||
if (!src->audio_queue.size()) {
|
||||
for (int i = 0; i < nBufferFrames * 2; i++) {
|
||||
out[i] = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
std::vector<float> nextBuffer = src->audio_queue.front();
|
||||
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();
|
||||
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;
|
||||
}
|
||||
nextBuffer = src->audio_queue.front();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void AudioThread::threadMain() {
|
||||
std::cout << "Audio thread initializing.." << std::endl;
|
||||
|
||||
PaError err;
|
||||
err = Pa_Initialize();
|
||||
if (err != paNoError) {
|
||||
std::cout << "Error starting portaudio :(" << std::endl;
|
||||
return;
|
||||
}
|
||||
if (dac.getDeviceCount() < 1) {
|
||||
std::cout << "No audio devices found!" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
int preferred_device = -1;
|
||||
RtAudio::StreamParameters parameters;
|
||||
parameters.deviceId = dac.getDefaultOutputDevice();
|
||||
parameters.nChannels = 2;
|
||||
parameters.firstChannel = 0;
|
||||
unsigned int sampleRate = AUDIO_FREQUENCY;
|
||||
unsigned int bufferFrames = 256;
|
||||
|
||||
outputParameters.device =
|
||||
(preferred_device != -1) ?
|
||||
preferred_device : Pa_GetDefaultOutputDevice();
|
||||
if (outputParameters.device == paNoDevice) {
|
||||
std::cout << "Error: No default output device.\n";
|
||||
}
|
||||
RtAudio::StreamOptions opts;
|
||||
opts.flags = RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_SCHEDULE_REALTIME;
|
||||
opts.streamName = "CubicSDR Audio Output";
|
||||
opts.priority = 0;
|
||||
|
||||
outputParameters.channelCount = 2;
|
||||
outputParameters.sampleFormat = paFloat32;
|
||||
outputParameters.suggestedLatency = Pa_GetDeviceInfo(
|
||||
outputParameters.device)->defaultHighOutputLatency;
|
||||
outputParameters.hostApiSpecificStreamInfo = NULL;
|
||||
try {
|
||||
dac.openStream(¶meters, NULL, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &audioCallback, (void *) this, &opts);
|
||||
dac.startStream();
|
||||
} catch (RtAudioError& e) {
|
||||
e.printMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
stream = NULL;
|
||||
while (!terminated) {
|
||||
AudioThreadInput inp;
|
||||
inputQueue->pop(inp);
|
||||
if (inp.data.size()) {
|
||||
audio_queue.push(inp.data);
|
||||
}
|
||||
}
|
||||
|
||||
err = Pa_OpenStream(&stream, NULL, &outputParameters, AUDIO_FREQUENCY,
|
||||
paFramesPerBufferUnspecified, paClipOff, NULL, NULL);
|
||||
try {
|
||||
// Stop the stream
|
||||
dac.stopStream();
|
||||
} catch (RtAudioError& e) {
|
||||
e.printMessage();
|
||||
}
|
||||
|
||||
err = Pa_StartStream(stream);
|
||||
if (err != paNoError) {
|
||||
std::cout << "Error starting stream: " << Pa_GetErrorText(err)
|
||||
<< std::endl;
|
||||
std::cout << "\tPortAudio error: " << Pa_GetErrorText(err) << std::endl;
|
||||
}
|
||||
|
||||
while (!terminated) {
|
||||
AudioThreadInput inp;
|
||||
inputQueue->pop(inp);
|
||||
if (inp.data.size()) {
|
||||
Pa_WriteStream(stream, &inp.data[0], inp.data.size()/2);
|
||||
}
|
||||
}
|
||||
|
||||
err = Pa_StopStream(stream);
|
||||
err = Pa_CloseStream(stream);
|
||||
Pa_Terminate();
|
||||
if (dac.isStreamOpen()) {
|
||||
dac.closeStream();
|
||||
}
|
||||
|
||||
std::cout << "Audio thread done." << std::endl;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#include "AudioThread.h"
|
||||
#include "ThreadQueue.h"
|
||||
#include "portaudio.h"
|
||||
#include "RtAudio.h"
|
||||
|
||||
class AudioThreadInput {
|
||||
public:
|
||||
@@ -28,7 +28,10 @@ typedef ThreadQueue<AudioThreadInput> AudioThreadInputQueue;
|
||||
|
||||
class AudioThread {
|
||||
public:
|
||||
AudioThread(AudioThreadInputQueue *inputQueue);
|
||||
std::queue<std::vector<float> > audio_queue;
|
||||
unsigned int audio_queue_ptr;
|
||||
|
||||
AudioThread(AudioThreadInputQueue *inputQueue);
|
||||
~AudioThread();
|
||||
|
||||
void threadMain();
|
||||
@@ -36,8 +39,7 @@ public:
|
||||
|
||||
private:
|
||||
AudioThreadInputQueue *inputQueue;
|
||||
PaStreamParameters outputParameters;
|
||||
PaStream *stream;
|
||||
RtAudio dac;
|
||||
std::atomic<bool> terminated;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
ScopeContext::ScopeContext(ScopeCanvas *canvas, wxGLContext *sharedContext) :
|
||||
PrimaryGLContext(canvas, sharedContext) {
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
SpectrumContext::SpectrumContext(SpectrumCanvas *canvas, wxGLContext *sharedContext) :
|
||||
PrimaryGLContext(canvas, sharedContext) {
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
WaterfallContext::WaterfallContext(WaterfallCanvas *canvas, wxGLContext *sharedContext) :
|
||||
PrimaryGLContext(canvas, sharedContext) {
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
Reference in New Issue
Block a user