MusicBox1

Date: 198x
Type: Program
Platform(s): TS 2068
Tags: Music

MusicBox1 is a BASIC music player that stores 14 compositions in DATA statements and plays them back using the BEEP command with user-selectable tempo and transposition. Each composition is stored starting at a DATA line calculated by the formula N*200+800, allowing direct RESTORE access by piece number without scanning through all data. A value of 99 in the frequency field signals a rest, implemented as a PAUSE rather than a BEEP. The program supports continuous looping (using ON ERR to detect end-of-data and restart), replay, and real-time semitone transposition via an offset variable FF added to each note frequency. Tempo is controlled by a multiplier TT applied to the duration divisor, defaulting to 10.


Program Structure

The program is organized into a title/menu loop, a composition selector, a playback engine, and a post-playback options screen, followed by a large block of DATA statements. Lines 10–40 display a scrolling title banner. Lines 50–80 list the 14 available compositions and accept a user selection. Lines 90–170 form the playback engine. Lines 180–380 handle post-play options. Line 390 saves the program. Lines 1000–3630 hold the music data.

Data Organization and Addressing

Each composition occupies a block of DATA lines anchored at a multiple of 200 offset by 800. The formula N*200+800 is used with RESTORE to jump directly to the correct data block for piece number N. The listing lines that hold each composition’s data are:

NLineTitle
11000Happy Birthday To You!
21200Jingle Bells
31400Bridal Chorus
41600The Irish Washerwoman
51800Joy to the World
62000March Militaire
72200Wooden Soldiers on Parade
82400O Sole Mio
92600Toreador Song
102800Vienna March
113000Torna a Sorento
123200Indigo March
133400The Blue Danube
143600Silent Night

The first two values after each anchor line are the title string N$ and a rate scalar R, which is multiplied by TT (default 10) to set the duration unit T. This allows each piece to have its own default tempo scaling independent of the user’s TT setting.

Playback Engine

Lines 130–160 form a tight loop reading pairs of values: a pitch F and a duration divisor V. If F equals 99, the note is a rest, implemented as PAUSE T/V*60 (line 150). Otherwise, BEEP T/V, F+FF is called (line 160), where FF is the user-supplied semitone transposition offset. The end of data is detected by ON ERR GO TO 170 at line 130, which traps the error raised when READ runs out of data.

Line 170 resets the error trap with ON ERR RESET, then checks whether Z$ equals "2" (continuous play mode). If so, it restores to N*200+810 — skipping the title/rate header — and loops back to the note-reading loop at line 130, enabling seamless repetition. This offset of 10 beyond the anchor line skips the first DATA line of each piece, which holds the title string and rate.

Tempo and Transposition

After a piece finishes, the user may press "3" to enter custom tempo and tonality values (lines 250–380). Tempo is re-entered as TT and tonality as FF. The program explains that smaller TT values produce faster playback and each unit of FF shifts the pitch by one semitone. After input, execution jumps back to line 90 to re-read the piece header and recalculate T with the new TT.

Duration Encoding

Note durations are encoded as divisors of T, so a smaller divisor value means a longer note. Fractional values such as 4/3, 5.33, 16/3, and 16/15 appear throughout the data, allowing dotted notes, triplets, and other rhythmic subdivisions to be expressed compactly within the DATA statements. The Spectrum’s BASIC evaluates these arithmetic expressions at READ time.

Post-Playback Options

Lines 180–230 present four options after a piece ends: replay ("1"), continuous play ("2"), change tempo/tonality ("3"), or select another piece ("0"). The keypress is captured using the PAUSE 0/INKEY$ idiom at line 230. Options "1" and "2" both branch to line 90, but only "2" sets Z$="2", which is later tested at line 170 to enable looping.

Notable Techniques

  • Direct RESTORE to a calculated line number (N*200+800) gives O(1) access to any piece without sequential scanning.
  • ON ERR GO TO 170 is used as a clean end-of-data sentinel, avoiding the need for a terminating sentinel value in every DATA block.
  • The offset N*200+810 in continuous-play mode neatly skips the two-element header (title and rate) to restart at the first note.
  • Duration divisors stored as unevaluated arithmetic expressions (e.g., 16/3) keep the DATA readable while still producing precise floating-point beat lengths.
  • The transposition feature (FF) adds directly to the BEEP pitch argument, making key changes trivial without re-encoding the music data.

