Sound Advice Series

We reach the end of the series with an explanation of amplitude envelopes.

Volume 2

Number 3

May 1984

One final effort and we've got the ENVELOPE licked

NIGEL PETERS concludes his sound advice

THE Editor staggered past my desk. "This is the last article in my Sound series, Sir", I cringed.

"Thank goodness for that", he slurred, "I only meant you to do a half page filler in the first place. I don't know why you got so carried away with it all".

He weaved his way to his office accompanied by the sound of my head banging against the desk.

IN the last article we explored the ENVELOPE command and saw how we could use it to vary the pitch of a note. We studiously ignored the last six of the 14 parameters that follow ENVELOPE.

This month we'll be dealing with these last six and seeing how the values we give them can affect the amplitude, or loudness, of a note.

The full set of ENVELOPE parameters is:

ENVELOPE N,T,PI1,PI2,P13,PN1,PN2,PN3, AA,AD,AS,AR,ALA,ALD

Let's go straight on to Program I and see what happens if we put values in two of these amplitude parameters.

10 REM PROGRAM 1
20 ENVELOPE 1,100,0,0,0,0,0,0,30,0,0,0,120,0
30 SOUND 1,1,50,80
40 SOUND 1,0,0,0

The note played by the SOUND command in line 30 gets louder (in four steps) and then stops. This is a result of the parameters we put in the last part of the envelope definition.

Table I gives a list of these parameters and their effects and ranges. I have stuck to the standard parameter name abbreviations for the sake of uniformity.

Parameter

Range

Meaning

AA

-127 to 127

Amplitude change per step in the attack phase.

AD

-127 to 127

Amplitude change per step in the decay phase.

AS

0 to

-127

Amplitude decrease during each step of sustain phase.

AR

0 to

-127

Amplitude decrease during each step of release phase.

ALA

0 to 126

Target (maximum) value achieved during attack phase.

ALD

0 to 126

Target (minimum) value achieved during decay phase.

Table I: Amplitude parameters of ENVELOPE

You might ask why have these amplitude parameters anyway — surely they just make things more complicated?

The answer is that they do, but they also allow the BBC Micro's sound generator to mimic musical instruments.

When a note is played on a violin or piano it doesn't have the same loudness all the time. It builds up from silence to a maximum and then gets quieter again.

Each instrument has a different amplitude envelope, as this characteristic increase and decrease in loudness is called.

Some achieve their maximum loud-ness rapidly, then fade away quickly. Others take relatively longer to reach their full power and then die away gently.

Until now all we have used to control the loudness of a sound was the amplitude parameter of the SOUND command. As you'll remember, this is the second one after the SOUND.

The note started at the loudness specified by that parameter and stayed there until it finished. It was simple, but it wasn't like real life.

The amplitude part of the ENVELOPE command was designed to overcome this. It divides the time the note plays into four sections, each with a different characteristic. Figure I shows this schematically.

The first section is the attack phase. In this the note builds up from nothing to its maximum loudness. The par-ameters ALA and AA control this phase.

After the note has reached its peak, it enters the decay phase, where it goes into a gradual decline. The parameters ALD and AD govern this.

The final two sections are the sustain phase and the release phase, controlled by AS and AR respectively.

We'll ignore these last two for the time being and go back to Program I to see what caused the changes in loudness we heard.

Notice that it is only the loudness which is changing. The pitch of the note stays exactly the same.

If you look at the envelope defined by line 20, you'll see it has the label 1 and each step lasts for one second. The six pitch parameters are all 0, so they don't have any effect on the note.

The only parameters that have a value assigned to them are ALA and AA, the ones that govern the attack phase.

The maximum loudness the note can reach is fixed by ALA. This can have values between 0 and 126, corresponding to the range of 0 to -15 allowed by the SOUND command.

As you can see, the amplitude envelope allows for much finer volume control.

In Program I the level is set at 120, mainly because it's loud and divides easily. The rate at which the loudness of the note increases to get to this maximum level is fixed by the parameter AA.

