1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
PlayDefaultMusic::
call WaitForSoundToFinish
xor a
ld c, a
ld d, a
ld [wLastMusicSoundID], a
jr PlayDefaultMusicCommon
PlayDefaultMusicFadeOutCurrent::
; Fade out the current music and then play the default music.
ld c, 10
ld d, 0
ld a, [wd72e]
bit 5, a ; has a battle just ended?
jr z, PlayDefaultMusicCommon
xor a
ld [wLastMusicSoundID], a
ld c, 8
ld d, c
PlayDefaultMusicCommon::
ld a, [wWalkBikeSurfState]
and a
jr z, .walking
cp $2
jr z, .surfing
call CheckForNoBikingMusicMap
jr c, .walking
ld a, MUSIC_BIKE_RIDING
jr .next
.surfing
ld a, MUSIC_SURFING
.next
ld b, a
ld a, d
and a ; should current music be faded out first?
ld a, BANK(Music_BikeRiding)
jr nz, .next2
; Only change the audio ROM bank if the current music isn't going to be faded
; out before the default music begins.
ld [wAudioROMBank], a
.next2
; [wAudioSavedROMBank] will be copied to [wAudioROMBank] after fading out the
; current music (if the current music is faded out).
ld [wAudioSavedROMBank], a
jr .next3
.walking
ld a, [wMapMusicSoundID]
ld b, a
call CompareMapMusicBankWithCurrentBank
jr c, .next4
.next3
ld a, [wLastMusicSoundID]
cp b ; is the default music already playing?
ret z ; if so, do nothing
.next4
ld a, c
ld [wAudioFadeOutControl], a
ld a, b
ld [wLastMusicSoundID], a
ld [wNewSoundID], a
jp PlaySound
CheckForNoBikingMusicMap::
; probably used to not change music upon getting on bike
ld a, [wCurMap]
cp ROUTE_23
jr z, .found
cp VICTORY_ROAD_1F
jr z, .found
cp VICTORY_ROAD_2F
jr z, .found
cp VICTORY_ROAD_3F
jr z, .found
cp INDIGO_PLATEAU
jr z, .found
and a
ret
.found
scf
ret
UpdateMusic6Times::
ld c, 6
UpdateMusicCTimes::
.loop
push bc
push hl
farcall Audio1_UpdateMusic
pop hl
pop bc
dec c
jr nz, .loop
ret
CompareMapMusicBankWithCurrentBank::
; Compares the map music's audio ROM bank with the current audio ROM bank
; and updates the audio ROM bank variables.
; Returns whether the banks are different in carry.
ld a, [wMapMusicROMBank]
ld e, a
ld a, [wAudioROMBank]
cp e
jr nz, .differentBanks
ld [wAudioSavedROMBank], a
and a
ret
.differentBanks
ld a, c ; this is a fade-out counter value and it's always non-zero
and a
ld a, e
jr nz, .next
; If the fade-counter is non-zero, we don't change the audio ROM bank because
; it's needed to keep playing the music as it fades out. The FadeOutAudio
; routine will take care of copying [wAudioSavedROMBank] to [wAudioROMBank]
; when the music has faded out.
ld [wAudioROMBank], a
.next
ld [wAudioSavedROMBank], a
scf
ret
PlayMusic::
ld b, a
ld [wNewSoundID], a
xor a
ld [wAudioFadeOutControl], a
ld a, c
ld [wAudioROMBank], a
ld [wAudioSavedROMBank], a
ld a, b
jr PlaySound
Func_2223::
xor a
ld [wChannelSoundIDs + Ch5], a
ld [wChannelSoundIDs + Ch6], a
ld [wChannelSoundIDs + Ch7], a
ld [wChannelSoundIDs + Ch8], a
ldh [rNR10], a
ret
StopAllMusic::
ld a, SFX_STOP_ALL_MUSIC
ld [wNewSoundID], a
; plays music specified by a. If value is $ff, music is stopped
PlaySound::
push hl
push de
push bc
ld b, a
ld a, [wNewSoundID]
and a
jr z, .next
xor a
ld [wChannelSoundIDs + Ch5], a
ld [wChannelSoundIDs + Ch6], a
ld [wChannelSoundIDs + Ch7], a
ld [wChannelSoundIDs + Ch8], a
.next
ld a, [wAudioFadeOutControl]
and a ; has a fade-out length been specified?
jr z, .noFadeOut
ld a, [wNewSoundID]
and a ; is the new sound ID 0?
jr z, .done ; if so, do nothing
xor a
ld [wNewSoundID], a
ld a, [wLastMusicSoundID]
cp $ff ; has the music been stopped?
jr nz, .fadeOut ; if not, fade out the current music
; If it has been stopped, start playing the new music immediately.
xor a
ld [wAudioFadeOutControl], a
.noFadeOut
xor a
ld [wNewSoundID], a
call DetermineAudioFunction
jr .done
.fadeOut
ld a, b
ld [wLastMusicSoundID], a
ld a, [wAudioFadeOutControl]
ld [wAudioFadeOutCounterReloadValue], a
ld [wAudioFadeOutCounter], a
ld a, b
ld [wAudioFadeOutControl], a
.done
pop bc
pop de
pop hl
ret
GetNextMusicByte::
ldh a, [hLoadedROMBank]
push af
ld a, [wAudioROMBank]
call BankswitchCommon
ld d, $0
ld a, c
add a
ld e, a
ld hl, wChannelCommandPointers
add hl, de
ld a, [hli]
ld e, a
ld a, [hld]
ld d, a
ld a, [de]
inc de
ld [hl], e
inc hl
ld [hl], d
ld e, a
pop af
call BankswitchCommon
ld a, e
ret
InitMusicVariables::
push hl
push de
push bc
homecall Audio2_InitMusicVariables
pop bc
pop de
pop hl
ret
InitSFXVariables::
push hl
push de
push bc
homecall Audio2_InitSFXVariables
pop bc
pop de
pop hl
ret
StopAllAudio::
push hl
push de
push bc
homecall Audio2_StopAllAudio
pop bc
pop de
pop hl
ret
DetermineAudioFunction::
ldh a, [hLoadedROMBank]
push af
ld a, [wAudioROMBank]
call BankswitchCommon
; determine the audio function, based on the bank
cp BANK(Audio1_PlaySound)
jr nz, .checkForAudio2
; audio 1
ld a, b
call Audio1_PlaySound
jr .done
.checkForAudio2
cp BANK(Audio2_PlaySound)
jr nz, .checkForAudio3
; audio 2
ld a, b
call Audio2_PlaySound
jr .done
.checkForAudio3
cp BANK(Audio3_PlaySound)
jr nz, .audio4
; audio 3
ld a, b
call Audio3_PlaySound
jr .done
.audio4
; invalid banks will default to audio 4
; this is seen when encountering Missingno,
; as its sprite dimensions overflow to wAudioROMBank
ld a, b
call Audio4_PlaySound
.done
pop af
call BankswitchCommon
ret
|