Audio device selection fixes for OSX

This commit is contained in:
Charles J. Cliffe 2014-12-31 22:38:59 -05:00
parent 3da7e18b88
commit 955ec54b44
4 changed files with 24 additions and 10 deletions

View File

@ -22,7 +22,7 @@
wxBEGIN_EVENT_TABLE(AppFrame, wxFrame)
//EVT_MENU(wxID_NEW, AppFrame::OnNewWindow)
//EVT_MENU(wxID_CLOSE, AppFrame::OnClose)
EVT_MENU(wxID_CLOSE, AppFrame::OnClose)
EVT_MENU(wxID_ANY, AppFrame::OnMenu)
EVT_COMMAND(wxID_ANY, wxEVT_THREAD, AppFrame::OnThread)
@ -122,7 +122,6 @@ AppFrame::AppFrame() :
wxMenu *menu = new wxMenu;
std::vector<RtAudio::DeviceInfo> devices;
std::vector<RtAudio::DeviceInfo>::iterator devices_i;
std::map<int,RtAudio::DeviceInfo>::iterator mdevices_i;
AudioThread::enumerateDevices(devices);
@ -143,6 +142,7 @@ AppFrame::AppFrame() :
for (mdevices_i = output_devices.begin(); mdevices_i != output_devices.end(); mdevices_i++) {
wxMenuItem *itm = menu->AppendRadioItem(wxID_RT_AUDIO_DEVICE+mdevices_i->first,mdevices_i->second.name,wxT("Description?"));
itm->SetId(wxID_RT_AUDIO_DEVICE+mdevices_i->first);
if (mdevices_i->second.isDefaultOutput) {
itm->Check(true);
}
@ -171,7 +171,7 @@ AppFrame::~AppFrame() {
}
void AppFrame::OnMenu(wxCommandEvent& event) {
if (event.GetId() >= wxID_RT_AUDIO_DEVICE && event.GetId() < wxID_RT_AUDIO_DEVICE+output_devices.size()) {
if (event.GetId() >= wxID_RT_AUDIO_DEVICE && event.GetId() < wxID_RT_AUDIO_DEVICE+devices.size()) {
if (activeDemodulator) {
activeDemodulator->setOutputDevice(event.GetId()-wxID_RT_AUDIO_DEVICE);
activeDemodulator = NULL;
@ -179,6 +179,10 @@ void AppFrame::OnMenu(wxCommandEvent& event) {
}
}
void AppFrame::OnClose(wxCommandEvent& WXUNUSED(event)) {
Close(true);
}
void AppFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event)) {
new AppFrame();
}

View File

@ -10,7 +10,7 @@
#include <map>
#define wxID_RT_AUDIO_DEVICE 10000
#define wxID_RT_AUDIO_DEVICE 1000
// Define a new frame type
class AppFrame: public wxFrame {
@ -22,6 +22,7 @@ public:
private:
void OnMenu(wxCommandEvent& event);
void OnClose(wxCommandEvent& event);
void OnNewWindow(wxCommandEvent& event);
void OnIdle(wxIdleEvent& event);
@ -35,6 +36,7 @@ private:
DemodulatorInstance *activeDemodulator;
std::vector<RtAudio::DeviceInfo> devices;
std::map<int,RtAudio::DeviceInfo> input_devices;
std::map<int,RtAudio::DeviceInfo> output_devices;
std::map<int,wxMenuItem *> output_device_menuitems;

View File

@ -25,7 +25,7 @@ AudioThread::~AudioThread() {
#ifdef __APPLE__
void AudioThread::bindThread(AudioThread *other) {
if (boundThreads.find(other) == boundThreads.end()) {
if (std::find(boundThreads.load()->begin(), boundThreads.load()->end(), other) == boundThreads.load()->end()) {
boundThreads.load()->push_back(other);
}
}
@ -275,13 +275,11 @@ void AudioThread::setupDevice(int deviceId) {
RtAudio::StreamOptions opts;
opts.streamName = "CubicSDR Audio Output";
output_device = deviceId;
try {
#ifdef __APPLE__
if (active && deviceController.find(parameters.deviceId) != deviceController.end()) {
deviceController[parameters.deviceId]->removeThread(this);
if (deviceController.find(output_device.load()) != deviceController.end()) {
deviceController[output_device.load()]->removeThread(this);
}
opts.priority = sched_get_priority_max(SCHED_FIFO);
@ -290,6 +288,7 @@ void AudioThread::setupDevice(int deviceId) {
if (deviceController.find(parameters.deviceId) == deviceController.end()) {
deviceController[parameters.deviceId] = new AudioThread(NULL, NULL);
deviceController[parameters.deviceId]->setInitOutputDevice(parameters.deviceId);
deviceController[parameters.deviceId]->bindThread(this);
deviceThread[parameters.deviceId] = new std::thread(&AudioThread::threadMain, deviceController[parameters.deviceId]);
} else if (deviceController[parameters.deviceId] == this) {
@ -315,6 +314,8 @@ void AudioThread::setupDevice(int deviceId) {
e.printMessage();
return;
}
output_device = deviceId;
}
int AudioThread::getOutputDevice() {
@ -324,6 +325,10 @@ int AudioThread::getOutputDevice() {
return output_device;
}
void AudioThread::setInitOutputDevice(int deviceId) {
output_device = deviceId;
}
void AudioThread::threadMain() {
#ifdef __APPLE__
pthread_t tID = pthread_self(); // ID of this thread
@ -339,7 +344,9 @@ void AudioThread::threadMain() {
return;
}
setupDevice(dac.getDefaultOutputDevice());
setupDevice((output_device.load() == -1)?(dac.getDefaultOutputDevice()):output_device.load());
std::cout << "Audio thread started." << std::endl;
while (!terminated) {
AudioThreadCommand command;

View File

@ -70,6 +70,7 @@ public:
static void enumerateDevices(std::vector<RtAudio::DeviceInfo> &devs);
void setupDevice(int deviceId);
void setInitOutputDevice(int deviceId);
int getOutputDevice();
void threadMain();
void terminate();