Migrate Sample to dasp_sample (#689)
This commit is contained in:
parent
8c31e908b0
commit
85d773d59f
17 changed files with 337 additions and 355 deletions
|
|
@ -3,7 +3,11 @@
|
|||
extern crate anyhow;
|
||||
extern crate cpal;
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use cpal::{
|
||||
traits::{DeviceTrait, HostTrait, StreamTrait},
|
||||
SizedSample,
|
||||
};
|
||||
use cpal::{FromSample, Sample};
|
||||
|
||||
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "full"))]
|
||||
fn main() {
|
||||
|
|
@ -16,15 +20,27 @@ fn main() {
|
|||
let config = device.default_output_config().unwrap();
|
||||
|
||||
match config.sample_format() {
|
||||
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::I8 => run::<i8>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::I16 => run::<i16>(&device, &config.into()).unwrap(),
|
||||
// cpal::SampleFormat::I24 => run::<I24>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::I32 => run::<i32>(&device, &config.into()).unwrap(),
|
||||
// cpal::SampleFormat::I48 => run::<I48>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::I64 => run::<i64>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::U8 => run::<u8>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::U16 => run::<u16>(&device, &config.into()).unwrap(),
|
||||
// cpal::SampleFormat::U24 => run::<U24>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::U32 => run::<u32>(&device, &config.into()).unwrap(),
|
||||
// cpal::SampleFormat::U48 => run::<U48>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::U64 => run::<u64>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()).unwrap(),
|
||||
cpal::SampleFormat::F64 => run::<f64>(&device, &config.into()).unwrap(),
|
||||
sample_format => panic!("Unsupported sample format '{sample_format}'"),
|
||||
}
|
||||
}
|
||||
|
||||
fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error>
|
||||
where
|
||||
T: cpal::Sample,
|
||||
T: SizedSample + FromSample<f32>,
|
||||
{
|
||||
let sample_rate = config.sample_rate.0 as f32;
|
||||
let channels = config.channels as usize;
|
||||
|
|
@ -54,10 +70,10 @@ where
|
|||
|
||||
fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
|
||||
where
|
||||
T: cpal::Sample,
|
||||
T: Sample + FromSample<f32>,
|
||||
{
|
||||
for frame in output.chunks_mut(channels) {
|
||||
let value: T = cpal::Sample::from::<f32>(&next_sample());
|
||||
let value: T = T::from_sample(next_sample());
|
||||
for sample in frame.iter_mut() {
|
||||
*sample = value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,11 @@ extern crate clap;
|
|||
extern crate cpal;
|
||||
|
||||
use clap::arg;
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use cpal::{
|
||||
traits::{DeviceTrait, HostTrait, StreamTrait},
|
||||
SizedSample,
|
||||
};
|
||||
use cpal::{FromSample, Sample};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Opt {
|
||||
|
|
@ -84,15 +88,27 @@ fn main() -> anyhow::Result<()> {
|
|||
println!("Default output config: {:?}", config);
|
||||
|
||||
match config.sample_format() {
|
||||
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()),
|
||||
cpal::SampleFormat::I8 => run::<i8>(&device, &config.into()),
|
||||
cpal::SampleFormat::I16 => run::<i16>(&device, &config.into()),
|
||||
// cpal::SampleFormat::I24 => run::<I24>(&device, &config.into()),
|
||||
cpal::SampleFormat::I32 => run::<i32>(&device, &config.into()),
|
||||
// cpal::SampleFormat::I48 => run::<I48>(&device, &config.into()),
|
||||
cpal::SampleFormat::I64 => run::<i64>(&device, &config.into()),
|
||||
cpal::SampleFormat::U8 => run::<u8>(&device, &config.into()),
|
||||
cpal::SampleFormat::U16 => run::<u16>(&device, &config.into()),
|
||||
// cpal::SampleFormat::U24 => run::<U24>(&device, &config.into()),
|
||||
cpal::SampleFormat::U32 => run::<u32>(&device, &config.into()),
|
||||
// cpal::SampleFormat::U48 => run::<U48>(&device, &config.into()),
|
||||
cpal::SampleFormat::U64 => run::<u64>(&device, &config.into()),
|
||||
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()),
|
||||
cpal::SampleFormat::F64 => run::<f64>(&device, &config.into()),
|
||||
sample_format => panic!("Unsupported sample format '{sample_format}'"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error>
|
||||
where
|
||||
T: cpal::Sample,
|
||||
T: SizedSample + FromSample<f32>,
|
||||
{
|
||||
let sample_rate = config.sample_rate.0 as f32;
|
||||
let channels = config.channels as usize;
|
||||
|
|
@ -122,10 +138,10 @@ where
|
|||
|
||||
fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
|
||||
where
|
||||
T: cpal::Sample,
|
||||
T: Sample + FromSample<f32>,
|
||||
{
|
||||
for frame in output.chunks_mut(channels) {
|
||||
let value: T = cpal::Sample::from::<f32>(&next_sample());
|
||||
let value: T = T::from_sample(next_sample());
|
||||
for sample in frame.iter_mut() {
|
||||
*sample = value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ extern crate hound;
|
|||
|
||||
use clap::arg;
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use cpal::{FromSample, Sample};
|
||||
use std::fs::File;
|
||||
use std::io::BufWriter;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
|
@ -112,9 +113,9 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
};
|
||||
|
||||
let stream = match config.sample_format() {
|
||||
cpal::SampleFormat::F32 => device.build_input_stream(
|
||||
cpal::SampleFormat::I8 => device.build_input_stream(
|
||||
&config.into(),
|
||||
move |data, _: &_| write_input_data::<f32, f32>(data, &writer_2),
|
||||
move |data, _: &_| write_input_data::<i8, i8>(data, &writer_2),
|
||||
err_fn,
|
||||
)?,
|
||||
cpal::SampleFormat::I16 => device.build_input_stream(
|
||||
|
|
@ -122,11 +123,21 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
move |data, _: &_| write_input_data::<i16, i16>(data, &writer_2),
|
||||
err_fn,
|
||||
)?,
|
||||
cpal::SampleFormat::U16 => device.build_input_stream(
|
||||
cpal::SampleFormat::I32 => device.build_input_stream(
|
||||
&config.into(),
|
||||
move |data, _: &_| write_input_data::<u16, i16>(data, &writer_2),
|
||||
move |data, _: &_| write_input_data::<i32, i32>(data, &writer_2),
|
||||
err_fn,
|
||||
)?,
|
||||
cpal::SampleFormat::F32 => device.build_input_stream(
|
||||
&config.into(),
|
||||
move |data, _: &_| write_input_data::<f32, f32>(data, &writer_2),
|
||||
err_fn,
|
||||
)?,
|
||||
sample_format => {
|
||||
return Err(anyhow::Error::msg(format!(
|
||||
"Unsupported sample format '{sample_format}'"
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
stream.play()?;
|
||||
|
|
@ -140,10 +151,10 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
}
|
||||
|
||||
fn sample_format(format: cpal::SampleFormat) -> hound::SampleFormat {
|
||||
match format {
|
||||
cpal::SampleFormat::U16 => hound::SampleFormat::Int,
|
||||
cpal::SampleFormat::I16 => hound::SampleFormat::Int,
|
||||
cpal::SampleFormat::F32 => hound::SampleFormat::Float,
|
||||
if format.is_float() {
|
||||
hound::SampleFormat::Float
|
||||
} else {
|
||||
hound::SampleFormat::Int
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -160,13 +171,13 @@ type WavWriterHandle = Arc<Mutex<Option<hound::WavWriter<BufWriter<File>>>>>;
|
|||
|
||||
fn write_input_data<T, U>(input: &[T], writer: &WavWriterHandle)
|
||||
where
|
||||
T: cpal::Sample,
|
||||
U: cpal::Sample + hound::Sample,
|
||||
T: Sample,
|
||||
U: Sample + hound::Sample + FromSample<T>,
|
||||
{
|
||||
if let Ok(mut guard) = writer.try_lock() {
|
||||
if let Some(writer) = guard.as_mut() {
|
||||
for &sample in input.iter() {
|
||||
let sample: U = cpal::Sample::from(&sample);
|
||||
let sample: U = U::from_sample(sample);
|
||||
writer.write_sample(sample).ok();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,11 @@ extern crate anyhow;
|
|||
extern crate clap;
|
||||
extern crate cpal;
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use cpal::{
|
||||
traits::{DeviceTrait, HostTrait, StreamTrait},
|
||||
SizedSample,
|
||||
};
|
||||
use cpal::{FromSample, Sample};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let stream = stream_setup_for(sample_next)?;
|
||||
|
|
@ -43,9 +47,23 @@ where
|
|||
let (_host, device, config) = host_device_setup()?;
|
||||
|
||||
match config.sample_format() {
|
||||
cpal::SampleFormat::F32 => stream_make::<f32, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::I8 => stream_make::<i8, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::I16 => stream_make::<i16, _>(&device, &config.into(), on_sample),
|
||||
// cpal::SampleFormat::I24 => stream_make::<I24, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::I32 => stream_make::<i32, _>(&device, &config.into(), on_sample),
|
||||
// cpal::SampleFormat::I48 => stream_make::<I48, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::I64 => stream_make::<i64, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::U8 => stream_make::<u8, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::U16 => stream_make::<u16, _>(&device, &config.into(), on_sample),
|
||||
// cpal::SampleFormat::U24 => stream_make::<U24, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::U32 => stream_make::<u32, _>(&device, &config.into(), on_sample),
|
||||
// cpal::SampleFormat::U48 => stream_make::<U48, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::U64 => stream_make::<u64, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::F32 => stream_make::<f32, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::F64 => stream_make::<f64, _>(&device, &config.into(), on_sample),
|
||||
sample_format => Err(anyhow::Error::msg(format!(
|
||||
"Unsupported sample format '{sample_format}'"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +88,7 @@ pub fn stream_make<T, F>(
|
|||
on_sample: F,
|
||||
) -> Result<cpal::Stream, anyhow::Error>
|
||||
where
|
||||
T: cpal::Sample,
|
||||
T: SizedSample + FromSample<f32>,
|
||||
F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static + Copy,
|
||||
{
|
||||
let sample_rate = config.sample_rate.0 as f32;
|
||||
|
|
@ -96,11 +114,11 @@ where
|
|||
|
||||
fn on_window<T, F>(output: &mut [T], request: &mut SampleRequestOptions, mut on_sample: F)
|
||||
where
|
||||
T: cpal::Sample,
|
||||
T: Sample + FromSample<f32>,
|
||||
F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static,
|
||||
{
|
||||
for frame in output.chunks_mut(request.nchannels) {
|
||||
let value: T = cpal::Sample::from::<f32>(&on_sample(request));
|
||||
let value: T = T::from_sample(on_sample(request));
|
||||
for sample in frame.iter_mut() {
|
||||
*sample = value;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue