Use an ENVELOPE to make your SOUNDs more scintillating
More sound advice from NIGEL PETERS
THE Editor staggered up behind me. "ENVELOPE, eh? About
time you got to that. Makes it a red letter day, doesn't it? Frankly
I'd get on with it, post haste".
He veered into his office and managed to slam the door, leaving
me waiting for the obvious and awful ENVELOPE pun still remaining.
SO far in this series of articles all we've done is explore
the SOUND command, its intricacies and vagaries.
We have managed to produce some interesting sounds on the way
and, I hope, had a lot of fun in the process.
However we haven't explored all of the BBC Micro's sound capabilities.
We've yet to use the ENVELOPE command.
Now this is a formidable looking beast, being followed as it
is by 14 numbers or parameters. The numbers and what they do are
shown in Table I, but don't let them put you off.
As long as you keep your nerve and take things step by step
you won't come to any harm.
Why have an ENVELOPE in the first place, you might ask. After
all, we've been doing quite well with the nice, comparatively
simple SOUND command. Why complicate matters?
The answer is that although we can do a lot with SOUND, the
ENVELOPE command allows us to do a whole lot more!
The note that we get from the BBC Micro with the SOUND command
tends to be rather "electric" and not very exciting.
Try:
SOUND 1,-15,100,200
Well, it's a sound, but not a very interesting one. Now let's
vary this sound using an ENVELOPE command
to define an envelope. Type in:
ENVELOPE 1,1,70,16,2,2,0,0,126,0,0,-126,126,126
then:
SOUND 1,1,100,200
and notice the difference.
The SOUND command is exactly the same as the previous one except
that the loudness parameter of - 15 has become a 1. This figure
1 just tells the micro to look for an envelope which has been
typed in as envelope 1.
I must point out that the envelope must have been defined before
the SOUND command can use it. The micro then obeys the SOUND command,
but the note that it plays is influenced by the ENVELOPE command
we defined previously.
In this case, the rather boring noise we produced earlier has
now become the sound of an alarm clock. I don't want anyone dozing
off while they're reading my article!
One thing to notice is that it is the SOUND command that makes
the noise.
You can type in ENVELOPES until you're blue in the face but
they won't make a noise. All they do is alter the noises made
by any SOUND command that refers to them.
The envelope defined by an ENVELOPE command varies the sound
produced by a SOUND command (provided the SOUND command refers
to the envelope by the number in its loudness parameter).
Now let's have a look at the parameters that follow the ENVELOPE
command:
ENVELOPE N,T,PI1,PI2,PI3,PN1,PN2,PN3,AA,AD,AS,AR,ALA,ALD
|
Parameter
|
Range
|
|
|
N
|
1 to 4
|
Envelope label
|
|
T
|
0 to 127
|
Length of each step in 100th of seconds.
|
|
(+ 128)
|
Added to stop auto-repeat
|
|
PI1
|
-128 to 127
|
How the pitch will change with each step in the first part
|
|
PI2
|
-128 to 127
|
Pitch change per step in the second part
|
|
PI3
|
-128 to 127
|
Pitch change per step in the third part
|
|
PN1
|
0 to 255
|
The duration of section one, measured in steps of T
|
|
PN2
|
0 to 255
|
The duration of the second section in steps of T centiseconds
length
|
|
PN3
|
0 to 255
|
The duration of the third section PN3 steps, each of T
centiseconds
|
|
AA
|
126
|
These values affect the loudness of the note and are fixed
for this article
|
|
AD
|
0
|
|
AS
|
0
|
|
AR
|
-126
|
|
ALA
|
126
|
|
ALD
|
126
|
Table I: Values and meaning of the ENVELOPE parameters
The beast still looks pretty formidable, but we'll go through
it step by step. I have used the same parameter names as you'll
find in the User Guide and some of the books, so you can cross
reference.
You'll be pleased to know that we're ignoring the last six parameters,
the ones that begin with A. These affect the loudness, or amplitude,
of a note and we'll cover them in the next article.
So we're left with the first eight parameters. These label the
envelope, decide how long its effects are going to last and vary
the pitch of the note.
Now why would you want to vary the pitch of a note? It seems
odd that you should specify the pitch in a SOUND command and then
use an envelope to vary it.
The reason is that in real life notes are never the pure, steady
sound that we get from the BBC Micro's sound channels.
What we call the pitch of a note is just an average. The actual
note "wobbles" around that value. It's this wobbling
that lets us tell the difference between the middle. C on a piano
and the middle C on a violin.
The average pitch is the same, but the wobbles vary. (I beg
the forgiveness of any musical genius who may read the above.)
The ENVELOPE command allows us to approximate these wobbles
and so make the noises our micro produces sound like a saxophone
or an alarm clock. I should point out that the loudness also wobbles,
but we'll leave that until later.
Now let's take a look at the first parameter following the ENVELOPE
command. As you'll see from Table I, this is called N and can
have values of 1 to 4.
N is a reference number. You decide what number you want to
refer to the envelope by and put that number in N Then when you
want a sound to be influenced by that envelope you just put that
number in the loudness parameter of that SOUND command.
If you look at the alarm clock again you'll see that we made
the N in the envelope equal to 1 and put a 1 in the loudness parameter
of the following sound statement. We could have used 2 as the
label, or 3, or 4.
From this you'll see that you can have up to four envelopes
ready for use.
The next parameter we come to is T which, as you might guess,
stands for time. As you'll see in a moment, the ENVELOPE command
works in a basic unit called a step. It will affect a SOUND command
in one way for a specified number of steps, then in another way
for another number of steps and so on.
What T does is to allow you to choose how long each of these
steps will be. It can have values from 0 to 127, and is measured
in hundredths of a second.
If we have T equal to 100 then each step is a hundred centi-seconds
long, which, if my maths is correct, makes each step last a second.
Looking at the alarm clock envelope again, you'll see that T
is equal to 1, so each step lasts for just one hundredth of a
second.
So N just labels the envelope while T decides how long each
of its steps will last.
Now let's explore the next six parameters which cause the wobbles
in the pitch of the note, allowing it to make all manner of amazing
sounds.
There are three stages or sections to the wobble, or change,
in pitch. The pitch of the note can go up, go down, or stay the
same in each section.
Figure I shows the effect of a hypothetical envelope on a hypothetical
sound. The horizontal line is the pitch of the note as defined
in the SOUND command. The wavy line shows the notes actually played
under the influence of the ENVELOPE command.

Figure I: Effect of ENVELOPE on SOUND
As you can see, there are three stages. The pitch rises in the
first stage, goes down in the second, and rises again in the third.
Of course it could have risen in all three stages, or gone down
in all three, but let's just stick to it going up, down, and up
again.
These ups and downs are caused by the parameters PI1, PI2 and
PI3 in the ENVELOPE command. PI1 is the increase or decrease of
pitch for each step in stage one. Similarly PI2 sets the increase
or decrease per step for stage two and PI3 that for stage three.
The units of pitch are the same as we used in the SOUND command.
Eight of them make up a range of one tone, four of them a semitone.
The length of each stage is determined by PN1, PN2, and PN3.
The value of PN1 determines that the first stage will consist
of PN1 steps, each of length T. Similarly the second stage is
of length PN2 steps, again of length T.
As you might guess, the third stage is of length PN3 steps,
each step lasting for T seconds.
Figure II shows all this. It's just another version of Figure
I with the parameters put in.
Figure II: ENVELOPE parameters
Let's have a look at this in practice. Type in and run Program
I.
10 REM PROGRAM I
20 ENVELOPE 1,100,4,0,0,10,0,0,126,0,0,-126,126,126
30 SOUND 1,1,50,200
Exciting, isn't it? Can you understand the ENVELOPE command
of line 20 and how it affects the noise made by the SOUND command
of line 30?
Looking at line 20 we first of all have the ENVELOPE keyword,
then comes the number 1. This is the label that the envelope will
be known by. Then comes 100, which means that each step of the
envelope will lake 100 centi-seconds or one second.
As we can see, PI 1 has the value of 4. This means that for
each step of one second the note produced by the SOUND command
will rise in pitch by one semitone.
Looking at the value for PN1 we see that there will be 10 steps.
Each will last for one second and every second the note will rise
by a semitone. Run it again and see that this is the case.
You'll notice that we've made the parameters of the other two
stages equal to zero to stop things getting too complicated. The
last six parameters effect the amplitude of the note. We'll deal
with this in the next article, and for the moment just stick to
the six values in line 20.
You might also notice that the envelope lasts for 10 seconds
(PN1 times T) and the SOUND command lasts for 10 seconds. What
happens if they don't coincide so neatly? Try Program II and see.
10 REM PROGRAM II
20 ENVELOPE 1,100,4,0,0,11,0,0,126,0,0,-126,126,126
30 SOUND 1,1,50,200
In this program you'll see that PN 1 has become 11 so we should
expect the envelope to last for 11 seconds. However as the note
is only being played by the SOUND statement for 10 seconds, the
last step never gets taken.
The reverse is the case in Program III.
10 REM PROGRAM III
20 ENVELOPE l,l00,4,0,0,5,0,0,126,0,0,-126,126,126
3 0 SOUND 1,l,50,200
As you can see, PN1 has now become 5. The SOUND statement is
the same, so the note will play for 10 seconds, but the envelope
will only last for five seconds (five steps, each of one second).
What happens during the last five seconds? As you can hear,
the envelope auto-repeats. That is, when it comes to the end of
its steps, it pauses for a couple of steps (one for each of the
other two stages), then starts again and continues until the SOUND
statement runs out of puff after 10 seconds.
This auto-repeat can be very useful for producing sound effects,
but it can also be a nuisance. Happily, it can be switched off
by adding 128 to whatever value of T you have put in the ENVELOPE
command.
In Program IV to switch off the auto-repeat I've added 128 to
the value of T, previously 100. This makes for a T value of 228.
Each step is still one second long, the extra value justs ensures
no auto-repeat.
Have a go at Program IV and see what I mean.
10 REM PROGRAM IV
20 ENVELOPE 1,228,4,0,0,6,0,0,126,0,0,-126,126,126
30 SOUND 1,1,50,200
The auto-repeat has been switched off so the sound increases
for six steps then stays at that pitch for the remaining four
seconds of the note.
Right, let's see about doing something with the second stage
of the pitch
envelope. Let's give PI2 (pitch increment two) a value of -4
and PN2 a value of 5.
Run Program V and see what happens.
10 REM PROGRAM V
20 ENVELOPE 1,228,4,-4,0,5,5,0,126,0,0,-126,126,126
30 SOUND 1,1,50,200
As you might have guessed, the whole thing lasts for 10 seconds
(PN1+PN2 steps, each of one second duration). The pitch of the
note goes up one semitone for each of five steps, then it goes
down one semitone for five steps.
Program VI gives values to PI3 and PN3 and so we make use of
the third stage of the pitch part of the envelope.
10 REM PROGRAM VI
20 ENVELOPE 2,228,4,-4,4,3,3,7,126,0,0,-126,126,126
30 SOUND 1,1,50,200
As you may have realised, the envelope lasts for nine seconds
while the sound lasts ten. This creates an uneven effect, the
last note lasting two seconds.
Program VII shortens the note played by the SOUND statement
to compensate for this.
10 REM PROGRAM VII
20 ENVELOPE l,228,4,-4,4,3,3,3,126,0,0,-126,126,126
30 SOUND 1,1,50,180
You can figure out how long the envelope lasts by adding up
PN1, PN2, and PN3 and multiplying the result by T. That is, you
add up the total number of steps and multiply them by the time
each step takes.
And that's really all there is to the pitch part of the ENVELOPE
command. It's just a matter of deciding how many steps you want
in each stage and
It's quite simple really. Practise using envelopes and they'll
soon stop seeming so difficult.
One thing that you should be wary of though, is shown by Program
VIII:
10 REM PROGRAM VIII
20 ENVELOPE 1,255,4,-4,0,3,3,0,126,0,0,-126,126,126
30 SOUND 1,1,50,255
You might think that as you start by going up three steps and
then go down three steps you'll start and end on the same note.
However this isn't the case — the sound ends up one semitone
below the beginning pitch. Figure III shows what's happened.

