Insert Crystal audio engine and music

most credit goes to Sanqui
This commit is contained in:
dannye 2017-05-07 15:30:08 -05:00
parent 832c28eaea
commit 87eed68710
586 changed files with 193763 additions and 6447 deletions

View file

@ -34,7 +34,7 @@ PlayDefaultMusicCommon::
ld b, a
ld a, d
and a ; should current music be faded out first?
ld a, BANK(Music_BikeRiding)
ld a, 0 ; BANK(Music_BikeRiding)
jr nz, .next2
; Only change the audio ROM bank if the current music isn't going to be faded
@ -64,155 +64,236 @@ PlayDefaultMusicCommon::
ld a, b
ld [wLastMusicSoundID], a
ld [wNewSoundID], a
jp PlaySound
ld [MusicFadeID], a
ld a, 8
ld [MusicFade], a
;call FadeMusic ; called in updatemusic
ret
UpdateMusic6Times::
; This is called when entering a map, before fading out the current music and
; playing the default music (i.e. the map's music or biking/surfing music).
ld a, [wAudioROMBank]
ld b, a
cp BANK(Audio1_UpdateMusic)
jr nz, .checkForAudio2
;jp UpdateSound
;ret ; XXX UpdateMusic
; audio 1
ld hl, Audio1_UpdateMusic
jr .next
.checkForAudio2
cp BANK(Audio2_UpdateMusic)
jr nz, .audio3
; audio 2
ld hl, Audio2_UpdateMusic
jr .next
.audio3
ld hl, Audio3_UpdateMusic
.next
ld c, 6
.loop
push bc
push hl
call Bankswitch
pop hl
pop bc
dec c
jr nz, .loop
CompareMapMusicBankWithCurrentBank:
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]
; plays <s>music</s>SFX specified by a. If value is $ff, music is stopped
PlaySound::
push de
cp $ff
jr nz, .notff
xor a
call PlayMusic
pop de
ret
.notff
ld e, a
ld a, [wAudioROMBank]
cp e
jr nz, .differentBanks
ld [wAudioSavedROMBank], a
and a
xor a
ld d, a
call PlaySFX
pop de
ret
.differentBanks
ld a, c ; this is a fade-out counter value and it's always non-zero
OpenSRAMForSound::
ld a, SRAM_ENABLE
ld [MBC1SRamEnable], a
xor a
ld [MBC1SRamBankingMode], a
ld [MBC1SRamBank], a
ret
;SoundRestart::
; push hl
; push de
; push bc
; push af
;
; call OpenSRAMForSound
;
; ld a, [hROMBank]
; push af
; ld a, BANK(_SoundRestart)
; ld [hROMBank], a
; ld [MBC1RomBank], a
;
; call _SoundRestart
;
; pop af
; ld [hROMBank], a
; ld [MBC1RomBank], a
;
; pop af
; pop bc
; pop de
; pop hl
; ret
UpdateSound::
; push hl
; push de
; push bc
; push af
ld a, [wHaltAudio]
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 nz
ld a, [hROMBank]
push af
ld a, BANK(_UpdateSound)
ld [hROMBank], a
ld [MBC1RomBank], a
call _UpdateSound
pop af
ld [hROMBank], a
ld [MBC1RomBank], a
; pop af
; pop bc
; pop de
; pop hl
ret
PlayMusic::
ld b, a
ld [wNewSoundID], a
ld e, a
xor a
ld [wAudioFadeOutControl], a
ld a, c
ld [wAudioROMBank], a
ld [wAudioSavedROMBank], a
ld a, b
ld d, a
; Play music de.
; 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 + Ch4], a
ld [wChannelSoundIDs + Ch5], a
ld [wChannelSoundIDs + Ch6], a
ld [wChannelSoundIDs + Ch7], 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
ld a, [H_LOADEDROMBANK]
ld [hSavedROMBank], a
ld a, [wAudioROMBank]
ld [H_LOADEDROMBANK], a
push af
ld a, [hROMBank]
push af
ld a, BANK(_PlayMusic) ; and BANK(_SoundRestart)
ld [hROMBank], a
ld [MBC1RomBank], a
cp BANK(Audio1_PlaySound)
jr nz, .checkForAudio2
; audio 1
ld a, b
call Audio1_PlaySound
jr .next2
call _PlayMusic
.checkForAudio2
cp BANK(Audio2_PlaySound)
jr nz, .audio3
; audio 2
ld a, b
call Audio2_PlaySound
jr .next2
.audio3
ld a, b
call Audio3_PlaySound
.next2
ld a, [hSavedROMBank]
ld [H_LOADEDROMBANK], a
pop af
ld [hROMBank], a
ld [MBC1RomBank], a
jr .done
jr PopAllRet
.fadeOut
ld a, b
ld [wLastMusicSoundID], a
ld a, [wAudioFadeOutControl]
ld [wAudioFadeOutCounterReloadValue], a
ld [wAudioFadeOutCounter], a
ld a, b
ld [wAudioFadeOutControl], a
.done
PlayCry::
; Play monster a's cry.
; Play a cry given parameters in header de
push hl
push de
push bc
push af
ld [wd11e], a
predef IndexToPokedex
ld a, [wd11e]
dec a
ld e, a
ld d, 0
; Save current bank
ld a, [hROMBank]
push af
; Cry headers are stuck in one bank.
ld a, BANK(CryHeaders)
ld [hROMBank], a
ld [$2000], a
; Each header is 6 bytes long:
ld hl, CryHeaders
add hl, de
add hl, de
add hl, de
add hl, de
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
inc hl
ld a, [hli]
ld [CryPitch], a
ld a, [hli]
ld [CryEcho], a
ld a, [hli]
ld [CryLength], a
ld a, [hl]
ld [CryLength+1], a
ld a, BANK(PlayCry_)
ld [hROMBank], a
ld [$2000], a
call PlayCry_
pop af
ld [hROMBank], a
ld [$2000], a
call WaitForSoundToFinish
jr PopAllRet
PlaySFX::
; Play sound effect de.
; Sound effects are ordered by priority (lowest to highest)
push hl
push de
push bc
push af
; Is something already playing?
;call CheckSFX
;jr nc, .play
; Does it have priority?
;ld a, [CurSFX]
;cp e
;jr c, .quit
PlaySFX_play
.play
ld a, [hROMBank]
push af
ld a, BANK(_PlaySFX)
ld [hROMBank], a
ld [$2000], a ; bankswitch
ld a, e
ld [CurSFX], a
call _PlaySFX
pop af
ld [hROMBank], a
ld [$2000], a ; bankswitch
.quit
PopAllRet:
pop af
pop bc
pop de
pop hl
ret
_LoadMusicByte::
; CurMusicByte = [a:de]
GLOBAL LoadMusicByte
ld [hROMBank], a
ld [MBC1RomBank], a
ld a, [de]
ld [CurMusicByte], a
ld a, BANK(LoadMusicByte)
ld [hROMBank], a
ld [MBC1RomBank], a
ret

