Fix volume zero and power of 2 (#375)

Fixes #371.

If the volume was zero, the calculation for leading zeros was incorrect.
So both reduce the amount of work done for 0 volume and also count the
leading zeros correctly in the fast path.

- [x] Changelog updated / no changelog update needed
This commit is contained in:
Gwilym Kuiper 2023-01-12 22:17:16 +00:00 committed by GitHub
commit 43aebe4092
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 19 deletions

View file

@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Text renderer can now be re-used which is useful for rpg style character/word at a time text boxes. - Text renderer can now be re-used which is useful for rpg style character/word at a time text boxes.
### Fixed
- Zero volume now plays no sound.
- Fixed issue where volume was incorrect for volumes which were powers of 2.
## [0.12.2] - 2022/10/22 ## [0.12.2] - 2022/10/22
This is a minor release to fix an alignment issue with background tiles. This is a minor release to fix an alignment issue with background tiles.

View file

@ -64,6 +64,8 @@ same_modification:
lsrs r7, r7, #1 lsrs r7, r7, #1
bne 1b bne 1b
sub r3, r3, #1
mov r5, #0 @ current index we're reading from mov r5, #0 @ current index we're reading from
ldr r8, =agb_rs__buffer_size @ the number of steps left ldr r8, =agb_rs__buffer_size @ the number of steps left
ldr r8, [r8] ldr r8, [r8]

View file

@ -446,26 +446,28 @@ impl MixerBuffer {
} }
} }
if channel.is_stereo { if channel.volume != 0.into() {
unsafe { if channel.is_stereo {
agb_rs__mixer_add_stereo( unsafe {
channel.data.as_ptr().add(channel.pos.floor()), agb_rs__mixer_add_stereo(
self.working_buffer.as_mut_ptr(), channel.data.as_ptr().add(channel.pos.floor()),
channel.volume, self.working_buffer.as_mut_ptr(),
); channel.volume,
} );
} else { }
let right_amount = ((channel.panning + 1) / 2) * channel.volume; } else {
let left_amount = ((-channel.panning + 1) / 2) * channel.volume; let right_amount = ((channel.panning + 1) / 2) * channel.volume;
let left_amount = ((-channel.panning + 1) / 2) * channel.volume;
unsafe { unsafe {
agb_rs__mixer_add( agb_rs__mixer_add(
channel.data.as_ptr().add(channel.pos.floor()), channel.data.as_ptr().add(channel.pos.floor()),
self.working_buffer.as_mut_ptr(), self.working_buffer.as_mut_ptr(),
playback_speed, playback_speed,
left_amount, left_amount,
right_amount, right_amount,
); );
}
} }
} }