Skip to content

Commit

Permalink
wip: store previous note
Browse files Browse the repository at this point in the history
  • Loading branch information
nenikitov committed Feb 17, 2024
1 parent f0a998a commit 7831da2
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions engine/src/asset/sound/dat/mixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ impl TSongMixerUtils for TSong {

// Process note
if let Some(note) = event.note {
channel.note = match note {
PatternEventNote::Off => note,
PatternEventNote::On(note) => {
PatternEventNote::On(note - FineTune::new(12))
}
};
channel.change_note(note);
}
if let Some(instrument) = &event.instrument {
channel.instrument = Some(instrument);
Expand Down Expand Up @@ -113,30 +108,58 @@ impl TSongMixerUtils for TSong {
}
}

struct ChannelNote {
finetune: FineTune,
on: bool,
}

#[derive(Default)]
struct Channel<'a> {
instrument: Option<&'a PatternEventInstrumentKind>,

sample_position: usize,

volume: PatternEventVolume,
note: PatternEventNote,
volume_envelope_position: usize,
note: Option<ChannelNote>,
}

impl<'a> Channel<'a> {
fn change_note(&mut self, note: PatternEventNote) {
match (&mut self.note, note) {
(None, PatternEventNote::Off) => {
self.note = None;
}
(None, PatternEventNote::On(target)) => {
self.note = Some(ChannelNote {
finetune: target,
on: true,
});
}
(Some(current), PatternEventNote::Off) => {
current.on = false;
}
(Some(current), PatternEventNote::On(target)) => {
current.finetune = target;
current.on = true;
}
}
}

fn tick(&mut self, duration: usize) -> Sample {
if let Some(instrument) = self.instrument
&& let PatternEventNote::On(note) = self.note
&& let Some(note) = &self.note
&& note.on
&& let PatternEventInstrumentKind::Predefined(instrument) = instrument
&& let TInstrumentSampleKind::Predefined(sample) =
&instrument.samples[note.note() as usize]
&instrument.samples[note.finetune.note() as usize]

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

View workflow job for this annotation

GitHub Actions / clippy

casting `i32` to `usize` may lose the sign of the value

warning: casting `i32` to `usize` may lose the sign of the value --> engine/src/asset/sound/dat/mixer.rs:155:37 | 155 | &instrument.samples[note.finetune.note() as usize] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
{
let volume = match self.volume {
PatternEventVolume::Sample => sample.volume,
PatternEventVolume::Value(value) => value,
};

let pitch_factor = (note + sample.finetune).pitch_factor();
let pitch_factor = (note.finetune + sample.finetune).pitch_factor();

let duration_scaled = (duration as f64 / pitch_factor).round() as usize;

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

View workflow job for this annotation

GitHub Actions / clippy

casting `f64` to `usize` may lose the sign of the value

warning: casting `f64` to `usize` may lose the sign of the value --> engine/src/asset/sound/dat/mixer.rs:164:35 | 164 | let duration_scaled = (duration as f64 / pitch_factor).round() as usize; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss

Expand Down

0 comments on commit 7831da2

Please sign in to comment.