Programming Music on the Atari: From DATA Statements to Interrupt-Driven Players

Getting a tune out of an Atari is easy. Getting it out in time, with some shape to each note, without freezing the rest of your program, takes a little more work. This guide builds three players, each better than the last: a timed BASIC loop, a BASIC player with volume envelopes and a "chorus" voice, and a tiny machine-language driver that runs in the vertical blank interrupt so the music keeps going while BASIC does something else. It finishes with the tracker tools today's scene uses.

You need: Atari BASIC (any machine, 16K is plenty) and the note values from POKEY Sound Basics. For the last example, either type in the DATA statements or assemble the source with any 6502 assembler.

1. The two problems: pitch and time

Pitch is solved by a table. With distortion 10 on NTSC machines, middle C is 121, D 108, E 96, F 91, G 81, A 72, B 64 and the next C 60. The full chromatic table is in the basics guide.

Time is the harder one. SOUND has no duration, and Chris Chabris showed in Antic (November 1983) that empty FOR/NEXT delay loops change speed depending on where they sit in the program. The reliable clock is the operating system's vertical blank. Location 540 ($21C, system timer 3) counts down by one every 1/60 second until it reaches zero:

100 SOUND 0,121,10,8
110 POKE 540,60
120 IF PEEK(540)>0 THEN 120
130 SOUND 0,0,0,0

Chabris notes that this timer "works very well (you can verify it with a stopwatch)". Musicians' units, at 60 ticks per second on NTSC (50 on PAL):

Tempo 120 bpm (4/4) Jiffies
Whole note 120
Half note 60
Quarter note 30
Eighth note 15

2. A DATA-driven player with envelopes

Jerry White's "Music with BASIC" (Best of Antic, Volume 1) introduced two tricks that make BASIC music sound far less like a doorbell:

  1. Use the remaining time as the volume, so each note fades away at its end ("a slight decay or decreasing volume").
  2. Double the melody on extra voices tuned one step sharp and flat, for "a richer, fuller sound".

Here is a compact player built on those ideas. Each note is a pair of DATA values: pitch and length in jiffies. Pitch 0 is a rest, and length 0 ends the tune.

10 REM TIMED TUNE PLAYER WITH DECAY
20 POKE 541,0:REM HIGH BYTE OF TIMER 3
30 RESTORE 200
40 READ P,D:IF D=0 THEN 120
50 POKE 540,D
60 T=PEEK(540):IF T=0 THEN 40
70 V=T:IF V>8 THEN V=8
80 IF P=0 THEN V=0
90 SOUND 0,P,10,V:SOUND 1,P+1,10,INT(V/2)
100 GOTO 60
120 SOUND 0,0,0,0:SOUND 1,0,0,0
130 END
200 DATA 121,14,108,14,96,14,121,14,0,2
210 DATA 121,14,108,14,96,14,121,14,0,2
220 DATA 96,14,91,14,81,28,0,2
230 DATA 96,14,91,14,81,28,0,2
240 DATA 81,7,72,7,81,7,91,7,96,14,121,14,0,2
250 DATA 81,7,72,7,81,7,91,7,96,14,121,14,0,2
260 DATA 121,14,162,14,121,28,0,2
270 DATA 121,14,162,14,121,28,0,30
290 DATA 0,0

That is "Frère Jacques". How it works:

  • Line 50 loads the timer with the note length. The loop in lines 60–100 keeps reading the timer and re-issuing SOUND with a volume that never exceeds 8 but falls over the last eight jiffies.
  • Voice 1 plays the same note one divider step higher in number (very slightly lower in pitch) at half volume. The tiny detune creates a slow beat that thickens the tone. This is the same interference effect you get from SOUND 0,255,10,8:SOUND 1,254,10,8.
  • Short 0,2 rests separate repeated notes, so two Cs in a row are heard as two notes instead of one long one.

To add a bass line or harmony, put it on voices 2 and 3 with a second pair of values per DATA entry. Expect two drawbacks: BASIC is slow, and with four voices the gap between the first SOUND statement and the fourth can be noticeable. De Re Atari's SIMUL$ USR routine, reproduced in Advanced POKEY, sets all four voices in a single call.

3. Better still: let the vertical blank play the music

Every BASIC player above stops your program while the tune plays. De Re Atari calls the alternative "probably the most versatile and practical of all methods": a small, table-driven machine-language routine hooked into the 60 Hz vertical blank interrupt. It reads the next note from a table when the current one runs out, writes POKEY and returns. The main program, whether BASIC, assembly or anything else, carries on and never notices.

