iterators can be played now

This commit is contained in:
Keagan McClelland
2022-05-16 10:37:41 -06:00
parent b9de5755d1
commit 2d44852ec4
2 changed files with 13 additions and 8 deletions

View File

@@ -359,7 +359,7 @@ pub async fn execute_inner(
}) })
.await .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::error!("Error recovering drive!: {}", e);
tracing::debug!("{:?}", e); tracing::debug!("{:?}", e);
*ctx.recovery_status.write().await = Some(Err(e.into())); *ctx.recovery_status.write().await = Some(Err(e.into()));

View File

@@ -112,19 +112,23 @@ pub struct Song<Notes> {
tempo_qpm: u16, tempo_qpm: u16,
note_sequence: Notes, note_sequence: Notes,
} }
impl<'a, T: 'a> Song<T> impl<'a, T> Song<T>
where where
&'a T: IntoIterator<Item = &'a (Option<Note>, TimeSlice)>, T: IntoIterator<Item = (Option<Note>, TimeSlice)> + Clone,
{ {
#[instrument(skip(self))] #[instrument(skip(self))]
pub async fn play(&'a self) -> Result<(), Error> { pub async fn play(&self) -> Result<(), Error> {
#[cfg(feature = "sound")] #[cfg(feature = "sound")]
{ {
let mut sound = SoundInterface::lease().await?; let mut sound = SoundInterface::lease().await?;
for (note, slice) in &self.note_sequence { for (note, slice) in self.note_sequence.clone() {
match note { match note {
None => tokio::time::sleep(slice.to_duration(self.tempo_qpm)).await, 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?; sound.close().await?;
@@ -285,6 +289,7 @@ pub fn circle_of_fourths(note: &Note) -> impl Iterator<Item = Note> {
iterate(|n| interval(&FOURTH, n), note) iterate(|n| interval(&FOURTH, n), note)
} }
#[derive(Clone, Debug)]
pub struct CircleOf<'a> { pub struct CircleOf<'a> {
current: Note, current: Note,
duration: TimeSlice, duration: TimeSlice,
@@ -404,7 +409,7 @@ pub const BEETHOVEN: Song<[(Option<Note>, TimeSlice); 9]> = song!(216, [
]); ]);
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub static ref CIRCLE_OF_5THS_SHORT: Song<Vec<(Option<Note>, TimeSlice)>> = Song { pub static ref CIRCLE_OF_5THS_SHORT: Song<std::iter::Take<CircleOf<'static>>> = Song {
tempo_qpm: 300, tempo_qpm: 300,
note_sequence: CircleOf::new( note_sequence: CircleOf::new(
&FIFTH, &FIFTH,
@@ -414,7 +419,7 @@ lazy_static::lazy_static! {
}, },
TimeSlice::Triplet(&TimeSlice::Eighth), TimeSlice::Triplet(&TimeSlice::Eighth),
) )
.take(6).collect(), .take(6),
}; };
pub static ref CIRCLE_OF_4THS_SHORT: Song<std::iter::Take<CircleOf<'static>>> = Song { pub static ref CIRCLE_OF_4THS_SHORT: Song<std::iter::Take<CircleOf<'static>>> = Song {
tempo_qpm: 300, tempo_qpm: 300,