audio.asm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. PlayDefaultMusic::
  2. call WaitForSoundToFinish
  3. xor a
  4. ld c, a
  5. ld d, a
  6. ld [wLastMusicSoundID], a
  7. jr PlayDefaultMusicCommon
  8. PlayDefaultMusicFadeOutCurrent::
  9. ; Fade out the current music and then play the default music.
  10. ld c, 10
  11. ld d, 0
  12. ld a, [wd72e]
  13. bit 5, a ; has a battle just ended?
  14. jr z, PlayDefaultMusicCommon
  15. xor a
  16. ld [wLastMusicSoundID], a
  17. ld c, 8
  18. ld d, c
  19. PlayDefaultMusicCommon::
  20. ld a, [wWalkBikeSurfState]
  21. and a
  22. jr z, .walking
  23. cp $2
  24. jr z, .surfing
  25. ld a, MUSIC_BIKE_RIDING
  26. jr .next
  27. .surfing
  28. ld a, MUSIC_SURFING
  29. .next
  30. ld b, a
  31. ld a, d
  32. and a ; should current music be faded out first?
  33. ld a, BANK(Music_BikeRiding)
  34. jr nz, .next2
  35. ; Only change the audio ROM bank if the current music isn't going to be faded
  36. ; out before the default music begins.
  37. ld [wAudioROMBank], a
  38. .next2
  39. ; [wAudioSavedROMBank] will be copied to [wAudioROMBank] after fading out the
  40. ; current music (if the current music is faded out).
  41. ld [wAudioSavedROMBank], a
  42. jr .next3
  43. .walking
  44. ld a, [wMapMusicSoundID]
  45. ld b, a
  46. call CompareMapMusicBankWithCurrentBank
  47. jr c, .next4
  48. .next3
  49. ld a, [wLastMusicSoundID]
  50. cp b ; is the default music already playing?
  51. ret z ; if so, do nothing
  52. .next4
  53. ld a, c
  54. ld [wAudioFadeOutControl], a
  55. ld a, b
  56. ld [wLastMusicSoundID], a
  57. ld [wNewSoundID], a
  58. jp PlaySound
  59. UpdateMusic6Times::
  60. ; This is called when entering a map, before fading out the current music and
  61. ; playing the default music (i.e. the map's music or biking/surfing music).
  62. ld a, [wAudioROMBank]
  63. ld b, a
  64. cp BANK(Audio1_UpdateMusic)
  65. jr nz, .checkForAudio2
  66. ; audio 1
  67. ld hl, Audio1_UpdateMusic
  68. jr .next
  69. .checkForAudio2
  70. cp BANK(Audio2_UpdateMusic)
  71. jr nz, .audio3
  72. ; audio 2
  73. ld hl, Audio2_UpdateMusic
  74. jr .next
  75. .audio3
  76. ld hl, Audio3_UpdateMusic
  77. .next
  78. ld c, 6
  79. .loop
  80. push bc
  81. push hl
  82. call Bankswitch
  83. pop hl
  84. pop bc
  85. dec c
  86. jr nz, .loop
  87. ret
  88. CompareMapMusicBankWithCurrentBank::
  89. ; Compares the map music's audio ROM bank with the current audio ROM bank
  90. ; and updates the audio ROM bank variables.
  91. ; Returns whether the banks are different in carry.
  92. ld a, [wMapMusicROMBank]
  93. ld e, a
  94. ld a, [wAudioROMBank]
  95. cp e
  96. jr nz, .differentBanks
  97. ld [wAudioSavedROMBank], a
  98. and a
  99. ret
  100. .differentBanks
  101. ld a, c ; this is a fade-out counter value and it's always non-zero
  102. and a
  103. ld a, e
  104. jr nz, .next
  105. ; If the fade-counter is non-zero, we don't change the audio ROM bank because
  106. ; it's needed to keep playing the music as it fades out. The FadeOutAudio
  107. ; routine will take care of copying [wAudioSavedROMBank] to [wAudioROMBank]
  108. ; when the music has faded out.
  109. ld [wAudioROMBank], a
  110. .next
  111. ld [wAudioSavedROMBank], a
  112. scf
  113. ret
  114. PlayMusic::
  115. ld b, a
  116. ld [wNewSoundID], a
  117. xor a
  118. ld [wAudioFadeOutControl], a
  119. ld a, c
  120. ld [wAudioROMBank], a
  121. ld [wAudioSavedROMBank], a
  122. ld a, b
  123. ; plays music specified by a. If value is $ff, music is stopped
  124. PlaySound::
  125. push hl
  126. push de
  127. push bc
  128. ld b, a
  129. ld a, [wNewSoundID]
  130. and a
  131. jr z, .next
  132. xor a
  133. ld [wChannelSoundIDs + Ch4], a
  134. ld [wChannelSoundIDs + Ch5], a
  135. ld [wChannelSoundIDs + Ch6], a
  136. ld [wChannelSoundIDs + Ch7], a
  137. .next
  138. ld a, [wAudioFadeOutControl]
  139. and a ; has a fade-out length been specified?
  140. jr z, .noFadeOut
  141. ld a, [wNewSoundID]
  142. and a ; is the new sound ID 0?
  143. jr z, .done ; if so, do nothing
  144. xor a
  145. ld [wNewSoundID], a
  146. ld a, [wLastMusicSoundID]
  147. cp $ff ; has the music been stopped?
  148. jr nz, .fadeOut ; if not, fade out the current music
  149. ; If it has been stopped, start playing the new music immediately.
  150. xor a
  151. ld [wAudioFadeOutControl], a
  152. .noFadeOut
  153. xor a
  154. ld [wNewSoundID], a
  155. ld a, [H_LOADEDROMBANK]
  156. ld [hSavedROMBank], a
  157. ld a, [wAudioROMBank]
  158. ld [H_LOADEDROMBANK], a
  159. ld [MBC1RomBank], a
  160. cp BANK(Audio1_PlaySound)
  161. jr nz, .checkForAudio2
  162. ; audio 1
  163. ld a, b
  164. call Audio1_PlaySound
  165. jr .next2
  166. .checkForAudio2
  167. cp BANK(Audio2_PlaySound)
  168. jr nz, .audio3
  169. ; audio 2
  170. ld a, b
  171. call Audio2_PlaySound
  172. jr .next2
  173. .audio3
  174. ld a, b
  175. call Audio3_PlaySound
  176. .next2
  177. ld a, [hSavedROMBank]
  178. ld [H_LOADEDROMBANK], a
  179. ld [MBC1RomBank], a
  180. jr .done
  181. .fadeOut
  182. ld a, b
  183. ld [wLastMusicSoundID], a
  184. ld a, [wAudioFadeOutControl]
  185. ld [wAudioFadeOutCounterReloadValue], a
  186. ld [wAudioFadeOutCounter], a
  187. ld a, b
  188. ld [wAudioFadeOutControl], a
  189. .done
  190. pop bc
  191. pop de
  192. pop hl
  193. ret