The routine below is a deferred VBI installed through the OS routine SETVBV ($E45C, with A = 7 for the deferred vector, Y = low byte and X = high byte of the routine). It ends with a JMP to XITVBV ($E462), exactly as De Re Atari prescribes. It lives in page 6 and plays a pitch/duration table on voice 1 (BASIC's voice 0), looping forever.

AUDF1  = $D200
AUDC1  = $D201
SETVBV = $E45C
XITVBV = $E462

        *= $0600
INSTALL PLA             ; drop USR argument count
        LDA #0
        STA PTR
        LDA #1
        STA TIMER       ; start on the next VBI
        LDY #<PLAYER
        LDX #>PLAYER
        LDA #7          ; 7 = deferred VBI vector
        JSR SETVBV
        RTS
PLAYER  DEC TIMER
        BNE DONE        ; current note still sounding
        LDX PTR
        LDA SONG+1,X    ; duration
        BNE PLAY
        LDX #0          ; duration 0 = end: loop to start
        LDA SONG+1,X
PLAY    STA TIMER
        LDA SONG,X      ; pitch
        STA AUDF1
        BEQ REST
        LDA #$A8        ; distortion 10, volume 8
        BNE SETC
REST    LDA #0
SETC    STA AUDC1
        INX
        INX
        STX PTR
DONE    JMP XITVBV
PTR     .BYTE 0
TIMER   .BYTE 1
SONG    ; pitch, duration pairs follow; 0,0 ends

The same routine, hand-assembled for BASIC. The 69 bytes of code and variables go to 1536–1604, and the song table starts at 1605:

10 FOR I=0 TO 68:READ B:POKE 1536+I,B:NEXT I
20 I=69
30 READ P,D:POKE 1536+I,P:POKE 1537+I,D:I=I+2:IF D THEN 30
40 SOUND 0,0,0,0
50 X=USR(1536)
60 PRINT "MUSIC IS PLAYING FROM THE VBI."
70 PRINT "TRY LIST - BASIC IS STILL YOURS."
80 END
100 DATA 104,169,0,141,67,6,169,1,141,68,6
110 DATA 160,21,162,6,169,7,32,92,228,96
120 DATA 206,68,6,208,38,174,67,6,189,70,6
130 DATA 208,5,162,0,189,70,6,141,68,6
140 DATA 189,69,6,141,0,210,240,4,169,168
150 DATA 208,2,169,0,141,1,210,232,232
160 DATA 142,67,6,76,98,228,0,1
200 DATA 121,14,108,14,96,14,121,14,0,2
210 DATA 121,14,108,14,96,14,121,14,0,2
220 DATA 96,14,91,14,81,28,0,2
230 DATA 96,14,91,14,81,28,0,30
290 DATA 0,0

Run it and the program ends. Hold on, though: END normally silences POKEY, so why does the music keep going? Because the VBI writes a fresh note every time the timer expires, and the tune loops. You can edit, LIST or even RUN other code while it plays. SYSTEM RESET restores the OS vectors (De Re Atari: the RAM vectors are reloaded from ROM on every reset) and stops it.

Practical notes:

  • Leave voice 0 to the driver. Use SOUND 1–SOUND 3 for effects, and remember that each SOUND resets AUDCTL.
  • The music pauses during disk or cassette I/O. The OS skips the deferred stage of the VBI while time-critical serial I/O is running (the CRITIC flag at location 66).
  • Mind page 6. An INPUT of more than 128 characters overflows into 1536–1663, so keep INPUT lines short or relocate the driver.
  • The song table can hold 93 notes before it runs past page 6 at 1791. For longer songs, assemble the driver elsewhere (for example in memory reserved below RAMTOP) and set the table address to match.

Commercial software solved the same problem in the 1980s. De Re Atari mentions INSOMNIA from the Atari Program Exchange, a table-driven, language-independent interrupt sound generator. Jerry White mentions P.D.I.'s Music Box, which played Atari Music Composer files "using a vertical blank assembler subroutine" while BASIC stayed usable.

4. Going further: envelopes, effects and instruments

A real music driver adds per-voice state for volume envelopes, vibrato, arpeggios and drum noises made from distortion 8 or 0. De Re Atari sketches the idea of an "envelope" applied to each note: change frequency or volume several times within the note. At 60 updates a second that is enough for convincing attack and decay. For true waveform playback you need volume-only mode and far faster updates than the VBI can give. See Advanced POKEY.

5. Today's tools: trackers and players

Most modern Atari music is written on a PC in a tracker and played by an optimised 6502 driver:

  • RASTER Music Tracker (RMT) is the best-known Windows tracker for POKEY. It exports modules plus an assembly player that your game calls once per frame. In January 2026, JAC! announced plans for an RMT 2.0 that fixes the problems of version 1.34, still loads and saves RMT 1.28 files, and exports every format in one go (RMT, stripped RMT, XEX, LZSS and others).
  • LZSS-compressed register streams are a popular alternative for games, because a full RMT player can use a lot of CPU time. An LZSS player replaces the full driver with a small decompressor that replays recorded POKEY register values. Players by DMSC and rensoupp exist, and conversion tools such as RMT2LZSS produce the data.
  • SAP files and the ASAP player (by Fox) are how Atari tunes are archived and played on modern systems. The ASMA archive collects them.

A common pitfall: the player produces strange sounds inside a game when POKEY has not been reset first, or when the low and high bytes of the module address are swapped in the init call.

Troubleshooting

  • Tempo is wrong on a European machine. PAL runs the VBI at 50 Hz, so every jiffy-based duration plays 20% slower. Scale the durations, or test for PAL first (see Realtime clock and timers).
  • Notes run together. Insert short rests between repeated pitches, or end each note's volume early.
  • The VBI player crashes the machine. Check that every DATA value was typed correctly, especially the JSR (32,92,228) and JMP (76,98,228). One wrong byte sends the CPU into random memory 60 times a second.
  • Silence after disk access. SOUND 0,0,0,0 reinitialises POKEY.

Further reading

  • The Musical Atari by Hal Glicksman and Laura Goodfriend (Datamost, 1984): songs arranged for piano or Atari, with BASIC harmony and round programs.
  • De Re Atari (Atari, Inc., 1982), Chapters 7 and 8: interrupt sound and the vertical blank.
  • Atari Sound and Graphics by Herb Moore, Judy Lower and Bob Albrecht (Wiley, 1982).
  • On this site: Vertical blank interrupts for a full explanation of VBI programming.

Credits