Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use builder to init output stream #641

Merged
merged 36 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
184b383
Support buffer size selection
PetrGlad Nov 6, 2022
dd48390
Make configurable buffer API public
PetrGlad Nov 14, 2022
5d42375
Correct API signatures
PetrGlad Nov 14, 2022
4c62dd3
Bump version
PetrGlad Oct 1, 2023
1a8d63a
Reduce API changes
PetrGlad Oct 1, 2023
c17712f
Reformat code
PetrGlad Oct 1, 2023
08207b1
Merge branch 'master' into configurable-buffer-size
PetrGlad Oct 20, 2023
dd1edbb
WIP, simplify output stream initialization
PetrGlad Sep 23, 2024
e82b208
WIP. Implementing OpenStreamBuilder
PetrGlad Nov 6, 2024
fcfbabc
Revert to using mixer as default output
PetrGlad Nov 8, 2024
7154439
Remove source plug prototype
PetrGlad Nov 8, 2024
e2c74a5
Streamline default stream logic
PetrGlad Nov 8, 2024
dbd1d1f
Remove Criterion dependency
PetrGlad Nov 8, 2024
5cfe61f
Update a basic example
PetrGlad Nov 9, 2024
cabdda7
Update tests and examples
PetrGlad Nov 9, 2024
352c399
Add example comments
PetrGlad Nov 10, 2024
2ee0a3c
Merge remote-tracking branch 'rust-audio/master' into init-revamp
PetrGlad Nov 10, 2024
c5850ab
Sync Cargo.toml with master branch
PetrGlad Nov 10, 2024
83c0025
Update doc examples
PetrGlad Nov 10, 2024
179d41a
Reformat code
PetrGlad Nov 13, 2024
af92566
Cleanup
PetrGlad Nov 14, 2024
ebc673e
Cleanup, remove unnecessay imports
PetrGlad Nov 14, 2024
b720273
Revert extra changes for merge request
PetrGlad Nov 14, 2024
689670d
UPdate documentation
PetrGlad Nov 15, 2024
6a6e94c
Rename dynamic_mixer.rs
PetrGlad Nov 15, 2024
1af733e
Cleanup: fix lint warnings
PetrGlad Nov 15, 2024
6f6385b
Add documentation for output stream public exports
PetrGlad Nov 16, 2024
77c50cd
Clarify API documentation
PetrGlad Nov 17, 2024
4101ae9
Remove unwrap and expect from example code
PetrGlad Nov 19, 2024
0ba6021
Hide OutputStreamConfig struct
PetrGlad Nov 21, 2024
95060b1
Stream initialization fix
PetrGlad Nov 21, 2024
678df03
Custom output stream config example
PetrGlad Nov 21, 2024
d1d5726
Rename try_open_stream and try_default_stream
PetrGlad Nov 21, 2024
c23ec2c
Update documentation
PetrGlad Nov 21, 2024
5972eeb
Update CHANGELOG.md
PetrGlad Nov 21, 2024
435222a
Merge remote-tracking branch 'rust-audio/master' into init-revamp
PetrGlad Nov 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/custom_config.rs
dvdsk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use cpal::traits::HostTrait;
use cpal::{BufferSize, SampleFormat, SampleRate};
use rodio::source::SineWave;
use rodio::Source;
use std::error::Error;
use std::thread;
use std::time::Duration;

fn main() -> Result<(), Box<dyn Error>> {
// You can use any other output device that can be queried from CPAL.
let default_device = cpal::default_host()
.default_output_device()
.ok_or("No default audio output device is found.")?;
let stream_handle = rodio::OutputStreamBuilder::from_device(default_device)?
// No need to set all parameters explicitly here,
// the defaults were set from the device's description.
.with_buffer_size(BufferSize::Fixed(256))
.with_sample_rate(SampleRate(48_000))
.with_sample_format(SampleFormat::F32)
// Note that the function below still tries alternative configs if the specified one fails.
// If you need to only use the exact specified configuration,
// then use OutputStreamBuilder::open_stream() instead.
.try_open_stream()?;
let mixer = stream_handle.mixer();

let wave = SineWave::new(740.0)
.amplify(0.1)
.take_duration(Duration::from_secs(1));
mixer.add(wave);

println!("Beep...");
thread::sleep(Duration::from_millis(1500));

Ok(())
}
5 changes: 3 additions & 2 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ impl OutputStreamBuilder {
OutputStream::open(device, &self.config)
}

/// Try to open a new output stream with the builder's current stream configuration.
/// Try opening a new output stream with the builder's current stream configuration.
/// Failing that attempt to open stream with other available configurations
/// provided by the device.
/// If all attempts to open stream have not succeeded returns initial error.
/// If all attempts did not succeed returns initial error.
pub fn try_open_stream(&self) -> Result<OutputStream, StreamError> {
dvdsk marked this conversation as resolved.
Show resolved Hide resolved
let device = self.device.as_ref().expect("output device specified");
OutputStream::open(device, &self.config).or_else(|err| {
for supported_config in supported_output_configs(device)? {
if let Ok(handle) = Self::default()
.with_device(device.clone())
.with_supported_config(&supported_config)
.open_stream()
{
Expand Down