SOUND programming

From BeebWiki
Jump to: navigation, search

The SOUND command is used to make the computer generate sounds using the internal loudspeaker or external hardware. The internal sound generator on the BBC series is capable of making four sounds at once. Each of the four sound channels can generate one note.

The keyword SOUND must be followed by four numbers which specify

  • which sound channel is to be used
  • the loudness of the note (or the envelope number)
  • the pitch of the note
  • how long the note is to last

For example:

SOUND 1,-15,101,20

will play a note on sound channel 1, with a loudness of -15 (maximum volume). A pitch value of 101 gives middle C and a duration of 20 will make the note last for 1 second.

SOUND C,A,P,D

The channel number (C) can be 0 to 15, but the BBC series only support 0 to 3 and the unexpanded Electron only supports channel 1. The channel number can include other control information. Channel numbers &2000 and higher are reserved for other sound functions.

Channel 0 is a special channel that can produce various noises, whereas channels 1, 2 and 3 are used to produce single notes. Other values of C (the channel number) produce special effects which are explained further on.

The amplitude or loudness (A) can have any whole number value between -15 and 16. Values -15 to 0 produce notes of fixed loudness throughout the whole note. A value of -15 is the loudest, -7 is half volume and 0 produces silence. Values of 1 to 16 enable the amplitude and pitch to be controlled while the note is playing. When you play a note on the piano the sound gradually fades away. Effects like this are selected by using one of the 16 user-defined envelopes which are selected by setting A to be 1 to 16.

Normally only envelopes 1 to 4 are available. On the BBC and Electron envelopes 5 to 16 share memory with the serial output buffer, so cannot be used at the same time as serial output.

The pitch (P) is used to set the pitch or frequency of the note. The pitch can have any value between 0 and 255. The note A above below C is selected with a value of 89. The table on page 181 shows the value of P needed to produce a particular note. You will see that to go up an octave P is increased by 48 and to go up a perfect 5th P must be increased by 28.

Increasing the value of P by one will increase the note produced by a quarter of a semi-tone.

To play the chord of C major which consists of the notes C, E and G for 2 seconds you could enter

100 SOUND 1,-15,53,40

110 SOUND 2,-15,69,40

120 SOUND 3,-15,81,40

Whereas to play a number of notes in succession you would enter

100 SOUND l,-15,97,10

110 SOUND 1,-15,105,10

120 SOUND 1,-15,89,10

130 SOUND 1,-15,41,10

140 SOUND 1,-15,69,20

which plays a well-known film theme.

The duration (D) can have any value between -1 and 254. Values in the range 0 to 254 give a note duration of that number of twentieths of a second. Thus if D=40 the note will last for 2 seconds. Setting D=-1 means that the note will continue to sound until you actually take steps to stop it. You can either press the ESCAPE key or stop it by sending another note, to the same channel, which has "Flush Control" set to 1.

As was mentioned earlier in this section, channel number 0 produces "noises" rather than notes and the value of P in the statement

SOUND 0,A,P,D

has a different effect from that described for channels 1, 2 and 3. Here is a summary of the effects of different values of P on the noise channel:

P / Effect

0 / High frequency periodic noise

1 / Medium frequency periodic noise

2 / Low frequency periodic noise

3 / Periodic noise of frequency determined by the pitch setting of channel 1

4 / High frequency "white" noise

5 / Medium frequcy "white" noise

6 / Low frequency "white" noise

7 / Noise of frequency determined (continuously) by the pitch setting of channel 1

Values of P between 0 and 3 produce a rather rasping, harsh note. With P set to 4 the noise is not unlike that produced by a radio when it is not tuned to a station - sort of "shssh" effect. P=6 sounds like the interference found on bad telephone call.

When P is set to 3 or 7 then the frequency of the noise is controlled by the pitch setting of sound channel number 1. If the pitch of channel 1 is changed while channel 0 is generating noise then the pitch of the noise will also change. The program below generates a noise on channel 0 and varies the pitch of the noise by changing the pitch of channel 1. Notice that the amplitude of channel one is very low (-1) so you will hardly hear it - but you will hear the noise on channel 0.

100 SOUND 0,-15,7,150

110 FOR P= 100 TO 250

120 SOUND 1,-1,P,1

130 NEXT P

Notice that we have not yet described how sounds can be affected by a superimposed envelope. An envelope can affect both the pitch and amplitude of a note as it is playing. Thus the statement

SOUND 1,-15,255,255

merely plays a continuous loud note, whereas

ENVELOPE 1,1,-26,-36,-45,255,255,255,127,0,0,-127,126,0

SOUND 1,1,255,255

produces a complex sound controlled largely by the envelope.

See the keyword ENVELOPE for more details.

As mentioned briefly at the start of the description of the SOUND statement, the channel number, C can be given values other than 0, 1, 2 and 3. You need not understand exactly why the following works to use it!

For C one can write a four figure hexadecimal number to achieve certain effects - for example:

SOUND &1213,-15,53,40

The first parameter in the above example has the value &1213. The ampersand (&) indicates to the computer that the number is to be treated as a hexadecimal number. The four figures which follow the ampersand each control one feature. In this new expanded form the SOUND statement looks like

SOUND &HSFC,A,P,D

and the functions H,S,F and C will be explained in turn. In essence these numbers enable one to synchronize notes so that one can play chords effectively.

The first number (H) can have the value 0 or 1. If H=1 then instead of playing a new note on the selected channel, the previous note on that channel is allowed to continue. If a note were gently dying away then it might be abruptly terminated when its time was up. Setting H=1 allows the note to continue instead of playing a new note. If H=1 then the note defined by the rest of the SOUND statement is ignored.

The second number (S) is used to synchronize the playing of a number of notes. If S=0 then the notes are played as soon as the last note on the selected channel has completed. (There is a slight simplification here; "completed" means "has reached the start of the release phase".) The user is referred to the keyword ENVELOPE for relevant detail.

A non-zero value of S indicates to the computer that this note is not to be played until you have a corresponding note on another channel ready to be played. A value of S=1 implies that there is one other note in the group. S=2 implies two other notes (i.e. a total of 3). If a note was sent to channel one with S set to 1 then it would not be played until a note was ready on another channel which also had S set to 1. For example:

110 SOUND &101,-15,50,200

110 SOUND 2,-15,200,100

120 SOUND &102,-15,100,200

When this program is run the note at line 100 will not play until channel 2 is free. Line 110 sounds a note immediately on channel 2 - and for 5 seconds (duration 100). When that note has completed then both the notes from lines 100 and 120 will sound together.

The third number (F) can have the value 0 or 1. If it is set to 1 then the sound statement in which it occurs flushes (throws away) any other notes waiting in the queue for a particular channel. It also stops whatever note is being generated on that channel at present. The sound statement in which F=1 then plays its note. Setting F behaves like an "over-ride". For example:

20 SOUND 2,-15,200,100

25 FOR X=1 TO 500:NEXT X

30 SOUND &12,-15,100,200

In the above situation line 20 will start a sound on channel2 but this will be stopped almost immediately by line 30 which will generate a lower and longer note on channel 2. Line 25 just gives a short delay.

Setting F=1 provides an easy way of stopping an everlasting note. Thus SOUND &13,0,0,0 stops the current note on channel 3 and instead plays one at zero loudness and of minimum length. This will stop channel 3 immediately.

The last number (C) is the channel number described earlier.

Jgharston (talk) 00:42, 8 March 2020 (CET)