BBC Micro Music Masterclass
Music incorporates a diverse range of signs and symbols and, unhelpfully enough, you will see that the way in which they are used is often incon-sistent with theoretically immutable rules. Using a computer to display these sharps, flats, clefs and quavers brings with it a variety of problems which we will have to learn to overcome. In this chapter we shall meet these problems head-on and develop the necessary techniques required to dismantle and solve them.
As is the case with many graphics applications there are two basic approaches to displaying music. The first is to use the PLOT, MOVE and DRAW commands to draw the symbols, etc., onto the graphics screen. This method has the advantage of flexibility in terms of object size and the ease of drawing straight lines. The disadvantage of this method is that it is slow. The second approach lies in the use of user-defined characters PRINTed on the graphics screen. This method is faster than the graphics technique and is very detailed; its main disadvantage centres on the size restrictions which are implicit in its use.
In practice I have found that it is best to use a mixture of the two techniques, with DRAW to display the stave (and possibly the clefs), and employing user-defined characters to PRINT notes, sharps and flats. Let us embark on this topic by taking a look at graphics techniques utilising MOVE and DRAW.
Before attempting to draw notes or clefs it is necessary to display the traditional group of five lines known as the stave. To do this we need to use the two graphics commands MOVE and DRAW.
MOVE moves the graphics cursor from the initial position to co-ordinates X, Y without drawing on the screen. The command takes the form: MOVE X, Y.
DRAW draws a line from the previous graphics cursor position to a new position given by the co-ordinates A, B. The command takes the form: DRAW A,B.
design is completed pressing ‘Q’ causes the VDU 23 statement to be PRINTed. At this stage you may continue adding to the design, reset to a blank square (press ‘R’) or end the program (press ‘E’). It is important to exit the program using ‘E’ rather than
The program itself uses two VDU 23 statements. One is necessary to produce a blank square (all zeros) and one produces a completely filled square (all 255). The call *FX 4,1 in Line 80 turns off cursor editing and allows the cursor keys to be used to control horizontal and vertical movement within the square. Lines 220 to 230 draw the red square. Lines 270 to 300 control position within the square and create a boundary. Lines 310 and 320 cause either a filled or blank square to be PRINTed at position X,Y. This fact is then recorded as a 1 or zero in the array HEX%(I,S). Finally, PROChex works out the VDU 23 values and PRINTs them on the screen.
Once a character has been created the next problem which presents itself is how to control its position on the screen. One useful aid to this end is the alternative graphics planning sheet explained on page 494 of the User Guide. This grid relates character positions to the graphics dimensions. A sensible arrangement for positioning the lines of the stave would be on five of the horizontal planning sheet lines. This would correspond to one text character in height, or 32 graphics divisions apart, as we indeed did to draw staves earlier.
Once text characters for notes have been created their position on the stave can be controlled by adding 16 for the next note position
The following example uses the above techniques to draw a piano-type double stave on the screen. Notes can then be selected using the notes’ alphabetical name. (We could have equally well used a keyboard arrange-ment for this purpose.) The octave of the note is controlled by pressing ‘O’. The sequence of notes can then be played back by pressing ‘P’.
10 20 30 REM"****WRITE**** 40 50 60 VDU23,224,3,3,3,3,3,3,3,3
Lines 60 to 200 are devoted to defining the user-programmable characters. The two clefs and the crotchet shape are constructed in this way. In the set-up part of the program the VDU 28 command sets up a text window to contain the octave and tempo information. The array P% contains Pitch DATA which is READ into the array at Line 380.
The main part of the program consists of a REPEAT... UNTIL loop containing a GET$ statement. The GET$ line waits for various INPUTS on which to act. “O” causes the O% variable to increase and so changes the octave of the INPUT Pitch. “T” acts in the same way on tempo. The smaller the Tempo% variable the faster the tempo. “P” causes the program to go to PROCplay and play the notes INPUT so far. If the INPUT does not consist of “O”, “T” or “P” the program compares the contents of A$ with the components of Note$, the alphabetical note names. If A$ is contained in Note$, a value of Note%>0 will be returned and the program will exit the inside loop and go to PROCnote. This draws the note as a crotchet on the screen and records the pitch value in Pote%(100). The x co-ordinate screen position is increased by 100 at Line 550 and end of screen is tested for in Line 560.
The PROClines routine draws the stave lines just as before, but incorporates a modification which makes it possible to draw a double stave. The lines then appear on the screen at Y co-ordinates 896, 864, 832, 800, 768 and 704, 672, 640, 608, 576. VDU 5 turns on PRINTing at the graphics cursor and VDU 4 turns it off. These commands have been included at the beginning and end of each PRocedure to facilitate the use of the entire PROC in your own programs. GCOL 0,3 selects white as the colour of the lines.
PROCclef uses the user-defined character method of PRINTing the treble and bass clef symbols. The top left-hand point of the character is positioned at the cursor. The character therefore appears below the line it is MOVEd to. GCOL 0,2 selects yellow.
PROCnewline is called up whenever the current stave is full. It simply erases the current screen and displays an empty stave in its place.
PROCNote converts the P% pitch and O% octave information into a note value Pote% which is stored in a 100 note array. T% is the number of pitches currently in the array.
PROCoct sets up the text display in the text window. COLOUR 1 selects red.
PROCplay plays all the notes currently stored in array Pote% at a tempo determined by Tempo%.
When PROCrite is called, this procedure draws a crotchet in yellow on the correct line or space of the stave. The lowest note possible in this program is F, on the space below the bass clef. This would require the top left-hand corner of the crotchet ball to be placed on the bottom line, 576. The Note% value for F is 1. The octave O% value would be zero, so the Y co-ordinate value can be calculated as:
Island Logic for which I have been writing tunes.
My own solution to the expansion of ‘Write’ into a program of greater flexibility is given below. Digest, absorb and work with it, and then transpose it into your own ideal.
ADVANCED MUSIC WRITER
The Advanced Music Writer lets the user write melodies on the screen using standard musical nomenclature. Letter keys select the notes displayed and if an error is made it is a simple matter to delete the mistake and try again. The filing system allows you to save your finished works on tape or disc and makes it possible to build a library of your favourite tunes or compositions.
On RUNning the program the screen display is as follows:
Octave: 0 :'O' Press 'S' for Sharp Tempo : 4 :'T' Press 'D' to Delete Duration: 4 'L' Press 'P’ to Play 'Z'..Transpose Pressto Stop To Reset 'R'...Read File Press <BREAK> 'W' .. Write File Current File: IRISH
Most of the extra facilities can be seen, displayed beneath the stave. Octave and Tempo values are changed in an identical fashion to the “Write” program. If ‘0’ is pressed the Octave value changes, in the range 0 to 4, with zero being the lowest octave and 4 the highest.
Tempo has a larger range of possible values with 1 being the fastest tempo and larger values progressively slower. Duration is a new addition. This value allows you to change the length of note displayed on the stave.
The program uses PROCload and PROCplay from “Advanced Music Writer”. A small Menu program using a GETS statement is set up to allow the choice of reading a new file, playing the current file or changing tempo. The program repeats melodies indefinitely or until <ESCAPE> is pressed to return control to the menu.
On RUNning you are immediately displayed the only page of the pro-gram. Your first action must be to press ‘R’, for Read, to load a file. Once loaded you may play it back by typing ‘P’ and stop the tune by pressing <ESCAPE>. The only other control is for varying the tempo, which is identical to that of the “Advanced Music Writer”.
Music graphics need not be entirely practical, of course. If you look forward to the interfacing chapter, you will see that I have included a program which draws graphs of sounds entering through the Beeb’s analogue interface. The following program PRINTs coloured bars on the screen at a position related to the pitch of notes played on the keyboard.
230 *FX 12,0 240 VDU4 250 PRINT "THE END"
This array, which should be familiar to you by now, is used to hold the active key information. In this example, the top two lines of letters correspond to the white and black notes. VDU 5 makes it possible to PRINT at the text cursor and Lines 130 and 140 set the key auto repeat. Line 160 tests for a keypress using INKEY$(4) and this value is compared with P$ in Line 170. If a valid key has been pressed a coloured bar is PLOTted on the screen at a Y co-ordinate position decided by N% and PROCNOTE is called with a positive value for N%. If there is no valid keypress nothing is drawn on the screen and PROCNOTE is called with a zero value for N%.
PROCNOTE plays all three SOUND channels with a value of one for the flush parameter of the extended SOUND statement. This means that the queue for each channel is constantly being flushed. If N%=0 then SOUND statements with zero amplitudes are called, which causes immediate silencing of the tone generators. If N%>0 the three tone generators play notes of a pitch calculated as N% * 4 + 28. To exit the program <ESCAPE> can be pressed. This causes an error 17, which sends the program to Line 230. The keyboard auto-repeat is then set to default, normal cursor control is returned and the program ends.