Expand description
Asynchronous cross-platform real-time audio recording & playback.
§Getting Started
Add the following to your Cargo.toml:
[dependencies]
pasts = "0.7"
wavy = "0.8"
fon = "0.4"
This example records audio and plays it back in real time as it’s being recorded. (Make sure to wear headphones to avoid feedback):
use fon::{stereo::Stereo32, Sink, Audio};
use pasts::{exec, wait};
use wavy::{Speakers, Microphone, SpeakersSink, MicrophoneStream};
/// An event handled by the event loop.
enum Event<'a> {
/// Speaker is ready to play more audio.
Play(SpeakersSink<'a, Stereo32>),
/// Microphone has recorded some audio.
Record(MicrophoneStream<'a, Stereo32>),
}
/// Shared state between tasks on the thread.
struct State {
/// Temporary buffer for holding real-time audio samples.
buffer: Audio<Stereo32>,
}
impl State {
/// Event loop. Return false to stop program.
fn event(&mut self, event: Event<'_>) {
match event {
Event::Play(mut speakers) => speakers.stream(self.buffer.drain()),
Event::Record(microphone) => self.buffer.extend(microphone),
}
}
}
/// Program start.
fn main() {
let mut state = State { buffer: Audio::with_silence(48_000, 0) };
let mut speakers = Speakers::default();
let mut microphone = Microphone::default();
exec!(state.event(wait! {
Event::Record(microphone.record().await),
Event::Play(speakers.play().await),
}));
}
Structs§
- Microphone
- Record audio samples from a microphone.
- Microphone
Stream - A stream of recorded audio samples from a microphone.
- Speakers
- Play audio samples through a speaker.
- Speakers
Sink - A sink that consumes audio samples and plays them through the speakers.