As of last time, we had an RC oscillator using a digital pot as a variable resistor. The digital pot provides 256 settings, so the synth can play 256 different frequencies, but none of these "notes" corresponds to a note in the tempered scale, which is what we would need to play conventional music.
So that got me thinking about the simplest way to generate tempered notes, and here's what I came up with. If we wire up two digital pots in parallel, we get 256 * 256 = 65536 different frequencies, and it turns out that there is a useful range in the middle where the notes are only a few cents apart (a cent is 1/100 of a semitone, and a semitone is the difference in frequency between two adjacent keys on the piano).
So there should be a setting for the two pots that yields any desired frequency, within 2-3 cents, which is a tolerable accuracy of tuning for most listeners.
Here's the wiring of the digital pots from last time:
And here's what it looks like now with the second pot in parallel:
The only difference is the two wires the connect the two pots, and a new yellow jumper that connects the output of the oscillator back to the Arduino. Why? Because we are going to tune the oscillator by sweeping through the pot settings and measuring the output frequencies.
Here's what the Arduino code looks like:
https://github.com/AllenDowney/a-bad-synthesizer/blob/master/synthesizer/synthesizer.ino
And here's the Python code that reads data off the serial port and processes it:
https://github.com/AllenDowney/a-bad-synthesizer/blob/master/python/tuner.py
During the measurement phase, the tuner reads data off the Arduino and writes it to a Redis database. During the processing phase, it reads the database and makes a lookup table that maps from MIDI note to the digital pot settings. It generates tables that looks like this:
https://github.com/AllenDowney/a-bad-synthesizer/blob/master/libraries/scale/table.h
I put the table into a library so any Arduino sketch that wants to play MIDI notes can import it. I'll show an example (and an audio file) next time.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.