Envelope Engineer #1

By Chris Dewhurst

Originally published in EUG #61

Most people regard the BBC and Electron's sound ENVELOPE statement as a highly complex and mysterious thing, and they end up ignoring it just because it seems so difficult and involved. Even I didn't fully understand it until recently.

Well, in this article and the next we'll shed some light on the matter and see that it's not that complicated after all. At the end I'll be presenting a sophisticated ENVELOPE editor with which you (as an accomplished envelope engineer) will be able design your own whacky sound effects.

First of all however, we must get one or two things straight. When we aren't talking about something you send a letter in, an Envelope on the BBC or Electron is really two envelopes. One Envelope controls the pitch of a sound through time, and the other Envelope controls the amplitude of a sound through time. "Amplitude" is just "volume" to you and me.

If you like poetic language then you can describe an Envelope as being the unfolding or opening of a sound through the time dimension.

Now, the fact is that when designing the BBC's baby brother, Acorn sacrificed the ability to control the volume of a sound. Which is to say that, as you should know, on the Electron a sound can either be off or on at full volume. In the SOUND statement:

      SOUND channel, volume, pitch, duration

it makes no difference as to whether 'volume' is -1 or -15; the loudness of the sound is the same. When 'volume'=0 the sound is off and the computer just plays silence. With the BBC, on the other hand, -1 gives the quietest sound possible (ppp) and -15 the loudest (fff).

This means that the Envelope on the Electron can only control pitch through time and not volume. Electron owners need not despair, however. It is usually the pitch shift that gives the most interesting effects.

Let us now look at the Envelope statement:

      ENVELOPE n, s, pi1,pi2,pi3, pr1,pr2,pr3, aa,ad,as,ar, ala,ald

What do all those letters mean? They are PARAMETERS. There are 14 of them in all, each describing the sort of numbers that you put into the ENVELOPE statement and what effect they might have.

