Remove UnknownTypeBuffer in favour of specifying sample type.

This is an implementation of the planned changes described in #119.

For a quick overview of how the API has changed, check out the updated
examples.

**TODO:**

- [x] Update API.
- [x] Update examples.
- [ ] Remove `data_type` field from `Format` (see [here](https://github.com/RustAudio/cpal/issues/119#issuecomment-573788380)).
- Update backends:
  - [x] null
  - [x] ALSA
  - [ ] ASIO
  - [ ] WASAPI
  - [ ] CoreAudio
  - [ ] Emscripten

Closes #119
Closes #260
This commit is contained in:
mitchmindtree 2020-01-13 15:27:41 +01:00
parent c4ef3ac14c
commit 05b62bb1c0
9 changed files with 543 additions and 420 deletions

View file

@ -49,50 +49,44 @@ fn main() -> Result<(), anyhow::Error> {
// Build streams.
println!("Attempting to build both streams with `{:?}`.", format);
let input_stream = input_device.build_input_stream(&format, move |data| {
match data {
cpal::StreamData::Input {
buffer: cpal::UnknownTypeInputBuffer::F32(buffer),
} => {
let mut output_fell_behind = false;
for &sample in buffer.iter() {
if producer.push(sample).is_err() {
output_fell_behind = true;
}
let input_stream = input_device.build_input_stream(
&format,
move |data| {
let mut output_fell_behind = false;
for &sample in data.iter() {
if producer.push(sample).is_err() {
output_fell_behind = true;
}
if output_fell_behind {
eprintln!("output stream fell behind: try increasing latency");
}
},
_ => panic!("Expected input with f32 data"),
}
}, move |err| {
eprintln!("an error occurred on input stream: {}", err);
})?;
let output_stream = output_device.build_output_stream(&format, move |data| {
match data {
cpal::StreamData::Output {
buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer),
} => {
let mut input_fell_behind = None;
for sample in buffer.iter_mut() {
*sample = match consumer.pop() {
Ok(s) => s,
Err(err) => {
input_fell_behind = Some(err);
0.0
},
};
}
if let Some(err) = input_fell_behind {
eprintln!("input stream fell behind: {:?}: try increasing latency", err);
}
},
_ => panic!("Expected output with f32 data"),
}
}, move |err| {
eprintln!("an error occurred on output stream: {}", err);
})?;
}
if output_fell_behind {
eprintln!("output stream fell behind: try increasing latency");
}
},
|err| {
eprintln!("an error occurred on stream: {}", err);
},
)?;
let output_stream = output_device.build_output_stream(
&format,
move |mut data| {
let mut input_fell_behind = None;
for sample in data.iter_mut() {
*sample = match consumer.pop() {
Ok(s) => s,
Err(err) => {
input_fell_behind = Some(err);
0.0
},
};
}
if let Some(err) = input_fell_behind {
eprintln!("input stream fell behind: {:?}: try increasing latency", err);
}
},
move |err| {
eprintln!("an error occurred on output stream: {}", err);
},
)?;
println!("Successfully built streams.");
// Play the streams.