I don’t like lugging my laptop up to my MIDI keyboard every time I want to play music, so I have been writing a MIDI synth that uses an Arduino and Collin Cunningham’s MidiVox Shield.
The MidiVox shield comes with software called Healer, a synthesizer with several waveforms, a resonant filter, and traditional envelope controls. Healer is nice, but it’s monophonic: it only plays one note at a time. I want a polyphonic synth, so I can play crazy chords.
Here are some notes on making a polyphonic MIDI synth from scratch.
First step? Make a noise. There are a number of ways to get sound output from an Arduino, and MidiVox is an excellent option. It has a high quality, 12-bit digital to analog converter on board: the MCP4921. The DAC is controlled by the Arduino’s microcontroller via the SPI bus, which can update it often enough to produce quality audio. It drives a headphone jack, so I can make secret noises that don’t annoy my wife.
This is the same DAC used in Adafruit’s Wave Shield. That board uses a different pin (PD2, you’ll see PB1 used below) as a chip select, so this code won’t work on one without modification. I intend to control my synth via MIDI, so the Wave Shield isn’t a single solution anyway.
I drive the DAC from a timer interrupt on the microcontroller. Human hearing is very sensitive to small changes in frequency, so we need the DAC to be updated on as stable a clock as possible. This code sets up a 15.6kHz timer and alternates the output of the DAC on each tick. This makes a square wave tone that is high pitched (half the clock rate, 7.8kHz).
uint16_t sample = 0; void setup() { cli(); /* Enable interrupt on timer2 == 127, with clk/8 prescaler. At 16MHz, this gives a timer interrupt at 15625Hz. */ TIMSK2 = (1 << OCIE2A); OCR2A = 127; /* clear/reset timer on match */ TCCR2A = 1<<WGM21 | 0<<WGM20; /* CTC mode, reset on match */ TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20; /* clk, /8 prescaler */ SPCR = 0x50; SPSR = 0x01; DDRB |= 0x2E; PORTB |= (1<<1); sei(); } ISR(TIMER2_COMPA_vect) { /* OCR2A has been cleared, per TCCR2A above */ OCR2A = 127; /* set the lower 12 bytes of sample */ sample = ~sample & 0xFFF; PORTB &= ~(1<<1); /* buffered, 1x gain, active mode */ SPDR = highByte(sample) | 0x70; while (!(SPSR & (1<<SPIF))); SPDR = lowByte(sample); while (!(SPSR & (1<<SPIF))); PORTB |= (1<<1); } void loop() { }
Ok, I managed to make my microcontroller whine like a TV. Next I tried to generate a more complex waveform. I dropped a table into the code with 8 bits worth of sine wave data, a perfect tone.
uint16_t sample = 0; uint8_t pos = 0; const uint8_t sine[] = { 0x80, 0x83, 0x86, 0x89, 0x8C, 0x8F, 0x92, 0x95, 0x98, 0x9B, 0x9E, 0xA2, 0xA5, 0xA7, 0xAA, 0xAD, 0xB0, 0xB3, 0xB6, 0xB9, 0xBC, 0xBE, 0xC1, 0xC4, 0xC6, 0xC9, 0xCB, 0xCE, 0xD0, 0xD3, 0xD5, 0xD7, 0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEB, 0xED, 0xEE, 0xF0, 0xF1, 0xF3, 0xF4, 0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD, 0xFD, 0xFC, 0xFB, 0xFA, 0xFA, 0xF9, 0xF8, 0xF6, 0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEE, 0xED, 0xEB, 0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC, 0xDA, 0xD7, 0xD5, 0xD3, 0xD0, 0xCE, 0xCB, 0xC9, 0xC6, 0xC4, 0xC1, 0xBE, 0xBC, 0xB9, 0xB6, 0xB3, 0xB0, 0xAD, 0xAA, 0xA7, 0xA5, 0xA2, 0x9E, 0x9B, 0x98, 0x95, 0x92, 0x8F, 0x8C, 0x89, 0x86, 0x83, 0x80, 0x7D, 0x7A, 0x77, 0x74, 0x71, 0x6E, 0x6B, 0x68, 0x65, 0x62, 0x5E, 0x5B, 0x59, 0x56, 0x53, 0x50, 0x4D, 0x4A, 0x47, 0x44, 0x42, 0x3F, 0x3C, 0x3A, 0x37, 0x35, 0x32, 0x30, 0x2D, 0x2B, 0x29, 0x26, 0x24, 0x22, 0x20, 0x1E, 0x1C, 0x1A, 0x18, 0x16, 0x15, 0x13, 0x12, 0x10, 0x0F, 0x0D, 0x0C, 0x0B, 0x0A, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04, 0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0A, 0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x12, 0x13, 0x15, 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24, 0x26, 0x29, 0x2B, 0x2D, 0x30, 0x32, 0x35, 0x37, 0x3A, 0x3C, 0x3F, 0x42, 0x44, 0x47, 0x4A, 0x4D, 0x50, 0x53, 0x56, 0x59, 0x5B, 0x5E, 0x62, 0x65, 0x68, 0x6B, 0x6E, 0x71, 0x74, 0x77, 0x7A, 0x7D }; void setup() { cli(); /* Enable interrupt on timer2 == 127, with clk/8 prescaler. At 16MHz, this gives a timer interrupt at 15625Hz. */ TIMSK2 = (1 << OCIE2A); OCR2A = 127; /* clear/reset timer on match */ TCCR2A = 1<<WGM21 | 0<<WGM20; /* CTC mode, reset on match */ TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20; /* clk, /8 prescaler */ SPCR = 0x50; SPSR = 0x01; DDRB |= 0x2E; PORTB |= (1<<1); sei(); } ISR(TIMER2_COMPA_vect) { /* OCR2A has been cleared, per TCCR2A above */ OCR2A = 127; /* shift left for volume */ sample = sine[pos++] << 3; PORTB &= ~(1<<1); /* buffered, 1x gain, active mode */ SPDR = highByte(sample) | 0x70; while (!(SPSR & (1<<SPIF))); SPDR = lowByte(sample); while (!(SPSR & (1<<SPIF))); PORTB |= (1<<1); } void loop() { }
When pos rolls over, the sine wave starts its cycle again. This gives us a mellow tone at 15625Hz / 256 = 61Hz (quite low pitched).
How about making a tone at any frequency? I have tied the DAC updates to a 15625Hz clock. I’ve seen that changing the index into the sine table on every tick will produce a 61Hz tone. What I need is an index that moves faster or slower than that, according to the frequency of tone I want to generate.
There is an efficient way to generate an index that increments by frequency, and that is Direct Digital Synthesis. I can update the DAC with the right value at each sample tick, and its output will mimic the waveform of a running sine wave oscillator.
First, I calculate the amount the output wave’s phase should shift in each clock cycle. Then, I track the current phase by adding that value to an accumulator in each tick.
Calculating the value that needs to be sent to the DAC becomes easy: I use the accumulator as the index into the sine table. I can even use a 16-bit accumulator, choose the phase increment accordingly, and the sine index becomes the top 8 bits of the accumulator.
Let me rephrase, because direct synthesis phase digital accumulator increment blah blah blah.
To create a sine wave at a frequency freq Hz, sampling at 15625Hz:
uint16_t sample = 0; /* incr = freq * (2^16 / 15625) * So for 440Hz, incr = 1845 */ uint16_t incr = 1845; /* oscillator position */ uint16_t pos = 0; const uint8_t sine[] = { 0x80, 0x83, 0x86, 0x89, 0x8C, 0x8F, 0x92, 0x95, 0x98, 0x9B, 0x9E, 0xA2, 0xA5, 0xA7, 0xAA, 0xAD, 0xB0, 0xB3, 0xB6, 0xB9, 0xBC, 0xBE, 0xC1, 0xC4, 0xC6, 0xC9, 0xCB, 0xCE, 0xD0, 0xD3, 0xD5, 0xD7, 0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEB, 0xED, 0xEE, 0xF0, 0xF1, 0xF3, 0xF4, 0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD, 0xFD, 0xFC, 0xFB, 0xFA, 0xFA, 0xF9, 0xF8, 0xF6, 0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEE, 0xED, 0xEB, 0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC, 0xDA, 0xD7, 0xD5, 0xD3, 0xD0, 0xCE, 0xCB, 0xC9, 0xC6, 0xC4, 0xC1, 0xBE, 0xBC, 0xB9, 0xB6, 0xB3, 0xB0, 0xAD, 0xAA, 0xA7, 0xA5, 0xA2, 0x9E, 0x9B, 0x98, 0x95, 0x92, 0x8F, 0x8C, 0x89, 0x86, 0x83, 0x80, 0x7D, 0x7A, 0x77, 0x74, 0x71, 0x6E, 0x6B, 0x68, 0x65, 0x62, 0x5E, 0x5B, 0x59, 0x56, 0x53, 0x50, 0x4D, 0x4A, 0x47, 0x44, 0x42, 0x3F, 0x3C, 0x3A, 0x37, 0x35, 0x32, 0x30, 0x2D, 0x2B, 0x29, 0x26, 0x24, 0x22, 0x20, 0x1E, 0x1C, 0x1A, 0x18, 0x16, 0x15, 0x13, 0x12, 0x10, 0x0F, 0x0D, 0x0C, 0x0B, 0x0A, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04, 0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0A, 0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x12, 0x13, 0x15, 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24, 0x26, 0x29, 0x2B, 0x2D, 0x30, 0x32, 0x35, 0x37, 0x3A, 0x3C, 0x3F, 0x42, 0x44, 0x47, 0x4A, 0x4D, 0x50, 0x53, 0x56, 0x59, 0x5B, 0x5E, 0x62, 0x65, 0x68, 0x6B, 0x6E, 0x71, 0x74, 0x77, 0x7A, 0x7D }; void setup() { cli(); /* Enable interrupt on timer2 == 127, with clk/8 prescaler. At 16MHz, this gives a timer interrupt at 15625Hz. */ TIMSK2 = (1 << OCIE2A); OCR2A = 127; /* clear/reset timer on match */ TCCR2A = 1<<WGM21 | 0<<WGM20; /* CTC mode, reset on match */ TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20; /* clk, /8 prescaler */ SPCR = 0x50; SPSR = 0x01; DDRB |= 0x2E; PORTB |= (1<<1); sei(); } ISR(TIMER2_COMPA_vect) { /* OCR2A has been cleared, per TCCR2A above */ OCR2A = 127; pos += incr; /* shift left a couple of bits for more volume */ sample = sine[highByte(pos)] << 2; PORTB &= ~(1<<1); /* buffered, 1x gain, active mode */ SPDR = highByte(sample) | 0x70; while (!(SPSR & (1<<SPIF))); SPDR = lowByte(sample); while (!(SPSR & (1<<SPIF))); PORTB |= (1<<1); } void loop() { }
There are two great things about Direct Digital Synthesis.
First, it’s pretty fast even on the Arduino’s ATmega328 microcontroller. It’s all integer math, one 16-bit addition and an array index. GCC even optimizes away the mess with the high 8 bits—it generates code that uses the register containing pos’s high byte, so the shift and mask aren’t executed.
Most of the microcontroller cycles are spent accessing memory: copying pos to and from RAM while adding incr, and making the index into the sine table.
Speed isn’t an issue for one oscillator, but with a 15625Hz clock each sample has only 64 microseconds to be calculated. (SPOILER ALERT: I ran out of time trying to advance and mix about 30 oscillators).
Second, DDS allows any waveform to be pushed through the DAC. I’m using a sine wave, but that table could be created from any data. For a little more processor time, I could even calculate waveform values on the fly.
Let’s wrap this thing up with a chord. Three sine oscillators, C major.
incr1 = 261.63 * 65536 / 15625 = 1097; /* C4 */ incr2 = 329.63 * 65536 / 15625 = 1383; /* E4 */ incr3 = 392.00 * 65536 / 15625 = 1644; /* G4 */
So the final code is:
uint16_t sample = 0; /* incr = freq * 2^16 / 15625 */ uint16_t incr1 = 1097; /* C4 */ uint16_t incr2 = 1383; /* E4 */ uint16_t incr3 = 1644; /* G4 */ uint16_t pos1 = 0; uint16_t pos2 = 0; uint16_t pos3 = 0; const uint8_t sine[] = { 0x80, 0x83, 0x86, 0x89, 0x8C, 0x8F, 0x92, 0x95, 0x98, 0x9B, 0x9E, 0xA2, 0xA5, 0xA7, 0xAA, 0xAD, 0xB0, 0xB3, 0xB6, 0xB9, 0xBC, 0xBE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, 0x06, 0x06, 0x07, 0x08, 0x0A, 0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x12, 0x13, 0x15, 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24, 0x26, 0x29, 0x2B, 0x2D, 0x30, 0x32, 0x35, 0x37, 0x3A, 0x3C, 0x3F, 0x42, 0x44, 0x47, 0x4A, 0x4D, 0x50, 0x53, 0x56, 0x59, 0x5B, 0x5E, 0x62, 0x65, 0x68, 0x6B, 0x6E, 0x71, 0x74, 0x77, 0x7A, 0x7D }; void setup() { cli(); /* Enable interrupt on timer2 == 127, with clk/8 prescaler. At 16MHz, this gives a timer interrupt at 15625Hz. */ TIMSK2 = (1 << OCIE2A); OCR2A = 127; /* clear/reset timer on match */ TCCR2A = 1<<WGM21 | 0<<WGM20; /* CTC mode, reset on match */ TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20; /* clk, /8 prescaler */ SPCR = 0x50; SPSR = 0x01; DDRB |= 0x2E; PORTB |= (1<<1); sei(); } ISR(TIMER2_COMPA_vect) { /* OCR2A has been cleared, per TCCR2A above */ OCR2A = 127; pos1 += incr1; pos2 += incr2; pos3 += incr3; sample = sine[highByte(pos1)] + sine[highByte(pos2)] + sine[highByte(pos3)]; PORTB &= ~(1<<1); /* buffered, 1x gain, active mode */ SPDR = highByte(sample) | 0x70; while (!(SPSR & (1<<SPIF))); SPDR = lowByte(sample); while (!(SPSR & (1<<SPIF))); PORTB |= (1<<1); } void loop() { }
Listen to this chord long enough, and you might start to hear the siren call of the Hammond organ. I already knew that they’re essentially a large number of sine oscillators mixed together. From here, my synth gained MIDI control and I started to simulate the tones of a Hammond B series.
You can find the current status of that project at roto.