diff --git a/examples/record_wav.rs b/examples/record_wav.rs index 36e0c86..fb48286 100644 --- a/examples/record_wav.rs +++ b/examples/record_wav.rs @@ -43,17 +43,17 @@ fn main() -> Result<(), anyhow::Error> { let stream = match format.data_type { cpal::SampleFormat::F32 => device.build_input_stream( &format, - move |mut data| write_input_data::(&*data, &writer_2), + move |data| write_input_data::(&*data, &writer_2), err_fn, ), cpal::SampleFormat::I16 => device.build_input_stream( &format, - move |mut data| write_input_data::(&*data, &writer_2), + move |data| write_input_data::(&*data, &writer_2), err_fn, ), cpal::SampleFormat::U16 => device.build_input_stream( &format, - move |mut data| write_input_data::(&*data, &writer_2), + move |data| write_input_data::(&*data, &writer_2), err_fn, ), }?; diff --git a/src/lib.rs b/src/lib.rs index e22e3b2..a80f3a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,13 +55,14 @@ //! Now that we have everything for the stream, we are ready to create it from our selected device: //! //! ```no_run +//! use cpal::OutputData; //! use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; //! # let host = cpal::default_host(); //! # let device = host.default_output_device().unwrap(); //! # let format = device.default_output_format().unwrap(); //! let stream = device.build_output_stream( //! &format, -//! move |data| { +//! move |data: OutputData| { //! // react to stream events and read or write stream data here. //! }, //! move |err| { @@ -71,10 +72,9 @@ //! ``` //! //! While the stream is running, the selected audio device will periodically call the data callback -//! that was passed to the function. The callback is passed an instance of type `StreamData` that -//! represents the data that must be read from or written to. The inner `UnknownTypeOutputBuffer` -//! can be one of `I16`, `U16` or `F32` depending on the format that was passed to -//! `build_output_stream`. +//! that was passed to the function. The callback is passed an instance of either `InputData` or +//! `OutputData` depending on whether the stream is an input stream or output stream +//! respectively. Type `T` represents the desired sample format type. Supported format types //! //! > **Note**: Creating and running a stream will *not* block the thread. On modern platforms, the //! > given callback is called by a dedicated, high-priority thread responsible for delivering @@ -88,37 +88,23 @@ //! In this example, we simply fill the given output buffer with zeroes. //! //! ```no_run -//! use cpal::{StreamData, UnknownTypeOutputBuffer}; +//! use cpal::{OutputData, Sample, SampleFormat}; //! use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; //! # let host = cpal::default_host(); //! # let device = host.default_output_device().unwrap(); //! # let format = device.default_output_format().unwrap(); -//! let stream = device.build_output_stream( -//! &format, -//! move |data| { -//! match data { -//! StreamData::Output { buffer: UnknownTypeOutputBuffer::U16(mut buffer) } => { -//! for elem in buffer.iter_mut() { -//! *elem = u16::max_value() / 2; -//! } -//! }, -//! StreamData::Output { buffer: UnknownTypeOutputBuffer::I16(mut buffer) } => { -//! for elem in buffer.iter_mut() { -//! *elem = 0; -//! } -//! }, -//! StreamData::Output { buffer: UnknownTypeOutputBuffer::F32(mut buffer) } => { -//! for elem in buffer.iter_mut() { -//! *elem = 0.0; -//! } -//! }, -//! _ => (), -//! } -//! }, -//! move |err| { -//! eprintln!("an error occurred on the output audio stream: {}", err); -//! }, -//! ); +//! let err_fn = move |err| eprintln!("an error occurred on the output audio stream: {}", err); +//! let stream = match format.data_type { +//! SampleFormat::F32 => device.build_output_stream(&format, write_silence::, err_fn), +//! SampleFormat::I16 => device.build_output_stream(&format, write_silence::, err_fn), +//! SampleFormat::U16 => device.build_output_stream(&format, write_silence::, err_fn), +//! }; +//! +//! fn write_silence(mut data: OutputData) { +//! for sample in data.iter_mut() { +//! *sample = Sample::from(&0.0); +//! } +//! } //! ``` //! //! Not all platforms automatically run the stream upon creation. To ensure the stream has started, @@ -129,7 +115,9 @@ //! # let host = cpal::default_host(); //! # let device = host.default_output_device().unwrap(); //! # let format = device.default_output_format().unwrap(); -//! # let stream = device.build_output_stream(&format, move |_data| {}, move |_err| {}).unwrap(); +//! # let data_fn = move |_data: cpal::OutputData| {}; +//! # let err_fn = move |_err| {}; +//! # let stream = device.build_output_stream(&format, data_fn, err_fn).unwrap(); //! stream.play().unwrap(); //! ``` //! @@ -141,7 +129,9 @@ //! # let host = cpal::default_host(); //! # let device = host.default_output_device().unwrap(); //! # let format = device.default_output_format().unwrap(); -//! # let stream = device.build_output_stream(&format, move |_data| {}, move |_err| {}).unwrap(); +//! # let data_fn = move |_data: cpal::OutputData| {}; +//! # let err_fn = move |_err| {}; +//! # let stream = device.build_output_stream(&format, data_fn, err_fn).unwrap(); //! stream.pause().unwrap(); //! ```