| Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago | 1 | //! Feeds back the input stream directly into the output stream. |
| 2 | //! |
| 3 | //! Assumes that the input and output devices can use the same stream configuration and that they |
| 4 | //! support the f32 sample format. |
| 5 | //! |
| 6 | //! Uses a delay of `LATENCY_MS` milliseconds in case the default input and output streams are not |
| 7 | //! precisely synchronised. |
| 8 | |
| 9 | extern crate anyhow; |
| 10 | extern crate cpal; |
| 11 | extern crate ringbuf; |
| 12 | |
| 13 | use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; |
| 14 | use ringbuf::{ |
| 15 | traits::{Consumer, Producer, Split}, |
| 16 | HeapRb, |
| 17 | }; |
| 18 | |
| 19 | const LATENCY_MS: f32 = 1000.0; |
| 20 | |
| 21 | pub fn run_example() -> Result<(), anyhow::Error> { |
| 22 | let host = cpal::default_host(); |
| 23 | |
| 24 | // Default devices. |
| 25 | let input_device = host |
| 26 | .default_input_device() |
| 27 | .expect("failed to get default input device"); |
| 28 | let output_device = host |
| 29 | .default_output_device() |
| 30 | .expect("failed to get default output device"); |
| 31 | println!("Using default input device: \"{}\"", input_device.name()?); |
| 32 | println!("Using default output device: \"{}\"", output_device.name()?); |
| 33 | |
| 34 | // We'll try and use the same configuration between streams to keep it simple. |
| 35 | let config: cpal::StreamConfig = input_device.default_input_config()?.into(); |
| 36 | |
| 37 | // Create a delay in case the input and output devices aren't synced. |
| 38 | let latency_frames = (LATENCY_MS / 1_000.0) * config.sample_rate as f32; |
| 39 | let latency_samples = latency_frames as usize * config.channels as usize; |
| 40 | |
| 41 | // The buffer to share samples |
| 42 | let ring = HeapRb::<f32>::new(latency_samples * 2); |
| 43 | let (mut producer, mut consumer) = ring.split(); |
| 44 | |
| 45 | // Fill the samples with 0.0 equal to the length of the delay. |
| 46 | for _ in 0..latency_samples { |
| 47 | // The ring buffer has twice as much space as necessary to add latency here, |
| 48 | // so this should never fail |
| 49 | producer.try_push(0.0).unwrap(); |
| 50 | } |
| 51 | |
| 52 | let input_data_fn = move |data: &[f32], _: &cpal::InputCallbackInfo| { |
| 53 | let mut output_fell_behind = false; |
| 54 | for &sample in data { |
| 55 | if producer.try_push(sample).is_err() { |
| 56 | output_fell_behind = true; |
| 57 | } |
| 58 | } |
| 59 | if output_fell_behind { |
| 60 | eprintln!("output stream fell behind: try increasing latency"); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| { |
| 65 | let mut input_fell_behind = false; |
| 66 | for sample in data { |
| 67 | *sample = match consumer.try_pop() { |
| 68 | Some(s) => s, |
| 69 | None => { |
| 70 | input_fell_behind = true; |
| 71 | 0.0 |
| 72 | } |
| 73 | }; |
| 74 | } |
| 75 | if input_fell_behind { |
| 76 | eprintln!("input stream fell behind: try increasing latency"); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | // Build streams. |
| 81 | println!( |
| 82 | "Attempting to build both streams with f32 samples and `{:?}`.", |
| 83 | config |
| 84 | ); |
| 85 | println!("Setup input stream"); |
| 86 | let input_stream = input_device.build_input_stream(config, input_data_fn, err_fn, None)?; |
| 87 | println!("Setup output stream"); |
| 88 | let output_stream = output_device.build_output_stream(config, output_data_fn, err_fn, None)?; |
| 89 | println!("Successfully built streams."); |
| 90 | |
| 91 | // Play the streams. |
| 92 | println!( |
| 93 | "Starting the input and output streams with `{}` milliseconds of latency.", |
| 94 | LATENCY_MS |
| 95 | ); |
| 96 | input_stream.play()?; |
| 97 | output_stream.play()?; |
| 98 | |
| 99 | // for the purposes of this demo, leak these so that after returning the audio units will |
| 100 | // keep running |
| 101 | std::mem::forget(input_stream); |
| 102 | std::mem::forget(output_stream); |
| 103 | Ok(()) |
| 104 | } |
| 105 | |
| 106 | fn err_fn(err: cpal::StreamError) { |
| 107 | eprintln!("an error occurred on stream: {}", err); |
| 108 | } |