'n' is for 'n'umber. You can define up to four envelopes, envelope numbers 1, 2, 3 and 4. (It's possible to have an extra 12 envelopes at the expense of some more memory but we'll come onto that later. Let's keep things simple to begin with!)

If we want the computer to use an Envelope, 'volume' in the SOUND statement must be set to the corresponding (positive) 'n' number. This is distinct from the negative numbers we talked about above. What envelope will be used in the statement SOUND 1,2,52,10? Yes, that's right, envelope number 2. What about SOUND 1,0,52,10? I hope you didn't say "envelope 0"! Remember the envelopes are numbered 1 to 4, and when 'volume'=0, there will be no sound at all.

's' is the scaling factor. That means the factor by which everything else in the Envelope statement is multiplied. Basically, the bigger the number, the longer the Envelope - but not necessarily the sound - will last. We'll see the significance of this in a moment.

For now, though, consider the next six parameters - the ones called pi1,pi2,pi3,pr1,pr2, and pr3. As we said, it's best to regard the ENVELOPE as being two separate Envelopes, one controlling pitch, and the other volume through time. These six parameters relate to how the pitch of the sound changes.

There are three sections to the pitch envelope. In each section we can specify whether the pitch goes up, down, or stays the same. pi1 is by how much the pitch goes up or down per step in section 1, pi2 by how much the pitch changes per step in section 2, and pi3 is by how much the pitch changes per step in section 3.

The 'pr' numbers specify how many times we want that change to occur, i.e. how many steps there will be. Say we had a note starting on middle C (pitch number 52) and, in the first section, we want the note to slide up to the C above middle C and stop there, like a smooth glissando.

What would our pi1 number be? It would be 1. A pitch change of 1 is the smallest change in pitch we can have, and there are 4 steps per semitone (meaning that the C# immediately above C has a pitch value of 52 + 4 = 56). There are 12 notes in a scale, so a change of 12 x 4 = 48 corresponds to going up one octave. To get the glissando effect we are after, we need the change of pitch to happen 48 times, so pr1=1.

Type in these two lines:

      ENVELOPE 1,1, 1,0,0, 48,0,0, 127,0,0,-127,127,127
      SOUND 1,1,52,20

We can hear something like a siren sound going up and down. What's gone wrong? Well, the duration in the SOUND statement is longer than it takes to play one glissando and the Envelope starts repeating itself.

The duration in the SOUND statement is measured in 1/20ths of a second, whereas the BBC and Electron's ENVELOPE measures its time in 1/100ths of a second. 1/100th of a second is called a centisecond.

Confused? You should be! Just remember that to find out the length of a sound in the SOUND statement in seconds, divide the duration by 20. To find out what the length is according to the Envelope, multiply by 5. To convert the sound duration as the Envelope sees it to seconds, divide by 100. So in the above example the duration is 20 / 2 = 1 second = 20 * 5 = 100 centiseconds = 100 / 100 = 1 second.

The duration of 1 glissando is 48 centiseconds = just under half a second. That is because each pitch change happens every 1 centisecond. What happens if we want it to happen every 2 centiseconds then? Well the answer lies in that scaling factor, or step, the second parameter 's' in the ENVELOPE statement.

At the moment it is set to 1 which means that there is a shift in pitch (and also in volume as we'll see later) every 1/100th of a second. If we want it to happen every two centiseconds, we just change the value of 's' to 2. Try it now:

      ENVELOPE 1,2, 1,0,0, 48,0,0, 127,0,0,-127,127,127
      SOUND 1,1,52,20

Now it takes 2 * 48 = 96 centiseconds, or just under a second, for the pitch to slide from middle C to the C above. You will hear the envelope just starting to repeat itself. The best we can do to overcome this is to reduce the SOUND duration to 19, because 19 * 5 = 95 which closely - but not quite - matches the duration of the glissando.

There is another solution. We can get the Envelope to hold onto the final pitch by adding 128 to the 's' parameter. That is, by setting bit 7 of the scaling parameter we are turning off the auto-repeat. Try this:

      ENVELOPE 1,130, 1,0,0, 48,0,0, 127,0,0,-127,127,127
      SOUND 1,1,52,20

I have found that when you need special effect Envelopes, it's best not to use this non-auto-repeat facility, i.e. don't add 128 to the 's' parameter. But experiment to suit yourself. Incidentally, you don't have to leave spaces in between some of the parameters in the ENVELOPE statement. I did to visually separate related parameters which makes for easier reading.

The pi values can be any number from -128 to 127, and the pr values anything from 0 to 255. If the pitch of the sound reaches 255 with some steps remaining, the sound will wrap around to 0. This gives the effect of a jump from a very high to a very low pitch, but this can be very effective for sounds using rapidly-unfolding envelopes.

Okay, so we have changed the pitch of the sound in the first section. We can do other pitch shifts in sections 2 and 3 in exactly the same way. What numbers do we need if we wanted to start on middle C, glissando UP two (note) octaves, go down by one octave (to the C above middle C) then go UP to G, and have a change in pitch every 4 centiseconds?

      s   = 4
      pi1 = 1
      pi2 = -1
      pi3 = 1   
      pr1 = 96          
      pr2 = 48         
      pr3 = 28

In the first section the pitch will change by 1 * 96 = 96, in the second section it will change by -1 * 48 = -48 (down an octave) and in the third section 1 * 28 = 28 (up a fifth). Alternatively:

      s   = 4
      pi1 = 2
      pi2 = -2
      pi3 = 2
      pr1 = 48
      pr2 = 24
      pr3 = 14

Multiplying each pi number by the corresponding pr number gives the same results, 2 * 48 = 96 (up two octaves), -2 * 24 = -48 (down an octave), and 2 * 14 = 28 (up a fifth) though it's not very meaningful to do it like this.

Finally, what will the duration of each section be? The scaling factor is 4, so:

      Section 1: 4 * ABS(pi1*pr1) = 4 * 96 = 384 centiseconds
      Section 2: 4 * ABS(pi2*pr1) = 4 * 48 = 192 centiseconds
      Section 3; 4 * ABS(pi3*pr1) = 4 * 28 = 112 centiseconds
                                             688 centiseconds TOTAL

So we would need a duration in a SOUND statement of 688 DIV 5 = 137 (about 7 seconds). Clearly this is too long if you wanted a short, effective sound in a game or something. But it demonstrates why we should be aware that the Envelope may not have fully 'opened' by the time the sound, as specified in the SOUND statement, has run out.

The ENVELOPE statement in full is:

      ENVELOPE 1,4, 1,-1,1,96,48,48, 127,0,0,-127,127,127
      SOUND 1,1,52,137

Finally, here's an exercise for you to work on for next time. How can you play the scale of C major using only two Envelopes and three sound statements?

Next issue we'll discuss the last six parameters of the ENVELOPE statement which constitute the amplitude (volume) envelope. Electron owners are welcome to join in, but remember you can't control the volume of a sound on your machine so only the envelope editor will be of use.