Sounding Out The Micro (Part 2)
Sound sampling Dodolator and the Kaleidosound picture machine for the Apple
A sound sampling Dodolator and Kaleidosound picture machine for the Apple
This month, we're going to get wrapped up in the sound sampling side of the micro, but, firstly, it might be sensible to have a brief recap of some of last month's ideas.
Since music is all about notes of particular frequencies for particular durations, the first thing any program aimed at music synthesis must be able to do is to act as an accurate timekeeper. So, in the simple examples we looked at for toggling cassette relays, output port bits, or whatever, there were two timed loops involved in generating the less than dulcet musical tones, which we can illustrate with the diagram in Figure 1.
The round brackets enclose the instructions required to perform the off/on toggle and set up an inner frequency-controlling loop. The two delays will determine the frequency of the toggling and can also be used to alter the mark/space ratio so that variable pulse-width waveforms can be outputted. The square brackets set up an outer loop that determines the number of circuits around the inner loop and, therefore, the duration of the note.
That's basically where we got to last time. The other thing that might also have sunk in is that there's a multiplicity of ways by which one can get sounds out of a micro to the outside world, and, so far, we've touched on methods involving radio interference, hammering printers, clattering cassette relays, speaker toggling, output port bits, and cassette interfaces. Whoever said that micros were silent creatures?
The main aim of this month's shindig is to introduce a further technique into the micro's repertoire, namely digital-to-analogue conversion, since this is ultimately the only means to a quality end. We could approach this from the waveform table angle, but it's probably more interesting to start off with another way of filling up memory with the numerical representation of sounds, i.e. by using analogue-to-digital conversion. Also, this practical implementation of the digitisation principle is what everyone inside and outside the media seems to be fascinated by, and, given that it's the only way you could stuff Mrs Slocombe's pussy inside a micro, who's arguing with that!
There's also a good deal of satisfaction to be had from making a lot out of a little (like the aforesaid feline), and so, for the moment, we're going to stick with the simplicity of the cassette interface in the Apple (sorry, all you BBC Micro/Spectrum/Commodore/etc. owners, but the Apple makes life rather easier than you lot in that respect!) and see what more we can get out of (and put into) it. There's another factor behind my backing of cassette port techniques, and that's the simple fact that cassette ports are cheaper than additional hardware for analogue-to-digital and digital-to-analogue conversion. In these times of financial stringency and cut-backs (yawn), that's not to be sneezed at. Mind you, the results are somewhat far removed from the DIN or any other Hi-Fi standard.
For good or worse, sound sampling seems to have become the '80s catchword, but the current exploitation of the technique prompts me to proffer a new version of an old adage, namely, "it's not what you do, it's how you use it." Sound sampling tends to be synonymous with sound regurgitation at present, and we've still got a long way to go as far as using natural (or whatever) sounds creatively is concerned. However, diatribes apart, let's have a go at some sound sampling on ye venerable olde Apple.
For starters, Listing 1 is a machine code program that turns the Apple into a sort of frequency analyzer, given some suitable audio directed into the cassette input. However, rather than just being content to display a stream of frequencies on the screen, Kaleidosound goes one step further, in terms of amusement value, by producing a kaleidoscopic display of LORES coloured blocks pulsating symmetrically out from the centre (high frequency) to edge (low frequency).
Patience is required in entering this wadge of code — so take it slowly! Once that has been done, the program can be saved onto disk from the monitor with the command:
BRUNning this file will then run the program automatically. Note that nothing will happen on-screen until some audio is plugged into the appropriate orifice. The level required isn't particularly critical, but a headphones output from an amplifier or tape deck seems to work well. Avoid excessively high input levels as this may lead to overloading of the input stage of the cassette circuitry.
So, what's going on here? Well, sound sampling works on the principle of making a continuous series of measurements of the amplitude of a waveform at any given instant. If the sampled sound happened to be a square wave, then the series of measurements might look something like Figure 2 and read as follows:
-1 -1 -1 -1 -1 +1 +1 +1 +1 +1 -1 -1 -1 -1 -1 +1 +1 +1 +1 +1
The values indicate that the waveform switches between just two amplitude states, positive and negative or high and low, and that the two states occur for the same amount of time. It's also apparent that a new cycle of the state switching occurs every 10 samples. So, if these samples were being taken 10,000 times a second, this would imply that the input waveform had a frequency of 10,000/10 cycles per second, or 1000 Hz.
On the other hand, the sampled values might instead appear like Figure 3 with measurements along these lines:
-1 -1 -1 +1 +1 +1 +1 +1 +1 +1 -1 -1 -1 +1 +1 +1 +1 +1 +1 +1
Even though there are still the same 10 samples for each cycle of the waveform, the equality between the times spent in each state is now no longer apparent. In fact, the sampled waveform now represents a pulse wave with 70% of its period spent in the high state, rather than the 50% in the case of the previous square wave.
Let's take one further example:
0 .7 1 .7 0 -.7 -1 -.7 0 .7 1 .7 0 -.7 -1 -.7 0
Here, we've got a rather different situation, but all makes sense when you plot the values out on a graph as in Figure 4. This time, the period between repeats of the waveform cycle is 8 samples, which translates to a frequency of 10,000/8 cycles per second, or 1250 Hz. What we've done here, then, is to sample a sine wave by converting the continually changing amplitude of the waveform into a series of discrete values that can be plotted on graph paper, drooled over, or, more constructively, stored in memory for processing at a later date.
However, the cassette input is designed for sampling waveforms like those in Figures 2 and 3, rather than registering all the amplitude subtleties of the sine wave of Figure 4 or any other complex waveform.
The Kaleidosound 'frequency analysis' program therefore works on a very basic level, in that it merely uses the cassette input circuitry to sample the audio input and ascertain its frequency. Each time the signal applied to the input circuit swings from positive to negative, or vice versa, the input circuitry changes state, e.g. if it was sending ones, it'll change to sending zeros — the action of a bi-stable comparator. The Kaleidosound program is able to inspect the state of the cassette input circuit by looking at location $C060, and, by doing this repeatedly (thereby establishing the sampling rate), it is able to get a reasonably accurate idea of the number of peaks in the stream of waveforms being fed into the Apple by this route. As a frequency detection method, this is a bit on the dubious side, since high amplitude high harmonics will create a lot of peaks within a single cycle of the fundamental and therefore fool the cassette sampling into thinking that it's receiving a high frequency input. Still, it's the principle of the thing that counts, and, apart from being fun, it serves as a useful introduction to at least one aspect of sound digitisation.
The other side of the sampling business is the measurement of all the amplitude values that fall in between the highs and the lows. The 1-bit resolution of the average cassette input circuit is a bit stuck at this point, but the quirks of the Apple's circuitry enables a cunning way around this limitation to be pursued. As we'll see shortly, it's possible to put this into practice in the context of a program that both digitizes and regurgitates sound via the cassette port. In general, though, the more bits of resolution the better the sampling accuracy is likely to be. The other vital component of the sampling equation is the sampling rate, since the faster a wave is changing (ie., the higher the frequency or the more high harmonics are present) the more often it's necessary to take a sample to check that the waveform hasn't done the dirty on you and changed its amplitude level since the last sample. What this boils down to is the all-important Nyquist sampling theorem, which states that the maximum sampling bandwidth is half the sampling rate. So, a 10 kHz sampling rate would be fine for waveforms of up to 5 kHz in frequency.
If life was full of square waves merrily switching away between zero and 255, one probably could cope with just 1 bit of resolution, but, with more musical waveforms with rather less in the way of rough edges, even 8 bits pose something of a problem. The point is that if a waveform presented itself with an amplitude value of 122.42 for sampling, then the only solution for 8-bit conversion is to take the nearest integer value, ie., 122. This is known as 'rounding down' or 'rounding up', and the long and the short of it is that the continuous stream of samples includes a similarly continuous stream of small sampling, or 'quantisation', errors. Unfortunately, such errors make themselves felt as noise throughout the audio spectrum, and no so-called anti-aliasing filter is going to do anything about that. So, next time you're listening to an Emulator, Fairlight, alphaSyntauri, or just about any other system on the market, enjoy that quantisation noise!
As we mentioned back at the beginning of the Macro Music series, the man behind all these digitisation techniques was Max Mathews at Bell Telephone Labs. The first practical implementation of these ideas was a program that subjected digitised speech to all manner of deliberately unpleasant processing, such that, when it finally emerged from the Bell Labs digital-to-analogue converter, it was replete with the grunge, noise, distortion, and treble cut typical of the modern telephone system. Funnily enough, the Apple sound-sampling program coming up next does roughly the same, though here it's a limitation of the sampling/regurgitation achievable with the basic Apple, rather than a deliberate ploy to assault the average reader's shell-likes.
In fact, it could be said that the 'Dodolator' is something of a dead duck as far as music's concerned, but that's to be expected from the low sampling rate (around 2.7 kHz) that's used. In compensation, the fair whack of memory (100 pages, or 25K) set aside for storage means that just over 9 seconds can be sampled. As you may have surmised, sampling is all about swings and roundabouts!
The reason why sampling is possible with the basic Apple lies in some extra components built into the cassette circuitry that make analogue life more susceptible to sampling than you might otherwise imagine from a straightforward DC comparator. In fact, there's a series capacitor and shunt resistor in the cassette input that acts as a high-pass filter, or, in mathematical terms, as a differentiator. Then, on the output side of things, there's a series resistor and shunt capacitor which acts as an integrator. Put the two together and you have a means of forcing the change of state comparator circuitry to sense some of the subtleties of amplitude variation and impart this information to the output circuitry on playback. That explanation is the bare bones of the situation, and, for more on that subject, I'd refer you to the original article describing the technique in Call-A.P.P.L.E., September '81.
In contrast to the lengthy whack of machine code used in the Kaleidosound program, the machine-code routines that do the sampling and regurgitation for the Dodolator are pretty straightforward. The one factor that's crucial with this unusual sampling technique is that timing in the taking and sending of samples must be closely matched, or otherwise you'll end up with a glorified buzz. So, there are five NOP instructions built into the sampling routine that delay the proceedings slightly to match the playback. As the routines stand, the maximum sampling time is around 9 seconds. This can be extended with the loss of fidelity (fidelity? What fidelity!) to just over 12 seconds by adding in a monitor WAIT routine by changing the byte at $0317 and $0377 to A8 rather than the present B3. In addition to the two routines, there's also a short Applesoft BASIC program that runs the whole shishkaboodle.
Firstly, here's the sampling routine to enter from the monitor (CALL- 151):
Secondly, the regurgitation routine:
Everything that comes in between the two routines is garbage, so don't worry about whatever else you see after the RTSs that end the two routines. Whilst still in the monitor, save the machine code on disk with the following command:
When the BASIC program is run, it'll BLOAD this particular file into the right place in memory. Running the program will display a menu from which you can make various choices as to your interaction with the Dodolator.
The ONERR at the beginning of the program will provide some degree of anti-idiot-trapping against premature crashes, but pushing your luck is inadvisable! Input to the Apple's 'cassette in' socket is probably best taken from the headphones socket of an amplifier or mixer, and, in practice, it seems to like being driven quite hard (give me more...). It's also worth fiddling about with treble EQ levels so as to optimise the differentiation process. Playback is both via the Apple speaker and the 'cassette out' socket. The program allows you to both select into how many pages you want the sample to go when recording, and also to set up a repeating loop out of those pages on playback.
When you're using the Dodolator, bear in mind that it's not designed for music (whoever heard of sampling music at 2.7 kHz and getting decent results?!). However, if you're careful, it will sample a monophonic synth input quite effectively, adding a generous helping of fuzz and dirt in the process. The thing to remember is that polyphonic input = complex input = complex waveforms = lousy sampling = horrible sound!
We'll probably return to the Dodolator in the future if and when there are any improvements to be incorporated. Any suggestions will be gratefully received!
Next month, we go slightly higher up the quality scale with a digital-to-analogue converter plus some intriguing programs for producing music with the minimum of input hassle.
Feature by David Ellis
mu:zines is the result of thousands of hours of effort, and will require many thousands more going forward to reach our goals of getting all this content online.
If you value this resource, you can support this project - it really helps!