-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathduplex.rs
More file actions
68 lines (61 loc) · 2.04 KB
/
duplex.rs
File metadata and controls
68 lines (61 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::util::sine::SineWave;
use anyhow::Result;
use interflow::{duplex::DuplexStreamConfig, prelude::*};
mod util;
#[cfg(os_alsa)]
fn main() -> Result<()> {
env_logger::init();
let device = default_duplex_device();
let mut config = device.default_duplex_config().unwrap();
config.buffer_size_range = (Some(128), Some(512));
let stream = device.create_duplex_stream(config, RingMod::new()).unwrap();
println!("Press Enter to stop");
std::io::stdin().read_line(&mut String::new())?;
stream.eject().unwrap();
Ok(())
}
#[cfg(not(os_alsa))]
fn main() -> Result<()> {
let input = default_input_device();
let output = default_output_device();
let mut input_config = input.default_input_config().unwrap();
input_config.buffer_size_range = (Some(128), Some(512));
let mut output_config = output.default_output_config().unwrap();
output_config.buffer_size_range = (Some(128), Some(512));
let duplex_config = DuplexStreamConfig::new(input_config, output_config);
let stream =
duplex::create_duplex_stream(input, output, RingMod::new(), duplex_config).unwrap();
println!("Press Enter to stop");
std::io::stdin().read_line(&mut String::new())?;
stream.eject().unwrap();
Ok(())
}
struct RingMod {
carrier: SineWave,
}
impl RingMod {
fn new() -> Self {
Self {
carrier: SineWave::new(440.0),
}
}
}
impl AudioDuplexCallback for RingMod {
fn on_audio_data(
&mut self,
context: AudioCallbackContext,
input: AudioInput<f32>,
mut output: AudioOutput<f32>,
) {
if input.buffer.num_samples() < output.buffer.num_samples() {
log::error!("Input underrun");
}
let sr = context.stream_config.samplerate as f32;
let num_samples = output.buffer.num_samples().min(input.buffer.num_samples());
for i in 0..num_samples {
let inp = input.buffer.get_frame(i)[0];
let c = self.carrier.next_sample(sr);
output.buffer.set_mono(i, inp * c);
}
}
}