Sound & Music

SOUND frequency, duration — frequency in hertz (37 to 32767), duration in clock ticks (18.2 per second).

24.1 BEEP and SOUND

SOUND frequency, duration — frequency in hertz (37 to 32767), duration in clock ticks (18.2 per second). SOUND 32767, n or a frequency of 0 is commonly used as a rest.

BEEP                              ' short system beep
SOUND 440, 18.2                   ' 440 Hz for 18.2 clock ticks = 1 second
FOR f = 200 TO 1200 STEP 50       ' rising siren
    SOUND f, 1
NEXT
FOR f = 1200 TO 200 STEP -50
    SOUND f, 1
NEXT

24.2 PLAY and Music Macro Language (MML)

notes; follow with # or + (sharp), - (flat), a length, . (dotted) octave 0–6 (default 4; octave 3 begins at middle C) octave down / up default note length (1 = whole, 4 = quarter, 8 = eighth...) tempo in quarter notes per minute (32–255, default 120) pause (rest) of length n play note number n (0–84; 0 = rest) MN / ML / MS normal / legato / staccato foreground (wait) / background

PLAY "T120 O4 L4 C D E F G A B > C"           ' C major scale
PLAY "T180 O3 L8 E E P8 E P8 C E P8 G P4 < G" ' a familiar game jingle opening
PLAY "MB T140 O4 L8 CDEC CDEC EFG4 EFG4"      ' MB = play in the background
PRINT "Music plays while the program continues..."

24.3 QB64 audio files

Statement / function load a sound, returns a handle (0 on failure)

_SNDPLAY h / _SNDLOOP h / _SNDSTOP h / _SNDPAUSE h true while playing _SNDVOL h, v / _SNDBAL h, x, y, z volume / stereo or 3-D position

_SNDLEN(h), _SNDGETPOS(h), _SNDSETPOS h, s length and position in seconds copy a handle so the same effect can overlap itself _SNDPLAYFILE file$ free the sound

DIM music AS LONG, laser AS LONG
music = _SNDOPEN("assets/theme.ogg")       ' WAV, OGG, MP3, FLAC and other formats
laser = _SNDOPEN("assets/laser.wav")
IF music = 0 OR laser = 0 THEN PRINT "Sound file missing": END

_SNDVOL music, 0.5                         ' volume 0.0 to 1.0
_SNDLOOP music                             ' play repeatedly
PRINT "Press SPACE to fire, Esc to quit"
DO
    k$ = INKEY$
    IF k$ = " " THEN _SNDPLAY laser
    _LIMIT 60
LOOP UNTIL k$ = CHR$(27)
_SNDSTOP music
_SNDCLOSE music: _SNDCLOSE laser

24.4 Synthesizing sound with _SNDRAW

_SNDRAW queues samples between -1 and 1 — the basis for sound-effect generators, synthesizers, and chiptune players.

Classic BASIC: C64 programs POKEd the SID chip registers (54272 onward); ZX Spectrum BEEP duration, pitch; BBC BASIC SOUND channel, amplitude, pitch, duration and ENVELOPE; GW-BASIC had SOUND, PLAY, BEEP. FreeBASIC: no built-in PLAY/SOUND in -lang fb; use libraries such as SDL2_mixer, FMOD, BASS, or raylib's audio module, or Windows Beep via the API. VB.NET: Console.Beep(440, 500), My.Computer.Audio.Play("file.wav") (Windows), System.Media.SoundPlayer; NAudio library for advanced audio.

DIM rate AS LONG, i AS LONG
rate = _SNDRATE                            ' samples per second of the output device
FOR i = 0 TO rate - 1                      ' one second of a 440 Hz tone with fade-out
    _SNDRAW SIN(2 * _PI * 440 * i / rate) * 0.3 * (1 - i / rate)
NEXT
DO WHILE _SNDRAWLEN > 0: _LIMIT 100: LOOP  ' wait until the queued samples finish