diff options
| author | Carl Hetherington <cth@carlh.net> | 2024-09-12 00:59:45 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2024-09-12 00:59:45 +0200 |
| commit | 5121341a45a940a170c5c07dcef432f0fc90b476 (patch) | |
| tree | 49d2997e3cb29f01446a4e90356f83c42a2d2ba5 /src/wx | |
| parent | 6c868a634615405a88b874eeae114ac3ef841750 (diff) | |
Cleanup: add AudioBackend::open_stream().
Diffstat (limited to 'src/wx')
| -rw-r--r-- | src/wx/audio_backend.cc | 70 | ||||
| -rw-r--r-- | src/wx/audio_backend.h | 1 | ||||
| -rw-r--r-- | src/wx/film_viewer.cc | 70 |
3 files changed, 74 insertions, 67 deletions
diff --git a/src/wx/audio_backend.cc b/src/wx/audio_backend.cc index fbf75e3df..c43153c81 100644 --- a/src/wx/audio_backend.cc +++ b/src/wx/audio_backend.cc @@ -255,3 +255,73 @@ AudioBackend::device_count() const return const_cast<RtAudio&>(_rtaudio).getDeviceCount(); } + +bool +AudioBackend::open_stream(optional<string> name, RtAudioCallback callback, void* context, int* channels, unsigned int* block_size) +{ + optional<unsigned int> chosen_device_id; +#if (RTAUDIO_VERSION_MAJOR >= 6) + if (name) { + for (auto device_id: _rtaudio.getDeviceIds()) { + if (_rtaudio.getDeviceInfo(device_id).name == *name) { + chosen_device_id = device_id; + break; + } + } + } + + if (!chosen_device_id) { + chosen_device_id = _rtaudio.getDefaultOutputDevice(); + } + *channels = _rtaudio.getDeviceInfo(*chosen_device_id).outputChannels; + RtAudio::StreamParameters sp; + sp.deviceId = *chosen_device_id; + sp.nChannels = *channels; + sp.firstChannel = 0; + if (_rtaudio.openStream(&sp, 0, RTAUDIO_FLOAT32, 48000, block_size, callback, context) != RTAUDIO_NO_ERROR) { + *channels = 0; + return false; + } +#else + unsigned int st = 0; + if (name) { + while (st < _rtaudio.getDeviceCount()) { + try { + if (_rtaudio.getDeviceInfo(st).name == *name) { + break; + } + } catch (RtAudioError&) { + /* Something went wrong with that device so we don't want to use it anyway */ + } + ++st; + } + if (st == audio.getDeviceCount()) { + try { + st = _rtaudio.getDefaultOutputDevice(); + } catch (RtAudioError&) { + /* Something went wrong with that device so we don't want to use it anyway */ + } + } + } else { + try { + st = _rtaudio.getDefaultOutputDevice(); + } catch (RtAudioError&) { + /* Something went wrong with that device so we don't want to use it anyway */ + } + } + + try { + *channels = audio.getDeviceInfo(st).outputChannels; + RtAudio::StreamParameters sp; + sp.deviceId = st; + sp.nChannels = *channels; + sp.firstChannel = 0; + audio.openStream(&sp, 0, RTAUDIO_FLOAT32, 48000, block_size, callback, context); + } catch (RtAudioError& e) { + *channels = 0; + return false; + } +#endif + + return true; +} diff --git a/src/wx/audio_backend.h b/src/wx/audio_backend.h index 12513aeb4..9a478d4fb 100644 --- a/src/wx/audio_backend.h +++ b/src/wx/audio_backend.h @@ -46,6 +46,7 @@ public: boost::optional<int> device_channels(std::string name); int device_count() const; + bool open_stream(boost::optional<std::string> name, RtAudioCallback callback, void* context, int* channels, unsigned int* block_size); void abort_stream_if_running(); boost::optional<std::string> start_stream(); diff --git a/src/wx/film_viewer.cc b/src/wx/film_viewer.cc index 82b4e5287..987fd5b39 100644 --- a/src/wx/film_viewer.cc +++ b/src/wx/film_viewer.cc @@ -610,81 +610,17 @@ FilmViewer::config_changed (Config::Property p) } if (Config::instance()->sound() && backend->device_count() > 0) { - optional<unsigned int> chosen_device_id; -#if (RTAUDIO_VERSION_MAJOR >= 6) - if (Config::instance()->sound_output()) { - for (auto device_id: audio.getDeviceIds()) { - if (audio.getDeviceInfo(device_id).name == Config::instance()->sound_output().get()) { - chosen_device_id = device_id; - break; - } - } - } - - if (!chosen_device_id) { - chosen_device_id = audio.getDefaultOutputDevice(); - } - _audio_channels = audio.getDeviceInfo(*chosen_device_id).outputChannels; - RtAudio::StreamParameters sp; - sp.deviceId = *chosen_device_id; - sp.nChannels = _audio_channels; - sp.firstChannel = 0; - if (audio.openStream(&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this) != RTAUDIO_NO_ERROR) { - _audio_channels = 0; + if (!backend->open_stream(Config::instance()->sound_output(), rtaudio_callback, this, &_audio_channels, &_audio_block_size)) { error_dialog( _video_view->get(), _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(backend->last_error()) ); } -#else - unsigned int st = 0; - if (Config::instance()->sound_output()) { - while (st < audio.getDeviceCount()) { - try { - if (audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) { - break; - } - } catch (RtAudioError&) { - /* Something went wrong with that device so we don't want to use it anyway */ - } - ++st; - } - if (st == audio.getDeviceCount()) { - try { - st = audio.getDefaultOutputDevice(); - } catch (RtAudioError&) { - /* Something went wrong with that device so we don't want to use it anyway */ - } - } - } else { - try { - st = audio.getDefaultOutputDevice(); - } catch (RtAudioError&) { - /* Something went wrong with that device so we don't want to use it anyway */ - } - } - - try { - _audio_channels = audio.getDeviceInfo(st).outputChannels; - RtAudio::StreamParameters sp; - sp.deviceId = st; - sp.nChannels = _audio_channels; - sp.firstChannel = 0; - audio.openStream(&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this); - } catch (RtAudioError& e) { - _audio_channels = 0; - error_dialog ( - _video_view->get(), - _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what()) - ); - } -#endif - destroy_and_maybe_create_butler(); - } else { _audio_channels = 0; - destroy_and_maybe_create_butler(); } + + destroy_and_maybe_create_butler(); } |
