loopback/
loopback.rs

1use rtaudio::{Api, Buffers, DeviceParams, SampleFormat, StreamInfo, StreamOptions, StreamStatus};
2
3fn main() {
4    let host = rtaudio::Host::new(Api::Unspecified).unwrap();
5    dbg!(host.api());
6
7    let out_device = host.default_output_device().unwrap();
8    let in_device = host.default_input_device().unwrap();
9
10    let mut stream_handle = host
11        .open_stream(
12            Some(DeviceParams {
13                device_id: out_device.id,
14                num_channels: 2,
15                first_channel: 0,
16            }),
17            Some(DeviceParams {
18                device_id: in_device.id,
19                num_channels: 2,
20                first_channel: 0,
21            }),
22            SampleFormat::Float32,
23            out_device.preferred_sample_rate,
24            256,
25            StreamOptions::default(),
26            |error| eprintln!("{}", error),
27        )
28        .unwrap();
29    dbg!(stream_handle.info());
30
31    stream_handle
32        .start(
33            move |buffers: Buffers<'_>, _info: &StreamInfo, _status: StreamStatus| {
34                if let Buffers::Float32 { output, input } = buffers {
35                    // Copy the input to the output.
36                    output.copy_from_slice(input);
37                }
38            },
39        )
40        .unwrap();
41
42    // Wait 3 seconds before closing.
43    std::thread::sleep(std::time::Duration::from_millis(3000));
44}