Skip to content

Commit

Permalink
feat(effect): start playback direction
Browse files Browse the repository at this point in the history
  • Loading branch information
nenikitov committed May 3, 2024
1 parent dae3928 commit 8a30cac
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
12 changes: 7 additions & 5 deletions engine/src/asset/sound/dat/mixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl TSongMixerUtils for TSong {
channel.effects[e].expect("effect is initialized after assignment")
}) {
match effect {
PatternEffect::Dummy => {}
PatternEffect::Dummy(_) => {}

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

View workflow job for this annotation

GitHub Actions / clippy

this match arm has an identical body to the `_` wildcard arm

warning: this match arm has an identical body to the `_` wildcard arm --> engine/src/asset/sound/dat/mixer.rs:85:29 | 85 | ... PatternEffect::Dummy(_) => {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the arm | = help: or try changing either arm body note: `_` wildcard arm here --> engine/src/asset/sound/dat/mixer.rs:105:29 | 105 | ... _ => {} | ^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_same_arms = note: `#[warn(clippy::match_same_arms)]` implied by `#[warn(clippy::pedantic)]`
PatternEffect::Speed(Speed::Bpm(s)) => {
bpm = s;
}
Expand All @@ -92,16 +92,17 @@ impl TSongMixerUtils for TSong {
PatternEffect::Volume(Volume::Value(volume)) => {
channel.volume = volume;
}
PatternEffect::Volume(Volume::Slide(Some(volume))) => {
channel.volume_slide = volume;
}
PatternEffect::SampleOffset(Some(offset)) => {
channel.sample_position = offset;
}
PatternEffect::PlaybackDirection(direction) => {
channel.playback_direaction = direction

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

View workflow job for this annotation

GitHub Actions / clippy

consider adding a `;` to the last statement for consistent formatting

warning: consider adding a `;` to the last statement for consistent formatting --> engine/src/asset/sound/dat/mixer.rs:99:33 | 99 | ... channel.playback_direaction = direction | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `channel.playback_direaction = direction;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned = note: `#[warn(clippy::semicolon_if_nothing_returned)]` implied by `#[warn(clippy::pedantic)]`
}
PatternEffect::Volume(Volume::Slide(None))
| PatternEffect::SampleOffset(None) => {
unreachable!("effect memory should already be initialized")
}
_ => {}
};
}

Expand Down Expand Up @@ -172,7 +173,8 @@ struct Channel<'a> {

volume: f32,
volume_evelope_position: usize,
volume_slide: f32,

playback_direaction: PlaybackDirection,
}

impl<'a> Channel<'a> {
Expand Down
16 changes: 12 additions & 4 deletions engine/src/asset/sound/dat/pattern_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ pub enum Volume {
Slide(Option<f32>),
}

#[derive(Debug, Default, Clone, Copy)]
pub enum PlaybackDirection {
#[default]
Forwards,
Backwards,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum PatternEffectMemoryKey {
VolumeSlide,
Expand All @@ -26,10 +33,11 @@ pub enum PatternEffectMemoryKey {

#[derive(Debug, Clone, Copy)]
pub enum PatternEffect {
Dummy,
Dummy(u8),
Speed(Speed),
Volume(Volume),
SampleOffset(Option<usize>),
PlaybackDirection(PlaybackDirection),
}

impl PatternEffect {
Expand Down Expand Up @@ -88,7 +96,7 @@ impl AssetParser<Wildcard> for Option<PatternEffect> {
0x00 | 0x01 | 0x02 | 0x03 | 0x04 | 0x05 | 0x06 | 0x07 | 0x08 | 0x0A | 0x0B
| 0x0C | 0x0D | 0x0F | 0x14 | 0x15 | 0x16 | 0x1D | 0x1E | 0x1F | 0x20
| 0x21 | 0x22 | 0x24 | 0x25 | 0x2E | 0x2F | 0x30 | 0x31 | 0x32 | 0x33
| 0x34 | 0x35 | 0x36 | 0x37 => PatternEffect::Dummy,
| 0x34 | 0x35 => PatternEffect::Dummy(kind),
// TODO(nenikitov): Add support for other effects
// 0x00 => Self::Arpegio,
// 0x01 => Self::PortaUp,
Expand Down Expand Up @@ -123,8 +131,8 @@ impl AssetParser<Wildcard> for Option<PatternEffect> {
// 0x33 => Self::SoundControlQuad,
// 0x34 => Self::FilterGlobal,
// 0x35 => Self::FilterLocal,
// 0x36 => Self::PlayForward,
// 0x37 => Self::PlayBackward,
0x36 => PatternEffect::PlaybackDirection(PlaybackDirection::Forwards),
0x37 => PatternEffect::PlaybackDirection(PlaybackDirection::Backwards),
// TODO(nenikitov): Should be a `Result`
kind => unreachable!("Effect is outside the range {kind}"),
}),
Expand Down
10 changes: 4 additions & 6 deletions engine/src/asset/sound/dat/t_instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,10 @@ impl AssetParser<Wildcard> for TSample {
// TODO(nenikitov): Look into resampling the sample to 48 KHz
loop_length,
data: Sample {
data: Box::new(
sample_data[sample_offset as usize..loop_end as usize]
.into_iter()
.map(|&s| [s])
.collect(),
),
data: sample_data[sample_offset as usize..loop_end as usize]
.into_iter()

Check warning on line 269 in engine/src/asset/sound/dat/t_instrument.rs

View workflow job for this annotation

GitHub Actions / clippy

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `slice`

warning: this `.into_iter()` call is equivalent to `.iter()` and will not consume the `slice` --> engine/src/asset/sound/dat/t_instrument.rs:269:30 | 269 | ... .into_iter() | ^^^^^^^^^ help: call directly: `iter` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref = note: `#[warn(clippy::into_iter_on_ref)]` on by default
.map(|&s| [s])
.collect(),
sample_rate: Self::SAMPLE_RATE,
},
},
Expand Down
4 changes: 2 additions & 2 deletions engine/src/asset/sound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ mod tests {
Sound::Song(s) => Some(s),
Sound::Effect(_) => None,
})
.collect::<Vec<_>>()[0x0];
dbg!(&test_music.patterns[0][0x17][2].effects);
.collect::<Vec<_>>()[0x9];
dbg!(&test_music.patterns[0][0x29][5].effects);

sounds
.iter()
Expand Down
10 changes: 4 additions & 6 deletions engine/src/asset/sound/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub enum Interpolation<S: SamplePoint, const CHANNELS: usize> {

#[derive(Debug, Clone)]
pub struct Sample<S: SamplePoint, const CHANNELS: usize> {
pub data: Box<Vec<[S; CHANNELS]>>,
pub data: Vec<[S; CHANNELS]>,
pub sample_rate: usize,
}

Expand Down Expand Up @@ -165,7 +165,7 @@ impl<S: SamplePoint, const CHANNELS: usize> Sample<S, CHANNELS> {
.stretch(sample_rate as f32 / self.sample_rate as f32, interpolation);

Self {
data: Box::new(data),
data,
sample_rate: self.sample_rate,
}
}
Expand Down Expand Up @@ -226,10 +226,8 @@ impl<S: SamplePoint, const CHANNELS: usize> SampleDataProcessing<S, CHANNELS>
let len = (self.len() as f32 * factor).round() as usize;

Check warning on line 226 in engine/src/asset/sound/sample.rs

View workflow job for this annotation

GitHub Actions / clippy

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

warning: casting `f32` to `usize` may lose the sign of the value --> engine/src/asset/sound/sample.rs:226:19 | 226 | let len = (self.len() as f32 * factor).round() as usize; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss

(0..len)
.into_iter()
.map(|(i_sample)| {
(0..CHANNELS)
.into_iter()
.map(|i_channel| match interpolation {
Interpolation::Nearest => {
self[(i_sample as f32 / factor).floor() as usize][i_channel]

Check warning on line 233 in engine/src/asset/sound/sample.rs

View workflow job for this annotation

GitHub Actions / clippy

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

warning: casting `f32` to `usize` may lose the sign of the value --> engine/src/asset/sound/sample.rs:233:34 | 233 | ... self[(i_sample as f32 / factor).floor() as usize][i_channel] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
Expand Down Expand Up @@ -272,7 +270,7 @@ impl<S: SamplePoint> Sample<S, 1> {
let data = self.data.iter().map(|[s]| [*s, *s]).collect();

Sample::<S, 2> {
data: Box::new(data),
data,
sample_rate: self.sample_rate,
}
}
Expand All @@ -299,7 +297,7 @@ impl<S: SamplePoint> Sample<S, 2> {
.collect_vec();

Sample::<S, 1> {
data: Box::new(data),
data,
sample_rate: self.sample_rate,
}
}
Expand Down

0 comments on commit 8a30cac

Please sign in to comment.