1 Commits

Author SHA1 Message Date
fa089100be backport sound fix 2024-06-07 14:47:30 +02:00
5 changed files with 983 additions and 904 deletions

1861
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -62,7 +62,6 @@ export fn sndGes(t: i32) -> f32 {
let phase = channelState!GesChannelState.Phase; let phase = channelState!GesChannelState.Phase;
let inline pulseWidth = channelReg?1; let inline pulseWidth = channelReg?1;
let phaseShift = (pulseWidth - 128) * 255;
let invPhaseInc = 1 as f32 / phaseInc as f32; let invPhaseInc = 1 as f32 / phaseInc as f32;
i = 0; i = 0;
@@ -131,7 +130,7 @@ export fn sndGes(t: i32) -> f32 {
let phaseInc = (freq * (65536.0 / 44100.0)) as i32; let phaseInc = (freq * (65536.0 / 44100.0)) as i32;
let phase = channelState!GesChannelState.Phase; let phase = channelState!GesChannelState.Phase;
if modSrc > ch { if modSrc < ch {
phase = phase - (phaseInc << 6); phase = phase - (phaseInc << 6);
} }

View File

@@ -419,6 +419,14 @@ fn init_sound(
mem[64..68].copy_from_slice(&current_time.to_le_bytes()); mem[64..68].copy_from_slice(&current_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 { if let Some(ref mut resampler) = resampler {
while !buffer.is_empty() { while !buffer.is_empty() {
let copy_size = resampler.output_buffers[0] let copy_size = resampler.output_buffers[0]
@@ -429,10 +437,12 @@ fn init_sound(
resampler.input_buffers[0].clear(); resampler.input_buffers[0].clear();
resampler.input_buffers[1].clear(); resampler.input_buffers[1].clear();
for _ in 0..resampler.resampler.input_frames_next() { for _ in 0..resampler.resampler.input_frames_next() {
resampler.input_buffers[0] resampler.input_buffers[0].push(clamp_sample(
.push(snd.call(&mut store, (sample_index,)).unwrap_or(0.0)); 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[1].push(clamp_sample(
snd.call(&mut store, (sample_index + 1,)).unwrap_or(0.0),
));
sample_index = sample_index.wrapping_add(2); sample_index = sample_index.wrapping_add(2);
} }
@@ -458,7 +468,7 @@ fn init_sound(
} }
} else { } else {
for v in buffer { 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); sample_index = sample_index.wrapping_add(1);
} }
} }

1
todo.txt Normal file
View File

@@ -0,0 +1 @@
* add support for 16bit sound (not just float)

View File

@@ -15,4 +15,4 @@ pollster = "0.2"
bytemuck = { version = "1.4", features = [ "derive" ] } bytemuck = { version = "1.4", features = [ "derive" ] }
anyhow = "1" anyhow = "1"
minifb = { version = "0.23.0", default-features = false, features = ["x11"] } minifb = { version = "0.23.0", default-features = false, features = ["x11"] }
winapi = "0.3.9" winapi = { version = "0.3.9", features = ["std"] }