Skip to content

Commit 7129efd

Browse files
committed
WIP global volume
1 parent 2502a9d commit 7129efd

File tree

6 files changed

+114
-4
lines changed

6 files changed

+114
-4
lines changed

nullsound/Makefile.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ all: nullsound.lib linkcheck.map
1919
-include ../Makefile.config
2020

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

2525
VERSION=@version@

nullsound/nss-adpcm.s

+34
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ _adpcm_a_loop:
235235
add d
236236
ld l, a
237237
ld a, (hl)
238+
;; add global volume attenuation and clamp
239+
call adpcm_a_global_attenuation
240+
238241
or #0xc0 ; default pan (L+R)
239242
ld c, a
240243

@@ -344,6 +347,32 @@ _off_bit:
344347
ld a, #1
345348
ret
346349

350+
;;; TODO doc
351+
;;; modified: c
352+
adpcm_a_global_attenuation::
353+
ld c, a
354+
ld a, (state_volume_adpcm_a_global)
355+
neg
356+
add c
357+
bit 7, a
358+
jr z, _adpcm_a_post_global_clamp
359+
ld a, #0
360+
_adpcm_a_post_global_clamp:
361+
ret
362+
363+
364+
adpcm_b_global_attenuation::
365+
ld c, a
366+
ld a, (state_volume_adpcm_b_global)
367+
cp c
368+
jr nc, _adpcm_b_vol_clamp
369+
neg
370+
add c
371+
ret
372+
_adpcm_b_vol_clamp:
373+
ld a, #0
374+
ret
375+
347376

348377
;;; ADPCM_A_VOL
349378
;;; Set playback volume of the current ADPCM-A channel
@@ -373,6 +402,9 @@ adpcm_a_vol::
373402

374403
;; c: volume + default pan (L/R)
375404
ld a, c
405+
;; add global volume attenuation and clamp
406+
call adpcm_a_global_attenuation
407+
376408
or #0xc0
377409
ld c, a
378410

@@ -443,6 +475,7 @@ _adpcm_b_post_loop_chk:
443475
;; current volume
444476
ld b, #REG_ADPCM_B_VOLUME
445477
ld a, (state_adpcm_b_vol)
478+
call adpcm_b_global_attenuation
446479
ld c, a
447480
call ym2610_write_port_a
448481

@@ -585,6 +618,7 @@ adpcm_b_vol::
585618

586619
;; new configured volume for ADPCM-B
587620
ld (state_adpcm_b_vol), a
621+
call adpcm_b_global_attenuation
588622

589623
;; set volume in the YM2610
590624
ld b, #REG_ADPCM_B_VOLUME

nullsound/nss-fm.s

+11-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ state_fm_channel::
6262
.db 0
6363
state_fm_ym2610_channel::
6464
.db 0
65-
65+
6666
;;; FM mirrored state
6767
state_fm:
6868
;;; FM1
@@ -388,7 +388,7 @@ _ops_loop:
388388
bit 0, e
389389
jr z, _ops_no_out
390390
_ops_out:
391-
;; configure OP from instrument TL faded with current volume
391+
;; configure OP from instrument's TL faded with current volume
392392
;; current OP's total level per instrument
393393
ld a, (hl)
394394
;; mix with note volume and clamp
@@ -397,7 +397,16 @@ _ops_out:
397397
jr z, _ops_post_clamp
398398
ld a, #127
399399
_ops_post_clamp:
400+
;; CHECK aren't we always below 128 here?
400401
and #0x7f
402+
;; apply global FM volume attenuation, clamp to 0
403+
ld c, a
404+
ld a, (state_volume_fm_global)
405+
add c
406+
bit 7, a
407+
jr z, _ops_post_global_clamp
408+
ld a, #127
409+
_ops_post_global_clamp:
401410
ld c, a
402411
jr _ops_set_and_next
403412
_ops_no_out:

nullsound/nss-ssg.s

+10-1
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,21 @@ ssg_mix_volume::
363363
;; l: current note volume for channel
364364
ld l, (hl)
365365

366-
;; mix volumes, min to 0
366+
;; attenuate instrument volume with note volume, clamp to 0
367367
ld a, c
368368
sub l
369369
jr nc, _mix_set
370370
ld a, #0
371371
_mix_set:
372+
;; apply global SSG volume attenuation, clamp to 0
373+
ld c, a
374+
ld a, (state_volume_ssg_global)
375+
neg
376+
add c
377+
bit 7, a
378+
jr z, _ssg_post_global_clamp
379+
ld a, #0
380+
_ssg_post_global_clamp:
372381
ld c, a
373382
ld a, b
374383
add #REG_SSG_A_VOLUME

nullsound/stream.s

+2
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ _stream_play_init_loop:
360360
;; reset state trackers
361361
call snd_stream_reset_state
362362

363+
call init_volume_state_tracker
364+
363365
;; start stream playback, it will get preempted
364366
;; as soon as a wait opcode shows up in the stream
365367
call update_stream_state_tracker

nullsound/volume.s

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
;;;
2+
;;; nullsound - modular sound driver
3+
;;; Copyright (c) 2024 Damien Ciabrini
4+
;;; This file is part of ngdevkit
5+
;;;
6+
;;; ngdevkit is free software: you can redistribute it and/or modify
7+
;;; it under the terms of the GNU Lesser General Public License as
8+
;;; published by the Free Software Foundation, either version 3 of the
9+
;;; License, or (at your option) any later version.
10+
;;;
11+
;;; ngdevkit is distributed in the hope that it will be useful,
12+
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
;;; GNU Lesser General Public License for more details.
15+
;;;
16+
;;; You should have received a copy of the GNU Lesser General Public License
17+
;;; along with ngdevkit. If not, see <http://www.gnu.org/licenses/>.
18+
19+
;;; The following driver is based on doc found on neogeodev wiki
20+
;;; https://wiki.neogeodev.org/index.php?title=68k/Z80_communication
21+
22+
.module nullsound
23+
24+
.include "ym2610.inc"
25+
.include "ports.inc"
26+
27+
28+
;;;
29+
;;; Volume state tracker
30+
;;; -------------------
31+
;;; Keep track of the global volume and fade out state
32+
;;;
33+
.area DATA
34+
35+
;;; Global volume attenuation for FM channels
36+
state_volume_fm_global:: .blkb 1
37+
;;; Global volume attenuation for SSG channels
38+
state_volume_ssg_global:: .blkb 1
39+
;;; Global volume attenuation for ADPCM-A channels
40+
state_volume_adpcm_a_global:: .blkb 1
41+
;;; Global volume attenuation for ADPCM-B channels
42+
state_volume_adpcm_b_global:: .blkb 1
43+
44+
45+
.area CODE
46+
47+
init_volume_state_tracker::
48+
ld a, #0x0
49+
ld (state_volume_fm_global), a
50+
ld a, #0x0
51+
ld (state_volume_ssg_global), a
52+
ld a, #0x0
53+
ld (state_volume_adpcm_a_global), a
54+
ld a, #0x0
55+
ld (state_volume_adpcm_b_global), a
56+
ret

0 commit comments

Comments
 (0)