summaryrefslogtreecommitdiff
path: root/audio/engine_4.asm
diff options
context:
space:
mode:
Diffstat (limited to 'audio/engine_4.asm')
-rw-r--r--audio/engine_4.asm191
1 files changed, 191 insertions, 0 deletions
diff --git a/audio/engine_4.asm b/audio/engine_4.asm
new file mode 100644
index 00000000..b3ed422e
--- /dev/null
+++ b/audio/engine_4.asm
@@ -0,0 +1,191 @@
+; The fourth of four partially duplicated sound engines.
+
+Audio4_PlaySound::
+ ld [wSoundID], a
+ ld a, [wSoundID]
+ cp $ff
+ jp z, .stopAllAudio
+ cp MAX_SFX_ID_4
+ jp z, .playSfx
+ jp c, .playSfx
+ cp $a3
+ jr z, .playMusic
+ jp nc, .playSfx
+
+.playMusic
+ call InitMusicVariables
+ jp .playSoundCommon
+
+.playSfx
+ ld l, a
+ ld e, a
+ ld h, 0
+ ld d, h
+ add hl, hl
+ add hl, de
+ ld de, SFX_Headers_4
+ add hl, de
+ ld a, h
+ ld [wSfxHeaderPointer], a
+ ld a, l
+ ld [wSfxHeaderPointer + 1], a
+ ld a, [hl]
+ and $c0
+ rlca
+ rlca
+ ld c, a
+.sfxChannelLoop
+ ld d, c
+ ld a, c
+ add a
+ add c
+ ld c, a
+ ld b, 0
+ ld a, [wSfxHeaderPointer]
+ ld h, a
+ ld a, [wSfxHeaderPointer + 1]
+ ld l, a
+ add hl, bc
+ ld c, d
+ ld a, [hl]
+ and $f
+ ld e, a ; software channel ID
+ ld d, 0
+ ld hl, wChannelSoundIDs
+ add hl, de
+ ld a, [hl]
+ and a
+ jr z, .playChannel
+ ld a, e
+ cp Ch8
+ jr nz, .notNoiseChannel
+ ld a, [wSoundID]
+ cp NOISE_INSTRUMENTS_END
+ jr nc, .notNoiseInstrument
+ ret
+.notNoiseInstrument
+ ld a, [hl]
+ cp NOISE_INSTRUMENTS_END
+ jr z, .playChannel
+ jr c, .playChannel
+.notNoiseChannel
+ ld a, [wSoundID]
+ cp [hl]
+ jr z, .playChannel
+ jr c, .playChannel
+ ret
+.playChannel
+ call InitSFXVariables
+ ld a, c
+ and a
+ jp z, .playSoundCommon
+ dec c
+ jp .sfxChannelLoop
+
+.stopAllAudio
+ call StopAllAudio
+ ret
+
+.playSoundCommon
+ ld a, [wSoundID]
+ ld l, a
+ ld e, a
+ ld h, 0
+ ld d, h
+ add hl, hl
+ add hl, de
+ ld de, SFX_Headers_4
+ add hl, de
+ ld e, l
+ ld d, h
+ ld hl, wChannelCommandPointers
+ ld a, [de] ; get channel number
+ ld b, a
+ rlca
+ rlca
+ and $3
+ ld c, a
+ ld a, b
+ and $f
+ ld b, c
+ inc b
+ inc de
+ ld c, 0
+.commandPointerLoop
+ cp c
+ jr z, .next
+ inc c
+ inc hl
+ inc hl
+ jr .commandPointerLoop
+.next
+ push af
+ push hl
+ push bc
+ ld b, 0
+ ld c, a
+ cp Ch4
+ jr c, .skipSettingFlag
+ ld hl, wChannelFlags1
+ add hl, bc
+ set BIT_NOISE_OR_SFX, [hl]
+.skipSettingFlag
+ pop bc
+ pop hl
+ ld a, [de] ; get channel pointer
+ ld [hli], a
+ inc de
+ ld a, [de]
+ ld [hli], a
+ inc de
+ pop af
+ push hl
+ push bc
+ ld b, 0
+ ld c, a
+ ld hl, wChannelSoundIDs
+ add hl, bc
+ ld a, [wSoundID]
+ ld [hl], a
+ pop bc
+ pop hl
+ inc c
+ dec b
+ ld a, b
+ and a
+ ld a, [de]
+ inc de
+ jr nz, .commandPointerLoop
+ ld a, [wSoundID]
+ cp CRY_SFX_START
+ jr nc, .maybeCry
+ jr .done
+.maybeCry
+ ld a, [wSoundID]
+ cp CRY_SFX_END
+ jr z, .done
+ jr c, .cry
+ jr .done
+.cry
+ ld hl, wChannelSoundIDs + Ch5
+ ld [hli], a
+ ld [hli], a
+ ld [hli], a
+ ld [hl], a
+ ld hl, wChannelCommandPointers + Ch7 * 2 ; sfx wave channel pointer
+ ld de, Audio4_CryRet
+ ld [hl], e
+ inc hl
+ ld [hl], d ; overwrite pointer to point to sound_ret
+ ld a, [wSavedVolume]
+ and a
+ jr nz, .done
+ ldh a, [rNR50]
+ ld [wSavedVolume], a
+ ld a, $77
+ ldh [rNR50], a ; full volume
+.done
+ ret
+
+Audio4_CryRet:
+ sound_ret