How to make a song play during the game
Posted: Sat Apr 24, 2021 2:16 am
I'm trying to figure out how to make a song play so that each frame, the next note is played. I'm using a data block to contain a sequence of notes and a simple loop counter to track which note to play. After a note is played, the note counter is incremented by 1 and the main game loop continues. The tricky part is getting the song to play at the correct speed.
This is my code (it uses the Konami VRC6 chip, aka Mapper 24, so you might not be familiar with this but similar logic should work with the built in sound channels. Since my asm file is pretty big I'm only leaving in the parts that deal with the music itself.
Problem is, this isn't working. It's late at night so I might not be thinking clearly enough to proofread this code.
This is my code (it uses the Konami VRC6 chip, aka Mapper 24, so you might not be familiar with this but similar logic should work with the built in sound channels. Since my asm file is pretty big I'm only leaving in the parts that deal with the music itself.
Code: Select all
song_illuminator:
db $9f,$9f,$9f,$9f,$9f,$9f
db $af,$af,$9f,$9f,$9f,$9f
db $9f,$9f,$cf,$cf
handleMusic:
pha
txa
pha
tya
pha
lda #%01001001 ;Volume 9, Duty Mode 04
ldx musicloop
jsr ChibiSoundVRC6
pla
tay
pla
tax
pla
rts
ChibiSoundVRC6:
STA VRC6_PULSE1_CTRL
LDA musicloop,x
STA VRC6_PULSE1_FREQ_LO
LDA #%10000000
STA VRC6_PULSE1_FREQ_HI
inx
cpx #$10 ; See Note 1
beq loopMusic ; See Note 1
RTS ; See Note 1
loopMusic: ; See Note 1
ldx #$00 ; See Note 1
rts
; Note 1: Only the first note plays if these lines of code are here. Otherwise the whole song does play but much too fast.