The Beeb Body Building Course Part 10

Make your micro the star of your Christmas party with the latest in our upgrade series.

Volume 1

Number 10

December 1983

Make your Micro the life and soul of the party

By MIKE COOK

WITH the festive season once more upon us, here is a little add-on that will make your BBC Micro a welcome guest at any party. As well as being available in kit form, it can also be purchased ready-built for the less intrepid among you.

It is a little-known fact that I used to be a disc jockey in my early youth. The truth of the matter was that I enjoyed wiring up all the amplifiers, turntables and tape decks.

Such was the mess of wiring spaghetti that I was the only one who would dare operate it. It also fell to my lot to wire up the disco lights and to flash them on and off. This was in the Dark Ages when such a thing was a novelty.

However, the control of disco lights was one of the first things I automated. A natural progression was to make the music control the switching of the lights.

When lights are fixed, the results produced do not get very complicated, but the overall effect adds great atmosphere to any party.

I have always had the feeling that something better could be done and for a long time have wanted to do something similar with a computer.

However, not until the advent of the BBC Micro has a computer incorporated sufficient facilities to make this project worthwhile.

This month's exercise is therefore an add-on which produces patterns related to music or, if you have built the solid state relay (see the September issue of the Micro User), controls conventional spot lights.

If you look at music on an oscilloscope you will see a complex ever-changing waveform. In fact it looks a mess. This is because when you use an oscilloscope you are looking at the signal as a plot of amplitude against time, known as a "time domain" view of the signal.

