Skip to content

Commit

Permalink
WIP global volume
Browse files Browse the repository at this point in the history
  • Loading branch information
dciabrin committed Aug 23, 2024
1 parent 2502a9d commit 7129efd
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
2 changes: 1 addition & 1 deletion nullsound/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ all: nullsound.lib linkcheck.map
-include ../Makefile.config

INCLUDE_FILES=helpers ports ym2610
OBJS=entrypoint bios-commands adpcm ym2610 stream timer nss-fm nss-adpcm nss-ssg fx-vibrato fx-slide
OBJS=entrypoint bios-commands adpcm ym2610 stream timer nss-fm nss-adpcm nss-ssg fx-vibrato fx-slide volume
LIB=nullsound.lib

VERSION=@version@
Expand Down
34 changes: 34 additions & 0 deletions nullsound/nss-adpcm.s
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ _adpcm_a_loop:
add d
ld l, a
ld a, (hl)
;; add global volume attenuation and clamp
call adpcm_a_global_attenuation

or #0xc0 ; default pan (L+R)
ld c, a

Expand Down Expand Up @@ -344,6 +347,32 @@ _off_bit:
ld a, #1
ret

;;; TODO doc
;;; modified: c
adpcm_a_global_attenuation::
ld c, a
ld a, (state_volume_adpcm_a_global)
neg
add c
bit 7, a
jr z, _adpcm_a_post_global_clamp
ld a, #0
_adpcm_a_post_global_clamp:
ret


adpcm_b_global_attenuation::
ld c, a
ld a, (state_volume_adpcm_b_global)
cp c
jr nc, _adpcm_b_vol_clamp
neg
add c
ret
_adpcm_b_vol_clamp:
ld a, #0
ret


;;; ADPCM_A_VOL
;;; Set playback volume of the current ADPCM-A channel
Expand Down Expand Up @@ -373,6 +402,9 @@ adpcm_a_vol::

;; c: volume + default pan (L/R)
ld a, c
;; add global volume attenuation and clamp
call adpcm_a_global_attenuation

or #0xc0
ld c, a

Expand Down Expand Up @@ -443,6 +475,7 @@ _adpcm_b_post_loop_chk:
;; current volume
ld b, #REG_ADPCM_B_VOLUME
ld a, (state_adpcm_b_vol)
call adpcm_b_global_attenuation
ld c, a
call ym2610_write_port_a

Expand Down Expand Up @@ -585,6 +618,7 @@ adpcm_b_vol::

;; new configured volume for ADPCM-B
ld (state_adpcm_b_vol), a
call adpcm_b_global_attenuation

;; set volume in the YM2610
ld b, #REG_ADPCM_B_VOLUME
Expand Down
13 changes: 11 additions & 2 deletions nullsound/nss-fm.s
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ state_fm_channel::
.db 0
state_fm_ym2610_channel::
.db 0

;;; FM mirrored state
state_fm:
;;; FM1
Expand Down Expand Up @@ -388,7 +388,7 @@ _ops_loop:
bit 0, e
jr z, _ops_no_out
_ops_out:
;; configure OP from instrument TL faded with current volume
;; configure OP from instrument's TL faded with current volume
;; current OP's total level per instrument
ld a, (hl)
;; mix with note volume and clamp
Expand All @@ -397,7 +397,16 @@ _ops_out:
jr z, _ops_post_clamp
ld a, #127
_ops_post_clamp:
;; CHECK aren't we always below 128 here?
and #0x7f
;; apply global FM volume attenuation, clamp to 0
ld c, a
ld a, (state_volume_fm_global)
add c
bit 7, a
jr z, _ops_post_global_clamp
ld a, #127
_ops_post_global_clamp:
ld c, a
jr _ops_set_and_next
_ops_no_out:
Expand Down
11 changes: 10 additions & 1 deletion nullsound/nss-ssg.s
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,21 @@ ssg_mix_volume::
;; l: current note volume for channel
ld l, (hl)

;; mix volumes, min to 0
;; attenuate instrument volume with note volume, clamp to 0
ld a, c
sub l
jr nc, _mix_set
ld a, #0
_mix_set:
;; apply global SSG volume attenuation, clamp to 0
ld c, a
ld a, (state_volume_ssg_global)
neg
add c
bit 7, a
jr z, _ssg_post_global_clamp
ld a, #0
_ssg_post_global_clamp:
ld c, a
ld a, b
add #REG_SSG_A_VOLUME
Expand Down
2 changes: 2 additions & 0 deletions nullsound/stream.s
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ _stream_play_init_loop:
;; reset state trackers
call snd_stream_reset_state

call init_volume_state_tracker

;; start stream playback, it will get preempted
;; as soon as a wait opcode shows up in the stream
call update_stream_state_tracker
Expand Down
56 changes: 56 additions & 0 deletions nullsound/volume.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
;;;
;;; nullsound - modular sound driver
;;; Copyright (c) 2024 Damien Ciabrini
;;; This file is part of ngdevkit
;;;
;;; ngdevkit is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; ngdevkit is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public License
;;; along with ngdevkit. If not, see <http://www.gnu.org/licenses/>.

;;; The following driver is based on doc found on neogeodev wiki
;;; https://wiki.neogeodev.org/index.php?title=68k/Z80_communication

.module nullsound

.include "ym2610.inc"
.include "ports.inc"


;;;
;;; Volume state tracker
;;; -------------------
;;; Keep track of the global volume and fade out state
;;;
.area DATA

;;; Global volume attenuation for FM channels
state_volume_fm_global:: .blkb 1
;;; Global volume attenuation for SSG channels
state_volume_ssg_global:: .blkb 1
;;; Global volume attenuation for ADPCM-A channels
state_volume_adpcm_a_global:: .blkb 1
;;; Global volume attenuation for ADPCM-B channels
state_volume_adpcm_b_global:: .blkb 1


.area CODE

init_volume_state_tracker::
ld a, #0x0
ld (state_volume_fm_global), a
ld a, #0x0
ld (state_volume_ssg_global), a
ld a, #0x0
ld (state_volume_adpcm_a_global), a
ld a, #0x0
ld (state_volume_adpcm_b_global), a
ret

0 comments on commit 7129efd

Please sign in to comment.