Fix crash on unaligned volume scale

Volume scaling would potentially crash when handling
unaligned blocks of samples, and also handled them
completely wrong. It should be counting up single
samples until the buffer is aligned to a multiple of 16
bytes, and it should not exceed the intended count.

BUG: It was not only counting the unaligned samples
backwards, it was ignoring the real sample count.

Fixes #380

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
Christopher Snowhill 2023-10-11 20:15:16 -07:00
parent 587b7900b5
commit 0f5f11a5a4
No known key found for this signature in database

View file

@ -76,15 +76,17 @@ void scale_by_volume(float *buffer, size_t count, float volume) {
if(volume != 1.0) {
size_t unaligned = (uintptr_t)buffer & 15;
if(unaligned) {
size_t count3 = unaligned >> 2;
while(count3 > 0) {
size_t count_unaligned = (16 - unaligned) / sizeof(float);
while(count > 0 && count_unaligned > 0) {
*buffer++ *= volume;
count3--;
count_unaligned--;
count--;
}
}
vDSP_vsmul(buffer, 1, &volume, buffer, 1, count);
if(count) {
vDSP_vsmul(buffer, 1, &volume, buffer, 1, count);
}
}
}