AntonioND / gbt-player

A music player library for the PSG audio channels of the GB, GBC and GBA.
MIT License
281 stars 20 forks source link

gbt_pause does not completely pause the track #15

Closed douglasd3 closed 2 years ago

douglasd3 commented 2 years ago

Hello, i'm having some issues when my track has continuous notes. When gbt_pause is called it pauses the song but keep continuous notes going in a lesser volume. I'm using RGBDS.

Am i missing something?

Thanks!

AntonioND commented 2 years ago

That's a bit weird, because the gbt_pause function writes 0 to NR50, so that should turn off all the channels.

Can you upload a minimal sample ROM that triggers the bug?

douglasd3 commented 2 years ago

Sure! After starting the game press start to pause, in that moment i call gbt_pause and you can hear a low noise even if the audio is paused. The problem remains in the real hardware as well.

This is my pause logic, where i pause and unpause the sound:

PauseLoop:
    push af
    xor a    
    call gbt_pause
    pop af

    halt
    nop 

    ld c, a ; Save last key inputed

    call ReadKeys    

    and KEY_START
    cp 0                 
    jr nz, .unpause

    jr PauseLoop
.unpause:
    ; Check hit:    
    bit 7, c
    jr nz, PauseLoop

    ld a, 1    
    call gbt_pause

    ret

space_princess.gb.zip

douglasd3 commented 2 years ago

Just a little update: I figured that the problem is that the sound volume is never completely off, even with 0 value at NR50. if a call gbt_update and do not set gbt_playing to 0 the music plays at the same low volume. The continuous pitch noise appears because i do not call gbt_update.

I believe that this is a possible hardware limitation. Am i still missing something or there is a possible work around?

AntonioND commented 2 years ago

Yeah, that seems to be the issue. I actually don't know if there is a workaround, I'll ask...

AntonioND commented 2 years ago

I guess one workaround would be to write 0 to NR51.

AntonioND commented 2 years ago

Yes, turns out that a value of 0 is actually 1/8th: https://github.com/LIJI32/SameBoy/blob/886363b398e739303be8d8133387fe0648b4d70b/Core/apu.c#L76

douglasd3 commented 2 years ago

Sorry the delay, i was able to fix by setting NR51 and NR52 as well, here how my gbt_pause looks like:

gbt_pause:: ; a = pause/unpause
    ld      [gbt_playing],a
    or      a,a
    jr      nz,.gbt_pause_unmute
    ld      [rNR50],a ; Mute sound: set L & R sound levels to Off
    ld      [rNR52],a ; Turn sound off
    ret

.gbt_pause_unmute: ; Unmute sound if playback is resumed
    ld      a,$80
    ld      [rNR52],a ; Restore L & R sound levels to 100%
    ld      a,$FF
    ld      [rNR51], a
    ld      a,$77
    ld      [rNR50],a
    ret

If these changes are interesting i can create a pull request. I'm asking because i have very little experience programming asm :)

AntonioND commented 2 years ago

Sorry for the delay, I think I cleared all notifications and forgot about this! :( Yes, it would be great to have this in a PR!

AntonioND commented 2 years ago

This should be fixed now! https://github.com/AntonioND/gbt-player/commit/305a6f0c317a5d3262cb05b079a0a23350c514aa

douglasd3 commented 2 years ago

Thank you @AntonioND! I Completely forgot about the PR. In case you are interested I have finished my project: https://douglasd3.itch.io/space-princess

AntonioND commented 2 years ago

@douglasd3 oh, nice, and with RGBDS!