CubicSDR/src/audio/AudioThread.cpp

72 lines
1.6 KiB
C++
Raw Normal View History

#include "AudioThread.h"
#include "CubicSDRDefs.h"
#include <vector>
//wxDEFINE_EVENT(wxEVT_COMMAND_AudioThread_INPUT, wxThreadEvent);
AudioThread::AudioThread(AudioThreadQueue* pQueue, int id) :
2014-11-19 23:41:57 -05:00
wxThread(wxTHREAD_DETACHED), m_pQueue(pQueue), m_ID(id), audio_queue_ptr(
0) { //, stream(NULL)
}
AudioThread::~AudioThread() {
2014-11-19 23:41:57 -05:00
ao_close(device);
ao_shutdown();
}
2014-11-19 23:41:57 -05:00
wxThread::ExitCode AudioThread::Entry() {
2014-11-19 23:41:57 -05:00
ao_initialize();
2014-11-19 23:41:57 -05:00
/* -- Setup for default driver -- */
2014-11-19 23:41:57 -05:00
int default_driver;
2014-11-19 23:41:57 -05:00
default_driver = ao_default_driver_id();
2014-11-19 23:41:57 -05:00
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = 2;
format.rate = AUDIO_FREQUENCY;
format.byte_format = AO_FMT_LITTLE;
2014-11-19 23:41:57 -05:00
/* -- Open driver -- */
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
fprintf(stderr, "Error opening device.\n");
return (wxThread::ExitCode) 1;
}
2014-11-19 23:41:57 -05:00
while (!TestDestroy()) {
2014-11-19 23:41:57 -05:00
if (m_pQueue->stackSize()) {
2014-11-19 23:41:57 -05:00
while (m_pQueue->stackSize()) {
AudioThreadTask task = m_pQueue->pop(); // pop a task from the queue. this will block the worker thread if queue is empty
switch (task.m_cmd) {
case AudioThreadTask::AUDIO_THREAD_DATA:
2014-11-19 23:41:57 -05:00
int16_t buf[task.data->data.size()];
2014-11-19 23:41:57 -05:00
for (int i = 0; i < task.data->data.size(); i++) {
buf[i] = (int) (task.data->data[i] * 32760.0);
}
2014-11-19 23:41:57 -05:00
ao_play(device, (char *) buf,
task.data->data.size() * sizeof(int16_t));
2014-11-19 23:41:57 -05:00
delete task.data;
break;
}
}
} else {
this->Yield();
this->Sleep(1);
}
}
std::cout << std::endl << "Audio Thread Done." << std::endl << std::endl;
2014-11-19 23:41:57 -05:00
return (wxThread::ExitCode) 0;
}