I'm creating a new game (from scratch again, but this time on a live stream... :P ) and I have a problem with the sound mixer.
It's pretty much identical to Handmade Hero's before doing SIMD.
However when I try speeding up the sounds (by changing the sound->speed_multiplier) I get a small weird behavior that tells me the way I'm blending the samples is not 100% correct. It kind of works, but I get that noise/missed-samples artifact.
I want to be able to change the speed_multiplier every frame for some sounds.
Here is how the code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | s16 *at = sound_buffer->samples; for (int i = 0; i < sound_buffer->samples_to_write; i++) { f32 left_sample = 0; f32 right_sample = 0; for (Playing_Sound *sound = playing_sounds; sound != playing_sounds + array_count(playing_sounds); sound++) { if (!sound->active) continue; if (sound->volume) { int sample = (int)sound->position; f32 frac = sound->position - (f32)sample; sample *= sound->sound->channel_count; int next_sample = sample + sound->sound->channel_count-1 + 1; if (next_sample > sound->sound->sample_count) next_sample-= sound->sound->sample_count; f32 left_sound_sample_1 = (f32)sound->sound->samples[sample]; f32 left_sound_sample_2 = (f32)sound->sound->samples[next_sample]; f32 right_sound_sample_1 = (f32)sound->sound->samples[sample + sound->sound->channel_count-1]; f32 right_sound_sample_2 = (f32)sound->sound->samples[next_sample + sound->sound->channel_count-1]; f32 left_sound_sample = lerp(left_sound_sample_1, frac, left_sound_sample_2)*sound->volume; f32 right_sound_sample = lerp(right_sound_sample_1, frac, right_sound_sample_2)*sound->volume; left_sample += (left_sound_sample*clampf(0, (1.f-sound->pan), 1.f) + right_sound_sample*clampf(0, (-sound->pan), 1.f)); right_sample += (right_sound_sample*clampf(0, (1.f+sound->pan), 1.f) + left_sound_sample*clampf(0, (sound->pan), 1.f)); } sound->position += sound->speed_multiplier; if (sound->position >= sound->sound->sample_count) { if (sound->looping) sound->position = 0.f; else stop_sound(sound); } } f32 min = (f32)MIN_S16; f32 max = (f32)MAX_S16; *at++ = (s16)clampf(min, left_sample*.5f, max); *at++ = (s16)clampf(min, right_sample*.5f, max); } |
Do you know that could be the problem?
Thanks,
Dan Zaidan