Skip to content

Commit

Permalink
🤏 [InputStream] Don't start the stream when setting the selected device
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFouchy committed Dec 27, 2023
1 parent 9fcb2e0 commit 0df3142
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
27 changes: 16 additions & 11 deletions src/InputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -33,7 +33,7 @@ void InputStream::update()
if (id == 0)
return;

set_device(device_info(id));
open_device(device_info(id));
}
}

Expand Down Expand Up @@ -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<UseDefaultDevice>(_selected_device))
{
set_device(device_info(_backend.getDefaultInputDevice()));
}
open_device(device_info(_backend.getDefaultInputDevice()));
else
{
set_device(find_device_info_by_name(std::get<UseGivenDevice>(_selected_device).name));
};
open_device(find_device_info_by_name(std::get<UseGivenDevice>(_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.

Expand All @@ -178,6 +182,7 @@ void InputStream::close()
{
if (_backend.isStreamOpen())
_backend.closeStream();
_current_device_id = 0;
}

} // namespace Audio
3 changes: 2 additions & 1 deletion src/InputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 0df3142

Please sign in to comment.