If, however, you take a short section of music (say half a second) and make a plot of the amplitude at each specific pitch you have what is known as a "frequency domain" view of the signal. This can be displayed using an instrument known as a spectrum analyser. (No, it doesn't work out what is wrong with rubber computers!)

The frequency domain view of a signal (or, as we say, its spectrum) shows, at any instant, the composition of the sound in terms of pitch. For most music, this means which instruments were playing at the instant the spectrum was taken.

So all we need for a picture of the sound is to feed our hi-fi into a spectrum analyser. As these cost around £3,000 it is not a very practical proposition.

We can make a crude simulation of a spectrum analyser by filtering the sound so that only a restricted band of frequencies is produced. This can be done with a band-pass filter.

If several band-pass filters are used, all at different frequencies, we can build up an approximation of the spectrum of the sound.

Figure I illustrates the principle: the sound is split up into four frequency-bands and the output of each filter is measured in order to build up the spectrum.

Figure I: Block diagram of our spectrum analyser

In terms of our ultimate goal, the samples can then be used as parameters to create patterns on the computer's display. The problem is to build four filters which will cover the same range of frequencies as the music.

A simple filter can be made with a capacitor and resistor. (See Figure II.) A capacitor does not allow DC to flow through it, only AC. The higher the frequency of this current, the more easily it flows.

Figure II: A low pass filter

In jargon we say the capacitor has a high impedance for low frequencies and a low impedance for high frequencies. Therefore a capacitor can be thought of as a frequency-dependent resistor.

So you will see in Figure II that if the input is a high frequency the output will be small, because the impedance of the capacitor is much smaller than the impedance of the resistor.

Conversely, for a low frequency input, the capacitor has such a high impedance it can be considered effectively removed. So the output will be the same as the input. Because low frequencies are passed through unchanged and high frequencies are attenuated (reduced) this is known as a low-pass filter.

See if you can work out why the circuit in Figure III is a high-pass filter.

Figure III: A high pass filter

By combining a high-pass and a low-pass filter we can produce a band-pass filter. Unfortunately, the circuits in Figures II and III are not usable in practice, due to the impedances of the input and output devices. We have to use amplifiers to reduce the effect of "loading impedances" as they are called.

When an amplifier is included in a filter it is called an active filter (not "Active Fitter" as one Ministry of Defence typist once titled a report of mine).

The simplest way of making an active filter is to use an operational amplifier (or op-amp).

Unfortunately, most op-amps need a split power supply, that is, one with a positive and negative voltage as well as a zero volt rail. This is available from the auxiliary power supply socket of the BBC Micro.

However, if you have a negative supply rail there is a danger that the output of the op-amp will go negative. This in itself is no problem, but if it is connected to the analogue input of the computer it will damage the A/D converter chip.

A better solution is to use an op-amp which does not need a negative supply. These little-known amplifiers work with input currents (as opposed to input voltages as used by the more conventional op-amps). They are known as Norton amplifiers and the symbol is shown in Figure IV.

Figure IV: A Norton amplifier

These current-differencing amplifiers have been designed to work off single-supply voltages over a wide range (4 to 36 volts) and as such are ideal for use with digital electronics.

Like any op-amp they have two inputs. The first is known as the positive input, because any current flowing into it will be amplified and cause the output to rise. The other input is known as the negative or inverting input - when current flows into it, the output falls (goes in a negative direction).

The trick of using op-amps is to arrange the feedback circuit (connecting the output to the input) so that these currents balance out and a stable output is achieved for a stable input. This is not nearly as difficult as it sounds.

Consider Figure V. When no voltage is applied at point P, the output voltage will rise due to the current flowing into the positive input. However, as it rises it makes more current flow into the negative input, which tends to force it down. This reaches equilibrium when the same amount of current is flowing in both inputs of the op-amp.

Figure V: An inverting amplifier

When the voltage at point P starts to rise, more current flows into the negative input causing the output to fall, thus reducing the amount of feedback current, until again an equilibrium point is reached.

But this time, some of the current in the negative input will come from the signal, and some from the output (feedback). As the output falls when the input signal increases, this configuration is known as an inverting amplifier. You should be able to see that the gain of such an amplifier is controlled by the values of the resistors in the circuit.

If you understood that on the first reading you are doing very well. If not, go back and go through it again and sketch yourself little graphs of the input and output - I find it often helps. The Norton amplifier is available in a chip called the LM3900N. There are four of them, all in the same package.

Now, promise me you won't stop reading this article, and go and have a peep at Figure VI. Pretty impressive hey? Well actually it's not that complicated, because as we need four filters, the basic circuit is just repeated four times. If you can bear it, have a closer look and you will see that the same circuit really is repeated. Only the component values differ.

Each strip of circuit corresponds to a separate filter/input circuit. The sound signal comes from the input socket. This is a 7-pin DIN socket wired up in exactly the same way as the cassette input socket of the BBC Micro. In this way you can use your cassette recorder to play the music (let's face it, it never was much good for programs!).

If you have a stereo input to this circuit, one channel is controlled by VR1 and the other by VR2. The input socket is wired up to allow this but if you only have a mono system the two halves should be joined together by making link LI.

If you are feeding the unit from the output of a hi-fi amplifier you can wire it up in parallel with the loudspeakers. If you are using stereo, make sure the common lead goes to earth. To be on the safe side, only wire up one speaker wire on one side and two on the other. If both channels do not work you can then safely swap wires until they do.

As the unit has a very high input impedance it will not place any loading on the sound source. A signal of about two volts RMS will be needed to drive the unit, but the sensitivity can be increased by software as we shall see later.

Construction can be done in a number of ways. I made the original prototype on Vero board, with flying leads connecting it to the computer. However, as it is a circuit which quite a lot of people might be interested in, I have produced a printed circuit board. This is available as a kit, with all the components.

The board uses a 15-way D-type plug and just plugs into the analogue input socket. Two pillars are used at the other end of the board to support it. If you want, however, it may be made in a box to make it look neater, although there is nothing nicer than a well-laid-out circuit.

Construction of the circuit board involves finding the component value from the components list and inserting the appropriate component in the correct place on the printed circuit board. For example, C7 is a 0.1 uF capacitor and should be placed at the component position marked C7 on the printed circuit board.

Note that the resistors only have their number on them, they don't start with 'R'. The diodes need to be placed the right way round. This is shown by the band at one end. Again, match it up with the markings on the printed circuit board.

Apart from the ICs the only other components that need to be the correct way round are the electrolytic capacitors C1 to C4. A small white stripe and a + should be at one end. Put this end to the hole marked + on the board.

The four band-pass filters have their frequencies evenly distributed over the range used for the fundamental notes in most music. The exact frequencies are l00Hz, 220Hz, 560Hz and l000Hz. Now, hands up all those who think I have made a mistake. Are those frequencies evenly distributed? Well in fact they are, as far as the ear is concerned.

This is because the response of the ear is very nearly logarithmic and so these frequencies sound evenly spaced. If you plot them out on a logarithmic scale you will see there is the same space between them.

Now - hold onto your hats - here is the simple version of how the circuit works. The signal is fed into the input of IC la through Rl and R2 resistors and the capacitor C10. The feedback is provided by R3 and C9.

As we saw earlier, capacitors act as frequency-dependent resistors, so the amount of signal flowing into the amplifier both from the source and the feedback are frequency-dependent.

At low frequencies, the capacitors can be considered as open circuits. So no signal gets to the imput of the amplifier.

At high frequencies, the capacitors are a very low impedance. So although the signal gets through to the input,

there is lots of negative feedback and the output does not move very much.

Only at some intermediate frequency is there any output. The resistor R4 provides a steady trickle of current, known as the bias current. This bias supply is provided from a potential divider formed by R41 and R42 and is used to prevent the resistor values becoming too high.

Once the signal has been filtered it is passed to the next stage (1C 1b) which is known as a peak detector.

Capacitor C5 removes any DC component from the signal and feeds it into the input of the next op-amp. Current is also being supplied from R5 and is acting as the bias for this stage. The output of the op-amp is then passed through a diode.

A diode acts as a one-way valve, allowing current to flow from the output of the op-amp into the capacitor, but not allowing the capacitor to discharge back through the amplifier.

Therefore the diode only conducts when the voltage at the op-amp output is higher than that on the capacitor.

Thus the voltage on the capacitor represents the peak voltage from the amplifier. Resistor R8 controls how fast this peak voltage builds up and R9 and R10 allow this voltage to decay. So the voltage fed into the analogue input socket represents the loudness of the music at that instant. This is known as following the envelope of the music.

All we have to do now is to feed our music signal into this circuit and use the values from the analogue input port to draw our patterns.

As usual I have written a program to illustrate how this is done, which gives very good results. But also, as usual, it is only a starting point for you to experiment with.

The signal being fed into the analogue input port is larger than I required, so it is cut down to size with the variable SCALE%. There is also a DC offset voltage on the input. This is removed by the variable OSET%. These values can be changed to increase the sensitivity of the circuit if the input signal is low.

To test the circuit, you only need to type in lines 10 to 180 (omitting line 150) and lines 950 to 1140.

This displays a bar graph of each input channel so you can make sure that it is moving over a good range. If not, adjust the volume into the filters either from the source, or by adjusting the variable resistors. Alternatively, alter the two variables already mentioned.

The procedure SAMP samples the appropriate input channel and scales the result so that it is between 0 and 40. This value is returned in the variable V%. The procedure BAR then prints a line of stars proportional to the size of the input.

The display looks very much like the rows of flashing lights you get on some modern hi-fi systems.

Having got that working, you can now type in the rest of the program (not forgetting to add line 150). This uses the sound in three ways to produce moving patterns which seem to be evocative of the music being played.

Each different procedure is ended by pressing a key. If this were being used at a party you might like to end each display procedure after a certain time has elapsed - not forgetting to zero the time at the start of each procedure . The lines that need changing are 670, 930 and 1360.

In order to speed up the patterns, use is made of a lookup table, X%(N) and Y%(N), to convert the sound input values to screen co-ordinates. These are pre-calculated in the procedure LOOKUP, along with the initialisation of most constants used in the program. Wherever the program is sensitive to speed I have used integer variables.

The procedure POLYGON uses the value of each sound channel to control the position of each corner of a quadrilateral. This changes shape and distorts in sympathy with the music.

A record of each shape is stored in the array OX% and OY% (old X and old Y) and when there are N% shapes displayed the oldest of them is removed to make way for a new one. This prevents the screen becoming cluttered with remnants of old patterns.

The display responds rapidly to the change in music and is very effective, especially if the music contains a lot of variation and sudden chords.

The second display is more subtle. The pattern woven is much more intricate and changes more gradually. The patterns are very beautiful and seem to follow the mood of the music. This type of display is best suited to contemplative music.

The pattern is formed by marking a point on each axis which corresponds to each channel of the sound. Then every point is joined up to every other point. This is repeated for each quadrant to give a pleasing symmetrical effect. Because the pattern is so complex, only two are displayed at any one time.

The final display is a rapid response display. Each quadrant of the screen is devoted to one channel. A triangle is drawn whose size and displacement from the centre are controlled by the sound-level on each channel.

Again, like the first procedure, the variable N% controls the number displayed at any one time.

In all the above procedures, the colour of the display has been chosen at random, as this seemed to give the most pleasing effect. But you could experiment with the choice of colour being made by the level on one of the channels.

I have found that, for a vigorous display, the patterns should be roughly predictable. I seem to get the greatest enjoyment from this when I can use my knowledge of the music to predict when the patterns will change.

I am more than pleased with the results given by the circuit. It has a very hpynotic effect - you could just stare at it all day. My wife made the point that it helps you listen to the music and gives you a focal point to concentrate on.

School music teachers might find it stops a class getting restless and helps the children pick out individual instruments when listening to music.

Unfortunately I cannot tell you how it fares under actual party conditions for two reasons. Firstly, this is being written before Christmas, so there are not many about, but mainly because I never seem to be asked to any. It might be the soap. I must start using some!

Next month we tell you how to cope with a slipped disc. Merry Christmas!

Body Build Pack No. 8:

C1-C4 luF Tantalum capacitor,
C5-C8, C17 0.luF paper capacitor,
C9 C10 4700 pF polystyrene capacitor,
C11 C12 3300 pF polystyrene capacitor,
C13 C14 1000 pF polystyrene capacitor,
C15 C16 470 pF polystyrene capacitor,
D1-D4 1N4148 Diode,
IC1 IC2 LM3900N Norton op-amp,
VR1 VR2 10 Skeleton preset pot,
7 pin DIN socket,
2 14 pin DIL IC sockets,
15 way D-Type plug,
2 16 mm plastic pillars,
2 self-tapping screws,
printed circuit board.

RESISTORS 5%
R1,R31 1M5
R2,R7,R27,R17,R32, R37 33K
R3,R4,R21,R33,R34 3M3
R5,R24,R25,R15,R35, R23 6M8
R6, R9, R10, R16, R19, R20, R26 1M
R29,R30,R36,R39,R40 1M
R8, R38 5K6
R28,R18,R41,R42 10K
R11 2M2
R13,R14 4M7
R22 18K
R12 11K

Figure VI: The sound-to-pattern converter

What the Beeb Body Building packs contain

Upgrade your BBC Micro with these Beeb Body Building Packs.

• Packs 1 and 2: Really use your User Port! These two packs let you safely and simply join your micro to the outside world. The best way to learn about interfacing.

• Pack 3: Give your BBC Micro real data-processing muscle by building yourself a dual cassette system - the way Acorn said you couldn't.

• Packs 4 and 5: These packs allow you to control mains equipment in complete electrical isolation from your micro - the only safe way to do it. Pack 4 is rated at 4 amps, pack 5 at 10 amps.

• Pack 6: All the electronic components you'll need to construct the Micro User light pen, with its special screen-sensing circuit.

• Pack 7: Two interesting experiments that should appeal both to schools and the amateur scientist: investigate the behaviour of a pendulum and study capacitor discharge.

• Pack 8: Construct this versatile sound to pattern converter. Its many applications include running a light show with your BBC Micro. It will also make the micro a resident fixture in many music classes.