Skip to content

Commit

Permalink
fix(playback): use multiple sinks
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Sep 23, 2023
1 parent 3dcf3b4 commit 594173d
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,28 @@ pub async fn run() -> Result<()> {

// Create the sink for audio playback.
let (_stream, handle) = OutputStream::try_default()?;
let sink = Sink::try_new(&handle)?;
let key_press_sink = Sink::try_new(&handle)?;
let key_release_sink = Sink::try_new(&handle)?;

// Handle events loop.
let mut key_released = true;
loop {
// Create a 2-channel mixer.
let (controller, mixer) = rodio::dynamic_mixer::mixer::<i16>(2, 44_100);

// Handle events - i.e. add data to the mixer controller.
if let Some(event) = receiver.recv().await {
tracing::debug!("{:?}", event);
match event.event_type {
EventType::KeyPress(_) => {
if key_released {
let sound = Sounds::get_sound(Sound::Keydown)?;
controller.add(rodio::Decoder::new(BufReader::new(sound))?);
sink.stop();
sink.append(mixer);
key_press_sink.stop();
key_press_sink.append(rodio::Decoder::new(BufReader::new(sound))?);
}
key_released = false;
}
EventType::KeyRelease(_) => {
let sound = Sounds::get_sound(Sound::Keyup)?;
controller.add(rodio::Decoder::new(BufReader::new(sound))?);
sink.stop();
sink.append(mixer);
key_release_sink.stop();
key_release_sink.append(rodio::Decoder::new(BufReader::new(sound))?);
key_released = true;
}
_ => {}
Expand Down

0 comments on commit 594173d

Please sign in to comment.