!!!Sound in Forth (translation pending)
Heute gehts es um Geräusche. Folgendes Basic Programm zum Erzeugen eines Sirenentons habe ich im Buch "Atari Basic spielend lernen" gefunden:
{{{
10 REM EUROPAEISCHE SIRENE
15 LOW=57:HIGH=45:P=45
20 FOR AGAIN=1 TO 20
30 SOUND 0,P,10,14
40 FOR WAIT = 1 TO 180: NEXT WAIT
50 P=LOW:LOW=HIGH:HIGH=P
60 NEXT AGAIN
70 SOUND 0,0,0,0
80 END
}}}
Im volksFORTH ist kein SOUND Befehl eingebaut, also bauen wir uns einen Atari-Basic kompatiblem Sound-Befehl in Forth:
{{{
\ Atari 8bit Sound Befehl
$D200 CONSTANT AUDBASE
: SOUND ( CH# FREQ DIST VOL -- )
SWAP $10 * + ROT DUP + AUDBASE +
ROT OVER C! 1+ C! ;
}}}
Und nun kommt die Sirene in Forth:
{{{
: SIRENE
57 54 ( Sound Werte fuer Sirenentoene )
20 0 DO
OVER
0 SWAP 10 14 SOUND
500 0 DO LOOP ( Warteschleife )
SWAP ( Toene wechseln )
LOOP
0 0 0 0 SOUND ;
}}}
Das BASIC Programm benutzt 5 Variablen (LOW, HIGH, P, AGAIN, WAIT), das Forth Programm kommt wieder ohne jede Variablendeklaration aus, alle Werte liegen auf dem Stack. Daher ist Forth so Speichereffizient.