This can have values from -127 to 127, is normally positive and is the change of amplitude per step. It corresponds to the gradient of the attack phase shown in Figure I. For each time step forward the volume of the note increases by A A.


Figure I :Amplitude parameters in action

In program I the value is 30. For each time step forward the volume of the note will increase by 30 until it reaches the level of 120.

The number of steps it takes to do this is 120 divided by 30, which give the answer four. We can actually hear the four steps as the program runs. Since each step lasts for one second the whole thing takes four seconds, which is the value I've made the note sound for in line 30.

Don't worry about the SOUND command in line 40. It's just a dummy note, there to catch garbage. You'll learn its significance later.

Run Program II and you'll hear it has eight steps. The sound gets louder, then fades away because there are now values for the decay phase of the amplitude envelope.

10 REM PROGRAM II
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,0,0,120,0
30 SOUND 1,1,50,160
40 SOUND 1,0,0,0

The target value ALD is now 0 (no volume) and the drop in amplitude per time step, AD, is now -30.

Figure II shows what has happened.


Figure II: Program II's amplitude envelope

Notice that the peak amplitude lasts for two steps.

This "doubling" effect occurs where two phases join and can lead to some unexpected results if you don't keep it in mind.

Still that's for later when you experiment with envelopes for yourself. Remember though, if you think that something has lasted a step more than it ought it's probably because of the "join" of two phases.

You'll notice that I have increased the duration of the SOUND command in line 30 to eight seconds, exactly the length of the envelope.

You might wonder what would happen if the duration of the SOUND command was shorter or longer than that dictated by the parameters of the ENVELOPE command governing the sound. Program III shows what happens if it's shorter:

10 REM PROGRAM III
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,0,0,120,0
30 SOUND 1,1,50,80
40 SOUND 1,0,0,0

Here the duration of the sound is only four seconds, while looking at the parameters of the envelope would lead us to expect it to last eight seconds as before.

What happens is that the sound lasts for four seconds, so only the first four seconds of the envelope get a chance to work. The rest, in this case the decay phase, is ignored.

Program IV shows us what happens if the duration of the sound exceeds that of the parameter:

10 REM PROGRAM IV
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,0,0,120,0
30 SOUND 1,1,50,200
40 SOUND 1,0,0,0

As you might expect, the note carries on at the final volume it reached, finishing off the duration parameter of the SOUND command.

The trouble is that since the final loudness of the sound is 0 we can't hear it! Still, it is there, playing away silently until the ten seconds are up.

If you don't believe me, make the SOUND command in line 40 produce a real sound on the same channel. You won't hear it until the ten seconds are up.

Now the value of ALD doesn't always have to be 0. We can have any value between 0 and 126.

In Program V ALA has the value of 60. This is the target volume for the decay phase and it is reached in steps of -30 (AD).

10 REM PR0GRAM V
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,0,0,120,60
30 SOUND 1,1,50,120
40 SOUND 1,0,0,0

With Program V the duration of the SOUND is such that it finishes at the same time as the amplitude reaches 60, after six seconds.

The trouble is that this means you can't hear the note at its final volume as you cut off the sound just as it reaches it.

Program VI is exactly the same, except that it lasts for nine seconds. Now you can hear the final decrease in volume.

10 REM PROGRAM VI
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,0,0,120,60
30 SOUND 1,1,50,180
40 SOUND 1,0,0,0

Notice again that the sound continues at the final loudness reached until all of the duration parameter of the SOUND command is used up.

But what of the sustain phase, governed by the parameter AS? What does this do?

It's quite simple really. All that it does is use up the rest of the duration of the SOUND command. The attack and decay phase both use up part and the sustain phase lasts for whatever period, if any, is left.

Now that we know how long the sustain phase lasts, let's see what it does by running Program VII, which gives AS the value of -15:

10 REM PROGRAM VII
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,-15,0,120,60
30 SOUND 1,1,50,200
40 SOUND 1,0,0,0

As you can hear, the volume increases to 120 and decreases down to 60 as you might expect from the parameters. This uses up six seconds of the 10 that the note will play for.

