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
{
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()));

View File

@@ -112,19 +112,23 @@ pub struct Song<Notes> {
tempo_qpm: u16,
note_sequence: Notes,
}
impl<'a, T: 'a> Song<T>
impl<'a, T> Song<T>
where
&'a T: IntoIterator<Item = &'a (Option<Note>, TimeSlice)>,
T: IntoIterator<Item = (Option<Note>, 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<Item = Note> {
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<Note>, TimeSlice); 9]> = song!(216, [
]);
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,
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<std::iter::Take<CircleOf<'static>>> = Song {
tempo_qpm: 300,