-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from SolarLiner/duplex
Initial duplex audio wrapper for single input/output stream pairs
- Loading branch information
Showing
11 changed files
with
304 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::util::sine::SineWave; | ||
use anyhow::Result; | ||
use interflow::duplex::AudioDuplexCallback; | ||
use interflow::prelude::*; | ||
|
||
mod util; | ||
|
||
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 stream = | ||
duplex::create_duplex_stream(input, input_config, output, output_config, RingMod::new()) | ||
.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>, | ||
) { | ||
let sr = context.stream_config.samplerate as f32; | ||
for i in 0..output.buffer.num_samples() { | ||
let inp = input.buffer.get_frame(i)[0]; | ||
let c = self.carrier.next_sample(sr); | ||
output.buffer.set_mono(i, inp * c); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,20 @@ | ||
use std::f32::consts::TAU; | ||
|
||
use anyhow::Result; | ||
|
||
use util::sine::SineWave; | ||
use interflow::prelude::*; | ||
|
||
mod util; | ||
|
||
fn main() -> Result<()> { | ||
env_logger::init(); | ||
|
||
let device = default_output_device(); | ||
println!("Using device {}", device.name()); | ||
let stream = device | ||
.default_output_stream(SineWave { | ||
frequency: 440., | ||
phase: 0., | ||
}) | ||
.default_output_stream(SineWave::new(440.0)) | ||
.unwrap(); | ||
println!("Press Enter to stop"); | ||
std::io::stdin().read_line(&mut String::new())?; | ||
stream.eject().unwrap(); | ||
Ok(()) | ||
} | ||
|
||
struct SineWave { | ||
frequency: f32, | ||
phase: f32, | ||
} | ||
|
||
impl AudioOutputCallback for SineWave { | ||
fn on_output_data(&mut self, context: AudioCallbackContext, mut output: AudioOutput<f32>) { | ||
eprintln!( | ||
"Callback called, timestamp: {:2.3} s", | ||
context.timestamp.as_seconds() | ||
); | ||
let sr = context.timestamp.samplerate as f32; | ||
for i in 0..output.buffer.num_samples() { | ||
output.buffer.set_mono(i, self.next_sample(sr)); | ||
} | ||
// Reduce amplitude to not blow up speakers and ears | ||
output.buffer.change_amplitude(0.125); | ||
} | ||
} | ||
|
||
impl SineWave { | ||
pub fn next_sample(&mut self, samplerate: f32) -> f32 { | ||
let step = samplerate.recip() * self.frequency; | ||
let y = (TAU * self.phase).sin(); | ||
self.phase += step; | ||
if self.phase > 1. { | ||
self.phase -= 1.; | ||
} | ||
y | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod enumerate; | ||
pub mod sine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use interflow::{AudioCallbackContext, AudioOutput, AudioOutputCallback}; | ||
use std::f32::consts::TAU; | ||
|
||
pub struct SineWave { | ||
pub frequency: f32, | ||
pub phase: f32, | ||
} | ||
|
||
impl AudioOutputCallback for SineWave { | ||
fn on_output_data(&mut self, context: AudioCallbackContext, mut output: AudioOutput<f32>) { | ||
eprintln!( | ||
"Callback called, timestamp: {:2.3} s", | ||
context.timestamp.as_seconds() | ||
); | ||
let sr = context.timestamp.samplerate as f32; | ||
for i in 0..output.buffer.num_samples() { | ||
output.buffer.set_mono(i, self.next_sample(sr)); | ||
} | ||
// Reduce amplitude to not blow up speakers and ears | ||
output.buffer.change_amplitude(0.125); | ||
} | ||
} | ||
|
||
impl SineWave { | ||
pub fn new(frequency: f32) -> Self { | ||
Self { | ||
frequency, | ||
phase: 0.0, | ||
} | ||
} | ||
|
||
pub fn next_sample(&mut self, samplerate: f32) -> f32 { | ||
let step = samplerate.recip() * self.frequency; | ||
let y = (TAU * self.phase).sin(); | ||
self.phase += step; | ||
if self.phase > 1. { | ||
self.phase -= 1.; | ||
} | ||
y | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.