Figure III: The ups and downs of ENVELOPE what happens to the
note for each step.
The ENVELOPE takes effect straight away and the first note played
is a semitone above the pitch of the note in the SOUND statement.
It then goes up two more semitones and drops three. The final
note is, as you can hear, a semitone below the first.
Program IX rectifies this by having one step less in stage two
than in stage one. The final note is now the same pitch as the
first.
10 REM PROGRAM IX
20 ENVELOPE 1,255,4,-4,0,3,2,0,126,0,0,-126,126,126
30 SOUND 1,1,50,255
And that's the end of our first excursion into the ENVELOPE
command. It's not all that hard when you get used to it, and you
can do amazing things with sound envelopes.
Try Program X and listen to the four different sounds produced.
If you look at the listing you'll see that the SOUND command is
the same in each case, only the envelope differs.
10 REM PROGRAM X
20 count=0
30 ENVELOPE 1,255,4,-4,4,3,3,3,126,0,0,-126,126,126
40 ENVELOPE 2,127,4,-4,4,3,3,3,126,0,0,-126,126,126
50 ENVELOPE 3,20,4,-4,4,3,3,3,126,0,0,-126,126,126
60 ENVELOPE 4,1,4,-4,4,3,3,3,126,0,0,-126,126,126
70 REPEAT
80 envelope =1 + count MOD4
90 PRINT:PRINT
100 PRINT "Envelope number "; envelope
110 PPINT:PRINT
120 PRINT"Press key for next envelope."
130 SOUND 1,envelope,50,255
140 count=count+l
150 WAIT$=GET$
160 SOUND &11,0,0,2
170 UNTIL FALSE
And that's it. After waiting all day the Editor finally came
out with the long expected pun. He staggered up behind me and
asked: "Have you got ENVELOPE licked yet?"
I stamped out.