Sorting out the chords from the discords
More sound advice from NIGEL PETERS
THE Editor came up to my desk smirking. "I need your help
with a program I've written. It's called Expenses. You type your
expenses claim into the micro and it comes back with 'Not allowed,
go away'.
"I want you to make the program make a rude noise".
He turned, and made it into the office on his third attempt.
SO far, we've covered the production of notes on the BBC Micro
using the SOUND command, and learned about the sound channels
and how to flush them.
This month we'll be covering the problems of getting two or
more notes to sound at the same time. This means synchronising
the notes on different channels. Try out Program I.
10 REM PROGRAM I
20 SOUND 1,-15,60,20
30 SOUND 2,-15,76,20
40 SOUND 3,-15,88,20
This plays three notes at the same time. This is called a chord.
If it sounds nasty, then it's a discord.
Peters' First Law states that it is much easier to produce discords
than harmonious chords!
I doubt if anyone has ears sharp enough to tell, but, because
it takes time for the micro to interpret a line of Basic, the
second and third notes start a split second after each other.
Since they have the same duration, this means they will end
in a ragged manner, first note one, then note two, then note three.
Program II has a couple of delay loops in it to accentuate this
effect.
10 REM PROGRAM II
20 SOUND 1,-15,60,20
30 FOR N=1 TO 1000:NEXT N
40 SOUND 2,-l5,76,20
50 FOR N=1 TO 1000:NEXT N
60 SOUND 3,-15,88,20
This is supposed to lessen the clarity of chords, though how
anyone can tell beats me! Still, BBC Basic has a way to overcome
this undetectable effect.
It's not as pointless as you might think, as it also comes in
useful when you're writing tunes for the micro, helping you make
sure that the right notes are played at the right time.
From my last article you'll remember that the channel parameter
can be treated as a four digit hexadecimal number &TUVW.
W was the channel number with values of 0, 1, 2 or 3 while V
was the flushing control which was set to 1 to clear a channel's
sound queue.
T we ignored, and will continue to do so until we get to the
ENVELOPE command. It's the U parameter that decides how notes
are synchronised.
If you think about it then you'll realise that for two or more
notes to play at the same time each must be on a separate channel.
The U parameter in a SOUND command tells the micro that it is
not to play that note until there is a certain number of other
channels with notes.
When the required number of channels containing notes is reached
then all of them start playing at the same time. The value of
U decides how many other channels must have notes before the chord
is played.
If U is 0, then the micro plays the note when it can, without
reference to other channels. If it is 1, the micro will hold that
note until there is another note available on another channel.
If it is 2, it will wait until two other notes are available,
that is all three notes will start simultaneously. If it is 3,
it will wait for three other notes when all four of the micro's
channels will be in use. The values are shown in Figure I.
Try Program III to see it in action.
10 REM PROGRAM III
20 SOUND &0201,-15,60,40
30 FOR N=1 TO 1000:NEXT N
40 SOUND &0202,-15,76,40
50 FOR N=1 TO 1000:NEXT N
60 SOUND &0203,-15,88,40
|
U Parameter
|
Effect
|
Note in chord
|
|
0
|
Plays without reference to other channels
|
1
|
|
1
|
Waits for note on one other channel
|
2
|
|
2
|
Waits for notes on two other channels
|
3
|
|
3
|
Waits for notes on all channels
|
4
|
Figure I: Synchronisation parameter values
What happens is that the micro reads the first SOUND statement
and, because it has 2 in the U parameter, it waits for two notes
to be available on other channels. When it has these it plays
all three notes together.
The delay loops make for a slight pause before the chord is
played. This shows that the micro really is waiting for the other
two notes.
Since the duration parameters are the same, the notes all stop
playing at the same time. If this weren't the case there would
be a ragged ending. Setting the U parameter only synchronises
the start of the notes.
Can you figure out why nothing seems to happen when you run
Program IV?
10 REM PROGRAM IV
20 SOUND &0301,-15,60,40
30 SOUND &0302,-15,76,40
40 SOUND &0303,-15,88,40
Don't think your computer has gone wrong! What happens is that
the micro reads the U parameter of the first SOUND statement.
As this is 3 it will not play that note until sounds are available
from three other channels. Unfortunately the program only gives
two other notes so the micro can't play. Hence the silence!
However, the notes in the channel don't just disappear. They
are still in the queues waiting for a note on the fourth channel.
Give them one by entering:
SOUND &0300,-15,88,40
and pressing Return. Now that the fourth channel has a note,
all four will sound.
Before you run the other programs in this article, it would
be wise to press Escape to clear the sound queues of any left-over
notes. If you don't, you might get some strange results. You have
been warned!
One point to bear in mind is that when the micro comes across
a U parameter that makes it search the channels for other notes,
it will quite happily accept ones that are already playing. Try
Program V:
10 REM PROGRAM V
20 SOUND &0101,-15,60,40
30 SOUND &0102,-15,76,40
40 SOUND &0203,-15,88,40
50 SOUND &0200,-15,88,40
At first glance you might think it will play the first two notes
as the U parameter ties them together. Then it will hang up as
the next two notes have a U parameter of 2 and need a third before
they can sound.
Playing the program will soon show you that this isn't so, as
all four notes are played at once.
This is because the micro comes to the last two SOUND statements
and starts searching the other channels for a note.
Since they're already playing notes and carry a U parameter,
it will quite happily accept either of them to make up the third
note. And so the U parameter's condition is fulfilled and the
note plays.
However, if we put in a line of Basic, as in Program VI, you'll
notice that only the first two notes are played and that the third
and fourth are left in the queues.
5 REM PROGRAM VI
10 SOUND &0101,-15,60,40
20 SOUND &0102,-15,76,40
25 FOR N=1 TO 4000:NEXT N
30 SOUND &0203,-15,88,40
40 SOUND &0200,-15,88,40
Enter one of the sound commands from the first two notes and
you'll hear these left-over sounds along with the one you've entered.
Remember if you're getting funny effects it's probably because
you've got some notes left in the queues. Get rid of them with
Escape.
Finally, Program VII combines the synchronisation parameter
U with the flushing parameter V, which we covered in the last
article.
10 REM PR0GRAM VII
20 SOUND &201,-15,52,254
30 SOUND &202,-15,68,254
40 SOUND &203,-15,80,254
50 PRINT"Press key for next chord"
60 pause$=GET$
70 SOUND &0211,-15,60,254
80 SOUND &0212,-15,76,254
90 SOUND &0213,-15,88,254
100 PRINT"Press key for next chord"
110 pause$=GET$
120 SOUND &0211,-15,68,254
130 SOUND &0212,-15,84,254
140 SOUND &0213,-15,96,254
150 PRINT"Press key to end"
160 pause$=GET$
170 SOUND &0211,0,0,0
180 SOUND &0212,0,0,0
190 SOUND &0213,0,0,0
This allows us to play three chords in succession by pressing
the key.
Notice that the final SOUND statements which flush the chords
are synchronised to prevent a ragged ending, though I don't know
who would be able to tell if they weren't!
Anyway, this is where I finish. The next article will cover
the sound effects channel, which I've just been using in the editor's
expenses program. I did it in my own time and sent him a bill.
He made a rude noise.