From b75f1bf6210667c9c215412b430c238183509584 Mon Sep 17 00:00:00 2001 From: nenikitov Date: Tue, 8 Oct 2024 23:55:38 -0400 Subject: [PATCH] refactor --- engine/src/asset/sound/dat/mixer.rs | 39 +++++++++++----------- engine/src/asset/sound/dat/t_instrument.rs | 2 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/engine/src/asset/sound/dat/mixer.rs b/engine/src/asset/sound/dat/mixer.rs index 8a59c0c..c007e46 100644 --- a/engine/src/asset/sound/dat/mixer.rs +++ b/engine/src/asset/sound/dat/mixer.rs @@ -205,13 +205,11 @@ impl PlayerChannel { if let Some(instrument) = &self.instrument && let TInstrumentVolume::Envelope(envelope) = &instrument.volume { - if (self.note.on - && if let Some(sustain) = envelope.sustain { - self.pos_volume_envelope < sustain - } else { - true - }) - || !self.note.on + if !self.note.on + || (self.note.on + && envelope + .sustain + .map_or(true, |s| self.pos_volume_envelope < s)) { self.pos_volume_envelope += 1; } @@ -310,8 +308,6 @@ impl<'a> Player<'a> { .channels .iter_mut() .map(|c| c.generate_sample(step)) - //.enumerate() - //.filter_map(|(i, s)| (i == 0).then_some(s)) .sum::(); self.volume_global_actual = advance_to( self.volume_global_actual, @@ -402,7 +398,7 @@ impl<'a> Player<'a> { { self.pos_row = 0; self.pos_pattern += 1; - }; + } if self.pos_pattern >= self.song.orders.len() { self.pos_pattern = self.song.restart_order as usize; self.pos_loop += 1; @@ -434,12 +430,12 @@ impl<'a> Player<'a> { channel.change_instrument(instrument.clone()); } - if let Some(note) = &event.note { - channel.change_note(note.clone()); + if let Some(note) = event.note { + channel.change_note(note); } - if let Some(volume) = &event.volume { - channel.change_volume(volume.clone()); + if let Some(volume) = event.volume { + channel.change_volume(volume); } channel.clear_effects(); @@ -452,7 +448,7 @@ impl<'a> Player<'a> { channel.change_effect(i, effect.clone()); use PatternEffect as E; - match channel.effects[i].unwrap() { + match channel.effects[i].expect("`change_effect` sets the effect") { // Init effects E::Speed(Speed::Bpm(bpm)) => { self.bpm = bpm; @@ -494,7 +490,7 @@ impl<'a> Player<'a> { } E::SampleOffset(Some(offset)) => { // TODO(nenikitov): Remove this hardcoded value - channel.pos_sample = 1. / 16_000. * offset as f64; + channel.pos_sample = offset as f64 / TSample::SAMPLE_RATE as f64; } E::NoteDelay(delay) => { channel.note_delay = delay; @@ -528,9 +524,14 @@ impl TSongMixer for TSong { let mut player = Player::new(self, SAMPLE_RATE, AMPLIFICATION); - let samples: Vec<_> = - std::iter::from_fn(|| (player.pos_loop == 0).then(|| player.generate_sample::())) - .collect(); + let samples: Vec<_> = std::iter::from_fn(|| { + if player.pos_loop == 0 { + Some(player.generate_sample::()) + } else { + None + } + }) + .collect(); AudioBuffer { data: samples, diff --git a/engine/src/asset/sound/dat/t_instrument.rs b/engine/src/asset/sound/dat/t_instrument.rs index 6d0f949..f56e48f 100644 --- a/engine/src/asset/sound/dat/t_instrument.rs +++ b/engine/src/asset/sound/dat/t_instrument.rs @@ -250,7 +250,7 @@ impl AssetParser for TSample { } impl TSample { - const SAMPLE_RATE: usize = 16_000; + pub const SAMPLE_RATE: usize = 16_000; // TODO(nenikitov): I think the whole `Sample` will need to be removed pub fn get(&self, position: f64) -> Option {