From d01eb6cc45ed26fd296fefd8eeea68062dc0f6df Mon Sep 17 00:00:00 2001 From: Andrew Alderwick Date: Sun, 25 Apr 2021 21:52:39 +0100 Subject: [PATCH] Added currently playing volume readout --- projects/demos/musictracker.usm | 11 +++++++++++ src/apu.c | 17 +++++++++++++++++ src/apu.h | 4 +++- src/emulator.c | 5 ++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/projects/demos/musictracker.usm b/projects/demos/musictracker.usm index 3b958c2..3ded08e 100644 --- a/projects/demos/musictracker.usm +++ b/projects/demos/musictracker.usm @@ -72,6 +72,7 @@ BRK @on-screen ( -> ) ;move-head JSR2 + ;draw-vu JSR2 .head/pos PEK #08 MOD #00 NEQ ,&skip JNZ ;bang JSR2 &skip @@ -421,6 +422,16 @@ RTN RTN +@draw-vu ( -- ) + .ctlframe/x1 PEK2 #0088 ADD2 .ctlframe/y1 PEK2 #0010 ADD2 + .Audio/volume DEI DUP STH #04 SFT + ;draw-knob/force JSR2 + .ctlframe/x1 PEK2 #0098 ADD2 .ctlframe/y1 PEK2 #0010 ADD2 + STHr #0f AND + ;draw-knob/force JSR2 + +RTN + @draw-channels .chnframe/x1 PEK2 .chnframe/y1 PEK2 .chnframe/x2 PEK2 .chnframe/y2 PEK2 #01 ;line-rect JSR2 diff --git a/src/apu.c b/src/apu.c index c53d616..cceabb6 100644 --- a/src/apu.c +++ b/src/apu.c @@ -79,3 +79,20 @@ apu_start(Apu *c, Uint16 adsr, Uint8 pitch) else /* sample repeat mode */ c->period = NOTE_PERIOD; } + +Uint8 +apu_get_vu(Apu *c, Apu *end) +{ + size_t i; + Sint32 sum[2] = {0, 0}; + for(; c < end; ++c) { + if(!c->advance) continue; + sum[0] += envelope(c, c->age) * c->volume_l; + sum[1] += envelope(c, c->age) * c->volume_r; + } + for(i = 0; i < 2; ++i) { + sum[i] /= 0x800; + if(sum[i] > 0xf) sum[i] = 0xf; + } + return (sum[0] << 4) | sum[1]; +} diff --git a/src/apu.h b/src/apu.h index 383901b..9a4757b 100644 --- a/src/apu.h +++ b/src/apu.h @@ -20,8 +20,10 @@ typedef struct { Uint8 *addr; Uint32 count, advance, period, age, a, d, s, r; Uint16 i, len; - Uint8 volume_l, volume_r, pitch, repeat; + Sint8 volume_l, volume_r; + Uint8 pitch, repeat; } Apu; void apu_render(Apu *c, Sint16 *sample, Sint16 *end); void apu_start(Apu *c, Uint16 adsr, Uint8 pitch); +Uint8 apu_get_vu(Apu *c, Apu *end); diff --git a/src/emulator.c b/src/emulator.c index 7603280..dcda3c1 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -248,7 +248,10 @@ static void audio_talk(Device *d, Uint8 b0, Uint8 w) { Apu *c; - if(!w) return; + if(!w) { + if(b0 == 0xe) d->dat[0xe] = apu_get_vu(apu, apu + POLYPHONY); + return; + } c = &apu[d->dat[0x7] % POLYPHONY]; SDL_LockAudioDevice(audio_id); if(b0 == 0x1) c->period -= (Sint16)mempeek16(d->dat, 0x0);