|
| 1 | +//! Feeds back the input stream directly into the output stream using a duplex stream. |
| 2 | +//! |
| 3 | +//! Unlike the `feedback.rs` example which uses separate input/output streams with a ring buffer, |
| 4 | +//! duplex streams provide hardware-synchronized input/output without additional buffering. |
| 5 | +//! |
| 6 | +//! Note: Currently only supported on macOS (CoreAudio). Windows (WASAPI) and Linux (ALSA) |
| 7 | +//! implementations are planned. |
| 8 | +
|
| 9 | +#[cfg(target_os = "macos")] |
| 10 | +mod imp { |
| 11 | + use clap::Parser; |
| 12 | + use cpal::duplex::DuplexStreamConfig; |
| 13 | + use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; |
| 14 | + use cpal::BufferSize; |
| 15 | + |
| 16 | + #[derive(Parser, Debug)] |
| 17 | + #[command(version, about = "CPAL duplex feedback example", long_about = None)] |
| 18 | + struct Opt { |
| 19 | + /// The audio device to use (must support duplex operation) |
| 20 | + #[arg(short, long, value_name = "DEVICE")] |
| 21 | + device: Option<String>, |
| 22 | + |
| 23 | + /// Number of input channels |
| 24 | + #[arg(long, value_name = "CHANNELS", default_value_t = 2)] |
| 25 | + input_channels: u16, |
| 26 | + |
| 27 | + /// Number of output channels |
| 28 | + #[arg(long, value_name = "CHANNELS", default_value_t = 2)] |
| 29 | + output_channels: u16, |
| 30 | + |
| 31 | + /// Sample rate in Hz |
| 32 | + #[arg(short, long, value_name = "RATE", default_value_t = 48000)] |
| 33 | + sample_rate: u32, |
| 34 | + |
| 35 | + /// Buffer size in frames |
| 36 | + #[arg(short, long, value_name = "FRAMES", default_value_t = 512)] |
| 37 | + buffer_size: u32, |
| 38 | + } |
| 39 | + |
| 40 | + pub fn run() -> anyhow::Result<()> { |
| 41 | + let opt = Opt::parse(); |
| 42 | + let host = cpal::default_host(); |
| 43 | + |
| 44 | + // Find the device by device ID or use default |
| 45 | + let device = if let Some(device_id_str) = opt.device { |
| 46 | + let device_id = device_id_str.parse().expect("failed to parse device id"); |
| 47 | + host.device_by_id(&device_id) |
| 48 | + .unwrap_or_else(|| panic!("failed to find device with id: {}", device_id_str)) |
| 49 | + } else { |
| 50 | + host.default_output_device() |
| 51 | + .expect("no default output device") |
| 52 | + }; |
| 53 | + |
| 54 | + println!("Using device: \"{}\"", device.description()?.name()); |
| 55 | + |
| 56 | + // Create duplex stream configuration. |
| 57 | + let config = DuplexStreamConfig { |
| 58 | + input_channels: opt.input_channels, |
| 59 | + output_channels: opt.output_channels, |
| 60 | + sample_rate: opt.sample_rate, |
| 61 | + buffer_size: BufferSize::Fixed(opt.buffer_size), |
| 62 | + }; |
| 63 | + |
| 64 | + println!("Building duplex stream with config: {config:?}"); |
| 65 | + |
| 66 | + let stream = device.build_duplex_stream::<f32, _, _>( |
| 67 | + &config, |
| 68 | + move |input, output, _info| { |
| 69 | + output.fill(0.0); |
| 70 | + let copy_len = input.len().min(output.len()); |
| 71 | + output[..copy_len].copy_from_slice(&input[..copy_len]); |
| 72 | + }, |
| 73 | + |err| eprintln!("Stream error: {err}"), |
| 74 | + None, |
| 75 | + )?; |
| 76 | + |
| 77 | + println!("Successfully built duplex stream."); |
| 78 | + println!( |
| 79 | + "Input: {} channels, Output: {} channels, Sample rate: {} Hz, Buffer size: {} frames", |
| 80 | + opt.input_channels, opt.output_channels, opt.sample_rate, opt.buffer_size |
| 81 | + ); |
| 82 | + |
| 83 | + println!("Starting duplex stream..."); |
| 84 | + stream.play()?; |
| 85 | + |
| 86 | + println!("Playing for 10 seconds... (speak into your microphone)"); |
| 87 | + std::thread::sleep(std::time::Duration::from_secs(10)); |
| 88 | + |
| 89 | + drop(stream); |
| 90 | + println!("Done!"); |
| 91 | + Ok(()) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +fn main() { |
| 96 | + #[cfg(target_os = "macos")] |
| 97 | + imp::run().unwrap(); |
| 98 | + |
| 99 | + #[cfg(not(target_os = "macos"))] |
| 100 | + { |
| 101 | + eprintln!("Duplex streams are currently only supported on macOS."); |
| 102 | + eprintln!("Windows (WASAPI) and Linux (ALSA) support is planned."); |
| 103 | + } |
| 104 | +} |
0 commit comments