diff --git a/src/InputStream.cpp b/src/InputStream.cpp index 87dbce8..a0b00fa 100644 --- a/src/InputStream.cpp +++ b/src/InputStream.cpp @@ -21,7 +21,7 @@ void InputStream::update() { auto const default_device_id = _backend.getDefaultInputDevice(); if (default_device_id != _current_device_id || !_backend.isStreamRunning()) - set_device(device_info(default_device_id)); + open_device(device_info(default_device_id)); } else { @@ -33,7 +33,7 @@ void InputStream::update() if (id == 0) return; - set_device(device_info(id)); + open_device(device_info(id)); } } @@ -131,29 +131,33 @@ auto audio_input_callback(void* /* output_buffer */, void* input_buffer, unsigne void InputStream::use_given_device(RtAudio::DeviceInfo const& info) { _selected_device = UseGivenDevice{info.name}; - set_device(info); + if (_backend.isStreamOpen()) + open_device(info); } void InputStream::use_default_device() { _selected_device = UseDefaultDevice{}; - set_device(device_info(_backend.getDefaultInputDevice())); + if (_backend.isStreamOpen()) + open_device(device_info(_backend.getDefaultInputDevice())); } void InputStream::use_device(SelectedDevice device) { _selected_device = std::move(device); + if (_backend.isStreamOpen()) + open_selected_device(); +} + +void InputStream::open_selected_device() +{ if (std::holds_alternative(_selected_device)) - { - set_device(device_info(_backend.getDefaultInputDevice())); - } + open_device(device_info(_backend.getDefaultInputDevice())); else - { - set_device(find_device_info_by_name(std::get(_selected_device).name)); - }; + open_device(find_device_info_by_name(std::get(_selected_device).name)); } -void InputStream::set_device(RtAudio::DeviceInfo const& info) +void InputStream::open_device(RtAudio::DeviceInfo const& info) { close(); // Close the current stream if there was one. We want to reopen one with the new device. @@ -178,6 +182,7 @@ void InputStream::close() { if (_backend.isStreamOpen()) _backend.closeStream(); + _current_device_id = 0; } } // namespace Audio \ No newline at end of file diff --git a/src/InputStream.hpp b/src/InputStream.hpp index 36dced3..1ac95c6 100644 --- a/src/InputStream.hpp +++ b/src/InputStream.hpp @@ -62,7 +62,8 @@ class InputStream { private: friend auto audio_input_callback(void* output_buffer, void* input_buffer, unsigned int frames_count, double stream_time, RtAudioStreamStatus status, void* user_data) -> int; - void set_device(RtAudio::DeviceInfo const& info); + void open_device(RtAudio::DeviceInfo const& info); + void open_selected_device(); /// /!\ YOU MUST LOCK `_samples_mutex` before using this function void shrink_samples_to_fit();