PLAY is a statement that plays notes of sound through the sound card in QB64 using a command STRING.
- PLAY commandstring$
- The commandstring can be any literal or variable STRING consisting of the following uppercase or lowercase commands:
- On - Sets the current octave (from 0 to 6). Example: PLAY "O3"
- < - Down one octave (cannot be below zero). Example: PLAY "<<" 'goes down two octaves.
- > - Up one octave (cannot be above 6). Example: PLAY ">>" ' goes up two octaves.
- A, B, C, D, E, F or G are the notes in the current octave. Can use the following suffixes:
- + or # for a sharp note. Example: PLAY "C#"}green
- - for a flat note. Example: PLAY "C-"
- Nn - Plays a note n by number(n can be between 0 to 84 in the 7 octaves, where 0 is a rest). Example: PLAY "N42"
- Ln - Sets length of a note (n can be 1 to 64 where 1 is a whole note and 4 is a quarter of a note etc.). Example: PLAY "L4"
- MS - Each note plays 3/4 of length set by L (staccato)
- MN - Each note plays 7/8 of length set by L (normal)
- ML - Each note plays full length set by L (legato)
- Pn - Pause in the duration of n quarternotes (n can be 1 to 64) corresponding to L, Example: PLAY "P32"
- Tn - Tempo sets number of L4 quarter notes per minute (n can be 32 to 255 where 120 is the default). Example: PLAY "T180"
- . - period after a note plays 1½ times the note length determined by L * T.
- .. - two periods plays 1-3/4 times the note length determined by L * T.
- , - commas in QB64 stop play advancement to allow more than one note to be played simultaneously. Example: PLAY "C,E,G,"
- Vn - Volume in QB64 only can be any volume from 0(none) to 100(full). When n is not specified, the default level is 50.
- MF - Play music in the foreground (each note must be completed before another can start).
- MB - Play music in the background (the background buffer can only hold 32 notes in QuickBASIC, QB64 has no limits).
- X + VARPTR$(string-expression) - executes a command string variable. MUST be used with variables!.
Example 1: Plays a sound with the volume and note varying from 0 to 50. Maximum note can only be 84.
PLAY "mll64" DO FOR x = 1 TO 50 a$ = a$ + "v" + LTRIM$(STR$(x)) + "n" + LTRIM$(STR$(x)) NEXT FOR x = 50 TO 1 STEP -1 a$ = a$ + "v" + LTRIM$(STR$(x)) + "n" + LTRIM$(STR$(x)) NEXT PLAY a$ a$ = "" LOOP UNTIL INKEY$ <> "" PLAY "v10l1c,l4egl2o5c,o4l4eg"
Example 2: Plays "Frosty the snowman". The lyric printing is not delayed by PLAY in QB64.
CLS : PRINT "Frosty the Snow Man" FOR X = 1 TO 2 PRINT IF X = 1 THEN PRINT "Fros-ty the Snow man was a jolly happy soul," IF X = 2 THEN PRINT "Fros-ty the Snow man knew the sun was hot that day" PLAY "mb t140o2p4g2e4.f8g4o3c2o2b8o3c8d4c4o2b4a8g2." IF X = 1 THEN PRINT "with a corn cob pipe and a button nose and two eyes made out of coal." IF X = 2 THEN PRINT "so he said Let's run and we'll have some fun now before I melt away." PLAY "o2b8o3c8d4c4o2b4a8a8g8o3c4o2e8e4g8a8g4f4e4f4g2." IF X = 1 THEN PRINT "Fros-ty the Snow Man is a fair-y tale, they say," IF X = 2 THEN PRINT "Down to the vil-lage, with a broom-stick in his hand," PLAY "g2e4.f8g4o3c2o2b8o3c8d4c4o2b4a8g2." IF X = 1 THEN PRINT "He was made of snow but the chil-dren knew how he come to life one day." IF X = 2 THEN PRINT "run-ning here and there all a-round the square, say-in' catch me if you can." PLAY "o2b8o3c8d4c4o2b4a8a8g8o3c4o2e8e4g8a8g4f4e4d4c2." IF X = 1 THEN PRINT "There must have been some magic in that old silk hat they found." IF X = 2 THEN PRINT "He led them down the streets of town right to the traffic cop." PLAY "c4a4a4o3c4c4o2b4a4g4e4f4a4g4f4e2." IF X = 1 THEN PRINT "For when they placed it on his head he be-gan to dance a round." IF X = 2 THEN PRINT "And he on-ly paused a moment when he heard him hol-ler Stop!" PLAY "e8e8d4d4g4g4b4b4o3d4d8o2b8o3d4c4o2b4a4g4p4" IF X = 1 THEN PRINT "Oh, Fros-ty the Snow Man was a-live as he could be," IF X = 2 THEN PRINT "For, Fros-ty the Snow Man had to hur-ry on his way" PLAY "g2g2e4.f8g4o3c2o2b8o3c8d4c4o2b4a8g8g2." IF X = 1 THEN PRINT "and the chil-dren say he could laugh and play just the same as you and me." IF X = 2 THEN PRINT "but he waved good-bye say-in' Don't you cry, I'll be back a-gain some day." PLAY "o2b8o3c8d4c4o2b4a8a8g8o3c4o2e8e4g8a8g4f4e4d4c2.p4" _DELAY 55 'PLAY in QB64 does not delay program NEXT X PRINT : PRINT "Thump-et-y thump thump, thump-et-y thump thump, look at Fros-ty go." PLAY "t180g8g8g4g4g4a8g8g4g4g4a4g4e4g4d1" PRINT "Thump-et-y thump thump, thump-et-y thump thump, ov-er the hills of snow." PLAY "t180g8g8g4g4g4a8g8g4g4g4g8g8g4a4b4o3c2c4p1" END
Example 3: Clicking on the grid enables various notes to be played simultaneously.
DIM SHARED grid(16, 16), grid2(16, 16), cur CONST maxx = 512 CONST maxy = 512 SCREEN _NEWIMAGE(maxx, maxy, 32) _TITLE "MusicGrid" cleargrid DO IF TIMER - t# > 1 / 8 THEN cur = (cur + 1) AND 15: t# = TIMER IF cur <> oldcur THEN figuregrid drawgrid playgrid oldcur = cur END IF domousestuff in$ = INKEY$ IF in$ = "C" OR in$ = "c" THEN cleargrid LOOP UNTIL in$ = CHR$(27) SUB drawgrid scale! = maxx / 16 scale2 = maxx \ 16 - 2 FOR y = 0 TO 15 y1 = y * scale! FOR x = 0 TO 15 x1 = x * scale! c& = _RGB32(grid2(x, y) * 64 + 64, 0, 0) LINE (x1, y1)-(x1 + scale2, y1 + scale2), c&, BF NEXT x NEXT y END SUB SUB figuregrid FOR y = 0 TO 15 FOR x = 0 TO 15 grid2(x, y) = grid(x, y) NEXT x NEXT y FOR y = 1 TO 14 FOR x = 1 TO 14 IF grid(x, y) = 1 AND cur = x THEN grid2(x, y) = 2 IF grid(x - 1, y) = 0 THEN grid2(x - 1, y) = 1 IF grid(x + 1, y) = 0 THEN grid2(x + 1, y) = 1 IF grid(x, y - 1) = 0 THEN grid2(x, y - 1) = 1 IF grid(x, y + 1) = 0 THEN grid2(x, y + 1) = 1 END IF NEXT x NEXT y END SUB SUB domousestuff DO WHILE _MOUSEINPUT IF _MOUSEBUTTON(1) THEN x = _MOUSEX \ (maxx \ 16) y = _MOUSEY \ (maxy \ 16) grid(x, y) = 1 - grid(x, y) END IF LOOP END SUB SUB playgrid n$ = "L16 " 'scale$ = "O1CO1DO1EO1FO1GO1AO1BO2CO2DO2EO2FO2GO2AO2BO3CO3D" scale$ = "o1fo1go1ao2co2do2fo2go2ao3co3do3fo3go3ao4co4do4fo" FOR y = 15 TO 0 STEP -1 IF grid(cur, y) = 1 THEN note$ = MID$(scale$, 1 + (15 - y) * 3, 3) n$ = n$ + note$ + "," 'comma plays 2 or more column notes simultaneously END IF NEXT y n$ = LEFT$(n$, LEN(n$) - 1) PLAY n$ END SUB SUB cleargrid FOR y = 0 TO 15 FOR x = 0 TO 15 grid(x, y) = 0 NEXT x NEXT y END SUB