An alternative approach to removing UnknownBufferType.
This is a potential alternative to #359. This PR is based on #359. This approach opts for a dynamically checked sample type approach with the aim of minimising compile time and binary size. You can read more discussion on this [here](https://github.com/RustAudio/cpal/pull/359#issuecomment-575931461) Implemented backends: - [x] null - [x] ALSA - [ ] CoreAudio - [ ] WASAPI - [ ] ASIO - [ ] Emscripten
This commit is contained in:
parent
01425b4b80
commit
58356f49b4
8 changed files with 189 additions and 219 deletions
|
|
@ -40,23 +40,15 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
eprintln!("an error occurred on stream: {}", err);
|
||||
};
|
||||
|
||||
let stream = match format.data_type {
|
||||
cpal::SampleFormat::F32 => device.build_input_stream(
|
||||
&format,
|
||||
move |data| write_input_data::<f32, f32>(&*data, &writer_2),
|
||||
err_fn,
|
||||
),
|
||||
cpal::SampleFormat::I16 => device.build_input_stream(
|
||||
&format,
|
||||
move |data| write_input_data::<i16, i16>(&*data, &writer_2),
|
||||
err_fn,
|
||||
),
|
||||
cpal::SampleFormat::U16 => device.build_input_stream(
|
||||
&format,
|
||||
move |data| write_input_data::<u16, i16>(&*data, &writer_2),
|
||||
err_fn,
|
||||
),
|
||||
}?;
|
||||
let data_fn = move |data: &cpal::Data| {
|
||||
match data.sample_format() {
|
||||
cpal::SampleFormat::F32 => write_input_data::<f32, f32>(data, &writer_2),
|
||||
cpal::SampleFormat::I16 => write_input_data::<i16, i16>(data, &writer_2),
|
||||
cpal::SampleFormat::U16 => write_input_data::<u16, i16>(data, &writer_2),
|
||||
}
|
||||
};
|
||||
|
||||
let stream = device.build_input_stream(&format, data_fn, err_fn)?;
|
||||
|
||||
stream.play()?;
|
||||
|
||||
|
|
@ -87,11 +79,12 @@ fn wav_spec_from_format(format: &cpal::Format) -> hound::WavSpec {
|
|||
|
||||
type WavWriterHandle = Arc<Mutex<Option<hound::WavWriter<BufWriter<File>>>>>;
|
||||
|
||||
fn write_input_data<T, U>(input: &[T], writer: &WavWriterHandle)
|
||||
fn write_input_data<T, U>(input: &cpal::Data, writer: &WavWriterHandle)
|
||||
where
|
||||
T: cpal::Sample,
|
||||
U: cpal::Sample + hound::Sample,
|
||||
{
|
||||
let input = input.as_slice::<T>().expect("unexpected sample format");
|
||||
if let Ok(mut guard) = writer.try_lock() {
|
||||
if let Some(writer) = guard.as_mut() {
|
||||
for &sample in input.iter() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue