Pulse Width Modulation and How 1-bit Music Works
Wednesday, 5th December 2012, 23:34
One thing that always fascinated me, not in the sense that I wondered how it worked, but just in the sense that I stared at it (with my ears if that's possible) for hours on end, was the music of Tim Follin on the ZX Spectrum. Since then, I'm older and wiser, and I know far more about the limitations of the beeper inside the Speccy than I did then, and I'm even more gobsmacked.
Before I show a quick clip of his work, and I have no idea how much of the code he wrote to produce it, whether it was entirely his or he just made these great tunes with someone else's code, let me just explain something.
Here I Go Explaining Something
At the centre of the ZX Spectrum, there was not a Z80 CPU, well yes there was one, but that wasn't what made a Speccy a Speccy, after all the Gameboy also used a Z80 chip, and it wasn't the only other machine to do so. No, what really makes a Spectrum a Spectrum, is the ULA chip.
It controls (if that is the right word) so much of the hardware, from the video display to the sound output. Now when I say it controls the sound output, it has a pin which can be on, or off, and that's it. And hopefully by now you are starting to think, on or off? No in-between?
You've seen sine waves, how can you do sine waves when you can only turn the output on or off? They'd be square, surely! Yes, they would be. You'll have heard a square wave already, somewhere, even if you don't realise it. So now take a step further, how would you represent two combined square waves at different frequencies, basically play two notes at a time?
And if you can figure that out in your head, then start to think how the hell you do this...
So Go On, How Does 1-bit Music Work?
Well the answer is in the title, Pulse Width Modulation, which takes into account a very important factor about the conversion of a digital signal that is either on or off, to an analogue device such as a speaker, that has many states in-between.
If you set the voltage to a speaker at its maximum value, representing the on digital state, it doesn't reach that state immediately, and further more if you cut that voltage before it gets there, it will begin to return to it's default minimum state as the rubber to which it is attached pulls.
Armed with that knowledge, you can see that for any given speaker, it takes a certain time to respond to a full on signal, and if instead of sending a full on signal you actually send a sequence of pulses at a specified length, you can actually push the speaker to a partially on position.
Here is a great picture from Wikipedia, which shows how you can create an effective sine wave from a set of square wave pulses:
A good way to appreciate the principal is to lie down on the floor with a ping pong ball in your mouth, and then try blowing a long continuous pulse of air, which should push the ball to it's highest point, and then experiment with long pulses that have short gaps, followed by short pulses with short gaps.
I guess with a lot of practise you could probably make the ball float up and down in a sine wave this way, personally it's not a party trick your friends are going to enjoy that much, but if you do manage it I'd love to see a YouTube video. :) No cheating and using different strength breaths though!
Go Go Complicated Waveforms
So from here all you really need now is to create a waveform which represents the sounds you wish to make, whether it be some funky synthesis like Mr Follin, or speech samples. Then calculate the pulse width modulated square wave that creates it, that requires knowledge of the clock speed which the computer you use is capable of, and how long the instructions you need to do the actual work of turning the beeper on and off take.
I never said it was easy, I'm just happy to finally understand it, even if I've not actually tried to do it myself.
I'll leave you with my personal favourite Tim Follin track, his Agent X 2 theme tune, on a 128K Spectrum. I just love the fact that it uses the beeper for all the hard work, and then the actual three channel swanky AY sound-chip to add nothing more than a snare drum into the mix.