-
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.
- Loading branch information
Showing
9 changed files
with
128 additions
and
162 deletions.
There are no files selected for viewing
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,67 +1,54 @@ | ||
use std::{sync::mpsc::{Receiver, TryRecvError}, thread, time::Duration}; | ||
|
||
use anyhow::Context; | ||
use rand::prelude::SliceRandom; | ||
use rodio::{OutputStream, Source}; | ||
|
||
use crate::{event_streamer::Event, sample::Samples}; | ||
|
||
pub fn play(events: Receiver<Event>) -> anyhow::Result<()> { | ||
let samples = Samples::load()?; | ||
// Get a output stream handle to the default physical sound device | ||
let (_stream, output) = OutputStream::try_default()?; | ||
|
||
use rodio::{Source, OutputStreamHandle}; | ||
use tokio_stream::{Stream, StreamExt}; | ||
|
||
use crate::events::Event; | ||
use super::sample::Samples; | ||
|
||
pub async fn play<S>( | ||
samples: &Samples, | ||
mut events: S, | ||
output: &OutputStreamHandle | ||
) -> anyhow::Result<()> | ||
where | ||
S: Stream<Item = Event> + Unpin, | ||
{ | ||
let mut rng = rand::thread_rng(); | ||
|
||
loop { | ||
let mut ambience = false; | ||
let mut soundscape = false; | ||
let mut synth = false; | ||
let mut pad = false; | ||
let mut badger = false; | ||
loop { | ||
match events.try_recv() { | ||
Ok(Event::Block) => soundscape = true, | ||
Ok(Event::PixCashier) => badger = true, | ||
Ok(Event::SpinMachine) => ambience = true, | ||
Ok(Event::Brlc) => synth = true, | ||
Ok(Event::Compound) => pad = true, | ||
Err(TryRecvError::Empty) => break, | ||
Err(TryRecvError::Disconnected) => return Ok(()), | ||
}; | ||
} | ||
|
||
if ambience { | ||
let ambience = samples.ambiences.choose(&mut rng).context("no ambiances")?; | ||
output.play_raw(ambience.decoder()?.convert_samples())?; | ||
} | ||
|
||
if synth { | ||
log::info!("😌🎹"); | ||
let synth = &samples.synths.choose(&mut rng).context("no synths")?; | ||
output.play_raw(synth.decoder()?.convert_samples())?; | ||
while let Some(event) = events.next().await { | ||
match event { | ||
Event::Block => { | ||
let soundscape = &samples | ||
.soundscapes | ||
.choose(&mut rng) | ||
.context("no soundscape")?; | ||
output.play_raw(soundscape.decoder()?.convert_samples())?; | ||
}, | ||
|
||
Event::PixCashier => { | ||
let badger = samples.badgers.choose(&mut rng).context("no badger")?; | ||
output.play_raw(badger.decoder()?.convert_samples())?; | ||
}, | ||
|
||
Event::SpinMachine => { | ||
let ambience = samples.ambiences.choose(&mut rng).context("no ambiances")?; | ||
output.play_raw(ambience.decoder()?.convert_samples())?; | ||
}, | ||
|
||
Event::Brlc => { | ||
log::info!("😌🎹"); | ||
let synth = &samples.synths.choose(&mut rng).context("no synths")?; | ||
output.play_raw(synth.decoder()?.convert_samples())?; | ||
}, | ||
|
||
Event::Compound => { | ||
let pad = &samples.pads.choose(&mut rng).context("no pads")?; | ||
output.play_raw(pad.decoder()?.convert_samples())?; | ||
}, | ||
} | ||
|
||
if soundscape { | ||
let soundscape = &samples | ||
.soundscapes | ||
.choose(&mut rng) | ||
.context("no soundscape")?; | ||
output.play_raw(soundscape.decoder()?.convert_samples())?; | ||
} | ||
|
||
if pad { | ||
let pad = &samples.pads.choose(&mut rng).context("no pads")?; | ||
output.play_raw(pad.decoder()?.convert_samples())?; | ||
} | ||
|
||
if badger { | ||
let badger = samples.badgers.choose(&mut rng).context("no badger")?; | ||
output.play_raw(badger.decoder()?.convert_samples())?; | ||
} | ||
|
||
// sleep to avoid looping too much | ||
thread::sleep(Duration::from_millis(20)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
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,71 +1,55 @@ | ||
use std::{sync::mpsc::{Receiver, TryRecvError}, time::Duration, thread}; | ||
|
||
use anyhow::Context; | ||
use rand::prelude::SliceRandom; | ||
use rodio::{OutputStream, Source}; | ||
|
||
use crate::{event_streamer::Event, sample::Samples}; | ||
|
||
pub fn play(events: Receiver<Event>) -> anyhow::Result<()> { | ||
let samples = Samples::load()?; | ||
// Get a output stream handle to the default physical sound device | ||
let (_stream, output) = OutputStream::try_default()?; | ||
|
||
use rodio::{Source, OutputStreamHandle}; | ||
use tokio_stream::{Stream, StreamExt}; | ||
|
||
use crate::events::Event; | ||
use super::sample::Samples; | ||
|
||
pub async fn play<S>( | ||
samples: &Samples, | ||
mut events: S, | ||
output: &OutputStreamHandle | ||
) -> anyhow::Result<()> | ||
where | ||
S: Stream<Item = Event> + Unpin, | ||
{ | ||
let mut rng = rand::thread_rng(); | ||
|
||
loop { | ||
let mut jazz_loop = false; | ||
let mut piano = false; | ||
let mut sax = false; | ||
let mut percussion = false; | ||
let mut bass = false; | ||
loop { | ||
match events.try_recv() { | ||
Ok(Event::Block) => jazz_loop = true, | ||
Ok(Event::PixCashier) => sax = true, | ||
Ok(Event::SpinMachine) => piano = true, | ||
Ok(Event::Brlc) => bass = true, | ||
Ok(Event::Compound) => percussion = true, | ||
Err(TryRecvError::Empty) => break, | ||
Err(TryRecvError::Disconnected) => return Ok(()), | ||
}; | ||
} | ||
|
||
if jazz_loop { | ||
let jazz_loop = samples | ||
.jazz_loops | ||
.choose(&mut rng) | ||
.context("no jazz loops")?; | ||
|
||
output.play_raw(jazz_loop.decoder()?.convert_samples())?; | ||
} | ||
|
||
if piano { | ||
let piano = &samples.pianos.choose(&mut rng).context("no pianos")?; | ||
output.play_raw(piano.decoder()?.convert_samples())?; | ||
} | ||
|
||
if percussion { | ||
let perc = &samples | ||
.percussions | ||
.choose(&mut rng) | ||
.context("no percussion")?; | ||
output.play_raw(perc.decoder()?.convert_samples())?; | ||
} | ||
|
||
if sax { | ||
log::info!("epic sax guy 🎷"); | ||
let sax = &samples.saxes.choose(&mut rng).context("no saxes")?; | ||
output.play_raw(sax.decoder()?.convert_samples())?; | ||
} | ||
|
||
if bass { | ||
let bass = samples.basses.choose(&mut rng).context("no basses")?; | ||
|
||
output.play_raw(bass.decoder()?.convert_samples())?; | ||
} | ||
|
||
// sleep to avoid looping too much | ||
thread::sleep(Duration::from_millis(20)); | ||
while let Some(event) = events.next().await { | ||
match event { | ||
Event::Block => { | ||
let jazz_loop = samples | ||
.jazz_loops | ||
.choose(&mut rng) | ||
.context("no jazz loops")?; | ||
|
||
output.play_raw(jazz_loop.decoder()?.convert_samples())?; | ||
} | ||
|
||
Event::PixCashier => { | ||
log::info!("epic sax guy 🎷"); | ||
let sax = &samples.saxes.choose(&mut rng).context("no saxes")?; | ||
output.play_raw(sax.decoder()?.convert_samples())?; | ||
} | ||
Event::SpinMachine => { | ||
let piano = &samples.pianos.choose(&mut rng).context("no pianos")?; | ||
output.play_raw(piano.decoder()?.convert_samples())?; | ||
} | ||
Event::Brlc => { | ||
let bass = samples.basses.choose(&mut rng).context("no basses")?; | ||
|
||
output.play_raw(bass.decoder()?.convert_samples())?; | ||
} | ||
Event::Compound => { | ||
let perc = &samples | ||
.percussions | ||
.choose(&mut rng) | ||
.context("no percussion")?; | ||
output.play_raw(perc.decoder()?.convert_samples())?; | ||
} | ||
}; | ||
} | ||
|
||
Ok(()) | ||
} |
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,37 +1,32 @@ | ||
mod ambient; | ||
mod jazz; | ||
mod sample; | ||
|
||
use std::thread; | ||
use rodio::OutputStream; | ||
use tokio_stream::Stream; | ||
|
||
use tokio_stream::{Stream, StreamExt}; | ||
|
||
use crate::event_streamer::Event; | ||
use crate::events::Event; | ||
use self::sample::Samples; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum Program { | ||
Ambient, | ||
Jazz, | ||
} | ||
|
||
pub async fn play<S>(mut events: S, program: Program) -> anyhow::Result<()> | ||
where | ||
S: Stream<Item = Event> + Unpin, | ||
{ | ||
let (sender, receiver) = std::sync::mpsc::channel(); | ||
thread::spawn(move || { | ||
let result = match program { | ||
Program::Jazz => jazz::play(receiver), | ||
Program::Ambient => ambient::play(receiver), | ||
}; | ||
|
||
if let Err(error) = result { | ||
log::error!("{}", error); | ||
impl Program { | ||
pub async fn play<S>(&self, events: S) -> anyhow::Result<()> | ||
where | ||
S: Stream<Item = Event> + Unpin, | ||
{ | ||
let samples = Samples::load()?; | ||
// Get a output stream handle to the default physical sound device | ||
let (_stream, output) = OutputStream::try_default()?; | ||
|
||
match self { | ||
Program::Jazz => jazz::play(&samples, events, &output).await, | ||
Program::Ambient => ambient::play(&samples, events, &output).await, | ||
} | ||
}); | ||
|
||
while let Some(event) = events.next().await { | ||
sender.send(event)?; | ||
} | ||
|
||
Ok(()) | ||
} |
File renamed without changes.
File renamed without changes.
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,11 @@ | ||
pub mod streamer; | ||
mod contract; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub enum Event { | ||
Block, | ||
Brlc, | ||
PixCashier, | ||
SpinMachine, | ||
Compound, | ||
} |
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
pub mod args; | ||
mod contract; | ||
pub mod engine; | ||
pub mod event_streamer; | ||
mod sample; | ||
pub mod events; |
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