Skip to content

Commit

Permalink
Remove useless threading model
Browse files Browse the repository at this point in the history
  • Loading branch information
gahag-cw committed Jul 7, 2022
1 parent 32ff61a commit 75e50e4
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 162 deletions.
105 changes: 46 additions & 59 deletions src/engine/ambient.rs
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(())
}

114 changes: 49 additions & 65 deletions src/engine/jazz.rs
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(())
}
39 changes: 17 additions & 22 deletions src/engine/mod.rs
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.
11 changes: 11 additions & 0 deletions src/events/mod.rs
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,
}
13 changes: 2 additions & 11 deletions src/event_streamer.rs → src/events/streamer.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
use std::{fmt::Debug, pin::Pin};
use std::pin::Pin;

use hex_literal::hex;
use tokio_stream::{Stream, StreamExt, StreamMap};

use crate::contract::Contract;

#[derive(Debug, Copy, Clone)]
pub enum Event {
Block,
Brlc,
PixCashier,
SpinMachine,
Compound,
}
use super::{contract::Contract, Event};

pub async fn start() -> anyhow::Result<impl Stream<Item = Event>> {
let brlc_address = hex!("A9a55a81a4C085EC0C31585Aed4cFB09D78dfD53");
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
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;
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ async fn main() -> anyhow::Result<()> {
SimpleLogger::new().with_level(LevelFilter::Info).init()?;
let args = Args::parse();

let stream = chaintrak::event_streamer::start().await?;
chaintrak::engine::play(stream, args.vibe).await
let events = chaintrak::events::streamer::start().await?;
args.vibe.play(events).await
}

0 comments on commit 75e50e4

Please sign in to comment.