fixup! Lay things out better with long audio output names.
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file src/wx/wx_util.cc
23  *  @brief Some utility functions and classes.
24  */
25
26
27 #include "file_picker_ctrl.h"
28 #include "password_entry.h"
29 #include "static_text.h"
30 #include "wx_util.h"
31 #include "lib/config.h"
32 #include "lib/cross.h"
33 #include "lib/job.h"
34 #include "lib/job_manager.h"
35 #include "lib/util.h"
36 #include "lib/version.h"
37 #include <dcp/locale_convert.h>
38 #include <dcp/warnings.h>
39 LIBDCP_DISABLE_WARNINGS
40 #include <wx/spinctrl.h>
41 #include <wx/splash.h>
42 #include <wx/progdlg.h>
43 #include <wx/filepicker.h>
44 #include <wx/sizer.h>
45 #include <RtAudio.h>
46 LIBDCP_ENABLE_WARNINGS
47 #include <boost/thread.hpp>
48
49
50 using std::string;
51 using std::vector;
52 using std::pair;
53 using std::shared_ptr;
54 using boost::optional;
55 using dcp::locale_convert;
56 using namespace dcpomatic;
57
58
59 wxStaticText *
60 #ifdef __WXOSX__
61 create_label (wxWindow* p, wxString t, bool left)
62 #else
63 create_label (wxWindow* p, wxString t, bool)
64 #endif
65 {
66 #ifdef __WXOSX__
67         if (left) {
68                 t += wxT (":");
69         }
70 #endif
71         return new StaticText (p, t);
72 }
73
74
75 #ifdef __WXOSX__
76 static
77 void
78 setup_osx_flags (wxSizer* s, bool left, int& flags)
79 {
80         if (left) {
81                 auto box = dynamic_cast<wxBoxSizer*>(s);
82                 if (!box || box->GetOrientation() != wxHORIZONTAL) {
83                         flags |= wxALIGN_RIGHT;
84                 }
85         }
86 }
87 #endif
88
89
90 /** Add a wxStaticText to a wxSizer.
91  *  @param s Sizer to add to.
92  *  @param p Parent window for the wxStaticText.
93  *  @param t Text for the wxStaticText.
94  *  @param left true if this label is a `left label'; ie the sort
95  *  of label which should be right-aligned on OS X.
96  *  @param prop Proportion to pass when calling Add() on the wxSizer.
97  */
98 wxStaticText *
99 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
100 {
101 #ifdef __WXOSX__
102         setup_osx_flags (s, left, flags);
103 #endif
104         auto m = create_label (p, t, left);
105         s->Add (m, prop, flags, DCPOMATIC_SIZER_GAP);
106         return m;
107 }
108
109
110 wxStaticText *
111 #ifdef __WXOSX__
112 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
113 #else
114 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
115 #endif
116 {
117 #ifdef __WXOSX__
118         setup_osx_flags (s, left, flags);
119 #endif
120         s->Add (t, prop, flags, DCPOMATIC_SIZER_GAP);
121         return t;
122 }
123
124
125 wxStaticText *
126 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
127 {
128         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
129 #ifdef __WXOSX__
130         setup_osx_flags (s, left, flags);
131 #endif
132         auto m = create_label (p, t, left);
133         s->Add (m, pos, span, flags);
134         return m;
135 }
136
137
138 wxStaticText *
139 #ifdef __WXOSX__
140 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
141 #else
142 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
143 #endif
144 {
145         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
146 #ifdef __WXOSX__
147         setup_osx_flags (s, left, flags);
148 #endif
149         s->Add (t, pos, span, flags);
150         return t;
151 }
152
153
154 /** Pop up an error dialogue box.
155  *  @param parent Parent.
156  *  @param m Message.
157  *  @param e Extended message.
158  */
159 void
160 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
161 {
162         auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
163         if (e) {
164                 wxString em = *e;
165                 em[0] = wxToupper (em[0]);
166                 d->SetExtendedMessage (em);
167         }
168         d->ShowModal ();
169         d->Destroy ();
170 }
171
172
173 /** Pop up an error dialogue box.
174  *  @param parent Parent.
175  *  @param m Message.
176  */
177 void
178 message_dialog (wxWindow* parent, wxString m)
179 {
180         auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
181         d->ShowModal ();
182         d->Destroy ();
183 }
184
185
186 /** @return true if the user answered "yes" */
187 bool
188 confirm_dialog (wxWindow* parent, wxString m)
189 {
190         auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
191         int const r = d->ShowModal ();
192         d->Destroy ();
193         return r == wxID_YES;
194 }
195
196
197 /** @param s wxWidgets string.
198  *  @return Corresponding STL string.
199  */
200 string
201 wx_to_std (wxString s)
202 {
203         return string (s.ToUTF8());
204 }
205
206
207 /** @param s STL string.
208  *  @return Corresponding wxWidgets string.
209  */
210 wxString
211 std_to_wx (string s)
212 {
213         return wxString (s.c_str(), wxConvUTF8);
214 }
215
216
217 string
218 string_client_data (wxClientData* o)
219 {
220         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
221 }
222
223
224 string
225 selection_string_client_data(wxChoice* choice)
226 {
227         return string_client_data(choice->GetClientObject(choice->GetSelection()));
228 }
229
230
231 void
232 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
233 {
234         if (widget->GetPath() != std_to_wx (value.string())) {
235                 if (value.empty()) {
236                         /* Hack to make wxWidgets clear the control when we are passed
237                            an empty value.
238                         */
239                         value = " ";
240                 }
241                 widget->SetPath (std_to_wx (value.string()));
242         }
243 }
244
245
246 void
247 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
248 {
249         if (widget->GetPath() != std_to_wx (value.string())) {
250                 if (value.empty()) {
251                         /* Hack to make wxWidgets clear the control when we are passed
252                            an empty value.
253                         */
254                         value = " ";
255                 }
256                 widget->SetPath (std_to_wx (value.string()));
257         }
258 }
259
260
261 void
262 checked_set (wxSpinCtrl* widget, int value)
263 {
264         if (widget->GetValue() != value) {
265                 widget->SetValue (value);
266         }
267 }
268
269
270 void
271 checked_set (wxSpinCtrlDouble* widget, double value)
272 {
273         /* XXX: completely arbitrary epsilon */
274         if (fabs (widget->GetValue() - value) > 1e-16) {
275                 widget->SetValue (value);
276         }
277 }
278
279
280 void
281 checked_set (wxChoice* widget, int value)
282 {
283         if (widget->GetSelection() != value) {
284                 widget->SetSelection (value);
285         }
286 }
287
288
289 void
290 checked_set (wxChoice* widget, string value)
291 {
292         wxClientData* o = nullptr;
293         if (widget->GetSelection() != -1) {
294                 o = widget->GetClientObject (widget->GetSelection ());
295         }
296
297         if (!o || string_client_data(o) != value) {
298                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
299                         if (string_client_data (widget->GetClientObject (i)) == value) {
300                                 widget->SetSelection (i);
301                         }
302                 }
303         }
304 }
305
306
307 void
308 checked_set (wxChoice* widget, vector<pair<string, string>> items)
309 {
310        vector<pair<string, string>> current;
311        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
312                current.push_back (
313                        make_pair(
314                                wx_to_std(widget->GetString(i)),
315                                widget->GetClientData() ? string_client_data(widget->GetClientObject(i)) : ""
316                                )
317                        );
318        }
319
320        if (current == items) {
321                return;
322        }
323
324        widget->Clear ();
325        for (auto i: items) {
326                widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
327        }
328 }
329
330
331 void
332 checked_set (wxTextCtrl* widget, string value)
333 {
334         if (widget->GetValue() != std_to_wx (value)) {
335                 widget->ChangeValue (std_to_wx (value));
336         }
337 }
338
339
340 void
341 checked_set (PasswordEntry* entry, string value)
342 {
343         if (entry->get() != value) {
344                 entry->set(value);
345         }
346 }
347
348
349 void
350 checked_set (wxTextCtrl* widget, wxString value)
351 {
352         if (widget->GetValue() != value) {
353                 widget->ChangeValue (value);
354         }
355 }
356
357
358 void
359 checked_set (wxStaticText* widget, string value)
360 {
361         if (widget->GetLabel() != std_to_wx (value)) {
362                 widget->SetLabel (std_to_wx (value));
363         }
364 }
365
366
367 void
368 checked_set (wxStaticText* widget, wxString value)
369 {
370         if (widget->GetLabel() != value) {
371                 widget->SetLabel (value);
372         }
373 }
374
375
376 void
377 checked_set (wxCheckBox* widget, bool value)
378 {
379         if (widget->GetValue() != value) {
380                 widget->SetValue (value);
381         }
382 }
383
384
385 void
386 checked_set (wxRadioButton* widget, bool value)
387 {
388         if (widget->GetValue() != value) {
389                 widget->SetValue (value);
390         }
391 }
392
393
394 void
395 dcpomatic_setup_i18n ()
396 {
397         int language = wxLANGUAGE_DEFAULT;
398
399         auto config_lang = Config::instance()->language ();
400         if (config_lang && !config_lang->empty ()) {
401                 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
402                 if (li) {
403                         language = li->Language;
404                 }
405         }
406
407         wxLocale* locale = nullptr;
408         if (wxLocale::IsAvailable (language)) {
409                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
410
411 #ifdef DCPOMATIC_WINDOWS
412                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
413 #endif
414
415 #ifdef DCPOMATIC_LINUX
416                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
417
418                 /* We have to include the wxWidgets .mo in our distribution,
419                    so we rename it to avoid clashes with any other installation
420                    of wxWidgets.
421                 */
422                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
423
424                 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
425                 locale->AddCatalog (wxT ("wxstd3"));
426 #endif
427
428                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
429                 locale->AddCatalog (wxT ("dcpomatic2"));
430
431                 if (!locale->IsOk()) {
432                         delete locale;
433                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
434                 }
435         }
436
437         if (locale) {
438                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
439         }
440 }
441
442
443 int
444 wx_get (wxSpinCtrl* w)
445 {
446         return w->GetValue ();
447 }
448
449
450 int
451 wx_get (wxChoice* w)
452 {
453         return w->GetSelection ();
454 }
455
456
457 double
458 wx_get (wxSpinCtrlDouble* w)
459 {
460         return w->GetValue ();
461 }
462
463
464 /** @param s String of the form Context|String
465  *  @return translation, or String if no translation is available.
466  */
467 wxString
468 context_translation (wxString s)
469 {
470         auto t = wxGetTranslation (s);
471         if (t == s) {
472                 /* No translation; strip the context */
473                 int c = t.Find (wxT ("|"));
474                 if (c != wxNOT_FOUND) {
475                         t = t.Mid (c + 1);
476                 }
477         }
478
479         return t;
480 }
481
482
483 wxString
484 time_to_timecode (DCPTime t, double fps)
485 {
486         auto w = t.seconds ();
487         int const h = (w / 3600);
488         w -= h * 3600;
489         int const m = (w / 60);
490         w -= m * 60;
491         int const s = floor (w);
492         w -= s;
493         int const f = lrint (w * fps);
494         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
495 }
496
497
498 void
499 setup_audio_channels_choice (wxChoice* choice, int minimum)
500 {
501         vector<pair<string, string>> items;
502         for (int i = minimum; i <= 16; i += 2) {
503                 if (i == 2) {
504                         items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
505                 } else if (i == 4) {
506                         items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
507                 } else if (i == 6) {
508                         items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
509                 } else if (i == 8) {
510                         items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
511                 } else if (i == 12) {
512                         items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
513                 } else {
514                         items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
515                 }
516         }
517
518         checked_set (choice, items);
519 }
520
521
522 wxSplashScreen *
523 maybe_show_splash ()
524 {
525         wxSplashScreen* splash = nullptr;
526         try {
527                 wxBitmap bitmap;
528                 if (bitmap.LoadFile(bitmap_path("splash.png"), wxBITMAP_TYPE_PNG)) {
529                         {
530                                 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
531                                 wxMemoryDC dc(bitmap);
532                                 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
533                                 auto screen_size = dc.GetSize();
534                                 auto text_size = dc.GetTextExtent(version);
535                                 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
536                         }
537 #ifdef DCPOMATIC_WINDOWS
538                         /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
539                         splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
540 #else
541                         splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
542 #endif
543                         wxYield ();
544                 }
545         } catch (boost::filesystem::filesystem_error& e) {
546                 /* Maybe we couldn't find the splash image; never mind */
547         }
548
549         return splash;
550 }
551
552
553 double
554 calculate_mark_interval (double mark_interval)
555 {
556         if (mark_interval > 5) {
557                 mark_interval -= lrint (mark_interval) % 5;
558         }
559         if (mark_interval > 10) {
560                 mark_interval -= lrint (mark_interval) % 10;
561         }
562         if (mark_interval > 60) {
563                 mark_interval -= lrint (mark_interval) % 60;
564         }
565         if (mark_interval > 3600) {
566                 mark_interval -= lrint (mark_interval) % 3600;
567         }
568
569         if (mark_interval < 1) {
570                 mark_interval = 1;
571         }
572
573         return mark_interval;
574 }
575
576
577 /** @return false if the task was cancelled */
578 bool
579 display_progress (wxString title, wxString task)
580 {
581         auto jm = JobManager::instance ();
582
583         wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
584
585         bool ok = true;
586
587         while (jm->work_to_do()) {
588                 dcpomatic_sleep_seconds (1);
589                 if (!progress.Pulse()) {
590                         /* user pressed cancel */
591                         for (auto i: jm->get()) {
592                                 i->cancel();
593                         }
594                         ok = false;
595                         break;
596                 }
597         }
598
599         return ok;
600 }
601
602
603 int
604 get_offsets (vector<Offset>& offsets)
605 {
606         offsets.push_back (Offset(_("UTC-11"),  -11,  0));
607         offsets.push_back (Offset(_("UTC-10"),  -10,  0));
608         offsets.push_back (Offset(_("UTC-9"),    -9,  0));
609         offsets.push_back (Offset(_("UTC-8"),    -8,  0));
610         offsets.push_back (Offset(_("UTC-7"),    -7,  0));
611         offsets.push_back (Offset(_("UTC-6"),    -6,  0));
612         offsets.push_back (Offset(_("UTC-5"),    -5,  0));
613         offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
614         offsets.push_back (Offset(_("UTC-4"),    -4,  0));
615         offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
616         offsets.push_back (Offset(_("UTC-3"),    -3,  0));
617         offsets.push_back (Offset(_("UTC-2"),    -2,  0));
618         offsets.push_back (Offset(_("UTC-1"),    -1,  0));
619         int utc = offsets.size();
620         offsets.push_back (Offset(_("UTC")  ,     0,  0));
621         offsets.push_back (Offset(_("UTC+1"),     1,  0));
622         offsets.push_back (Offset(_("UTC+2"),     2,  0));
623         offsets.push_back (Offset(_("UTC+3"),     3,  0));
624         offsets.push_back (Offset(_("UTC+4"),     4,  0));
625         offsets.push_back (Offset(_("UTC+5"),     5,  0));
626         offsets.push_back (Offset(_("UTC+5:30"),  5, 30));
627         offsets.push_back (Offset(_("UTC+6"),     6,  0));
628         offsets.push_back (Offset(_("UTC+7"),     7,  0));
629         offsets.push_back (Offset(_("UTC+8"),     8,  0));
630         offsets.push_back (Offset(_("UTC+9"),     9,  0));
631         offsets.push_back (Offset(_("UTC+9:30"),  9, 30));
632         offsets.push_back (Offset(_("UTC+10"),   10,  0));
633         offsets.push_back (Offset(_("UTC+11"),   11,  0));
634         offsets.push_back (Offset(_("UTC+12"),   12,  0));
635
636         return utc;
637 }
638
639
640 wxString
641 bitmap_path (string name)
642 {
643         boost::filesystem::path base;
644
645 #ifdef DCPOMATIC_DEBUG
646         /* Hack to allow Linux and OS X to find icons when running from the source tree */
647         char* path = getenv ("DCPOMATIC_GRAPHICS");
648         if (path) {
649                 base = path;
650         } else {
651                 base = resources_path();
652         }
653 #else
654         base = resources_path();
655 #endif
656
657         auto p = base / name;
658         return std_to_wx (p.string());
659 }
660
661
662 wxString
663 icon_path(string name)
664 {
665         return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
666 }
667
668
669 wxSize
670 small_button_size (wxWindow* parent, wxString text)
671 {
672         wxClientDC dc (parent);
673         auto size = dc.GetTextExtent (text);
674         size.SetHeight (-1);
675         size.IncBy (32, 0);
676         return size;
677 }
678
679
680 bool
681 gui_is_dark ()
682 {
683 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
684         auto appearance = wxSystemSettings::GetAppearance();
685         return appearance.IsDark();
686 #else
687         return false;
688 #endif
689 }
690
691
692 #if wxCHECK_VERSION(3,1,0)
693 double
694 dpi_scale_factor (wxWindow* window)
695 {
696         return window->GetDPIScaleFactor();
697 }
698 #else
699 double
700 dpi_scale_factor (wxWindow*)
701 {
702         return 1;
703 }
704 #endif
705
706
707
708 int
709 search_ctrl_height ()
710 {
711 #ifdef __WXGTK3__
712         return 30;
713 #else
714         return -1;
715 #endif
716 }
717
718
719 void
720 report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
721 {
722         switch (what) {
723         case Config::LoadFailure::CONFIG:
724                 message_dialog(parent, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
725                 break;
726         case Config::LoadFailure::CINEMAS:
727                 message_dialog(
728                         parent,
729                         _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load.  Please check the numbered backup files in %s",
730                                            std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
731                         );
732                 break;
733         case Config::LoadFailure::DKDM_RECIPIENTS:
734                 message_dialog(
735                         parent,
736                         _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load.  Please check the numbered backup files in %s",
737                                            std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))
738                         );
739                 break;
740         }
741 }
742
743
744 static AudioAPI wasapi(_("WASAPI"), "wasapi", RtAudio::WINDOWS_WASAPI);
745 static AudioAPI asio(_("ASIO"), "asio", RtAudio::WINDOWS_ASIO);
746 static AudioAPI pulse(_("PulseAudio"), "pulse", RtAudio::LINUX_PULSE);
747 static AudioAPI alsa(_("ALSA"), "alsa", RtAudio::LINUX_ALSA);
748 static AudioAPI core(_("CoreAudio"), "core", RtAudio::MACOSX_CORE);
749
750
751 vector<AudioAPI>
752 audio_apis()
753 {
754 #ifdef DCPOMATIC_WINDOWS
755         return { wasapi, asio };
756         };
757 #endif
758 #ifdef DCPOMATIC_LINUX
759         return { pulse, alsa };
760 #endif
761 #ifdef DCPOMATIC_OSX
762         return { core };
763 #endif
764 }
765
766
767 AudioAPI
768 id_to_audio_api(string id)
769 {
770         const auto apis = audio_apis();
771         auto iter = std::find_if(apis.begin(), apis.end(), [&id](AudioAPI const& api) { return api.id() == id; });
772         if (iter == apis.end()) {
773                 return apis[0];
774         }
775
776         return *iter;
777 }
778
779
780 AudioAPI
781 id_to_audio_api(optional<string> id)
782 {
783         const auto apis = audio_apis();
784         if (!id) {
785                 return apis[0];
786         }
787
788         return id_to_audio_api(*id);
789 }
790
791
792 AudioAPI
793 index_to_audio_api(int index)
794 {
795         const auto apis = audio_apis();
796         if (index < 0 || index >= static_cast<int>(apis.size())) {
797                 return apis[0];
798         }
799
800         return apis[index];
801 }
802
803