Update docs for removal of UnknownTypeBuffer

This commit is contained in:
mitchmindtree 2020-01-14 22:41:44 +01:00
parent 6fc2185c99
commit 5a619877f9
2 changed files with 27 additions and 37 deletions

View File

@ -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::<f32, f32>(&*data, &writer_2),
move |data| write_input_data::<f32, f32>(&*data, &writer_2),
err_fn,
),
cpal::SampleFormat::I16 => device.build_input_stream(
&format,
move |mut data| write_input_data::<i16, i16>(&*data, &writer_2),
move |data| write_input_data::<i16, i16>(&*data, &writer_2),
err_fn,
),
cpal::SampleFormat::U16 => device.build_input_stream(
&format,
move |mut data| write_input_data::<u16, i16>(&*data, &writer_2),
move |data| write_input_data::<u16, i16>(&*data, &writer_2),
err_fn,
),
}?;

View File

@ -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<f32>| {
//! // 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<T>` or
//! `OutputData<T>` 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::<f32>, err_fn),
//! SampleFormat::I16 => device.build_output_stream(&format, write_silence::<i16>, err_fn),
//! SampleFormat::U16 => device.build_output_stream(&format, write_silence::<u16>, err_fn),
//! };
//!
//! fn write_silence<T: Sample>(mut data: OutputData<T>) {
//! 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<f32>| {};
//! # 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<f32>| {};
//! # let err_fn = move |_err| {};
//! # let stream = device.build_output_stream(&format, data_fn, err_fn).unwrap();
//! stream.pause().unwrap();
//! ```