About Me

Curriculum Vitae

A brief list of my current skill set

Bloggybits

How Do Spammers Get My Email Address?
Wednesday, 15th May 2013, 18:03

I think these days I have a pretty good idea

XSLT, node.js 0.10 and a Fun Two Days of Native Modules and Memory Leaks
Thursday, 25th April 2013, 17:14

documentation makes things less cryptic, so lets not write much of it

Fixing CentOS high cpu usage when running as a virtual machine under VirtualBox
Sunday, 21st April 2013, 20:28

innotek rocks! I mean Sun... I mean Oracle...

Repairing a dK'Tronics Keyboard and Scoping Out a ZX Spectrum 48k - Part One
Sunday, 17th March 2013, 23:51

What signals inside of it actually look like

Tabs vs Spaces and Why You Should Always Use Tabs
Monday, 4th March 2013, 19:51

Spaces are bad, just real bad

Why you should ban Amazon's Cloud IPs
Thursday, 27th December 2012, 14:50

And how to do it in nginx, Merry Christmas Amazon

Building Better jQuery DOM Inserts
Thursday, 20th December 2012, 15:18

Break it down baby

SEO Companies - Don't Waste Your Money
Wednesday, 12th December 2012, 16:16

Spammers by any other name

Pulse Width Modulation and How 1-bit Music Works
Wednesday, 5th December 2012, 23:34

Beep beep multi-channel!

Making PDFs from HTML on your webapp in CentOS
Thursday, 29th November 2012, 14:00

Not as easy as it should be

Some Days I Wish For an Async String.replace
Monday, 19th November 2012, 12:59

MinnaHTML to the rescue!

Object Oriented Programming for Javascript Dummies - Part 2
Tuesday, 6th November 2012, 15:33

OOP it up to the OOPballs

Object Oriented Programming for Javascript Dummies - Part 1
Tuesday, 30th October 2012, 16:01

Not using OOP? Are you mad?

Strange Windows Errors and a Bowser in My Event Log
Wednesday, 24th October 2012, 11:10

It's like my own DailyWTF round-up!

Why Do People Come Here?
Monday, 15th October 2012, 15:47

They come to look at porn!

Projects and Sillyness

MAME Cabinet Diary

How I built my own arcade cabinet

Loading Screen Simulator

I don't miss the ZX Spectrum, I still use it!

The Little Guy Chat Room

It's a Pitfall inspired chat room

GPMad MP3

A fully featured MP3 player what I wrote

GP Space Invaders

My first little emulator

GP32 Development Page

Some info and links about this cute little handheld

Disney Nasties

Uncensored images, you must be 18 to view them

Minna's World

The cuttest fluffy puppy in the world

Diary of a Hamster

Learn about how hamsters think, first hand

Utilities

Time Calculator

A simple little online utility for working out how many hours to bill a client

A Few Links

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.

Inertia.

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.

Comments

Add Your Own Comment