Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nenikitov committed Oct 9, 2024
1 parent a41c48c commit b75f1bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
39 changes: 20 additions & 19 deletions engine/src/asset/sound/dat/mixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Check warning on line 212 in engine/src/asset/sound/dat/mixer.rs

View workflow job for this annotation

GitHub Actions / clippy

this boolean expression can be simplified

warning: this boolean expression can be simplified --> engine/src/asset/sound/dat/mixer.rs:208:16 | 208 | if !self.note.on | ________________^ 209 | | || (self.note.on 210 | | && envelope 211 | | .sustain 212 | | .map_or(true, |s| self.pos_volume_envelope < s)) | |________________________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool = note: `#[warn(clippy::nonminimal_bool)]` on by default help: try | 208 ~ if !(self.note.on && !envelope 209 + .sustain 210 + .map_or(true, |s| self.pos_volume_envelope < s)) | 208 ~ if !self.note.on || envelope 209 + .sustain 210 + .map_or(true, |s| self.pos_volume_envelope < s) |
{
self.pos_volume_envelope += 1;
}
Expand Down Expand Up @@ -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::<f32>();
self.volume_global_actual = advance_to(
self.volume_global_actual,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -452,7 +448,7 @@ impl<'a> Player<'a> {
channel.change_effect(i, effect.clone());

Check warning on line 448 in engine/src/asset/sound/dat/mixer.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `PatternEffect` which implements the `Copy` trait

warning: using `clone` on type `PatternEffect` which implements the `Copy` trait --> engine/src/asset/sound/dat/mixer.rs:448:42 | 448 | channel.change_effect(i, effect.clone()); | ^^^^^^^^^^^^^^ help: try removing the `clone` call: `effect` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy = note: `#[warn(clippy::clone_on_copy)]` on by default

use PatternEffect as E;

Check warning on line 450 in engine/src/asset/sound/dat/mixer.rs

View workflow job for this annotation

GitHub Actions / clippy

adding items after statements is confusing, since items exist from the start of the scope

warning: adding items after statements is confusing, since items exist from the start of the scope --> engine/src/asset/sound/dat/mixer.rs:450:17 | 450 | use PatternEffect as E; | ^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements = note: `#[warn(clippy::items_after_statements)]` implied by `#[warn(clippy::pedantic)]`
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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::<i16>()))
.collect();
let samples: Vec<_> = std::iter::from_fn(|| {
if player.pos_loop == 0 {
Some(player.generate_sample::<i16>())
} else {
None
}
})
.collect();

AudioBuffer {
data: samples,
Expand Down
2 changes: 1 addition & 1 deletion engine/src/asset/sound/dat/t_instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl AssetParser<Wildcard> 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<i16> {
Expand Down

0 comments on commit b75f1bf

Please sign in to comment.