REM guitar tuner by simon laszcz
'
CLEAR
OPTION BASE 1
DIM narray%(6,2)
'
GOSUB initialize
GOSUB draw_screen
GOSUB choice
WHILE mchoice%<>27
  GOSUB tuner
  GOSUB choice
WEND
END
'
PROCEDURE initialize
  LOCAL sub%
  DATA 5,1,10,1,3,1,8,1,12,1,5,3
  FOR sub%=6 TO 1 STEP -1
    READ narray%(sub%,1)
    READ narray%(sub%,2)
  NEXT sub%
RETURN
'
PROCEDURE draw_screen
  LOCAL ypos%,sub%
  CLS
  DEFTEXT 1,1,0,32
  TEXT 60,30,"Guitar Tuner"
  CIRCLE 160,100,50
  '
  ypos%=65
  DEFTEXT 1,0,0,8
  DEFLINE 0,3
  FOR sub%=6 TO 4 STEP -1
    TEXT 40,ypos%,STR$(sub%)
    LINE 50,ypos%-4,270,ypos%-4
    ADD ypos%,15
  NEXT sub%
  '
  DEFLINE 0,2
  FOR sub%=3 TO 1 STEP -1
    TEXT 40,ypos%,STR$(sub%)
    LINE 50,ypos%-4,270,ypos%-4
    ADD ypos%,15
  NEXT sub%
  '
  DEFTEXT 1,4,0,8
  TEXT 70,195,"Press ESC to quit"
  DEFTEXT 1,16,0,4
  TEXT 40,180,"Press number of string to tune"
RETURN
'
PROCEDURE choice
  LOCAL key$
  REPEAT
    REPEAT
      LET key$=INKEY$
    UNTIL key$<>""
    mchoice%=ASC(key$)
    string%=mchoice-48
  UNTIL mchoice%=27 OR (string%>0 AND string%<7)
RETURN
'
PROCEDURE tuner
  SOUND 1,15,narray%(string%,1),narray%(string%,2)
RETURN

The program first clears the memory before declaring the 2D integer array narray%. OPTION BASE 1 forces subscripting to start at 1 as opposed to the default of 0. The program then initializes the array with note data within the initialize sub-routine. The screen is then drawn and the keyboard scanned for user input. A loop is defined that executes as long as the user chooses not to quit by pressing ESCAPE (ASCII code 27). Within this loop, a note is played and the keyboard re-scanned.

initialize uses the local integer variable sub% to initialize narray% with the constants defined by the DATA statement. Note values are placed in narray%(sub%,1) and octave values in narray%(sub%,2). The value of sub% ranges from 6 down to 1, so in effect the array is initialized backwards. With each READ statement the DATA pointer is incremented automatically. To reset it to the first constant, the RESTORE command would be used.

Guitar tuner screen shot

draw_screen uses formatted text, a circle and 6 lines to draw a rough representation of a guitar sound hole. In low-resolution the screen is 320 pixels wide and 200 pixels deep. The top left hand corner has the co-ordinates (0,0). A circle with a radius of 50 pixels is drawn in the centre of the screen. The guitar strings are then drawn, the bass strings are drawn with a thicker line style. Each string is labelled with a number, using the expression STR$(sub%). Line and text styles are changed using the DEFLINE and DEFTEXT commands.

choice initiates a loop that repeats until a valid key is pressed (either ESCAPE or a string number). INKEY$ returns the next character from the keyboard buffer and as such doesn't wait for a key to be pressed. It must be placed in a loop that executes until a key is pressed. After a key is pressed, its ASCII value is derived and the string value calculated by subtracting 48, the ASCII code for 0 (49 is the ASCII code for 1).

tuner references narray% using the string value entered in choice. The note is played.

The apostrophe may be used as a shorthand for REM. Here it's used to introduce vertical spacing.

Those variables not explicitly declared as local are global, and are defined when first used.