Bugs and Anomalies

  • Line 210 is missing a space before TO: "PRESS ""0""TO PLAY ANOTHER" — the ""0""TO will display as "0"TO on screen, missing the intended space.
  • Line 320 contains a spelling error: "TOALITY" instead of "TONALITY".
  • The continuous-play offset N*200+810 assumes the title and rate data occupy exactly one line of 10 line-number units. If a composition’s header DATA were split across lines at a different offset, this would fail; however, all 14 pieces follow the convention consistently.
  • Line 230 contains two embedded ink/paper color control codes before PAUSE 0 (visible as \{16}\{0}), which reset the display color before capturing input — a cosmetic measure rather than a functional one.

Content

Appears On

Related Products

Related Articles

Related Content

Image Gallery

Source Code

   10 BORDER 4:PAPER 6:CLS :FOR A=0 TO 12
   20 PRINT TAB 3; INK 1;"MUSIC BOX 1"; TAB 18; INK 3;"MUSIC BOX 1":NEXT A
   30 PRINT AT 15,0; PAPER 5;''"THIS IS YOUR MUSIC BOX 1.  PRESS ""ENTER"" TO SEE THE CONTENTS."
   40 PAUSE 0
   50 CLS :PRINT PAPER 5;"********* BOX CONTENTS *********":PRINT ''
   60 FOR I=1 TO 14:RESTORE I*200+800:READ N$:PRINT I; TAB 4;N$:NEXT I
   70 PRINT PAPER 5; AT 20,0;"ENTER THE NUMBER OF THE COMPOSI-TION THAT YOU WANT TO PLAY      " 
   80 INPUT N:CLS :LET TT=10:LET FF=0:LET Z$=""
   90 RESTORE N*200+800:READ N$,R:LET T=R*TT
  100 CLS :PRINT PAPER 5;"********* NOW PLAYING **********"; AT 21,0;"********* NOW PLAYING **********"
  110 PRINT AT 10,(31- LEN N$)/2;N$
  120 PRINT PAPER 3; INK 7; AT 20,0;"TO STOP, PRESS ""SHIFT"" + ""BREAK"""
  130 READ F,V:ON ERR GO TO 170
  140 IF F <>99 THEN GO TO 160
  150 PAUSE T/V*60:GO TO 130
  160 BEEP T/V,F+FF:GO TO 130
  170 ON ERR RESET :IF Z$="2" THEN RESTORE N*200+810:GO TO 130
  180 CLS :PAPER 7:PRINT AT 7,0;"PRESS ""1"" TO PLAY AGAIN."
  190 PRINT "PRESS ""2"" TO PLAY CONTINUOUSLY."
  200 PRINT ;"PRESS ""3"" TO PLAY DIFFERENTLY."
  210 PRINT ;"PRESS ""0""TO PLAY ANOTHER"
  220 PRINT "COMPOSITION.":PAPER 6
  230 \{16}\{0} PAUSE 0:LET Z$= INKEY$:IF Z$="1" OR Z$="2" THEN GO TO 90
  240 IF Z$="0" THEN GO TO 50
  250 CLS :PRINT "INPUT A NUMBER FOR THE DESIRED"
  260 PRINT "TEMPO.  THE SUGGESTED TEMPO IS"
  270 PRINT "10: A SMALLER NUMBER WILL MAKE"
  280 PRINT "THE TEMPO FASTER; A LARGER"
  290 PRINT "NUMBER WILL MAKE IT SLOWER."
  300 INPUT TT
  310 PRINT ''"INPUT A POSITIVE OR A NEGATIVE"
  320 PRINT "NUMBER FOR THE DESIRED TOALITY."
  330 PRINT "THE PROGRAMMED TONALITY IS 0."
  340 PRINT "EACH POSITIVE UNIT WILL RAISE"
  350 PRINT "THE TONALITY BY ONE SEMITONE."
  360 PRINT "EACH NEGATIVE UNIT WILL LOWER IT"
  370 PRINT "BY ONE SEMITONE."
  380 INPUT FF:GO TO 90
  390 SAVE "BOX 1"
 1000 DATA "HAPPY BIRTHDAY TO YOU!",.5
 1010 DATA 7,8,7,8,9,4,7,4,12,4,11,2,7,8,7,8,9,4,7,4,14,4,12,2,7,8,7,8,19,4,16,4,12,8,12,8,11,4,9,4,17,8,17,8,16,4,12,4,14,4,12,4/3
 1200 DATA "JINGLE BELLS",.08
 1210 DATA 13,4,13,4,13,4,99,4,13,4,13,4,13,4,99,4,13,4,16,4,9,4,11,4,13,4/3,99,4,14,4,14,4,14,4,14,4,14,4,13,4
 1220 DATA 13,4,99,4,13,4,11,4,11,4,13,4,11,4,99,4,16,4,99,4,13,4,13,4,13,4,99,4,13,4,13,4,13,4,99,4,13,4,16,4
 1230 DATA 9,4,11,4,13,4/3,99,4,14,4,14,4,14,4,14,4,14,4,13,4,13,4,99,4,16,4,16,4,14,4,11,4,9,4,99,4,214,99,4
 1400 DATA "BRIDAL CHORUS",.2
 1410 DATA 7,4,7,5.33,7,16,7,4,7,4,7,1,7,4,12,5.33,12,16,12,2,7,4,14,5.33,11,16,12,2,7,4,12,5.33,17,16,17,4,16,8,14,8
 1420 DATA 12,4,11,5.33,12,16,14,2,7,4,12,5.33,12,16,12,2,7,4,14,5.33,11,16,12,2,7,4,12,8,16,8,19,4,16,8,12,8,9,4,14,8
 1430 DATA 16,8,12,2,17,4,16,8,14,8,9,4,9,4,11,4,12,5.33,14,16,14,2,17,4,16,8,14,8,9,4,9,4,11,4,12,5.33,14,16,14,2
 1440 DATA 7,4,12,5.33,12,16,12,2,7,4,14,5.33,11,16,12,2,7,4,12,8,16,8,19,4,16,8,12,8,21,2,19,8,17,8,16,8,14,8,12,1
 1600 DATA "THE IRISH WASHERWOMAN",.08
 1610 DATA 19,16,17,16,16,8,12,8,12,8,7,8,12,8,12,8,16,8,12,8,16,8,19,8,17,8,16,8,17,8,14,8,14,8,7,8,14,8
 1620 DATA 14,8,17,8,14,8,17,8,21,8,19,8,17,8,16,8,12,8,12,8,7,8,12,8,12,8,16,8,12,8,16,8,19,8,17,8,16,8,17,8
 1630 DATA 16,8,17,8,14,8,19,8,17,8,16,8,12,8,12,8,12,4,19,16,17,16,16,8,12,8,12,8,7,8,12,8,12,8,16,8,12,8,16,8
 1640 DATA 19,8,17,8,16,8,17,8,14,8,14,8,7,8,14,8,14,8,17,8,14,8,17,8,21,8,19,8,17,8,16,8,12,8,12,8,7,8,12,8
 1650 DATA 12,8,16,8,12,8,16,8,19,8,17,8,16,8,17,8,16,8,17,8,14,8,19,8,17,8,16,8,12,8,12,8,12,4
 1800 DATA "JOY TO THE WORLD",.25
 1810 DATA 24,4,23,5.33,21,16,19,2.67,17,8,16,4,14,4,12,8/3,19,8,21,2.67,21,8,23,2.67,23,8,24,2,99,16,24,8,24,8,23,8,21,8,19,8,19,5.33
 1820 DATA 17,16,16,8,24,8,24,8,23,8,21,8,19,8,19,5.33,17,16,16,8,16,8,16,8,16,8,16,8,16,16,17,16,19,16/5,99,16,17,16,16,16,14,8
 1830 DATA 14,8,14,8,14,16,16,16,17,16/5,99,16,16,16,14,16,12,8,24,4,21,8,19,5.33,17,16,16,8,17,8,16,4,14,4,12,2
 2000 DATA "MARCH MILITAIRE",.08
 2010 DATA 7,4,2,8,2,8,1,4,2,4,7,4,2,8,2,8,1,4,2,4,7,4,2,4,7,4,2,4,7,4,2,4,-1,4,2,4,7,4,2,4
 2020 DATA 7,4,11,4,14,1,14,2,12,4,11,4,9,2,11,4,9,4,7,2,9,4,11,4,2,2.67,4,8,2,2,14,2,12,4,11,4,16,2,14,4
 2030 DATA 11,4,12,4,14,8,12,8,11,4,12,8,11,8,9,4,4,8,6,8,7,8,6,8,7,8,8,8,9,4,9,8,9,8,7,4,9,4,11,4
 2040 DATA 12,4,14,2,9,4,11,4,12,2,11,4,9,4,7,2,9,4,9,8,9,8,7,4,9,4,11,4,12,4,14,2,6,4,7,4,9,2.67,11,8,7,2,19,2
 2200 DATA "WOODEN SOLDIERS ON PARADE",.15
 2210 DATA 11,5.33,12,16,14,4,16,4,14,4,12,4,11,4,9,4,7,4,11,5.33,12,16,14,4,16,4,14,4,19,4,18,4,16,4,14,4,11,5.33,12,16
 2220 DATA 14,4,16,4,14,4,12,4,11,4,9,4,7,4,11,5.33,12,16,14,4,16,4,14,4,19,4,18,4,16,4,14,4,2,5.33,4,16,6,4,4,4
 2230 DATA 2,4,16,4,14,4,12,4,11,4,2,5.33,4,16,6,4,4,4,2,4,12,4,11,4,9,4,11,4,11,5.33,12,16,14,4,16,4,14,4,12,4
 2240 DATA 11,4,9,4,7,4,0,5.33,2,16,4,4,2,4,0,4,2,4,6,4,7,2
 2400 DATA "O SOLE MIO",.25
 2410 DATA 99,8,26,8,24,8,23,8,21,4,19,4,19,8,21,8,23,8,19,8,18,4,16,4,99,8,18,8,19,8,21,8,18,9,16,8,18,8/3,18,8
 2420 DATA 19,8,21,8,16,8,14,8,14,4,99,8,26,8,24,8,23,8,21,4,19,4,19,8,21,8,23,8,19,8,18,4,16,4,99,8,24,8,23,8
 2430 DATA 21,8,26,8,23,8,21,8,19,8,21,8/3,23,8,21,24,23,24,21,24,19,8/3,99,8,19,8,19,8,18,16,18,16,14,4,14,4,99,8,18,8,18,8
 2440 DATA 16,16,16,16,12,8/5,18,8,18,8,16,16,16,16,12,4,12,4,12,8,9,8,11,8,12,8,14,4/3,99,8,14,8,15,8/5,12,8,19,8,15,8,14,4,14,4,14,8,11,8,9,8,7,8,14,8/5,11,16,9,32,11,32,9,16/3,6,16,7,8/5,99,8/3
 2600 DATA "TOREADOR SONG",.17
 2610 DATA 12,4,14,8,12,8,9,4,9,4,9,8,7,8,9,8,10,8,9,2,10,4,7,8,12,8,9,2,5,4,2,8,7,8,0,2,7,4,7,8
 2620 DATA 7,8,7,8,14,8,12,8,10,8,9,8,7,8,9,8,10,8,9,2,4,4,9,8,9,8,9,4,8,8,11,8,16,1,99,6.4,14,32,16,32
 2630 DATA 14,32,13,8,14,8,7,8,9,8,10,4,99,6.4,9,32,10,32,9,32,5,8,14,8,12,2,99,6.4,5,32,7,32,5,32,0,8,10,8,9,4
 2640 DATA 7,4,5,4,12,12,12,12,12,12,12,4,12,4,12,4,14,8,12,8,9,4,9,4,9,8,7,8,9,8,10,8,9,2,10,4,7,8,12,8
 2650 DATA 9,2,5,4,2,8,7,8,0,2,7,4,7,8,7,8,7,8,14,8,12,8,10,8,9,8,7,8,9,8,10,8,9,2,4,4,9,8,9,8
 2660 DATA 9,4,8,8,11,8,16,1,99,6.4,14,32,16,32,14,32,13,8,14,8,7,8,9,8,10,4,99,6.4,9,32,10,32,9,32,5,8,14,8,12,2
 2670 DATA 99,6.4,5,32,7,32,7,32,0,8,10,8,9,4,7,4,5,1
 2800 DATA "VIENNA MARCH",.07
 2810 DATA 7,8,7,8,7,4,7,4,99,4,8,8,8,8,9,4,9,4,99,4,7,8,7,8,7,4,12,4,11,4,9,4,11,1.33,7,8,7,8
 2820 DATA 7,4,7,4,99,4,8,8,8,8,9,4,9,4,99,4,7,8,7,8,7,4,17,4,16,4,15,4,16,1.33,7,8,7,8,7,4,7,4,99,4
 2830 DATA 8,8,8,8,9,4,9,4,99,4,7,8,7,8,7,4,12,4,11,4,9,4,11,1.33,14,8,14,8,14,4,12,2,12,8,12,8,12,4,11,2
 2840 DATA 11,8,11,8,9,4,12,4,11,4,9,4,7,4,7,8,7,8,7,4,7,4,16,1.33,11,4,12,4,12,4,7,4,4,4,5,2.67,7,8,7,4
 2850 DATA 7,4,7,1,17,1.33,11,4,12,4,11,4,9,4,5,4,4,2.67,7,8,7,4,7,4,7,1,16,1.33,11,4,10,4,10,4,10,4,10,4,9,2.67
 2860 DATA 14,8,14,4,14,4,15,1,16,4,0,8,-1,8,-3,4,-5,4,-7,4,-8,4,-10,4,-12,4,9,4,99,4,11,4,99,4,12,1.33
 3000 DATA "TORNA A SORENTO",.15
 3010 DATA 26,8,28,8,25,2.67,23,8,26,4,26,2,99,8,25,8,26,8,28,8,25,8,23,8,21,4,21,2,19,4,22,4,26,4,29,2.67,28,8,26,4
 3020 DATA 99,8,28,8,25,2.67,25,8,26,1.33,14,8,16,8,17,8,19,8,21,8,17,8,21,4,21,2,19,8,21,8,22,8,19,8,22,8,19,8,26,4
 3030 DATA 26,2,26,8,28,8,29,8,28,8,26,8,28,8,21,4,21,2,19,8,21,8,19,8,17,8,16,8,17,8,14,1.33,26,8,25,8,21,8,23,8
 3040 DATA 25,8,21,8,23,4,23,2,25,8,23,8,21,8,23,8,25,8,21,8,23,4,23,2,18,8,19,8,21,8,18,8,16,8,14,8,19,4,19,2
 3050 DATA 21,8,23,8,25,8,23,8,21,8,25,8,18,1.33,26,8,25,8,21,8,23,8,25,8,21,8,23,4,23,2,28,8,26,8,25,8,26,8,28,8
 3060 DATA 25,8,26,4,26,2,26,8,28,8,29,8,28,8,26,8,28,8,21,4,21,2,19,8,21,8,19,8,17,8,16,8,17,8,14,1.33,26,8,28,8
 3070 DATA 25,2.67,23,8,26,4,26,2,99,8,25,8,26,8,28,8,25,8,23,8,21,4,21,2,19,4,22,4,26,4,29,2.67,28,8,26,4,99,8,28,8,25,2.67,25,8,26,1.33
 3200 \{16}\{2}\{16}\{2}\{17}\{0}\{17}\{7} DATA \{17}\{7}"INDIGO MARCH",.15
 3210 DATA 8,16,9,8,9,8,9,5.55,8,16,9,8,9,8,9,5.33,9,16,12,5.33,5,15,9,5.33,0,16,7,8,7,8,9,5.33,8,16,9,8,9,8,9,5.33
 3220 DATA 8,16,9,8,9,8,9,5.33,9,16,17,5.33,16,16,14,5.33,12,16,12,16,11,16,9,16,11,16,12,5.33,8,16,9,8,9,8,9,5.33,8,16,9,8
 3230 DATA 9,8,9,5.33,9,16,12,5.33,5,15,9,5.33,0,16,7,8,7,8,9,5.33,8,16,9,8,9,8,9,5.33,8,16,9,8,9,8,9,5.33,9,16,17,5.33
 3240 DATA 16,16,14,5.33,12,16,12,16,11,16,9,16,11,16,12,8,9,8,10,8,16,8,16,8,16,8,17,8,19,16,17,16,16,8,14,8,16,8,17,16
 3250 DATA 26,16/3,99,16/5,23,16/3,99,16,23,16/3,99,16/5,4,4,4,4,7,4,11,8,99,8,11,16/15,99,16,8,16/3,99,16,9,16/3,99,16,18,16/15
 3260 DATA 17,8,17,8,17,8,9,8,10,8,16,8,16,8,16,8,17,8,19,16,17,16,16,8,14,8,16,8,17,16,16,16,14,8,12,8,14,8,16,16
 3270 DATA 14,16,12,8,9,8,10,8,16,8,16,8,16,8,17,8,19,16,17,16,16,8,14,8,12,8,21,4,19,8,17,8,17,8,17,4
 3400 DATA "THE BLUE DANUBE",.08
 3410 DATA 2,4,6,4,9,4,9,4,99,4,21,16/3,99,16,21,16/3,99,16/5,18,16/3,99,16,18,16/3,99,16/5,2,4,2,4,6,4,9,4
 3420 DATA 9,4,99,4,21,16/3,99,16,21,16/3,99,16/5,19,16/3,99,16,19,16/3,99,16/5,1,4,1,4,4,4,11,4,11,4,99,4,23,16/3,99,16,23,16/3,99,16/5,19,16/3,99,16
 3430 DATA 19,16/3,99,16/5,1,4,1,4,4,4,11,4,11,4,99,4,23,16/3,99,16,23,16/3,99,16/5,18,16/3,99,16,18,16/3,99,16/5,2,4
 3440 DATA 2,4,6,4,9,4,14,4,99,4,26,16/3,99,16,26,16/3,99,16/5,21,16/3,99,16,21,16/3,99,16/5,2,4,2,4,6,4,9,4,14,4,99,4,26,16/3,99,16
 3460 DATA 99,16,14,16/3,99,16,6,16/3,99,16,6,2,4,16/3,99,16,11,2,9,16/3,99,16,2,4,99,8,2,8,2,4
 3470 DATA 99,4,26,8,99,8,25,8,99,8,25,8,99,8,23,8,99,8,23,8,99,8/3,23,8,99,8,22,8,99,8,22,8,99,8,23,8,99,8,23,8,99,8/3,16,8,99,8
 3480 DATA 16,8,99,8,18,2,16,4,99,4,16,8,99,8,16,8,99,8,23,2,21,4,99,4,26,8,99,8,25,8,99,8,25,8,99,8,23,8,99,8,23,8,99,8/3
 3490 DATA 23,8,99,8,25,8,99,8,28,8,99,8,26,8,99,8,26,8,99,8/3,20,8,99,8,23,8,99,8,23,2,21,16/3,99,16,20,16/5,99,16,18,8,14,8,11,8,18,8,18,8,18,16/3,99,16,16,16/3,99,16,9,4
 3500 DATA 99,4,9,16/3,99,16,7,4,99,4,9,16/3,99,16,7,4,99,4,9,16/3,99,16,18,16/15,99,16,16,4,9,16/3,99,16,6,4,99,4,9,16/3,99,16,6,4,99,4,9,16/3,99,16
 3510 DATA 16,16/15,99,16,14,4,9,16/3,99,16,7,4,99,4,9,16/3,99,16,7,4,99,4,9,16/3,99,16,18,16/15
 3520 DATA 99,16,16,16/3,99,16,9,16/3,99,16,14,16/3,99,16,16,16/3,99,16,18,16/3,99,16,21,2,19,16/3,99,16,18,8,18,8,18,16/3,99,16,16,16/3,99,16,14,4
 3600 DATA "SILENT NIGHT",.18
 3610 DATA 7,8/3,9,8,7,4,4,4/3,7,8/3,9,8,7,4,4,4/3,14,2,14,4,11,4/3,12,2,12,4,7,4/3,9,2,9,4,12,8/3,11,8,9,4,7,8/3
 3620 DATA 9,8,7,4,4,4/3,9,2,9,4,12,8/3,11,8,9,4,7,8/3,9,8,7,4,4,4/3,14,2,14,4,17,4,14,4,11,4,12,4/3,16,1,12,4
 3630 DATA 7,4,4,4,7,4,5,4,2,4,0,1

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top