From 0df3142cd1d01db25ebb90428d03ea28e43ec4a9 Mon Sep 17 00:00:00 2001 From: Jules Fouchy Date: Wed, 27 Dec 2023 15:17:02 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=8F=20[InputStream]=20Don't=20start=20?= =?UTF-8?q?the=20stream=20when=20setting=20the=20selected=20device?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/InputStream.cpp | 27 ++++++++++++++++----------- src/InputStream.hpp | 3 ++- 2 files changed, 18 insertions(+), 12 deletions(-) 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();