1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-15 18:32:48 +00:00

sample looping support

This commit is contained in:
Alula 2021-06-27 08:02:21 +02:00
parent 0e5d85ff05
commit 2b873a92a2
No known key found for this signature in database
GPG key ID: 3E00485503A1D8BA
2 changed files with 60 additions and 6 deletions

View file

@ -78,6 +78,14 @@ impl SoundManager {
let _ = self.tx.send(PlaybackMessage::PlaySample(id)); let _ = self.tx.send(PlaybackMessage::PlaySample(id));
} }
pub fn loop_sfx(&self, id: u8) {
let _ = self.tx.send(PlaybackMessage::LoopSample(id));
}
pub fn stop_sfx(&self, id: u8) {
let _ = self.tx.send(PlaybackMessage::StopSample(id));
}
pub fn play_song( pub fn play_song(
&mut self, &mut self,
song_id: usize, song_id: usize,
@ -255,7 +263,6 @@ impl SoundManager {
let _ = splits.next(); let _ = splits.next();
if let Some(str) = splits.next() { if let Some(str) = splits.next() {
println!("{}", str);
return str.trim().parse::<T>().map_err(|_| GameError::ParseError("failed to parse the value as specified type.".to_string())) return str.trim().parse::<T>().map_err(|_| GameError::ParseError("failed to parse the value as specified type.".to_string()))
} else { } else {
break; break;
@ -314,6 +321,8 @@ enum PlaybackMessage {
#[cfg(feature = "ogg-playback")] #[cfg(feature = "ogg-playback")]
PlayOggSongMultiPart(Box<OggStreamReader<File>>, Box<OggStreamReader<File>>), PlayOggSongMultiPart(Box<OggStreamReader<File>>, Box<OggStreamReader<File>>),
PlaySample(u8), PlaySample(u8),
LoopSample(u8),
StopSample(u8),
SetSpeed(f32), SetSpeed(f32),
SaveState, SaveState,
RestoreState, RestoreState,
@ -428,6 +437,13 @@ where
Ok(PlaybackMessage::PlaySample(id)) => { Ok(PlaybackMessage::PlaySample(id)) => {
pixtone.play_sfx(id); pixtone.play_sfx(id);
} }
Ok(PlaybackMessage::LoopSample(id)) => {
pixtone.loop_sfx(id);
}
Ok(PlaybackMessage::StopSample(id)) => {
pixtone.stop_sfx(id);
}
Ok(PlaybackMessage::Stop) => { Ok(PlaybackMessage::Stop) => {
if state == PlaybackState::Stopped { if state == PlaybackState::Stopped {
saved_state = PlaybackStateType::None; saved_state = PlaybackStateType::None;

View file

@ -177,7 +177,12 @@ impl PixToneParameters {
} }
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub struct PlaybackState(u8, f32, u32); pub struct PlaybackState {
id: u8
pos: f32,
tag: u32,
looping: bool,
}
pub struct PixTonePlayback { pub struct PixTonePlayback {
pub samples: HashMap<u8, Vec<i16>>, pub samples: HashMap<u8, Vec<i16>>,
@ -213,17 +218,50 @@ impl PixTonePlayback {
pub fn play_sfx(&mut self, id: u8) { pub fn play_sfx(&mut self, id: u8) {
for state in self.playback_state.iter_mut() { for state in self.playback_state.iter_mut() {
if state.0 == id && state.2 == 0 { if state.id == id && state.tag == 0 {
state.1 = 0.0; state.pos = 0.0;
state.looping = false;
return; return;
} }
} }
self.playback_state.push(PlaybackState(id, 0.0, 0)); self.playback_state.push(PlaybackState {
id,
pos: 0.0,
tag: 0,
looping: false
});
}
pub fn loop_sfx(&mut self, id: u8) {
for state in self.playback_state.iter_mut() {
if state.id == id && state.tag == 0 {
state.looping = true;
return;
}
}
self.playback_state.push(PlaybackState {
id,
pos: 0.0,
tag: 0,
looping: true
});
}
pub fn stop_sfx(&mut self, id: u8) {
if let Some(pos) = self.playback_state.iter().position(|s| s.id == id && s.tag == 0) {
self.playback_state.remove(pos);
}
} }
pub fn play_concurrent(&mut self, id: u8, tag: u32) { pub fn play_concurrent(&mut self, id: u8, tag: u32) {
self.playback_state.push(PlaybackState(id, 0.0, tag)); self.playback_state.push(PlaybackState {
id,
pos: 0.0,
tag,
looping: false
});
} }
pub fn mix(&mut self, dst: &mut [u16], sample_rate: f32) { pub fn mix(&mut self, dst: &mut [u16], sample_rate: f32) {