From 77b2e273461b2b29f31eeed1e8a86505b3437883 Mon Sep 17 00:00:00 2001 From: Dennis Ranke Date: Wed, 23 Aug 2023 00:20:35 +0200 Subject: [PATCH] clamp snd samples into valid -1.0..1.0 range --- platform/src/ges.cwa | 3 +-- src/run_native.rs | 20 +++++++++++++++----- todo.txt | 1 + 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 todo.txt diff --git a/platform/src/ges.cwa b/platform/src/ges.cwa index 9ea6c73..ca67c5f 100644 --- a/platform/src/ges.cwa +++ b/platform/src/ges.cwa @@ -62,7 +62,6 @@ export fn sndGes(t: i32) -> f32 { let phase = channelState!GesChannelState.Phase; let inline pulseWidth = channelReg?1; - let phaseShift = (pulseWidth - 128) * 255; let invPhaseInc = 1 as f32 / phaseInc as f32; i = 0; @@ -131,7 +130,7 @@ export fn sndGes(t: i32) -> f32 { let phaseInc = (freq * (65536.0 / 44100.0)) as i32; let phase = channelState!GesChannelState.Phase; - if modSrc > ch { + if modSrc < ch { phase = phase - (phaseInc << 6); } diff --git a/src/run_native.rs b/src/run_native.rs index a9a982d..b011cdb 100644 --- a/src/run_native.rs +++ b/src/run_native.rs @@ -423,6 +423,14 @@ fn init_sound( mem[64..68].copy_from_slice(¤t_time.to_le_bytes()); } + fn clamp_sample(s: f32) -> f32 { + if s.is_nan() { + 0.0 + } else { + s.max(-1.0).min(1.0) + } + } + if let Some(ref mut resampler) = resampler { while !buffer.is_empty() { let copy_size = resampler.output_buffers[0] @@ -433,10 +441,12 @@ fn init_sound( resampler.input_buffers[0].clear(); resampler.input_buffers[1].clear(); for _ in 0..resampler.resampler.input_frames_next() { - resampler.input_buffers[0] - .push(snd.call(&mut store, (sample_index,)).unwrap_or(0.0)); - resampler.input_buffers[1] - .push(snd.call(&mut store, (sample_index + 1,)).unwrap_or(0.0)); + resampler.input_buffers[0].push(clamp_sample( + snd.call(&mut store, (sample_index,)).unwrap_or(0.0), + )); + resampler.input_buffers[1].push(clamp_sample( + snd.call(&mut store, (sample_index + 1,)).unwrap_or(0.0), + )); sample_index = sample_index.wrapping_add(2); } @@ -462,7 +472,7 @@ fn init_sound( } } else { for v in buffer { - *v = snd.call(&mut store, (sample_index,)).unwrap_or(0.0); + *v = clamp_sample(snd.call(&mut store, (sample_index,)).unwrap_or(0.0)); sample_index = sample_index.wrapping_add(1); } } diff --git a/todo.txt b/todo.txt new file mode 100644 index 0000000..44aff37 --- /dev/null +++ b/todo.txt @@ -0,0 +1 @@ +* add support for 16bit sound (not just float)