From 2d44852ec4be71e50135d77c5cdc8dc952146499 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Mon, 16 May 2022 10:37:41 -0600 Subject: [PATCH] iterators can be played now --- backend/src/setup.rs | 2 +- backend/src/sound.rs | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/backend/src/setup.rs b/backend/src/setup.rs index 898d9bad8..7b92923fc 100644 --- a/backend/src/setup.rs +++ b/backend/src/setup.rs @@ -359,7 +359,7 @@ pub async fn execute_inner( }) .await { - BEETHOVEN.play().await.unwrap_or_default(); // ignore error in playing the song + (&BEETHOVEN).play().await.unwrap_or_default(); // ignore error in playing the song tracing::error!("Error recovering drive!: {}", e); tracing::debug!("{:?}", e); *ctx.recovery_status.write().await = Some(Err(e.into())); diff --git a/backend/src/sound.rs b/backend/src/sound.rs index 69b7b2f30..f78a4627b 100644 --- a/backend/src/sound.rs +++ b/backend/src/sound.rs @@ -112,19 +112,23 @@ pub struct Song { tempo_qpm: u16, note_sequence: Notes, } -impl<'a, T: 'a> Song +impl<'a, T> Song where - &'a T: IntoIterator, TimeSlice)>, + T: IntoIterator, TimeSlice)> + Clone, { #[instrument(skip(self))] - pub async fn play(&'a self) -> Result<(), Error> { + pub async fn play(&self) -> Result<(), Error> { #[cfg(feature = "sound")] { let mut sound = SoundInterface::lease().await?; - for (note, slice) in &self.note_sequence { + for (note, slice) in self.note_sequence.clone() { match note { None => tokio::time::sleep(slice.to_duration(self.tempo_qpm)).await, - Some(n) => sound.play_for_time_slice(self.tempo_qpm, n, slice).await?, + Some(n) => { + sound + .play_for_time_slice(self.tempo_qpm, &n, &slice) + .await? + } }; } sound.close().await?; @@ -285,6 +289,7 @@ pub fn circle_of_fourths(note: &Note) -> impl Iterator { iterate(|n| interval(&FOURTH, n), note) } +#[derive(Clone, Debug)] pub struct CircleOf<'a> { current: Note, duration: TimeSlice, @@ -404,7 +409,7 @@ pub const BEETHOVEN: Song<[(Option, TimeSlice); 9]> = song!(216, [ ]); lazy_static::lazy_static! { - pub static ref CIRCLE_OF_5THS_SHORT: Song, TimeSlice)>> = Song { + pub static ref CIRCLE_OF_5THS_SHORT: Song>> = Song { tempo_qpm: 300, note_sequence: CircleOf::new( &FIFTH, @@ -414,7 +419,7 @@ lazy_static::lazy_static! { }, TimeSlice::Triplet(&TimeSlice::Eighth), ) - .take(6).collect(), + .take(6), }; pub static ref CIRCLE_OF_4THS_SHORT: Song>> = Song { tempo_qpm: 300,