diff --git a/src/AppFrame.cpp b/src/AppFrame.cpp index 9308687..794817a 100644 --- a/src/AppFrame.cpp +++ b/src/AppFrame.cpp @@ -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 devices; std::vector::iterator devices_i; std::map::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(); } diff --git a/src/AppFrame.h b/src/AppFrame.h index be1b2ea..7dc83a9 100644 --- a/src/AppFrame.h +++ b/src/AppFrame.h @@ -10,7 +10,7 @@ #include -#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 devices; std::map input_devices; std::map output_devices; std::map output_device_menuitems; diff --git a/src/audio/AudioThread.cpp b/src/audio/AudioThread.cpp index 5a2d012..f1b75b6 100644 --- a/src/audio/AudioThread.cpp +++ b/src/audio/AudioThread.cpp @@ -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; diff --git a/src/audio/AudioThread.h b/src/audio/AudioThread.h index 1e9ed8e..36f53f2 100644 --- a/src/audio/AudioThread.h +++ b/src/audio/AudioThread.h @@ -70,6 +70,7 @@ public: static void enumerateDevices(std::vector &devs); void setupDevice(int deviceId); + void setInitOutputDevice(int deviceId); int getOutputDevice(); void threadMain(); void terminate();