Skip to navigation

Sound: ProcessMusic

Name: ProcessMusic [Show more] Type: Subroutine Category: Sound Summary: Play the configured music in the background
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * ProcessSound calls ProcessMusic
.ProcessMusic LDX musicCounter \ Set X to the music counter, which points to the \ current place in the music data BMI musi4 \ If bit 7 of musicCounter is set then there is no music \ playing, so jump to musi4 to return from the \ subroutine INC musicCounter \ Otherwise there is some music being played, so \ increment the music counter to move through the music \ data LDA musicData,X \ Set A to the next byte of music data CMP #&FF \ If A = &FF then we just reached the end of this piece BEQ musi3 \ of music, so jump to musi3 to stop playing music \ If we get here then we need to process the byte of \ music data in A CMP #200 \ If A < 200 then jump to musi1 to make the sound BCC musi1 \ If we get here then A contains the sound counter to \ set for all following notes, with the form 200 + x \ setting the counter to 4 * x SBC #200 \ Set noteCounter = (A - 200) * 4 ASL A \ ASL A \ This subtraction works because we just passed through STA noteCounter \ a BCC, so we know the C flag is set JMP ProcessMusic \ Loop back to the start of the routine to process the \ next byte of music data .musi1 STA soundData+20 \ Set the third parameter of sound data block #2 to A, \ to set the pitch LDA noteCounter \ Set soundCounter to the note duration so the note STA soundCounter \ plays for the correct amount of time \ \ This means that the ProcessSound routine will do \ nothing until this counter has run down, so we can \ play three-note chords by setting the delay to zero \ for the first two notes in the chord, so that there is \ no delay between them, and then setting the delay for \ the last note to the duration of the chord, so the \ three chord notes play together for that period \ \ All we need to do is ensure that the three notes can \ play together, which is what we do next LDA soundData+16 \ Set A to the first parameter of sound data block #2, \ which contains the channel \ \ This is in the form &1x for sound channel x, and it \ starts out at &12 for channel 2 \ \ Channels 1 to 3 are for tones, so we work our way \ through channels &11 to &13, playing each note on the \ next channel number \ \ This plays the music with the last three notes being \ sustained, which lets us play three-note chords as \ described above, giving the game music its distinctive \ chord style CLC \ Add 1 to move A onto the number of the next channel ADC #1 CMP #&14 \ If A < &14 then we are still in the range &11 to &13, BCC musi2 \ so jump to musi2 to use this channel LDA #&11 \ Otherwise warp around to channel &11 .musi2 STA soundData+16 \ Set the first parameter of sound data block #2 to A, \ which sets the channel LDA #4 \ Set the second parameter of sound data block #2 to 4, STA soundData+18 \ which sets the amplitude LDA #3 \ Make sound #3 (music), which plays the next note of JMP MakeSound \ music, returning from the subroutine using a tail call .musi3 \ If we get here then we just reached the end of the \ piece of music and A = &FF STA musicCounter \ Set bit 7 of the music counter to flag that we are no \ longer playing any music .musi4 RTS \ Return from the subroutine