1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-01 03:54:15 +00:00

fix integer overflow handling in sample mixer

This commit is contained in:
Alula 2020-09-20 17:37:53 +02:00
parent 087218b4fd
commit 9acb513fde
No known key found for this signature in database
GPG key ID: 3E00485503A1D8BA

View file

@ -13,6 +13,7 @@ use crate::sound::pixtone::PixTonePlayback;
use crate::sound::playback::{PlaybackEngine, SavedPlaybackState}; use crate::sound::playback::{PlaybackEngine, SavedPlaybackState};
use crate::sound::wave_bank::SoundBank; use crate::sound::wave_bank::SoundBank;
use crate::str; use crate::str;
use num_traits::clamp;
mod wave_bank; mod wave_bank;
mod organya; mod organya;
@ -266,7 +267,7 @@ fn run<T>(rx: Receiver<PlaybackMessage>, bank: SoundBank,
(sample & 0xff) << 8 (sample & 0xff) << 8
} }
}; };
let pxt_sample: u16 = pxt_buf[pxt_index] ^ 0x8000; let pxt_sample: u16 = pxt_buf[pxt_index];
if pxt_index < (pxt_buf.len() - 1) { if pxt_index < (pxt_buf.len() - 1) {
pxt_index += 1; pxt_index += 1;
@ -276,7 +277,10 @@ fn run<T>(rx: Receiver<PlaybackMessage>, bank: SoundBank,
pixtone.mix(&mut pxt_buf, sample_rate / speed); pixtone.mix(&mut pxt_buf, sample_rate / speed);
} }
let sample = org_sample.wrapping_add(pxt_sample); let sample = clamp(
(((org_sample ^ 0x8000) as i16) as isize)
+ (((pxt_sample ^ 0x8000) as i16) as isize)
, -0x7fff, 0x7fff) as u16 ^ 0x8000;
let value: T = Sample::from::<u16>(&sample); let value: T = Sample::from::<u16>(&sample);
for sample in frame.iter_mut() { for sample in frame.iter_mut() {