From 1c7e4c9f65d2056db61797e292a8e6c9a147812f Mon Sep 17 00:00:00 2001 From: dawnDus <96957561+dawndus@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:45:54 -0500 Subject: [PATCH] SSS operand implemented --- src/scripting/tsc/text_script.rs | 7 +++---- src/sound/mod.rs | 11 +++++++++++ src/sound/pixtone.rs | 20 ++++++++++++++++---- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/scripting/tsc/text_script.rs b/src/scripting/tsc/text_script.rs index a585def..40b2418 100644 --- a/src/scripting/tsc/text_script.rs +++ b/src/scripting/tsc/text_script.rs @@ -1637,11 +1637,10 @@ impl TextScriptVM { exec_state = TextScriptExecutionState::Running(event, cursor.position() as u32); } TSCOpCode::SSS => { - let _freq = read_cur_varint(&mut cursor)?; + let freq = read_cur_varint(&mut cursor)? as f32 / 2205.0; - // todo change freq - state.sound_manager.loop_sfx(40); - state.sound_manager.loop_sfx(41); + state.sound_manager.loop_sfx_freq(40, freq); + state.sound_manager.loop_sfx_freq(41, freq); exec_state = TextScriptExecutionState::Running(event, cursor.position() as u32); } diff --git a/src/sound/mod.rs b/src/sound/mod.rs index 48da74a..5e5f953 100644 --- a/src/sound/mod.rs +++ b/src/sound/mod.rs @@ -122,6 +122,13 @@ impl SoundManager { let _ = self.tx.send(PlaybackMessage::LoopSample(id)); } + pub fn loop_sfx_freq(&self, id: u8, freq: f32) { + if self.no_audio { + return; + } + let _ = self.tx.send(PlaybackMessage::LoopSampleFreq(id, freq)); + } + pub fn stop_sfx(&self, id: u8) { if self.no_audio { return; @@ -394,6 +401,7 @@ pub(in crate::sound) enum PlaybackMessage { PlayOggSongMultiPart(Box>, Box>), PlaySample(u8), LoopSample(u8), + LoopSampleFreq(u8, f32), StopSample(u8), SetSpeed(f32), SaveState, @@ -520,6 +528,9 @@ where Ok(PlaybackMessage::LoopSample(id)) => { pixtone.loop_sfx(id); } + Ok(PlaybackMessage::LoopSampleFreq(id, freq)) => { + pixtone.loop_sfx_freq(id, freq); + } Ok(PlaybackMessage::StopSample(id)) => { pixtone.stop_sfx(id); } diff --git a/src/sound/pixtone.rs b/src/sound/pixtone.rs index 9f14642..41c38ff 100644 --- a/src/sound/pixtone.rs +++ b/src/sound/pixtone.rs @@ -182,6 +182,7 @@ pub struct PlaybackState { pos: f32, tag: u32, looping: bool, + freq: f32, } pub struct PixTonePlayback { @@ -226,7 +227,7 @@ impl PixTonePlayback { } } - self.playback_state.push(PlaybackState { id, pos: 0.0, tag: 0, looping: false }); + self.playback_state.push(PlaybackState { id, pos: 0.0, tag: 0, looping: false, freq: 1.0 }); } pub fn loop_sfx(&mut self, id: u8) { @@ -237,7 +238,18 @@ impl PixTonePlayback { } } - self.playback_state.push(PlaybackState { id, pos: 0.0, tag: 0, looping: true }); + self.playback_state.push(PlaybackState { id, pos: 0.0, tag: 0, looping: true, freq: 1.0 }); + } + + pub fn loop_sfx_freq(&mut self, id: u8, freq: f32) { + for state in &mut self.playback_state { + if state.id == id && state.tag == 0 { + state.looping = true; + return; + } + } + + self.playback_state.push(PlaybackState { id, pos: 0.0, tag: 0, looping: true, freq }); } pub fn stop_sfx(&mut self, id: u8) { @@ -247,7 +259,7 @@ impl PixTonePlayback { } pub fn play_concurrent(&mut self, id: u8, tag: u32) { - self.playback_state.push(PlaybackState { id, pos: 0.0, tag, looping: false }); + self.playback_state.push(PlaybackState { id, pos: 0.0, tag, looping: false, freq: 1.0 }); } pub fn mix(&mut self, dst: &mut [u16], sample_rate: f32) { @@ -285,7 +297,7 @@ impl PixTonePlayback { let sam = (*result ^ 0x8000) as i16; *result = sam.saturating_add(s as i16) as u16 ^ 0x8000; - state.pos += delta; + state.pos += delta * state.freq; } if remove {