mvdnes / rboy

A Gameboy Emulator in Rust
MIT License
607 stars 39 forks source link

[Question] Clarify the volume amplification logic used. #28

Closed Altaflux closed 1 month ago

Altaflux commented 1 month ago

Hi, I am trying to learn about gameboy emulation and stumbled on your emulator which has been very insightful. I am a bit stumbled by the logic around how sound volume is being calculated and would love to better understand the math behind it.

I am referring specifically to the following lines: https://github.com/mvdnes/rboy/blob/main/src/sound.rs#L891

        let left_vol = (self.volume_left as f32 / 7.0) * (1.0 / 15.0) * 0.25;
        let right_vol = (self.volume_right as f32 / 7.0) * (1.0 / 15.0) * 0.25;

I understand (self.volume_left as f32 / 7.0) as 7 is the maximum volume of the gameboy registers so it is calculating the "max" volume. But I am not sure what is the purpose of (1.0 / 15.0) neither of * 0.25;.

I you could help me understand this logic I would greatly appreciate it.

mvdnes commented 1 month ago

I think my rationale back in the day was:

Re-reading the pandocs, I am not overly confident this is correct, but it seems to work well enough.

Altaflux commented 1 month ago

Thanks for the clarification, really appreciated!