Fri, Dec 2, 2022, 5:36pm Starting from Almost Nowhere 1: 1-bit Sound on an Apple ][+
Creation » Programming
(Last updated: Sat, Dec 3, 2022, 3:16am)
O
ne of the first computers my family owned when I was growing up was the Apple ][+. This computer was the most open computer you'd ever want. It was a hacker's dream. It came with a schematic of the circuit of the whole computer, in case you wanted to invent things for it or just modify it somehow. No, really: People used to pass around ways that you could solder in jumper wires on the motherboard to make it do some new trick.

It came with not just gobs of manuals explaining how everything works, but it included full source code listings of the ROM chips. Everything was available to modify, and it was normal to trick out your Apple in some way. The easiest place to start was to create something that plugged in to the game port, since it just required passive resisters and some wires to get it to do something interesting, and it wasn't hard for a teenager to create their own input device. Try that these days.

At any rate, the reason I mention this in my music blog is that it also contained the very worst sound you've ever heard in a computer. It had one volume. The state of the speaker diaphram was either on or off, that's it. You controlled it by ostensibly reading a memory address, which would instead of returning any useful info, trigger the speaker to change state. A classic Wozniak hack. This is 1-bit sound, and it was kinda great for learning about how sound is made, in the same way that Legos are great for learning about how to make buildings.

Making a tone was easy and taught you things about programming and trade-offs as well as how speakers worked in a very basic way. You want a tone to play at 500 Hz? Then you write a program to toggle that speaker diaphram back-n-forth at that speed. Your line of code to toggle the speaker state just turns on the electricity flowing in a loop that creates a magnetic field that pulls the diaphram out, or if it's off, then it relaxes and the diaphram goes back. It's quite a jolt going from nothing to everything in one go, and it pushes air in bursts, and the chain reaction hits your ears in a steady pattern. It's not a sine wave by any means, but you hear it as roughly a certain tone, and all Apple owners from that era know that exact sound.

So maybe you write a loop in BASIC. How fast does your loop go, after all? No idea, so you just make the fastest loop you know how and play it, and you hear a tone. The fact that you even hear it means that it's not going so fast that the frequency is above human hearing, as would happen with a contemporary computer, even a Raspberry Pi Zero. (Extra credit: Do it and let me know what happens.)

I have an Apple ][+ here, but it's not hooked up, so I tried an experiment with an emulator, and it gives pretty accurate results. I created this program, which "peeks" at the memory address in question, that actually toggles the speaker's on/off state in BASIC, using the simplest and fastest loop:
( basic )
10 A = PEEK(-16336)
20 GOTO 10

and that produces the following melodious note:


which, according to Zen Tuner on my phone, is roughly a C note. Adding in a line of code in the middle
( basic )
10 A = PEEK(-16336)
15 PRINT "TESTING!"
20 GOTO 10

produces a much lower, perhaps a low A♯, maybe, because of how much time it takes to display text:


and trying to do something less demanding like just reading a random spot in memory for no reason with:
( basic )
10 A = PEEK(-16336)
15 B = PEEK(800)
20 GOTO 10


gives a slightly less high note of E.

If you bought 3 Apple ][+ machines, which is roughly $7k each in today's money, you could produce a terrible yet haunting C7 chord. You're on your way. (Also, programming in BASIC, congrats, a skill that will keep you well-employed. I'm not sure if I'm being sarcastic.) Play all 3 at once and stretch your imagination to see if that might be a C7 chord. Very bad indeed.

Maybe you learn just a little bit of machine language on the 6502: You learn how to make an infinite loop and read that memory address for the speaker. It's really the best intro to assembly language you could want. You learn how to make a very small program that something that you can perceive, and you get intuitive feedback about the relative speed of doing something in machine code vs higher level languages like BASIC. It's a quick win all around.

What about that 500Hz tone you wanted? It's up to you to experiment with your loops to see what tone they play. Apple ][s don't multitask, so when you run a program you have all the attention of the CPU, so you can at least rely on getting a consistent tone, but it will be hard to figure out how to make your loop go slightly faster or slower in order to make the right tones. There were people who figured this out (though in the faster 6502 assembly), and it was hard enough that they then sold this software to others.

But this was all just an obnoxious learning exercise with no practical end game for most people. What a real user ended up doing was buying a plug-in board, like the great Mockingboard card, which is really where you start to learn about synthesis in the modern sense. Tones with waveforms and volume envelopes, mixed with different kinds of noise. But anything beyond 1-bit is too advanced for this blog post. Stay tuned.

  • J4S (Sat, December 3rd, 2022, 10:43am UTC)
    Don’t you have x3 Apple ][s ? Make that chord !

  • Jeff (Sat, December 3rd, 2022, 8:52pm UTC)
    Ha! You know too well. I do have 3 such machines (Apple ][+, Apple //e and a Laser 128), but still have to figure out how to get the video going. Perhaps I could use my EyeTV as input to my Mac laptop? At any rate, for this, I'll take the coward's way out and just hit play on all 3 of the audio files above. So bad it might be good. Perhaps 1-bit music is due for a resurgence.

Leave a comment