As I alluded to in my last post (earlier today) I had a notion of how I might make this thing actually work. I did some experimenting and sure enough, I’m onto something. There’re still quite a few demons hiding in the details (tuning issues in higher octaves being the most obvious), but I’m at least generating *some* useful tones. In fact, in the middle octave, chords already sound pretty with only very minimal filtering. I think the tuning issues in the higher octaves are there because it’s very hard to be precise on timing and small timing issues are a bigger deal at higher octaves (see footnote if you want to know why that is). I recorded a short demo of what this thing currently sounds like. What you’re hearing are four digital oscillators (square waves) summed together and filtered by a very simple low pass filter (one resistor and one capacitor). The filter smooths out the sound of the square wave substantially, giving it a much softer, rounder tone. There are two styles of random notes in the file. The first part is a series or random chords with all four voices changing together, the second is more chaotic, with each voice randomly changing both pitch and note duration. I think it sounds kinda cool (but I’m a freak).
Footnote: Why are higher octaves more difficult? Higher notes equal higher frequency. A computer can only run so fast and so that leaves less and less time between flipping the digital output to do calculations. The microcontroller I’m using works at 16,000,000 cycles per second (16MHz). Humans can here as high as 20,000Hz (20KHz), but it varies a lot. The highest piano frequency is around 4,000Hz. The way I’m generating sound, one cycle means flipping a digital output pin high and then low, so really I need to do two flips per cycle. That means 8,000 flips per second for the highest piano key. Which means about 2000 operations between flips (if I’m doing all the math and stuff right). That’s really not all that much when you consider that an operation is a very basic unit of computation and any given task (for example… reading an analog input) might take quite a few operations. And that’s not all! Even if it’s possible to get all the work done in between, you still need to get the timing exactly right. Also consider that since the time between cycles is smaller at higher frequencies, small mistakes become a bigger deal! It’s even more pronounced because our perception of pitch is logarithmic in relation to frequency. For example, A4 (440Hz) is one octave higher than A3 (220Hz). Which might make you think that the next octave would be at 660Hz… but it’s not! A5 is 880Hz!