BasicV a Dabhand Guide Part 5
Chapter 11 : Sound
The Archimedes is provided with a sophisticated stereo sound system with up to eight voices or channels. The sound system is controlled by a number of modules which form part of the Arthur Operating System and RISC OS. BASIC V provides a number of instructions for controlling sound, including the sound command which provides some compatibility with previous versions of BBC BASIC.
Full control of the sound system is really only possible by directly using the SWI, or Operating System, calls provided for this purpose. A detailed description of these SWI calls is really beyond the scope of this book, but a number of these are accessible via star commands, and these are detailed below along with the instructions in BASIC for controlling sound.
BASIC Sound Instructions
Almost the simplest sound instructions must be SOUND ON and SOUND OFF, which may be used at any time to turn the Archimedes' whole sound system on or off. You can easily check this by pressing CTRL-G or typing VDU7, both of which make the computer 'beep'. Once SOUND OFF has been executed no beep will be heard until SOUND ON switches the sound system back on again.
The sound system uses up to eight voices, the number active at any time being set with the VOICES command. The number of voices must be a power of two - any other number is rounded up to the next value in sequence (1, 2, 4, or 8). Acorn recommends that the number of active voices should always be kept to a minimum as sound places heavy demands upon any computer, even the Archimedes.
A star command is needed to assign a waveform, or instrument, to a channel. There is some confusion of terminology here for which we can thank Acorn. BASIC refers to voices and waveforms, while Arthur and RISC OS refers to channels and voices. As a result, even Acorn's own manuals need reading with some care. A waveform may be assigned to a channel in one of two ways, by index or by name.
A list of the nine default voices (or waveforms) may be obtained using the command *VOICES. This will list the voices by name and by index number as follows:
If you try executing this command, you should also see a figure '1' to the left of the first voice, WaveSynth-Beep. This shows that that voice is currently assigned to channel 1. This is the Archimedes 'beep' which is always connected to channel 1.
Any of the default, or any other voices, can be assigned to a channel using the command:
*CHANNELVOICE <channel> <voice index/name>
Thus the beep could be given the voice 'Percussion-Soft' by writing:
*CHANNELVOICE 1 6
*CHANNELVOICE 1 Percussion-Soft
Although longer, the second format is perhaps preferable, as using an index is dependent upon the order in which a set of voices is loaded. There is also an alternative BASIC keyword, VOICE, not documented in the original User Guide, which performs the same function in this case, so the last command above could also be written:
The second parameter is the name of a voice and, as with other BASIC instructions, should be either a string variable or expression, or enclosed in quotes as here. Try experimenting, using either method, by using other voices to become the 'beep' in turn.
Setting the Stereo Position
The stereo position of each channel may be specified with the STEREO instruction. The position is given as a number in the range -127 to +127, where -127 indicates the extreme left, and +127 the extreme right. For example:
would position channel 1 centrally, ie, mono reproduction. However, as indicated in the User Guide, there are only seven discrete positions. As a result you could use the following set of values to specify all the stereo positions:
-80, -48, -16, 0, +16, +48, +80
moving from left to right. The effect of this instruction will only be apparent on the sound output through the miniature stereo jack socket at the rear of the Archimedes.
Setting and Reading the Beat
The BEATS statement allows channels to be synchronised together. The beat counter counts from 0 to n-1 where n is the value specified in the beats statement. When the value of n is reached the beat counter resets itself to 0. The syntax is simply:
The keyword BEATS can also be used to return its current setting, for example by typing:
There is a second very similar keyword, BEAT. The value of this pseudo- variable returns the current value of the beat counter, and will therefore be in the range 0 to n, where n is the value specified with the BEATS instruction.
The value specified in the BEATS statement can be used in conjunction with the SOUND instruction (see later) to fix the position of notes within a bar of music. BEAT can be used to ensure that sounds are correctly synchronised. An example will tie all this together shortly.
Setting and Reading the Tempo
The rate at which the beat counter is incremented is determined by the tempo. This is set with the TEMPO statement, and as with BEATS, TEMPO can set the corresponding rate, or return the value to which the tempo is currently set. Therefore:
will set the tempo to n, while:
will print out the current setting of the tempo. The value of n should be thought of as a four digit hexadecimal number, starting with '&', where the three right-most digits are a fraction less than one. A rate of &1000, the default, represents a tempo of one beat per centi-second, whilst &2000 represents double that rate, and so on.
To create a sound or note, you need to use the SOUND instruction. The format of this is:
SOUND <channel>, <amplitude>, <pitch>, <duration> [,after]
For compatibility, this follows the same format as on the BBC Micro and Master series except for the optional 'after' parameter. The 'channel' parameter specifies which sound channel to use, with a maximum value of 8, but within the range determined by the VOICES instruction.
The 'amplitude' parameter provides volume control with -15 being the loudest and 0 the quietest. The pitch number is related to the standard musical scale by the table given in the User Guide, while duration takes a value in the range 0 to 254 (255 being forever) to specify the duration of the note in twentieths of a second.
The sound source will depend upon the voice which has been assigned to the channel specified in the SOUND command. The 'after' parameter, which is not essential, is used to specify the number of beats to be executed before a sound is to be made audible. As such it may be used to define the position of each note within the bar.
The program in listing 11.1 draws various of these aspects together. This is no musical masterpiece, but does serve to show some of the essential elements. If you run this program you will find it repeatedly plays the first three notes of a well known tune. The program starts by setting the TEMPO and BEATS count, and the maximum number of active voices. The voices themselves, selected from the built-in set, are allocated to four different channels.
Listing 11.1 Simple tune playing program.
10 REM >Chapll-l 100 TEMPO &1000 110 BEATS 200 120 VOICES 4 130 VOICE 1,"StringLib-Soft" 140 VOICE2,"StringLib-Pluck" 150 VOICE2,"StringLib-Steel" 160 VOICE2,"Percussion-Medium" 170 REPEAT 180 REPEAT UNTIL BEAT=0 190 SOUND 1, -15, 69, 5, 50 200 SOUND 2, -15, 61, 5, 100 210 SOUND 3, -15, 53, 5, 150 220 SOUND 4, -15, 200, 5, 50 230 SOUND 4, -15, 200, 5, 100 240 SOUND 4, -15, 200, 5, 150 250 REPEAT UNTIL BEAT <> 0 260 UNTIL FALSE 270 END
The REPEAT at line 170 marks the start of the main loop in the program. The following REPEAT...UNTIL statement at line 180 serves to synchronise the start of the bar for all channels, and the subsequent SOUND statements specify the notes to be played, and with the 'after' parameter, their relative positions within a bar of music. Thus, if BEATS has been specified at 200, an 'after' parameter of 50 will ensure a note sounds a quarter of the way through a bar.
Lastly, the REPEAT...UNTIL at line 250 ensures that the end of the bar is reached before the whole process is repeated.
As previously stated, many of the finer points of music generation on the Archimedes are best, or only, handled through Operating System (SWI) calls. Some of these are also provided in the form of star commands, and these are detailed briefly below for completeness.
Sound is a highly complex subject, and anyone wishing to pursue it seriously and write their own programs will need to refer to the detailed section in the Programmer's Reference Manual. Be prepared for some machine code programming, or at the very least to handle a multitude of appropriate SWi calls to achieve the sophisticated sound output of which the Archimedes is capable. If you just want to use the sound system, then the Music Editor included on Acorn's Welcome disc should serve the purpose adequately.