Bind audio API choice to do something.
[dcpomatic.git] / src / wx / config_dialog.cc
index c3d17748c3308e926e5d7ddaa1bc2711d5c0a074..3055d248b500adc74aa16adefeb61997c6521063 100644 (file)
 */
 
 
+#include "audio_api.h"
 #include "audio_mapping_view.h"
 #include "check_box.h"
+#include "dcpomatic_choice.h"
 #include "config_dialog.h"
 #include "dcpomatic_button.h"
 #include "film_viewer.h"
@@ -862,8 +864,10 @@ SoundPage::setup ()
        _sound = new CheckBox (_panel, _("Play sound via"));
        table->Add (_sound, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
        auto s = new wxBoxSizer (wxHORIZONTAL);
+       _sound_api = new Choice(_panel);
+       s->Add(_sound_api);
        _sound_output = new wxChoice (_panel, wxID_ANY);
-       s->Add (_sound_output, 0);
+       s->Add(_sound_output, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
        _sound_output_details = new wxStaticText (_panel, wxID_ANY, wxT(""));
        s->Add (_sound_output_details, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, DCPOMATIC_SIZER_X_GAP);
        table->Add (s, wxGBPosition(r, 1));
@@ -884,6 +888,25 @@ SoundPage::setup ()
        font.SetPointSize (font.GetPointSize() - 1);
        _sound_output_details->SetFont (font);
 
+       for (auto const& api: audio_apis()) {
+               _sound_api->add(api.name(), new wxStringClientData(api.id()));
+       }
+
+       update_sound_outputs();
+
+       _sound->bind(&SoundPage::sound_changed, this);
+       _sound_api->bind(&SoundPage::sound_api_changed, this);
+       _sound_output->Bind(wxEVT_CHOICE, bind(&SoundPage::sound_output_changed, this));
+       _map->Changed.connect(bind(&SoundPage::map_changed, this, _1));
+       _reset_to_default->Bind(wxEVT_BUTTON, bind(&SoundPage::reset_to_default, this));
+}
+
+
+
+void
+SoundPage::update_sound_outputs()
+{
+       _sound_output->Clear();
        auto& audio = _viewer->audio_backend();
        for (unsigned int i = 0; i < audio.getDeviceCount(); ++i) {
                try {
@@ -895,13 +918,9 @@ SoundPage::setup ()
                        /* Something went wrong so let's just ignore that device */
                }
        }
-
-       _sound->bind(&SoundPage::sound_changed, this);
-       _sound_output->Bind (wxEVT_CHOICE,   bind(&SoundPage::sound_output_changed, this));
-       _map->Changed.connect (bind(&SoundPage::map_changed, this, _1));
-       _reset_to_default->Bind (wxEVT_BUTTON,   bind(&SoundPage::reset_to_default, this));
 }
 
+
 void
 SoundPage::reset_to_default ()
 {
@@ -920,6 +939,15 @@ SoundPage::sound_changed ()
        Config::instance()->set_sound (_sound->GetValue ());
 }
 
+
+void
+SoundPage::sound_api_changed()
+{
+       Config::instance()->set_sound_api(id_to_audio_api(get_sound_api()).id());
+       Config::instance()->unset_sound_output();
+}
+
+
 void
 SoundPage::sound_output_changed ()
 {
@@ -945,6 +973,22 @@ SoundPage::config_changed ()
 
        checked_set (_sound, config->sound ());
 
+       auto const current_api = get_sound_api();
+       auto const configured_api = id_to_audio_api(config->sound_api()).id();
+
+       if (current_api != configured_api) {
+               /* Update _sound_api with the configured value */
+               unsigned int index = 0;
+               while (index < _sound_api->GetCount()) {
+                       if (string_client_data(_sound_api->GetClientObject(index)) == configured_api) {
+                               _sound_api->SetSelection(index);
+                       }
+                       ++index;
+               }
+       }
+
+       update_sound_outputs();
+
        auto const current_so = get_sound_output ();
        optional<string> configured_so;
 
@@ -1025,13 +1069,26 @@ SoundPage::setup_sensitivity ()
        _sound_output->Enable (_sound->GetValue());
 }
 
+
+optional<string>
+SoundPage::get_sound_api() const
+{
+       auto const sel = _sound_api->get();
+       if (!sel) {
+               return {};
+       }
+
+       return index_to_audio_api(*sel).id();
+}
+
+
 /** @return Currently-selected preview sound output in the dialogue */
 optional<string>
-SoundPage::get_sound_output ()
+SoundPage::get_sound_output() const
 {
        int const sel = _sound_output->GetSelection ();
        if (sel == wxNOT_FOUND) {
-               return optional<string> ();
+               return {};
        }
 
        return wx_to_std (_sound_output->GetString (sel));