Sound: implement the BASIC 7.0 sound verbs and the PLAY parser

SOUND, ENVELOPE, VOL, PLAY and TEMPO against the akbasic_AudioBackend record,
plus FILTER, which is in the table only so that it can be refused with a reason.

The PLAY note-string parser and TEMPO are here rather than in libakgl because
that is where its audio commit says they belong: a tone generator synthesises
pitches, but deciding that O4CDEFG is five quarter notes starting at middle C is
a language question.

PLAY does not block. On a C128 it holds the program until the last note ends,
which section 1.6 forbids outright, so it parses the string into a fixed queue
and returns; akbasic_runtime_step() releases one note at a time against whatever
time the host last passed to akbasic_runtime_settime(). The driver now steps one
at a time with the clock refreshed in between rather than making a single
unbounded run() call -- a tune whose notes all measured themselves against a
frozen zero would rush out at once. That loop is its own function because CATCH
expands to a break and PASS expands to a return of the context, and main()
returns an int; wrapping the loop is what the protocol prescribes for that shape.

src/audio_tables.c holds the three conversions, laid out as tables because each
is somewhere a wrong constant produces a plausible wrong pitch rather than an
error anybody would notice. Two are transcriptions -- the SID frequency formula
and its non-linear ADSR rate tables, where decay is exactly three times attack.
The third is not: BASIC 7.0 never published what a whole note lasts at a given
TEMPO, so 16000 ms at TEMPO 1 is a calibration choice putting a default quarter
note at 120 bpm, and it is labelled as a choice where it is made.

SOUND's frequency sweep is refused rather than faked, and filed upstream as
akgl_audio_sweep. The only way to fake it here is to re-issue tones from step(),
which ties audible pitch to how often the host calls us -- a tune that changes
key with the frame rate. FILTER is refused for the reason upstream already gave:
there is no filter stage and SDL3 has no primitive to build one from.

The PLAY parser is tested through akbasic_play_parse() directly rather than
through a program, because running one also runs the queue service -- and with
the clock at zero every duration has already expired, so the queue empties before
an assertion can look at it. Draining is correct behaviour and is tested on its
own; the parse tests ask a different question.

68/68 ctest, clean under -Wall -Wextra, doxygen clean.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-31 08:29:04 -04:00
parent 9d99d0a67e
commit e24926d429
16 changed files with 1453 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
10 REM FILTER has no device capability behind it -- akgl_audio_* synthesises and
20 REM mixes but has no filter stage, and SDL3 supplies no primitive to build one
30 REM from. It is refused rather than silently ignored, so a program that asked
40 REM for a low-pass finds out it did not get one.
50 PRINT "BEFORE"
60 FILTER 1000, 1, 0, 0, 5
70 PRINT "UNREACHABLE"

View File

@@ -0,0 +1,3 @@
BEFORE
? 60 : RUNTIME ERROR FILTER needs a filter stage this device does not have

View File

@@ -0,0 +1,8 @@
10 REM The standalone driver lends the script no audio device. ENVELOPE, VOL and
20 REM TEMPO only change interpreter state, so they work regardless; SOUND and
30 REM PLAY need the device and must name themselves when there is none.
40 ENVELOPE 1, 5, 9, 12, 2
50 VOL 12
60 TEMPO 16
70 PRINT "STATE VERBS OK"
80 SOUND 1, 16777, 30

View File

@@ -0,0 +1,3 @@
STATE VERBS OK
? 80 : RUNTIME ERROR SOUND needs an audio device and this runtime has none