During the remaining four seconds of the note the amplitude envelope enters the sustain phase. Here the AS parameter is -15. This, as you might guess, means that the volume decreases by a factor of 15 for each time step.

In this program the volume in the sustain phase goes down from 60 by 15 each second. It gets to 0 volume just as the time runs out.

Program VIII is the same as Program VII except that the duration of the note caused by the SOUND statement is now 12 seconds. The volume still reaches 0 after ten seconds but the note keeps playing, silently, for the last two seconds.

10 REM PROGRAM VIII
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,-15,0,120,60
30 SOUND 1,1,50,240
40 SOUND 1,0,0,0

Again, if you don't believe me put a real note on channel 1 in line 40 and it won't sound until the note in line 30 has had its full 12 seconds.

Of course you don't have to arrange it so that AS eventually reduces to 0 volume. In Program IX it has the value -5 so the volume doesn't have a chance to reach 0 before the time runs out.

10 REM PROGRAM IX
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,-5,0,120,60
30 SOUND 1,1,50,240
40 SOUND 1,0,0,0

Looking at Program X two things are apparent. Line 40, the garbage collecting line, has gone and the value of AS is now -10.

10 REM PROGRAM X
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,-10,0,120,60
30 SOUND 1,1,50,240

A quick calculation will show that the attack and decay phases will take six seconds. A decline of -10 for each of the six remaining one second steps of the sustain phase should take the volume to 0 just as the note finishes playing.

Try it and see!

The note carries on beyond the 12 seconds you'd expect from the SOUND command.

What's happened is that the sound generator has come to the end of the sustain phase and entered the release phase.

This is a rather weird construction which is independent of the duration parameter of the SOUND command.

The envelope reaches the end of the sustain phase when the duration of the note runs out. The sound generator then searches around for something to do next.

If a note is waiting on the same channel it will play it. If not it will carry on playing the last note until the next note comes along.

Never mind that the duration set by the SOUND command has been used up. The release phase carries on regardless.

During this release phase, the volume of the note can be made to fade away by giving AR a negative value. It will carry on decreasing by this amount per time period until it eventually reaches 0 or another note is placed on that channel.

The eventual target volume is 0, the decrease per step is AR and the whole phase is independent of the duration parameter of the note.

As you can see from Program XI, if AS and AD are both 0 and there is not other note on that channel queue, the note carries on and on. This is because there's no decline to 0 volume in the release phase.

10 REM PROGRAM XI
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,0,0,120,60
30 SOUND 1,1,50,240

Hence the dummy note I've put in line 40 of the previous program — without it the note is endless and would confuse all the examples.

As I said, it's a strange part of the amplitude envelope. It's mainly meant to mimic the dying away of a note on a musical instrument.

You'll notice that if there's a note following it on the same channel the envelope never enters the release phase. It just gets on with the next note.

There are times when we might want to give a note a release phase, despite other notes behind it in the same queue.

We can force the note to enter its release phase, despite any following notes, by putting a dummy note after it such as:

SOUND &1001,0,0,0

This makes the T parameter of the SOUND command equal to one, which forces the previous note to enter the release phase.

See my article in the January 1984 Micro User if you don't know where the T parameter goes or why we've got a & in the SOUND command.

I find the sustain and release phases a nuisance most of the time, so I just use the values of -127 for each. This effectively switches them off because as soon as the note enters either phase the volume is decreased to 0, whatever it was before.

Program XII shows this in action:

10 REM PROGRAM XII
20 ENVELOPE 1,100,0,0,0,0,0,0,30,-30,-127,-127,120,60
30 SOUND 1,1,50,240

And that's the end of the article and the series. I've covered the SOUND and ENVELOPE commands and their parameters. The rest is up to you.

Now that you understand them, why not use the ENVELOPE defining programs we've run in the pages of The Micro User to make up your own ENVELOPES? If you come up with any good ones, send them in and let us have a look at them.

In the meantime I'm working on my own sound. It'll be the noise made by the Editor when he's hit with a disc drive.

Of course I've no idea what that sounds like as yet, but I'm going to find out.

This may be the last article from me for a while!

Nigel Peters