Random music generator (BBC BASIC)

discussion of beeb/electron applications, languages, utils and educational s/w
Post Reply
User avatar
lurkio
Posts: 2147
Joined: Tue Apr 09, 2013 11:30 pm
Location: Doomawangara
Contact:

Random music generator (BBC BASIC)

Post by lurkio » Fri Mar 22, 2019 11:33 pm

Just thought I'd share this BBC BASIC program from the August 1986 issue of Your Computer magazine. Here's the intro from the mag:
Someone credited as “P. Bender” wrote:This program will produce patterns of random music on BBC and Electron computers. When the program is run, a choice of three scales -- Scottish, Modal and Eastern -- is displayed. The scale chosen will determine which notes are used in the melody which is produced by a step of one or two notes in either direction while moving slowly from one end of the scale to the other.

The notes are stored as strings of characters set up by a PROCscale; should the end of such a string be reached, the direction is reversed and the melody continues.

There is also a choice of two rhythm patterns, the data in lines 390 and 400 containing common note patterns for bars of 4/4 and 3/4 respectively.

Line 20 tests to see if the machine is an Electron so that the appropriate changes can be made to compensate for the single sound channel available.

All the parameters may be changed to vary the beat, phrase -- the musical equivalent of a sentence -- and the pitch of sounds, so the program could be used to provide backing music for a program of your own.

Run the code in JSBeeb:

Here's the listing:

Code: Select all

 10REM * MUZIK * (C) PHILSOFT 1986
 15REM typed in by lurkio @ Stardot, 2019
 20IFINKEY-256<>0ANDINKEY-256<>-1 THEN el%=1 ELSE el%=0
 30PROCsetup
 40REPEAT
 50PROCdur
 60PROCpitch
 70IFch%THENPROCchord ELSE SOUND1,-15,P%,D%*tempo%
 80UNTILINKEY(-17)
 90*FX15,0
100RUN
110 
120DEFPROCpitch
130REPEAT:I%=RND(3)-RND(3):UNTILI%<>0
140IF Z%+I%<1 OR Z%+I%>LEN s$ THEN I%=-I%
150Z%=Z%+I%
160P%=ASC(MID$(s$,Z%,1))
170ENDPROC
180 
190DEFPROCdur
200ch%=0
210IF d$="" THEN d$=note$(RND(4)):ch%=1:ph%=ph%+1
220IF ph%=pl% THEN d$=note$(RND(2)+3):ph%=0
230IF el% THEN ch%=0
240D%=VAL LEFT$(d$,1)
250d$=RIGHT$(d$,LEN d$-1)
260ENDPROC
270 
280DEFPROCsetup
290DIM note$(5)
300d$="":s$="":ch1$="":ch2$=""
310CLS
320PRINT:PRINT"Press Q to stop":PRINT
330*FX15,0
340INPUT"1-Scottish  2-Modal  3-Eastern ",X:pl%=RND(-TIME):IFX=0X=RND(3):PRINTSTRING$(8,CHR$127);X
350IF X>0 AND X<4 THEN PROCscale(X) ELSE 340
360INPUT"Enter 3 for 3/4, 4 for 4/4     ",X:IFX=0X=2+RND(2):PRINTSTRING$(8,CHR$127);X
370IF X=3 THEN RESTORE 400 ELSE RESTORE 390
380FORJ%=1TO5:READ note$(J%):NEXT
390DATA1111,211,22,31,4
400DATA21,111,21,3,3
410INPUT"Please enter a tempo           ",tempo%:IFtempo%=0tempo%=5:PRINTSTRING$(8,CHR$127);tempo%
420INPUT"Please enter phrase length     ",pl%:IFpl%=0pl%=10:PRINTSTRING$(8,CHR$127);pl%
430Z%=RND(LEN s$):ph%=0
440ENDPROC
450 
460DEFPROCscale(X)
470IF X=1 THEN RESTORE 540 ELSEIF X=2 THEN RESTORE 550 ELSE RESTORE 560
480READ N:FORJ=1TON
490READ sc%,ch1%,ch2%
500s$=s$+CHR$sc%
510ch1$=ch1$+CHR$ch1%
520ch2$=ch2$+CHR$ch2%
530NEXT
540DATA11,32,20,4,40,24,4,52,32,20,60,48,32,68,52,32,80,60,48,88,72,52,100,80,68,108,96,80,116,100,80,128,108,96
550DATA13,40,20,4,48,32,20,52,32,20,60,40,24,68,52,32,72,60,40,80,68,48,88,68,52,96,80,68,100,80,68,108,88,72,116,100,88,120,108,88
560DATA15,32,12,0,36,16,0,48,32,12,52,32,16,60,48,32,64,52,32,76,60,52,80,60,48,84,64,48,96,80,60,100,80,64,108,96,80,112,100,80,124,108,100,128,108,96
570ENDPROC
580 
590DEFPROCchord
600ch1%=ASC MID$(ch1$,Z%,1)
610ch2%=ASC MID$(ch2$,Z%,1)
620SOUND&0201,-15,P%,D%*tempo%
630SOUND&0202,-10,ch1%,D%*tempo%
640SOUND&0203,-10,ch2%,D%*tempo%
650ENDPROC
Btw, line 390 as printed in the mag looks a bit weird:

IMG_4101-.jpg

Have I made the right correction?

:?:
Last edited by lurkio on Sat Mar 23, 2019 1:41 am, edited 2 times in total.

User avatar
jgharston
Posts: 3593
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: Random music generator (BBC BASIC)

Post by jgharston » Sat Mar 23, 2019 4:04 am

Looks like line 20 is wrong as well. It should be:
20 IF INKEY-256=1 THEN el%=1 ELSE el%=0

otherwise it is saying that everything that isn't a Beeb is an Electron, instead of saying that everything that is an Electron is an Electron.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

Post Reply