egotism/
lib.rs

1use cpal::{
2    platform::Host, traits::{DeviceTrait, HostTrait, StreamTrait}
3};
4
5use ringbuf::HeapRb;
6
7pub struct Loopback<'a> {
8    pub host: Host,
9    pub input_device: &'a str,
10    pub output_device: &'a str,
11    pub latency: f32,
12}
13
14impl Loopback<'_> {
15    pub fn new() -> Self {
16        Loopback {
17            host: cpal::default_host(),
18            input_device: "default",
19            output_device: "default",
20            latency: 150.0,
21        }
22    }
23
24    pub fn start(&mut self) -> anyhow::Result<()> {
25            let host = &self.host;
26        
27            // Find devices.
28            let input_device = if &self.input_device == &"default" {
29                host.default_input_device()
30            } else {
31                host.input_devices()?
32                    .find(|x| x.name().map(|y| &stringify!(y) == &self.input_device).unwrap_or(false))
33            }
34            .expect("Failed to find input device!");
35        
36            let output_device = if &self.output_device == &"default" {
37                host.default_output_device()
38            } else {
39                host.output_devices()?
40                    .find(|x| x.name().map(|y| &stringify!(y) == &self.output_device).unwrap_or(false))
41            }
42            .expect("Failed to find output device!");
43        
44            println!("Using input device: \"{}\"", input_device.name()?);
45            println!("Using output device: \"{}\"", output_device.name()?);
46        
47            // We'll try and use the same configuration between streams to keep it simple.
48            let config: cpal::StreamConfig = input_device.default_input_config()?.into();
49        
50            // Create a delay in case the input and output devices aren't synced.
51            let latency_frames = (&self.latency / 1_000.0) * config.sample_rate.0 as f32;
52            let latency_samples = latency_frames as usize * config.channels as usize;
53        
54            // The buffer to share samples
55            let ring = HeapRb::<f32>::new(latency_samples * 2);
56            let (mut producer, mut consumer) = ring.split();
57        
58            // Fill the samples with 0.0 equal to the length of the delay.
59            for _ in 0..latency_samples {
60                // The ring buffer has twice as much space as necessary to add latency here,
61                // so this should never fail
62                producer.push(0.0).unwrap();
63            }
64        
65            let input_data_fn = move |data: &[f32], _: &cpal::InputCallbackInfo| {
66                let mut output_fell_behind = false;
67                for &sample in data {
68                    if producer.push(sample).is_err() {
69                        output_fell_behind = true;
70                    }
71                }
72                if output_fell_behind {
73                    eprintln!("Output stream fell behind! Try increasing latency.");
74                }
75            };
76        
77            let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
78                let mut input_fell_behind = false;
79                for sample in data {
80                    *sample = match consumer.pop() {
81                        Some(s) => s,
82                        None => {
83                            input_fell_behind = true;
84                            0.0
85                        }
86                    };
87                }
88                if input_fell_behind {
89                    eprintln!("Input stream fell behind! Try increasing latency.");
90                }
91            };
92        
93            // Build streams.
94            println!(
95                "Attempting to build both streams with f32 samples and `{:?}`.",
96                config
97            );
98            let input_stream = input_device.build_input_stream(&config, input_data_fn, err_fn, None)?;
99            let output_stream = output_device.build_output_stream(&config, output_data_fn, err_fn, None)?;
100            println!("Successfully built streams.");
101        
102            // Play the streams.
103            println!(
104                "Starting the input and output streams with `{}` milliseconds of latency.",
105                &self.latency
106            );
107            input_stream.play()?;
108            output_stream.play()?;
109        
110            // Run for 3 seconds before closing.
111            println!("Playing for 3 seconds... ");
112            std::thread::sleep(std::time::Duration::from_secs(3));
113            drop(input_stream);
114            drop(output_stream);
115            println!("Done!");
116            Ok(())
117    }
118}
119
120
121fn err_fn(err: cpal::StreamError) {
122    eprintln!("An error occurred on stream: {}", err);
123}