implemented buffersizes for asio

This commit is contained in:
JoshuaBatty 2020-05-22 13:30:13 +02:00
parent 6edee6c6c2
commit 9e660da433
11 changed files with 142 additions and 65 deletions

View file

@ -4,7 +4,9 @@ extern crate cpal;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
fn main() -> Result<(), anyhow::Error> {
let host = cpal::default_host();
//let host = cpal::default_host();
let host =
cpal::host_from_id(cpal::platform::HostId::Asio).expect("failed to initialise ASIO host");
let device = host
.default_output_device()
.expect("failed to find a default output device");
@ -23,6 +25,9 @@ fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyh
where
T: cpal::Sample,
{
let mut config: cpal::StreamConfig = config.clone();
config.buffer_size = cpal::BufferSize::Fixed(256);
let sample_rate = config.sample_rate.0 as f32;
let channels = config.channels as usize;
@ -36,7 +41,7 @@ where
let err_fn = |err| eprintln!("an error occurred on stream: {}", err);
let stream = device.build_output_stream(
config,
&config,
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
write_data(data, channels, &mut next_value)
},

View file

@ -11,6 +11,7 @@ fn main() -> Result<(), anyhow::Error> {
for host_id in available_hosts {
println!("{}", host_id.name());
let host = cpal::host_from_id(host_id)?;
let default_in = host.default_input_device().map(|e| e.name().unwrap());
let default_out = host.default_output_device().map(|e| e.name().unwrap());
println!(" Default Input Device:\n {:?}", default_in);

View file

@ -16,8 +16,9 @@ use ringbuf::RingBuffer;
const LATENCY_MS: f32 = 150.0;
fn main() -> Result<(), anyhow::Error> {
let host = cpal::default_host();
//let host = cpal::default_host();
let host =
cpal::host_from_id(cpal::platform::HostId::Asio).expect("failed to initialise ASIO host");
// Default devices.
let input_device = host
.default_input_device()
@ -30,8 +31,8 @@ fn main() -> Result<(), anyhow::Error> {
// We'll try and use the same configuration between streams to keep it simple.
let mut config: cpal::StreamConfig = input_device.default_input_config()?.into();
config.buffer_size = cpal::BufferSize::Fixed(1024);
//config.buffer_size = cpal::BufferSize::Fixed(1024);
// Create a delay in case the input and output devices aren't synced.
let latency_frames = (LATENCY_MS / 1_000.0) * config.sample_rate.0 as f32;
let latency_samples = latency_frames as usize * config.channels as usize;
@ -44,10 +45,10 @@ fn main() -> Result<(), anyhow::Error> {
for _ in 0..latency_samples {
// The ring buffer has twice as much space as necessary to add latency here,
// so this should never fail
producer.push(0.0).unwrap();
producer.push(0).unwrap();
}
let input_data_fn = move |data: &[f32], _: &cpal::InputCallbackInfo| {
let input_data_fn = move |data: &[i16], _: &cpal::InputCallbackInfo| {
println!("data len = {}", data.len());
let mut output_fell_behind = false;
for &sample in data {
@ -60,14 +61,14 @@ fn main() -> Result<(), anyhow::Error> {
}
};
let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
let output_data_fn = move |data: &mut [i16], _: &cpal::OutputCallbackInfo| {
let mut input_fell_behind = None;
for sample in data {
*sample = match consumer.pop() {
Ok(s) => s,
Err(err) => {
input_fell_behind = Some(err);
0.0
0
}
};
}