As we have seen, the nature of sound is quite an intricate and complex thing. Synthesisers do exist which can recreate and duplicate instrumental and natural sounds perfectly, but these cost several thousands of pounds. All of them, as you would expect, are computer based, but produce their sounds in a slightly different way to the BBC sound chip. Nevertheless, the BBC micro offers exceptional control over its sound system and we shall see in this chapter just exactly what can be done with it.
The waveform and timbre of the sound generator's output is fixed and set: there is nothing we can do to alter it under normal conditions. We can, however, alter the other three attributes of sound, namely pitch, volume and duration, and it is by controlling these parameters that we create our effects.
The range of the sound generator extends over five octaves as we saw in the last chapter. We can not merely program the normal notes of the western scale but we can, should we feel inclined, experiment with quarter tone scales and other tonal systems.
The western scale divides the octave into 12 equal pitches which we call semitones. Experiments have been carried out on other octave divisions and music has been written for scales consisting of 13, 14, 15, l6 parts, etc. Such tunings, which divide an octave into other than 12 parts, are known as 'microtonal'. If handled correctly, they can produce fascinating results and provide a relatively undeveloped area of musical research for the experimenter.
Such tunings need even greater control over the sound source than we have with the BBC micro, but you can experiment with 16 and 24 part tunings and even devise your own scales. I'll leave these ideas with the more capable adventurers.
The subdivision of a semitone into four enables us to program portamento effects as we have already seen. This is not possible on all micros. It also allows us to 'bend' notes in a way similar to a guitar player, as this program demonstrates.
10 REM Hawaiian Guitar
20 ENVELOPE1,132,0,0,0,0,0,0,126,-10,0,-4,126,100
30 ENVELOPE2,132,-8,0,1,1,0,8,126,-10,0,-4,126,100
40 FOR Note=1 TO 8
50 READ Env,Pitch,Dur
60 SOUND 1,Env,Pitch,Dur
70 NEXT Note
80 END
90 DATA1,53 ,5,1,41,10,1,53,10,2,73,15
100 DATA1,41,5,1,33,10, 1,53,10,2,69,20
The principle involved is simply one of switching envelopes: when you consider that you can create 16 separate envelopes, you begin to see the versatility of this idea. If your imagination really runs riot you can create even more by redefining envelopes while a program is running. For most purposes, 16 envelopes will probably be sufficient.
In previous chapters we have mentioned envelopes only in relation to volume and loudness contours but, as you can see and hear from the program, the ENVELOPE command can also alter the pitch of a note. This principle will be familiar to most synthesiser owners and players, who are used to controlling pitch and timbre with the envelope generator. The ENVELOPE command performs similar functions but it doesn't operate in quite the same way.
The lowest note produced by the sound generator is the B 13 semitones below middle C. The highest note is the D just over four octaves above middle C. This arrangement is a little one-sided, catering more for the piccolo player than the tuba player, and, in any event, most music tends to be written around the centre of the musical spectrum. A lot of music has a wide range, some even more than the five octaves the sound chip can produce, and a lot will have a good balance of high and low notes. Before programming such tunes, they will have to be arranged or transposed.
You will find the arrangement in Figure 2.4 more convenient when tackling such programs, as it provides an instant transposition which tends towards the range of notes you are most likely to find. If a piece of music contains notes outside this range, or which are predominantly high or low, it is an easy matter to transpose the notes another octave. For this purpose, if you wish, you may photocopy the diagram. If you cut between the keyboard and the stave, you can position the keyboard, which_ covers the range of the sound chip, beneath the stave in such a Chapter 3 The BBC Micro and Sound and Music chip you want to use. It is always a good idea to check the range of notes in the music before commencing programming.
There is, however, an easier and more convenient way. If a tune is entered exactly in accord with Figure 2.4 and you want to play it an octave lower, you can deduct 48 from each pitch value, either during calculation of the pitch or when the pitch is sent to the SOUND command. Most of the programs in this book use a variable called Key, which can be set at the start of the program to make such alterations.
This is such an easy and simple way around the problem it is almost like cheating, but the facility is there for us to use, and use it we should.
For the purpose of programming, these can be considered together. This is the real domain of the ENVELOPE command and, for synthesiser users, the ENVELOPE control of the ADSR functions should be easy to grasp.
ADSR stands for Attack, Decay, Sustain and Release and refers to the change in volume of a note during its production. The ADSR section of the ENVELOPE command is very comprehensive and allows us to imitate the volume characteristics of many musical instruments.
The ENVELOPE command is at the heart of most of the effects we create on the BBC micro and deserves a section to itself. In Chapter 5, we will investigate the natural envelopes produced by various instruments and look at ways of making easy the creation of the envelope parameters. We will also look at ways of relating the numbers to the sounds and effects they produce.
Early models of the BBC micro emit a continuous buzz from the speaker which changes pitch as a program is being executed. Acorn have said that this is due to the sound being generated in a digital fashion, but it seems more likely that it is caused by interference reaching the audio circuits. Whatever the reason, it can be annoying.
Acorn recommend a modification which involves connecting a }OK resistor between pin 8 on IC20 and ground, which is a small hole just below pin 8. IC20 can be found on the main board near the front of the computer underneath the keyboard. Figure 3.1 illustrates the modification.
This should only be attempted by someone competent in electronics, and it will probably be taken as invalidating your guarantee. Your dealer will be able to fit it for you, probably without invalidating your guarantee, at suitable cost.
An alternative solution is to wire the resistor between pins 15 and 16 an the 1 MHz expansion bus The easiest way to do this is to solder it to a socket and fit the socket to the plug under the computer. This will not invalidate your guarantee. You need an IDC (Insulation Displacement Connector) Speedblock type, female header socket 2*17 pin.
The modification is not a 100 per cent cure, but it is certainly worth performing.
Figure 3.1
Although the two-inch speaker produces a better sound than that of most micros, it can seem quiet at times and, because it is a small speaker, some of the range and depth of the sound quality will be lost.
As a first step, there is a small preset volume control close to the loudspeaker connector under the keyboard and this can often be turned up a little. This will help in situations where it is not possible to connect another speaker but, in order to appreciate the full range of sounds available from the BBC micro, we really do need to feed the output to a larger speaker.
The audio amplifier in the BBC micro is an LM386 and it is quite capable of putting out half a watt through an 8-ohm speaker. The following modification, again, will probably be taken as invalidating your guarantee and should be carried out with care. The procedure is a very simple and straightforward one, but should not be attempted by anyone who cannot handle a soldering iron.
The simplest method is to disconnect the wires running to the internal speaker and connect them to an external speaker. If we are going to delve inside the machine, it makes more sense to adapt the system so that we can select either the internal or the external speaker. We can do this by using a socket which disconnects the internal speaker when a plug is inserted (and connects it when no plug is inserted). This socket can be mounted in the back of the computer where the reset button used to be. The reset function was replaced by the BREAK key but on early models the hole will still be there. Newer models have no reset hole: you can either use the econet opening or else put a hole in the case or rear panel at another point. Some owners have mounted the socket hole in the case at another point. Some owners have mounted the socket on the side of the case beside the speaker. Wherever you mount the plug, take special care or get a qualified engineer to do the job.
The speaker is connected to the main board with two wires which run into plug PL15. You need to remove the case and the keyboard PCB to get to it; the plug can be removed, the adaptions made and the plug then refitted. The connections required are shown in Figure 3.2. The wires will normally be red and black and, if the external output seems rather quiet, reverse the leads.
Figure 3.2
This is certainly a modification worth doing. The increase in volume and quality will more than repay the small effort required to perform it.
There is yet another alternative, which consists of routing the sound through the UHF modulator into your television set. Unless you are an accomplished electronics constructor, it will be simpler and safer to buy a ready-made unit from a manufacturer. They cost in the region of £10 and some can be fitted without soldering. The sound is undoubtedly an improvement, but is subject to the constraints of the modulator and TV set.
If you have a monitor, or use a TV set which has been adapted for RGE input, then this will not work. Many monitors and adapted TVs are fitted with a separate audio input and, if you are considering the purchase of such a unit, it may be a good idea to check its sound potential. It is, after all, a major part of your computer.
Hopefully, the newer models of the BBC micro will no longer have the speaker buzz, although it is unlikely that they will include an extension speaker socket.
Dirty mains and mains fluctuations are problems which can affect the whole computer. A severe glitch in the mains power supply can cause the computer to crash completely. The severity of such fluctuations varies with location and time of use, etc. The BBC micro is very tolerant of such things and it is extremely unlikely to succumb and crash completely but fluctuations can produce a hiss from the speaker. If this worries you enough you can consider the purchase of a filter plug which 'cleans up the mains'. These retail at around £15.
The command:
*FX210,1
will turn off the sound generator output and:
*FX210,0
will turn it on again. Unfortunately, this does not turn off the buzz.
Standard music notation allows us to communicate our musical ideas to anyone who speaks the same language. As this is known and accepted world-wide, we rarely have any problems apart from matters of artistic interpretation. The micro has not yet been schooled in music which leaves us to devise our own methods of communication. This can be quite an exciting and challenging task but, as there are no industry standards, our methods are only likely to be of use to ourselves. Such considerations need not worry us unduly so we will consider some ways of talking music to the BBC micro.
An interesting place to start is to see how other micro manufacturers have approached the problem. Some require figures to be POKEd into certain memory locations. Others use ideas similar to those used in the BBC micro with variations of the SOUND command and some even allow the user to specify a particular note by name and octave number. Not all can synchronize four channels of sound; some of those which can, require the use of machine code.
The lesson to be learned here is that no personal micro manufacturer has yet devised a good BASIC music programming language. BASIC is just not a very suitable language for the programming of music. The more expensive, dedicated music micro manufacturers tend to develop their own MCL (Music Composition Language).
So where does that leave us? Really, we're free to design a system to suit our own ideas, unhampered by precedence. Needs may vary and we can design different systems to suit different applications. Throughout the book, though, we will normally assume that we want to enter notes from conventional notation and play them through the computer. There are several methods of turning the notes into numbers.
This is the method used by the SOUND command: it is simply a list of numbers which correspond to a particular pitch, eg 53=middle C. This is fine, but if you want to enter a lot of notes it is easy to lose your place; and if you subsequently want to edit the tune, you have few clues as to what sound a particular sequence of numbers makes. Computers like numbers, but most humans prefer something a little less abstract.
It is worth bearing in mind that, if you only require a fanfare or other short tune, this is the method to choose. A few dozen notes will not usually cause severe problems in entering or editing and this is the method used in the previous examples in this book.
These are all variations on a theme. The idea is that we input notes in a form we find easy to understand and let the computer convert them to numbers it can understand. The variations lie in the ways in which we initially code the notes. Preference will vary from person to person.
There already exist several forms of notation for specifying notes without using a stave. Most consist of the note name followed by a number to represent the octave such as C1, B5, F#3, etc. Other systems employ a set of apostrophe marks (') to indicate the octave such as C', A'', D#''', etc or D'''#, G''#. Some systems place the lines horizontally, directly over the note, some use roman numerals to indicate the octave and some count in semitones so that C4 would be followed by C#5, D6, D#7, etc.
I think the easiest to understand, to enter and to edit, and which produces the least number of mistakes en route, is the octave number version. You may disagree, so feel free to adapt the system to your own needs. Although the principle behind the octave number systems are the same, they may vary according to which octave the numbers represent.
On page 181 of the User Guide is an octave number system which puts middle C, pitch value 53, in octave 3. The only note in octave 1 is B - not counting the A# for reasons we shall discuss later - which seems like a waste of an octave. Other numbering systems count middle C as C4 and work up and down from there, some use a superscript and subscript system which designates middle C simply as C, the octaves above C as C1, C2, etc and the octaves below as C1, C2 etc. The system described below can be adapted to any other notation system you feel more comfortable with.
Figure 3.3
OCTAVE NUMBER | |||||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | |
C | 5 | 53 | 101 | 149 | 197 | 245 | |
C# | 9 | 57 | 105 | 153 | 201 | 249 | |
D | 13 | 61 | 109 | 157 | 205 | 253 | |
D# | 17 | 65 | 113 | 161 | 209 | ||
E | 21 | 69 | 117 | 165 | 213 | ||
F | 25 | 73 | 121 | 169 | 217 | ||
F# | 29 | 77 | 125 | 173 | 221 | ||
G | 33 | 81 | 129 | 177 | 225 | ||
G# | 37 | 85 | 133 | 181 | 229 | ||
A | 41 | 89 | 137 | 185 | 233 | ||
A# | 45 | 93 | 141 | 189 | 237 | ||
B | 1 | 49 | 97 | 145 | 193 | 241 |
This is based upon the notation illustrated in Figure 3.3 and Figure 2.4. Octave 1 begins with the lowest available C which has a pitch value of 5. It is usually musically convenient to begin an octave with a C. The lower tones of the sound chip are richer than the upper ones and are generally more pleasing: I tend to find myself working more with these lower tones, especially for 'orchestral' pieces. Using the method described above in relation to Figure 2.4 you can, of course, use any range you wish.
10 REM PROGRAM 3.1
20 REM Basic Program to Convert
30 REM Note Names & Octave Numbers
40 REM Into PITCH values
50
60 PROCSetup
70 FOR Count=1 TO 16
80 PROCChooseNote
90 PROCPlayNote
100 NEXT Count
110 END
120
130 DEF PROCSetup
140 Scale$=" C C# D D# E F F# G
G# A A# B"
150
160 REM Key Sets Value for Transposing
170 REM Key=1 Will Play as You Would
180 REM Expect. Key=5 Will Play
190 REM 1 Semitone Up. Key=9 will
200 REM Play 1 Tone Up etc.
210
220 Key=1
230
240 DIM NotesToChooseFrom$(15)
250 FOR S%=1 TO 15
260 READ Note$
270 NotesToChooseFrom$(S%)=Note$
280 NEXT S%
290 ENDPROC
300
310 DATAG1,A0,B0,C2,D2,E2,F#2,G2,A2,B2
,C3,D3,E3,F#3,G3
320
330 DEF PROCChooseNote
340 Note=RND(15)
350 Note$=NotesToChooseFrom$(Note)
360 ENDPROC
370
380 DEF PROCPlayNote
390 PROCAnalyseNote
400 PROCCalculatePitch
410 PRINT Note$,Pitch,Octave
420 SOUND1,-15,Pitch,5:SOUND1,0,0,0
430 ENDPROC
440
450 DEF PROCAnalyseNote
460 IF LEN(Note$)<2 OR LEN(Note$)>3 TH
EN PRINT"ERROR IN DATA ";Note$:STOP
470 IF LEN(Note$)=2 THEN NoteName$=LEF
T$(Note$,1) ELSE NoteName$=LEFT$(Note$,2
)
480 PositionInScale=INSTR(Scale$,NoteN
ame$)/3
490 Octave=VAL(RIGHT$(Note$,1))
500 ENDPROC
510
520 DEF PROCCalculatePitch
530 Pitch=Key+PositionInScale*4+(Octav
e-1)*48
540 IF Pitch<0 OR Pitch>255 THEN PRINT
"ERROR IN DATA ";Note$:STOP
550 ENDPROC
As a demonstration, the program plays a series of 16 notes chosen at random from the 15 notes in the DATA statement at line 310. The notes are allocated to the array, NotesToChooseFrom$.
Line 140 sets up Scale$ which contains the notes of the scale. The spaces are important. It is worth pointing out that the black notes are all written in sharps. It is generally not a problem for us to enter notes in this way, although if the music contains flats you will have to do a mental conversion to sharps. Figure 2.4 will help. Also, the keyboard has a ready-made sharp sign (#) the use of which avoids any possible confusion between the flat sign and the letter b. The program can be adapted to cater for the input of both sharps and flats if this suits you better.
The use of the variable, Key, at fine 220 is optional but makes any transposition easy. As mentioned earlier, setting Key to -47 (ie 1-48) will play the tune an octave lower.
The note name is analysed at fine 450. As each note will be either two or three letters long, fine 460 checks for errors in the DATA.
Line 470 separates the note name from the octave number and 480 determines its position in the scale. The higher its position, the higher the pitch.
Line 490 determines the octave and converts it into a numeral, and 530 calculates the pitch.
Line 540 is there in case something has gone wrong.
The task of calculating the pitch has been divided into two procedures, PROCAnalyseNote and PROCCalculatePitch. These could be combined into one procedure or turned into a function so that FNPitch(Note$) would return the pitch value to be used in place of the pitch variable in line 420. This will be the optimum solution in many cases but, by splitting it, it allows us to calculate note names and octaves separately. We may want to do this if the computer is following a set of rules, for example during composition.
The second SOUND command in line 420 produces a silence of one two-hundredths of a second which is used to separate the notes. This is produced by a duration value of 0 and is useful for separating notes in simple arrangements.
The octave numbering system is one lower than that in the User Guide. It seems to make more sense to call the lowest octave, and one Chapter 3 The BBC Micro and Sound and Music writing Cl, D1, etc to C0, D0 and it still allows a pitch value of 1 which is B0. This means we need to deduct 1 from the octave number to produce the correct pitch, which we do in line 530. As the computer is doing the calculating work, once we have the conversion program all we have to do is enter the DATA.
There is an error in the DATA statement in line 310 which shows how one of the error routines operates. The DATA also illustrates how the system caters for B0.
We will use this method of note conversion, and variations on it, throughout the book.
The User Guide states that a pitch value of 0 produces an A note. This is not true as this program shows.
10 FOR Pitch=13 TO 1 STEP -4
20 SOUND1,-15,Pitch,8
30 NEXT Pitch
40 SOUND,-15,0,8
You will hear that the difference between pitch values I and 0 is not as great as that between pitch values separated by a factor of four. It produces the same interval as that between any two adjacent pitch values, ie a quarter of a semitone, which is what you would expect.
From the above, it would therefore appear more sensible to use a pitch value of 0 as the lowest note. This would also be more in keeping with the true pitches produced by the sound chip: Chapter 6 gives a detailed listing of its actual output frequencies. As the official pitch values and note names are fairly well known, it would probably only serve to confuse if we were to start using other pitch names and values. In practice this will make little difference and enough information is given throughout the book for you to make any adjustments necessary for your own purposes.