View file

@ -97,7 +97,7 @@ rLCDC_DEFAULT EQU %11100011
predef LoadSGB
ld a, BANK(SFX_Shooting_Star)
ld a, 0 ; BANK(SFX_Shooting_Star)
ld [wAudioROMBank], a
ld [wAudioSavedROMBank], a
ld a, $9c
@ -126,7 +126,12 @@ ClearVram:
StopAllSounds::
ld a, BANK(Audio1_UpdateMusic)
call OpenSRAMForSound
ld hl, MusicPlaying
ld bc, (wChannelSelectorSwitches+8) - Crysaudio
call FillMemory
ld a, 0 ; BANK(Audio1_UpdateMusic)
ld [wAudioROMBank], a
ld [wAudioSavedROMBank], a
xor a

View file

@ -771,15 +771,28 @@ HandleBlackOut::
jp SpecialEnterMap
StopMusic::
ld [wAudioFadeOutControl], a
ld a, $ff
ld [wNewSoundID], a
call PlaySound
.wait
ld a, [wAudioFadeOutControl]
and a
jr nz, .wait
jp StopAllSounds
xor a
ld [MusicFadeID], a
ld a, 1
ld [MusicFade], a
.wait0
ld a, [MusicFadeCount]
and a
jr z, .wait0
.wait1
ld a, [MusicFadeCount]
and a
jr nz, .wait1
ret
; ld [wAudioFadeOutControl], a
; ld a, $ff
; ld [wNewSoundID], a
; call PlaySound
;.wait
; ld a, [wAudioFadeOutControl]
; and a
; jr nz, .wait
; jp StopAllSounds
HandleFlyWarpOrDungeonWarp::
call UpdateSprites
@ -1244,9 +1257,15 @@ CollisionCheckOnLand::
call CheckTilePassable
jr nc,.noCollision
.collision
ld a,[wChannelSoundIDs + Ch4]
cp SFX_COLLISION ; check if collision sound is already playing
jr z,.setCarry
;ld a,[wChannelSoundIDs + Ch4]
;cp SFX_COLLISION ; check if collision sound is already playing
; curSFX is not cleared for some reason.
; ch5 on?
ld hl, Channel5 + Channel1Flags - Channel1
bit 0, [hl]
jr nz,.setCarry
ld a,SFX_COLLISION
call PlaySound ; play collision sound (if it's not already playing)
.setCarry

View file

@ -50,27 +50,28 @@ VBlank::
ld [H_FRAMECOUNTER], a
.skipDec
call FadeOutAudio
; call FadeOutAudio
ld a, [wAudioROMBank] ; music ROM bank
ld [H_LOADEDROMBANK], a
ld [MBC1RomBank], a
call UpdateSound
; ld a, [wAudioROMBank] ; music ROM bank
; ld [H_LOADEDROMBANK], a
; ld [MBC1RomBank], a
cp BANK(Audio1_UpdateMusic)
jr nz, .checkForAudio2
.audio1
call Audio1_UpdateMusic
jr .afterMusic
.checkForAudio2
cp BANK(Audio2_UpdateMusic)
jr nz, .audio3
.audio2
call Music_DoLowHealthAlarm
call Audio2_UpdateMusic
jr .afterMusic
.audio3
call Audio3_UpdateMusic
.afterMusic
; cp BANK(Audio1_UpdateMusic)
; jr nz, .checkForAudio2
;.audio1
; call Audio1_UpdateMusic
; jr .afterMusic
;.checkForAudio2
; cp BANK(Audio2_UpdateMusic)
; jr nz, .audio3
;.audio2
; call Music_DoLowHealthAlarm
; call Audio2_UpdateMusic
; jr .afterMusic
;.audio3
; call Audio3_UpdateMusic
;.afterMusic
callba TrackPlayTime ; keep track of time played