Skip to content

Commit

Permalink
Audio engine fixes (Rangi42#838)
Browse files Browse the repository at this point in the history
* Remove useless rNR52 writes

Lower 4 bits are read-only, and its attempting to duplicate what
wSoundOutput already does

* Misc changes

bc isn't in use during InitSound
Moved a comment
SOUND_CHANNEL_ON doesnt need to be reset before ChannelInit

* Don't count rest notes as noise hits

* Fixes Rangi42#837

...i think

* Don't call ReloadWaveform from the music player

Does not actually work. Writes are ignored since the dac is on

* Change waveform loading for audio channel 3

Only reloads as needed now to avoid turning off the dac

Also doesn't bother writing rNR31 as its value is ignored anyway

* Combine DutyCycle and SFXDutyCycle

Has the neat side effect of making duty cycle patterns visible in
the music player

* duty_cycle changes in hgss/ceruleancity

Removes some redundant commands and restores dummied out
duty_cycle_pattern calls

* Apply mono/stereo option once when writing rNR51

Removes a few places mono/stereo take different code paths, and
allows changing between them without restarting the music
  • Loading branch information
ariscop authored and nickjwilde committed Jul 7, 2023
1 parent d47eebf commit cbfa14c
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 129 deletions.
2 changes: 1 addition & 1 deletion audio/cries.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ Cry_Seel_Ch8:
sound_ret

Cry_Slowpoke_Ch5:
duty_cycle_pattern 0, 0, 0, 0
duty_cycle 0
square_note 8, 15, 5, 1152
square_note 2, 14, 1, 1504
square_note 8, 13, 1, 1500
Expand Down
110 changes: 42 additions & 68 deletions audio/engine.asm
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ _InitSound::
jr nz, .clearsound

ld hl, wChannels ; start of channel data
ld de, wChannelsEnd - wChannels ; length of area to clear (entire sound wram area)
.clearchannels ; clear wChannel1-$c2bf
ld bc, wChannelsEnd - wChannels ; length of area to clear (entire sound wram area)
xor a
ld [hli], a
dec de
ld a, e
or d
jr nz, .clearchannels
rst ByteFill

ld a, MAX_VOLUME
ld [wVolume], a
Expand Down Expand Up @@ -120,6 +115,7 @@ _UpdateSound::
ld hl, wChannel1DutyCycle - wChannel1
add hl, bc
ld a, [hli]
and $c0
ld [wCurTrackDuty], a
; intensity
ld a, [hli]
Expand Down Expand Up @@ -189,7 +185,15 @@ _UpdateSound::
ld a, [wVolume]
ldh [rNR50], a
; write SO on/off to hardware register
ld a, [wSoundOutput]
ld hl, wSoundOutput
ld a, [wOptions1]
bit STEREO, a ; stereo
ld a, [hl]
jr nz, .stereo
; No stereo, or left and right masks
swap a
or [hl]
.stereo
ldh [rNR51], a
ret

Expand Down Expand Up @@ -267,9 +271,6 @@ UpdateChannels:
ret

.ch1_rest
ldh a, [rNR52]
and %10001110 ; ch1 off
ldh [rNR52], a
ld hl, rNR10
jmp ClearChannel

Expand Down Expand Up @@ -319,9 +320,6 @@ UpdateChannels:
ret

.ch2_rest
ldh a, [rNR52]
and %10001101 ; ch2 off
ldh [rNR52], a
ld hl, rNR20
jmp ClearChannel

Expand Down Expand Up @@ -355,35 +353,29 @@ UpdateChannels:
ret

.ch3_rest
ldh a, [rNR52]
and %10001011 ; ch3 off
ldh [rNR52], a
ld hl, rNR30
jmp ClearChannel

.ch3_noise_sampling
ld a, $3f
ldh [rNR31], a
xor a
ldh [rNR30], a
call .load_wave_pattern
ld a, $80
ldh [rNR30], a
ld hl, wCh3LoadedWaveform
ld a, [wCurTrackIntensity]
ld d, a
maskbits NUM_WAVEFORMS
cp [hl] ; Loaded waveform == this notes waveform?
ld [hl], a
call nz, ReloadWaveform

ld a, d
rlca
ldh [rNR32], a
ld a, [wCurTrackFrequency]
ldh [rNR33], a
ld a, [wCurTrackFrequency + 1]
or $80
ldh [rNR30], a ; dac on, in case ReloadWaveform turned it off
ldh [rNR34], a
ret

.load_wave_pattern
call ReloadWaveform
ld a, [wCurTrackIntensity]
and $f0
sla a
ldh [rNR32], a
ret

.Channel4:
.Channel8:
ld hl, wChannel1NoteFlags - wChannel1
Expand All @@ -395,9 +387,6 @@ UpdateChannels:
ret

.ch4_rest
ldh a, [rNR52]
and %10000111 ; ch4 off
ldh [rNR52], a
ld hl, rNR40
jmp ClearChannel

Expand Down Expand Up @@ -714,7 +703,7 @@ HandleTrackVibrato:
add hl, bc
bit SOUND_DUTY_LOOP, [hl] ; duty
jr z, .next
ld hl, wChannel1SFXDutyLoop - wChannel1
ld hl, wChannel1DutyCycle - wChannel1
add hl, bc
ld a, [hl]
rlca
Expand Down Expand Up @@ -1012,7 +1001,8 @@ ParseMusic:
ld a, [wChannelSelectorSwitches+3]
and a
jr nz, .notnoise
ld a, 1
ld a, [wCurMusicByte]
and $f0
ld [wNoiseHit], a
.notnoise
; wCurMusicByte contains current note
Expand All @@ -1030,7 +1020,6 @@ ParseMusic:
ld a, [wCurMusicByte]
and $f
call SetNoteDuration
; get note pitch (top nybble)

ld a, [wCurChannel]
cp CHAN5
Expand All @@ -1046,6 +1035,7 @@ ParseMusic:
jr .rest

.notMuted
; get note pitch (top nybble)
ld a, [wCurMusicByte]
swap a
and $f
Expand Down Expand Up @@ -1214,7 +1204,7 @@ GetNoiseSample:
swap a
; non-rest note?
and $f
ret z
jr z, .rest
; use 'pitch' to seek noise sample set
ld e, a
ld d, 0
Expand All @@ -1231,6 +1221,12 @@ GetNoiseSample:
ld [wNoiseSampleDelay], a
ret

.rest
ld hl, wChannel1NoteFlags - wChannel1
add hl, bc
set NOTE_REST, [hl]
ret

MusicCommands:
; entries correspond to audio constants (see macros/scripts/audio.asm)
table_width 2, MusicCommands
Expand Down Expand Up @@ -1540,11 +1536,6 @@ Music_SoundDuty:
call GetMusicByte
rrca
rrca
ld hl, wChannel1SFXDutyLoop - wChannel1
add hl, bc
ld [hl], a
; update duty cycle
and $c0 ; only uses top 2 bits
ld hl, wChannel1DutyCycle - wChannel1
add hl, bc
ld [hl], a
Expand Down Expand Up @@ -1631,6 +1622,9 @@ Music_DutyCycle1:
Music_DutyCycle2:
Music_DutyCycle3:
; duty cycle
ld hl, wChannel1Flags2 - wChannel1
add hl, bc
res SOUND_DUTY_LOOP, [hl]
ld a, [wCurMusicByte]
rrca
rrca
Expand Down Expand Up @@ -1701,11 +1695,6 @@ Music_PanCenter:
ld a, $FF
; fallthrough
_ForcePanning:
; stereo on?
ld hl, wOptions1
bit STEREO, [hl]
ret z

ld d, a
call SetLRTracks
ld a, d
Expand Down Expand Up @@ -2178,13 +2167,8 @@ _PlayCryHeader::
and a
jr z, .next

; Stereo only: Play cry from the monster's side.
; Play cry from the monster's side.
; This only applies in-battle.

ld a, [wOptions1]
bit STEREO, a ; stereo
jr z, .next

; [Tracks] &= [wCryTracks]
ld hl, wChannel1Tracks - wChannel1
add hl, bc
Expand Down Expand Up @@ -2315,12 +2299,6 @@ PlayStereoSFX::

call MusicOff

; standard procedure if stereo's off
ld a, [wOptions1]
bit STEREO, a
jmp z, _PlaySFX

; else, let's go ahead with this
ld hl, wMusicID
ld a, e
ld [hli], a
Expand Down Expand Up @@ -2397,9 +2375,6 @@ LoadChannel:
ld a, [hli]
ld c, a
ld b, [hl] ; bc = channel pointer
ld hl, wChannel1Flags - wChannel1
add hl, bc
res SOUND_CHANNEL_ON, [hl] ; channel off
call ChannelInit
; load music pointer
ld hl, wChannel1MusicAddress - wChannel1
Expand Down Expand Up @@ -2449,17 +2424,16 @@ ChannelInit:
pop de
ret

ReloadWaveform::
; called from the music player
ld a, [wCurTrackIntensity]
and $f ; only NUM_WAVEFORMS are valid
ReloadWaveform:
; each wavepattern is $f bytes long, so seeking is done in $10s
swap a ; a << 4
add LOW(WaveSamples)
ld l, a
adc HIGH(WaveSamples)
sub l
ld h, a
xor a
ldh [rNR30], a ; dac off
; load wavepattern into rWave_0-rWave_f
for x, 16
ld a, [hli]
Expand Down
12 changes: 6 additions & 6 deletions audio/music/b2w2/marinetube.asm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Music_MarineTubeB2W2:

Music_MarineTubeB2W2_Ch1:
tempo 132
duty_cycle_pattern 3, 3, 3, 3
duty_cycle 3
vibrato 18, 1, 5
pitch_offset 1
note_type 12, 6, 1
Expand Down Expand Up @@ -217,7 +217,7 @@ Music_MarineTubeB2W2_Ch1:
octave 3
note F#, 8
note F_, 8
duty_cycle_pattern 2, 2, 2, 2
duty_cycle 2
volume_envelope 6, 0
note D_, 8
note C_, 8
Expand Down Expand Up @@ -255,7 +255,7 @@ Music_MarineTubeB2W2_Ch1:
note D#, 6
note F_, 8
note F_, 6
duty_cycle_pattern 1, 1, 1, 1
duty_cycle 1
volume_envelope 8, 4
octave 1
note G_, 2
Expand Down Expand Up @@ -470,7 +470,7 @@ Music_MarineTubeB2W2_Ch2:
vibrato 8, 2, 6
pitch_offset 1
note_type 12, 10, 0
duty_cycle_pattern 2, 2, 2, 2
duty_cycle 2
octave 5
note F_, 8
volume_envelope 10, 7
Expand Down Expand Up @@ -550,7 +550,7 @@ Music_MarineTubeB2W2_Ch2:
note A#, 4
volume_envelope 10, 7
note A#, 12
duty_cycle_pattern 0, 0, 0, 0
duty_cycle 0
note C_, 4
octave 4
volume_envelope 10, 0
Expand Down Expand Up @@ -633,7 +633,7 @@ Music_MarineTubeB2W2_Ch2:
note G_, 16
rest 16
rest 12
duty_cycle_pattern 0, 0, 0, 0
duty_cycle 0
volume_envelope 2, 0
octave 1
note A#, 2
Expand Down
Loading

0 comments on commit cbfa14c

Please sign in to comment.