Showing posts with label synth. Show all posts
Showing posts with label synth. Show all posts

Friday, November 30, 2012

Quick And Dirty Synth For Arduino Due

The first RCArduino project for the Arduino Due is the Quick And Dirty Synth. The synth is a simple showcase for audio output through the DAC running at the Audio CD sample rate of 44.1Khz.


Creative Commons License
RCArduino Quick And Dirty Synth by RCArduino is licensed under a
Creative Commons Attribution 3.0 Unported License.
Based on a work at rcarduino.blogspot.com.


The showcase is based on the simplest synth engine I could create - 3 counters counting up at a rate controlled by three analog inputs.

It might sound simple but its a surprisingly rich sounding synth engine, here is how it works - 

Two of the counters generate ramp waveforms - think about it, they are counting up from 0 to 4095, when they overflow, they go back to 0 and start the count again.


Example Ramp Output -  




Example Ramp At A Higher Frequency -



These ramp waveforms are summed together at the output to generate a more complex waveform - two ramp waves of independent frequency superimposed on each other.

Example - Two triangle at frequency F and 3F mixed together to create a new output waveform.

The third ramp waveform is used to control the pitch. It is not mixed with the output waveforms instead it achieves pitch control by resetting the first two waveforms.


Output waveform reset at frequency determined by third counter - notice that the counter is not directly present in the output but controls the repetition/synchronization of the output which in turn creates the pitch. 


To hear the technique in action connect three potentiometers to your Arduino Due on analog inputs 0,1,2 and an audio amplifier to DAC0.

All of the Audio projects on RCArduino use the following simple amplifier circuit -
http://rcarduino.blogspot.com/2012/08/adding-audio-to-arduino-projects.html

To learn more about the synthesis technique used in the quick and dirty synthesizer see the following link -

http://rcarduino.blogspot.com/2012/08/adding-audio-to-arduino-projects.html

The code 

Caution : The SAM3X8E microcontroller at the heart of the Arduino Due is less able to sink and source current than the AVR family of chips used in the 8-Bit Arduinos. I would suggest using a series resistor of around 500 Ohms when connecting the Arduino Due DAC to an external device. See this thread on the Arduino forum for more information - http://arduino.cc/forum/index.php/topic,139733.0.html
 
// RCArduino Quick And Dirty Synth for Arduino Due// RCArduino Quick And Dirty Synth by RCArduino is licensed under a Creative Commons Attribution 3.0 Unported License.
// Based on a work at rcarduino.blogspot.com.

// For helpful background information on Arduino Due Timer Configuration, refer to the following link
// thanks to Sebastian Vik
// http://arduino.cc/forum/index.php?action=post;topic=130423.15;num_replies=20

// The following folders within the arduino install provide access to source code and documentation
// its very low level, the documentation is next to useless, but it might help someone
// C:\arduino-1.5.1r2\hardware\arduino\sam\system\libsam\source
// C:/arduino-1.5.1r2/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/html/tc1.html
// C:/arduino-1.5.1r2/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/html/SAM3XA.html
// C:\arduino-1.5.1r2\hardware\arduino\sam\system\CMSIS\CMSIS\Documentation

// These are the clock frequencies available to the timers /2,/8,/32,/128
// 84Mhz/2 = 42.000 MHz
// 84Mhz/8 = 10.500 MHz
// 84Mhz/32 = 2.625 MHz
// 84Mhz/128 = 656.250 KHz
//
// 44.1Khz = CD Sample Rate
// Lets aim for as close to the CD Sample Rate as we can get -
//
// 42Mhz/44.1Khz = 952.38
// 10.5Mhz/44.1Khz = 238.09 // best fit divide by 8 = TIMER_CLOCK2 and 238 ticks per sample
// 2.625Hmz/44.1Khz = 59.5
// 656Khz/44.1Khz = 14.88

// 84Mhz/44.1Khz = 1904 instructions per tick

// These variables represent our synth engine, its similar to a vastly simplified Auduino and has a similar sound.
// Phase Accumulator controls the main pitch, grain1 and grain two phase accumulator control the pitch for the two grains
// The grainPhaseAccumulators are essentially simple counters, when you count up and overflow back to 0, you generate a ramp waveform
// we generate two and mix them together.
uint32_t ulPhaseAccumulator = 0; // 32 bit phase accumulator, if we shift >> 20 bits we get a 12 bit value for our output ADC
volatile uint32_t ulPhaseIncrement = 0;   // 32 bit phase increment, see below
uint32_t ulGrain1PhaseAccumulator = 0; // 32 bit phase accumulator, if we shift >> 20 bits we get a 12 bit value for our output ADC
volatile uint32_t ulGrain1PhaseIncrement = 0;   // 32 bit phase increment, see below
uint32_t ulGrain2PhaseAccumulator = 0; // 32 bit phase accumulator, if we shift >> 20 bits we get a 12 bit value for our output ADC
volatile uint32_t ulGrain2PhaseIncrement = 0;   // 32 bit phase increment, see below

// full waveform = 0 to 4294967296
// Phase Increment for 1 Hz =(4294967296/44100) = 1Hz
// Phase Increment for frequency F = (4294967296/44100)*F
#define SAMPLE_RATE 44100.0
#define TICKS_PER_CYCLE (4294967296.0/SAMPLE_RATE)

// Create a table to hold the phase increments we need to generate midi note frequencies at our 44.1Khz sample rate
#define MIDI_NOTES 128
uint32_t nMidiPhaseIncrement[MIDI_NOTES];

void createNoteTable(float fSampleRate)
{
  for(uint32_t unMidiNote = 0;unMidiNote < MIDI_NOTES;unMidiNote++)
  {
    // Correct calculation for frequency
    Serial.print(unMidiNote);
    Serial.print(" ");
    float fFrequency = ((pow(2.0,(unMidiNote-69.0)/12.0)) * 440.0);
    Serial.print(fFrequency);
    Serial.print(" ");
    nMidiPhaseIncrement[unMidiNote] = fFrequency*TICKS_PER_CYCLE;
    Serial.println(nMidiPhaseIncrement[unMidiNote]);
  }
}

void setup()
{
  Serial.begin(9600);

  createNoteTable(SAMPLE_RATE);
 
  /* turn on the timer clock in the power management controller */
  pmc_set_writeprotect(false);
  pmc_enable_periph_clk(ID_TC4);

  /* we want wavesel 01 with RC */
  TC_Configure(/* clock */TC1,/* channel */1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2);
  TC_SetRC(TC1, 1, 238); // sets <> 44.1 Khz interrupt rate
  TC_Start(TC1, 1);
 
  // enable timer interrupts on the timer
  TC1->TC_CHANNEL[1].TC_IER=TC_IER_CPCS;
  TC1->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS;
 
  /* Enable the interrupt in the nested vector interrupt controller */
  /* TC4_IRQn where 4 is the timer number * timer channels (3) + the channel number (=(1*3)+1) for timer1 channel1 */
  NVIC_EnableIRQ(TC4_IRQn);

  // this is a cheat - enable the DAC
  analogWrite(DAC0,0);
}

void loop()
{
  // read analog input 0 drop the range from 0-1024 to 0-127 with a right shift 3 places,
  // then look up the phaseIncrement required to generate the note in our nMidiPhaseIncrement table
  uint32_t ulInput = analogRead(0);
  ulPhaseIncrement = nMidiPhaseIncrement[ulInput>>3];
 
  // set the phase increment for grains 1 and 2, we do not want a precise frequency for these,
  // they set the character of the note rather than pitch
  ulGrain1PhaseIncrement = analogRead(1)<<18;
  ulGrain2PhaseIncrement = analogRead(2)<<18;
}

void TC4_Handler()
{
  // We need to get the status to clear it and allow the interrupt to fire again
  TC_GetStatus(TC1, 1);
 
  ulPhaseAccumulator += ulPhaseIncrement;   // 32 bit phase increment, see below
  // if the phase accumulator over flows - we have been through one cycle at the current pitch,
  // now we need to reset the grains ready for our next cycle
  if(ulPhaseAccumulator < ulPhaseIncrement)
  {
   ulGrain1PhaseAccumulator = 0;
   ulGrain2PhaseAccumulator = 0;
  }
  else
  {
   // increment the grains
   ulGrain1PhaseAccumulator += ulGrain1PhaseIncrement;
   ulGrain2PhaseAccumulator += ulGrain2PhaseIncrement;
  }
 
  // mix the grains by adding them together - to the result of adding these two 32bit values into the 12 bit ADC Output
  // we shift both values right 21 places to sum two 11 bit values giving a 12 bit result.
  uint32_t ulOutput = (ulGrain1PhaseAccumulator>>21)+(ulGrain2PhaseAccumulator>>21);
 
  // we cheated and user analogWrite to enable the dac, but here we want to be fast so
  // write directly 
  dacc_write_conversion_data(DACC_INTERFACE, ulOutput);
}

Thursday, November 15, 2012

Auduino With Delay

This is the first in a series of posts introducing simple audio effects that can be used in micro controller projects.

Next Week - Bit Crushing effects

The Delay effect is one of the simplest and most effective enhancements we can add to our audio projects.

The delay effect works by recording the output as it is being generated and then mixing this sound back in with itself - after a delay. The result should be familiar to anyone who has every played an electric guitar through an amp with reverb.

In the case of the Auduino synthesizer the result is a mild echo effect and slightly smoother, more metallic sound - the effect can be turned on or off through a push button in the code provided below.

How do we create the delay effect
Delay is very simple to add in a microcontroller, all we need is a block of memory to record the output in.

The larger the block of memory, the longer the delay we can record and the deeper the effect.

In this case we are using a 1K block of memory in the array named sDelayBuffer -

// Duane B
// rcarduino.blogspot.com
// 15/11/2012
// Very simple ring buffer delay
// we record the output in this array
// and then mix it back in with the output as the buffer wraps around
// can be switched on and off by a button on DELAY_BUTTON
#define MAX_DELAY 1024
unsigned char sDelayBuffer[MAX_DELAY];
unsigned int nDelayCounter = 0;
unsigned char bDelay;

The other modification is inside the interrupt service routine which generates the Audiuno output, essentially what we are doing is adding the sound we recorded 1/8th of a second ago on top of the current output value -


  // Duane B
  // rcarduino.blogspot.com
  // 15/11/2012
  // add a button to set bDelay true or false to turn delay on and off
  if(bDelay)
  {
    // Output to PWM (this is faster than using analogWrite) 
    // Here we add the delay buffer to the output value, this produces
    // an subtle echo effect, the delay buffer is effectivley replaying the sound from
    // 1/8th of a second ago.
 
    LED_PORT |= 1 << LED_BIT; // Faster than using digitalWrite
    PWM_VALUE = (output + (sDelayBuffer[nDelayCounter]))>>1;
   
    // add the new output to the buffer so we can use it when the buffer next wraps around
    sDelayBuffer[nDelayCounter] = PWM_VALUE;
    nDelayCounter++;
    if(nDelayCounter == MAX_DELAY)
    {
      nDelayCounter = 0;
    }
  }
  else
  {
    LED_PORT &= ~(1 << LED_BIT); // Faster than using digitalWrite
   
    PWM_VALUE = output;
  }

We test whether delay is enabled, if it is we calculate the output value by adding the initial output to the earlier recorded output from our delay buffer. After outputting this combined value we record it in the delay buffer replacing the value we just used. Over time, the code cycles through the delay buffer over and over again, mixing the current output with a sample from 1/8th of a second back - a bit like playing your instrument in a large hall where the distinct sound is the result of the current sound being constantly mixed with its echo.

Thats all there is to generating delay in a micro controller synth engine - exactly the same code is used to create the delay effect in the RCArduino Five Dollar Synthesizer.


The RCArduino Five Dollar Synthesizer is another audio project enhanced with this delay effect -

http://rcarduino.blogspot.com/2012/10/five-dollar-synthesiser.html 


Further Development
The amount of delay we can provide is determined to the memory we use to record the samples. In the Auduino we are using 1K which at an 8K play back rate gives us 125ms of delay. This can be increased by bit crushing the samples - using 4 bits per sample we get 250ms, 2 bits gets us half a second, with 1 bit we can get a whole second. Unfortunately initial experiments suggest that the effect is largely lost when applying these techniques, its a bit like shouting into a cave and getting a different echo back - your ears just don't buy it.

Auduino Accreditation
The Auduino is an original work by Peter Knight, the original project can be found here -
http://code.google.com/p/tinkerit/wiki/Auduino

Auduino with delay

Auduino with delay is a very slight modification by Duane B (rcarduino) to the original work of Peter Knight.

Notes
- This code also include the volatile fix which allows the Auduino to work correctly in Arduino 1.0 and later
- Remember to use a pull up or pull down resistor if you are not using a push button or switch for the delay button or if your more comfortable modifying the code, replace the button code with true or false.
- LED 13, now indicates whether delay is on or off.


// Auduino, the Lo-Fi granular synthesiser
//
// by Peter Knight, Tinker.it http://tinker.it
//
// Help:      http://code.google.com/p/tinkerit/wiki/Auduino
// More help: http://groups.google.com/group/auduino
//
// Analog in 0: Grain 1 pitch
// Analog in 1: Grain 2 decay
// Analog in 2: Grain 1 decay
// Analog in 3: Grain 2 pitch
// Analog in 4: Grain repetition frequency
//
// Digital 3: Audio out (Digital 11 on ATmega8)
//
// Changelog:
// 19 Nov 2008: Added support for ATmega8 boards
// 21 Mar 2009: Added support for ATmega328 boards
// 7 Apr 2009: Fixed interrupt vector for ATmega328 boards
// 8 Apr 2009: Added support for ATmega1280 boards (Arduino Mega)

#include <avr/io.h>
#include <avr/interrupt.h>

uint16_t syncPhaseAcc;
volatile uint16_t syncPhaseInc;
uint16_t grainPhaseAcc;
volatile uint16_t grainPhaseInc;
uint16_t grainAmp;
volatile uint8_t grainDecay;
uint16_t grain2PhaseAcc;
volatile uint16_t grain2PhaseInc;
uint16_t grain2Amp;
volatile uint8_t grain2Decay;

// Map Analogue channels
#define SYNC_CONTROL         (4)
#define GRAIN_FREQ_CONTROL   (0)
#define GRAIN_DECAY_CONTROL  (2)
#define GRAIN2_FREQ_CONTROL  (3)
#define GRAIN2_DECAY_CONTROL (1)

// DB
#define SMOOTH_PIN 8


// Changing these will also requires rewriting audioOn()

#if defined(__AVR_ATmega8__)
//
// On old ATmega8 boards.
//    Output is on pin 11
//
#define LED_PIN       13
#define LED_PORT      PORTB
#define LED_BIT       5
#define PWM_PIN       11
#define PWM_VALUE     OCR2
#define PWM_INTERRUPT TIMER2_OVF_vect



#elif defined(__AVR_ATmega1280__)
//
// On the Arduino Mega
//    Output is on pin 3
//
#define LED_PIN       13
#define LED_PORT      PORTB
#define LED_BIT       7
#define PWM_PIN       3
#define PWM_VALUE     OCR3C
#define PWM_INTERRUPT TIMER3_OVF_vect
#else
//
// For modern ATmega168 and ATmega328 boards
//    Output is on pin 3
//
#define PWM_PIN       3
#define PWM_VALUE     OCR2B
#define LED_PIN       13
#define LED_PORT      PORTB
#define LED_BIT       5
#define PWM_INTERRUPT TIMER2_OVF_vect
#endif

// Duane B
// rcarduino.blogspot.com
// 15/11/2012
// Very simple ring buffer delay
// we record the output in this array
// and then mix it back in with the output as the buffer wraps around
// can be switched on and off by a button on DELAY_BUTTON
#define MAX_DELAY 1024
unsigned char sDelayBuffer[MAX_DELAY];
unsigned int nDelayCounter = 0;
unsigned char bDelay;

#define DELAY_BUTTON 4


// Smooth logarithmic mapping
//
uint16_t antilogTable[] = {
  64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
  54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
  45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
  38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
};
uint16_t mapPhaseInc(uint16_t input) {
  return (antilogTable[input & 0x3f]) >> (input >> 6);
}

// Stepped chromatic mapping
//
uint16_t midiTable[] = {
  17,18,19,20,22,23,24,26,27,29,31,32,34,36,38,41,43,46,48,51,54,58,61,65,69,73,
  77,82,86,92,97,103,109,115,122,129,137,145,154,163,173,183,194,206,218,231,
  244,259,274,291,308,326,346,366,388,411,435,461,489,518,549,581,616,652,691,
  732,776,822,871,923,978,1036,1097,1163,1232,1305,1383,1465,1552,1644,1742,
  1845,1955,2071,2195,2325,2463,2610,2765,2930,3104,3288,3484,3691,3910,4143,
  4389,4650,4927,5220,5530,5859,6207,6577,6968,7382,7821,8286,8779,9301,9854,
  10440,11060,11718,12415,13153,13935,14764,15642,16572,17557,18601,19708,20879,
  22121,23436,24830,26306
};
uint16_t mapMidi(uint16_t input) {
  return (midiTable[(1023-input) >> 3]);
}

// Stepped Pentatonic mapping
//
uint16_t pentatonicTable[54] = {
  0,19,22,26,29,32,38,43,51,58,65,77,86,103,115,129,154,173,206,231,259,308,346,
  411,461,518,616,691,822,923,1036,1232,1383,1644,1845,2071,2463,2765,3288,
  3691,4143,4927,5530,6577,7382,8286,9854,11060,13153,14764,16572,19708,22121,26306
};

uint16_t mapPentatonic(uint16_t input) {
  uint8_t value = (1023-input) / (1024/53);
  return (pentatonicTable[value]);
}


void audioOn() {
#if defined(__AVR_ATmega8__)
  // ATmega8 has different registers
  TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
  TIMSK = _BV(TOIE2);
#elif defined(__AVR_ATmega1280__)
  TCCR3A = _BV(COM3C1) | _BV(WGM30);
  TCCR3B = _BV(CS30);
  TIMSK3 = _BV(TOIE3);
#else
  // Set up PWM to 31.25kHz, phase accurate
  TCCR2A = _BV(COM2B1) | _BV(WGM20);
  TCCR2B = _BV(CS20);
  TIMSK2 = _BV(TOIE2);
#endif
}


void setup() {
  pinMode(PWM_PIN,OUTPUT);
  audioOn();
  pinMode(LED_PIN,OUTPUT);

  pinMode(DELAY_BUTTON,INPUT);
 
  // set pin mode and turn on pull up so that default mode
  // is PENTATONIC, pull the pin low to switch to smooth
  pinMode(SMOOTH_PIN,INPUT);
  digitalWrite(SMOOTH_PIN,HIGH);
}

void loop() {
  // The loop is pretty simple - it just updates the parameters for the oscillators.
  //
  // Avoid using any functions that make extensive use of interrupts, or turn interrupts off.
  // They will cause clicks and poops in the audio.
 
  // defaults to pentatonic stepped tones, pull pin low for smooth frequency without distinct tones
  //    syncPhaseInc = mapPhaseInc(analogRead(SYNC_CONTROL)) / 4;

  syncPhaseInc = mapPentatonic(analogRead(SYNC_CONTROL));
 
  // updated 29/01/2013
  // pull the DELAY_BUTTON pin high for delay, low for no delay
  // use either a pull up/pull down resistor
  // or a pull up resistor with a toggle switch between the pin and ground
  bDelay = digitalRead(DELAY_BUTTON);
 
  // Stepped mapping to MIDI notes: C, Db, D, Eb, E, F...
  //syncPhaseInc = mapMidi(analogRead(SYNC_CONTROL));
 
  // Stepped pentatonic mapping: D, E, G, A, B
 

  grainPhaseInc  = mapPhaseInc(analogRead(GRAIN_FREQ_CONTROL)) / 2;
  grainDecay     = analogRead(GRAIN_DECAY_CONTROL) / 8;
  grain2PhaseInc = mapPhaseInc(analogRead(GRAIN2_FREQ_CONTROL)) / 2;
  grain2Decay    = analogRead(GRAIN2_DECAY_CONTROL) / 4;
}


SIGNAL(PWM_INTERRUPT)
{
  uint8_t value;
  uint16_t output;

  syncPhaseAcc += syncPhaseInc;
  if (syncPhaseAcc < syncPhaseInc) {
    // Time to start the next grain
    grainPhaseAcc = 0;
    grainAmp = 0x7fff;
    grain2PhaseAcc = 0;
    grain2Amp = 0x7fff;
//    LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
  }
 
  // Increment the phase of the grain oscillators
  grainPhaseAcc += grainPhaseInc;
  grain2PhaseAcc += grain2PhaseInc;

  // Convert phase into a triangle wave
  value = (grainPhaseAcc >> 7) & 0xff;
  if (grainPhaseAcc & 0x8000) value = ~value;
  // Multiply by current grain amplitude to get sample
  output = value * (grainAmp >> 8);

  // Repeat for second grain
  value = (grain2PhaseAcc >> 7) & 0xff;
  if (grain2PhaseAcc & 0x8000) value = ~value;
  output += value * (grain2Amp >> 8);

  // Make the grain amplitudes decay by a factor every sample (exponential decay)
  grainAmp -= (grainAmp >> 8) * grainDecay;
  grain2Amp -= (grain2Amp >> 8) * grain2Decay;

  // Scale output to the available range, clipping if necessary
  output >>= 9;
  if (output > 255) output = 255;

  // Duane B
  // rcarduino.blogspot.com
  // 15/11/2012
  // add a button to set bDelay true or false to turn delay on and off
  if(bDelay)
  {
    // Output to PWM (this is faster than using analogWrite) 
    // Here we add the delay buffer to the output value, this produces
    // an subtle echo effect, the delay buffer is effectivley replaying the sound from
    // 1/8th of a second ago.
 
    LED_PORT |= 1 << LED_BIT; // Faster than using digitalWrite
    PWM_VALUE = (output + (sDelayBuffer[nDelayCounter]))>>1;
   
    // add the new output to the buffer so we can use it when the buffer next wraps around
    sDelayBuffer[nDelayCounter] = PWM_VALUE;
    nDelayCounter++;
    if(nDelayCounter == MAX_DELAY)
    {
      nDelayCounter = 0;
    }
  }
  else
  {
    LED_PORT &= ~(1 << LED_BIT); // Faster than using digitalWrite
   
    PWM_VALUE = output;
  }
}


Sunday, October 7, 2012

Arduino Modular Synthesizer - Part One

The RC Arduino site has featured a number of Audio projects recently, and while they all produce very different sounds they all use the same basic technique to generate their sound.

With this in mind an obvious development of these projects would be a modular synthesizer which allows the user to easily combine the techniques used by individual projects to produce new instruments.

Modular Synthesizer
A modular synthesizer is traditionally a large analogue device often including a mix and match of vintage components. Early models were often enormous DIY machines that could easily take up a whole wall of a room.

Current models are still large in comparison to digital synthesizers this is partly due to the physical user interface which is also what gives these machines their character.

A typical modular synthesizer user interface - Analog inputs to control the module oscillators and a patchwork of wires to direct the signals between different modules.
Original photo by Nina Richards


Arduino as a Digital Modular Synth ?

While the Arduino is a digital device, the Auduino and Illutron B projects both demonstrate the appeal of using analog inputs to control an Arduino based digital synthesizer.

Auduino Illutron B

Auduino Features - 
A grain synthesizer which uses two triangle waveforms with independently controlled frequency and decay to create a more complex waveform. A fifth control is then used to set the pitch by controlling the repetition rate of the complex waveform.

The tone of the waveform can be dramatically altered by adjusting the frequencies of the two component waveforms.


Illutron B Features
A wavetable synthesizer which uses pre calculated waveforms stored within the program memory. These waveforms are played back at different speeds to produce sound at the required pitch. To further increase the musical quality of the waveforms, they are combined with an envelope, this describes how the sound developes over time and can provide an approximation of many musical instruments.




More information and examples of both of these projects can be found here -

http://rcarduino.blogspot.com/2012/08/the-must-build-arduino-project-illutron.html
http://rcarduino.blogspot.com/2012/08/adding-audio-to-arduino-projects.html

Taking it further
 
The goal of this series of posts is to extend these ideas by -

1) Building a set of software components that will provide digital versions of the most common modules found in a modular synthesizer.


2) Building a simple, low cost simulated patch panel to direct how the digital sound will be built and processed by the software running on the Arduino.

Progress so far

Below are two examples of vastly different Arduino synthesisers built for this project with the same four oscillators connected in different ways - in effect this is a demonstration of the concept of an Arduino modular synthesiser.

XLT2 - eXtremely Limited Techno Toy


 
The eXtremeley Limited Techno Toy or XLT2

As an initial experiment I wanted to build something that could be built from a few common components and that would be able to generate a reasonable range of new sounds.

The first result is the XLT2 - eXtemeley Limited Techno Toy.

An added bonus is that this synth and the Gated Grain Synth below are both directly compatible with the Auduino synth circuit, so if you have never built an Auduino before, now you have two more reasons to build one.

The XLT2 features two audio oscillators that are able to generate square, ramp or sine waveforms at audio frequencies.

These audio waveforms are then combined with individual low frequency oscillators ( LFOs ) which again generate square, ramp or sine waves but at much lower frequencies. These low frequency waveforms may not be audible on their own, but when we direct them to control the amplitude (volume) of an audio signal they can add a rythm, pulse or vibrato effect.

Each LFO also has an associated LED which reflects the LFO output both in frequency and profile - i.e. a squarewave will switch the LED hard on, then hard off whereas a sinewave will fade on and fade off reflecting the effect the LFO has on the audio signal.

A final control is provided to mix the two channels together according to a user selected ratio.

The Gated Grain Synthesizer

Once the XLT2 was up and running I wanted to try creating a similar sound to the Auduino using the same four oscillators as the XLT2, the grain synthesizer is the result.

This synth can produce a sound similar to the Auduino or even a passable violin if a sinewave is used as the output gate.

The main feature of the Gated Grain synth is that it sounds very different from the XLT2 but is built from the same four oscillators driving different aspects of the audio signal.

Proof of concept

The difference in sound between these two devices demonstrates the potential for an Arduino based synthesiser which allows the end user to configure the sound generation path using a simulated patch pannel. A follow up post will explore some of the options for implementing the patch pannel and how the relevant routing can be implemented at run time in software.

Getting Serious and moving to the Arduino Due

While this project has lots of potential for development the limited processing power of the 8-bit 16Mhz Arduino UNO will only get us so far.

The soon to be release Arduino Due platform however will allow the idea to be taken much further.

The Due has a built in analog to digital converter giving a true analog output rather than the UNOs PWM output, this together with higher resolution should provide far better sound quality.

The higher processing speeds of the Arduino Due will also allow for more oscillators and more importantly - complex relationships between the oscillators. It should also be possible to implement some software filters.

Perhaps the most exciting possibility is the high sampling rate offered by the Due, it may be possible to capture samples directly with the due and use them there and then as part of the synthesizer.




As the project stands, the user must select the waveforms at compile time however an obvious development would be to add a button for each oscillator allowing the user to cycle through the available waveforms. As my intention is to provide this function and others through a simulated patch panel, I will leave it to the user to add these buttons.


Note : The Audio output in the video is through a PC Speaker driven by the simple amplifier outlined in this post -

http://rcarduino.blogspot.com/2012/08/adding-audio-to-arduino-projects.html



#define SAMPLE_MAX (65535.0)
#define SAMPLE_FREQUENCY (8000.0)
#define TIMER1_FREQUENCY 2000000
#define UPDATE_RATE 8000

#define MIDI_NOTE_MAX 127

#define MIX_MIDDLE 127

#define GRAIN1_FREQUENCY_INPUT A0
#define GRAIN1_AMPLITUDE_INPUT A1

#define GRAIN2_FREQUENCY_INPUT A2
#define GRAIN2_AMPLITUDE_INPUT A3

#define LFO_FREQUENCY_INPUT A4
#define LFO_AMPLITUDE_INPUT A5

#define GRAIN1_SHAPE_INPUT 4
#define GRAIN2_SHAPE_INPUT 7
#define LFO_SHAPE_INPUT 8

#define PWM_OUT OCR0A

// Will put this in progmem - eventually
/*PROGMEM*/
unsigned char sineWave[256] =
{
127    ,    //0
130    ,    //1
133    ,    //2
136    ,    //3
139    ,    //4
142    ,    //5
145    ,    //6
148    ,    //7
151    ,    //8
154    ,    //9
157    ,    //10
160    ,    //11
163    ,    //12
166    ,    //13
169    ,    //14
172    ,    //15
175    ,    //16
178    ,    //17
181    ,    //18
184    ,    //19
186    ,    //20
189    ,    //21
192    ,    //22
194    ,    //23
197    ,    //24
200    ,    //25
202    ,    //26
205    ,    //27
207    ,    //28
209    ,    //29
212    ,    //30
214    ,    //31
216    ,    //32
218    ,    //33
221    ,    //34
223    ,    //35
225    ,    //36
227    ,    //37
229    ,    //38
230    ,    //39
232    ,    //40
234    ,    //41
235    ,    //42
237    ,    //43
239    ,    //44
240    ,    //45
241    ,    //46
243    ,    //47
244    ,    //48
245    ,    //49
246    ,    //50
247    ,    //51
248    ,    //52
249    ,    //53
250    ,    //54
250    ,    //55
251    ,    //56
252    ,    //57
252    ,    //58
253    ,    //59
253    ,    //60
253    ,    //61
253    ,    //62
253    ,    //63
254    ,    //64
253    ,    //65
253    ,    //66
253    ,    //67
253    ,    //68
253    ,    //69
252    ,    //70
252    ,    //71
251    ,    //72
250    ,    //73
250    ,    //74
249    ,    //75
248    ,    //76
247    ,    //77
246    ,    //78
245    ,    //79
244    ,    //80
243    ,    //81
241    ,    //82
240    ,    //83
239    ,    //84
237    ,    //85
235    ,    //86
234    ,    //87
232    ,    //88
230    ,    //89
229    ,    //90
227    ,    //91
225    ,    //92
223    ,    //93
221    ,    //94
218    ,    //95
216    ,    //96
214    ,    //97
212    ,    //98
209    ,    //99
207    ,    //100
205    ,    //101
202    ,    //102
200    ,    //103
197    ,    //104
194    ,    //105
192    ,    //106
189    ,    //107
186    ,    //108
184    ,    //109
181    ,    //110
178    ,    //111
175    ,    //112
172    ,    //113
169    ,    //114
166    ,    //115
163    ,    //116
160    ,    //117
157    ,    //118
154    ,    //119
151    ,    //120
148    ,    //121
145    ,    //122
142    ,    //123
139    ,    //124
136    ,    //125
133    ,    //126
130    ,    //127
127    ,    //128
124    ,    //129
121    ,    //130
118    ,    //131
115    ,    //132
112    ,    //133
109    ,    //134
106    ,    //135
103    ,    //136
100    ,    //137
97    ,    //138
94    ,    //139
91    ,    //140
88    ,    //141
85    ,    //142
82    ,    //143
79    ,    //144
76    ,    //145
73    ,    //146
70    ,    //147
68    ,    //148
65    ,    //149
62    ,    //150
60    ,    //151
57    ,    //152
54    ,    //153
52    ,    //154
49    ,    //155
47    ,    //156
45    ,    //157
42    ,    //158
40    ,    //159
38    ,    //160
36    ,    //161
33    ,    //162
31    ,    //163
29    ,    //164
27    ,    //165
25    ,    //166
24    ,    //167
22    ,    //168
20    ,    //169
19    ,    //170
17    ,    //171
15    ,    //172
14    ,    //173
13    ,    //174
11    ,    //175
10    ,    //176
9    ,    //177
8    ,    //178
7    ,    //179
6    ,    //180
5    ,    //181
4    ,    //182
4    ,    //183
3    ,    //184
2    ,    //185
2    ,    //186
1    ,    //187
1    ,    //188
1    ,    //189
1    ,    //190
1    ,    //191
0    ,    //192
1    ,    //193
1    ,    //194
1    ,    //195
1    ,    //196
1    ,    //197
2    ,    //198
2    ,    //199
3    ,    //200
4    ,    //201
4    ,    //202
5    ,    //203
6    ,    //204
7    ,    //205
8    ,    //206
9    ,    //207
10    ,    //208
11    ,    //209
13    ,    //210
14    ,    //211
15    ,    //212
17    ,    //213
19    ,    //214
20    ,    //215
22    ,    //216
24    ,    //217
25    ,    //218
27    ,    //219
29    ,    //220
31    ,    //221
33    ,    //222
36    ,    //223
38    ,    //224
40    ,    //225
42    ,    //226
45    ,    //227
47    ,    //228
49    ,    //229
52    ,    //230
54    ,    //231
57    ,    //232
60    ,    //233
62    ,    //234
65    ,    //235
68    ,    //236
70    ,    //237
73    ,    //238
76    ,    //239
79    ,    //240
82    ,    //241
85    ,    //242
88    ,    //243
91    ,    //244
94    ,    //245
97    ,    //246
100    ,    //247
103    ,    //248
106    ,    //249
109    ,    //250
112    ,    //251
115    ,    //252
118    ,    //253
121    ,    //254
123        //255
};

// should pre calculate these and store them in progmem to save memory
// for prototyping, lets calculate them and use ram
unsigned char rampWave[256];
unsigned char squareWave[256];

// used to convert midi note numbers into the increments required to generate the note in the ISR
PROGMEM unsigned int midiNoteToWavePhaseIncrement[128] =
{
 66 // 0,8.18,66.98,66
,70 // 1,8.66,70.96,70
,75 // 2,9.18,75.18,75
,79 // 3,9.72,79.65,79
,84 // 4,10.30,84.38,84
,89 // 5,10.91,89.40,89
,94 // 6,11.56,94.72,94
,100 // 7,12.25,100.35,100
,106 // 8,12.98,106.32,106
,112 // 9,13.75,112.64,112
,119 // 10,14.57,119.34,119
,126 // 11,15.43,126.43,126
,133 // 12,16.35,133.95,133
,141 // 13,17.32,141.92,141
,150 // 14,18.35,150.35,150
,159 // 15,19.45,159.29,159
,168 // 16,20.60,168.77,168
,178 // 17,21.83,178.80,178
,189 // 18,23.12,189.43,189
,200 // 19,24.50,200.70,200
,212 // 20,25.96,212.63,212
,225 // 21,27.50,225.28,225
,238 // 22,29.14,238.67,238
,252 // 23,30.87,252.86,252
,267 // 24,32.70,267.90,267
,283 // 25,34.65,283.83,283
,300 // 26,36.71,300.71,300
,318 // 27,38.89,318.59,318
,337 // 28,41.20,337.53,337
,357 // 29,43.65,357.60,357
,378 // 30,46.25,378.87,378
,401 // 31,49.00,401.40,401
,425 // 32,51.91,425.27,425
,450 // 33,55.00,450.55,450
,477 // 34,58.27,477.34,477
,505 // 35,61.74,505.73,505
,535 // 36,65.41,535.80,535
,567 // 37,69.30,567.66,567
,601 // 38,73.42,601.42,601
,637 // 39,77.78,637.18,637
,675 // 40,82.41,675.07,675
,715 // 41,87.31,715.21,715
,757 // 42,92.50,757.74,757
,802 // 43,98.00,802.79,802
,850 // 44,103.83,850.53,850
,901 // 45,110.00,901.11,901
,954 // 46,116.54,954.69,954
,1011 // 47,123.47,1011.46,1011
,1071 // 48,130.81,1071.60,1071
,1135 // 49,138.59,1135.32,1135
,1202 // 50,146.83,1202.83,1202
,1274 // 51,155.56,1274.36,1274
,1350 // 52,164.81,1350.13,1350
,1430 // 53,174.61,1430.42,1430
,1515 // 54,185.00,1515.47,1515
,1605 // 55,196.00,1605.59,1605
,1701 // 56,207.65,1701.06,1701
,1802 // 57,220.00,1802.21,1802
,1909 // 58,233.08,1909.38,1909
,2022 // 59,246.94,2022.92,2022
,2143 // 60,261.63,2143.20,2143
,2270 // 61,277.18,2270.64,2270
,2405 // 62,293.66,2405.66,2405
,2548 // 63,311.13,2548.71,2548
,2700 // 64,329.63,2700.27,2700
,2860 // 65,349.23,2860.83,2860
,3030 // 66,369.99,3030.95,3030
,3211 // 67,392.00,3211.18,3211
,3402 // 68,415.30,3402.12,3402
,3604 // 69,440.00,3604.42,3604
,3818 // 70,466.16,3818.75,3818
,4045 // 71,493.88,4045.83,4045
,4286 // 72,523.25,4286.41,4286
,4541 // 73,554.37,4541.29,4541
,4811 // 74,587.33,4811.33,4811
,5097 // 75,622.25,5097.42,5097
,5400 // 76,659.26,5400.53,5400
,5721 // 77,698.46,5721.67,5721
,6061 // 78,739.99,6061.89,6061
,6422 // 79,783.99,6422.36,6422
,6804 // 80,830.61,6804.25,6804
,7208 // 81,880.00,7208.85,7208
,7637 // 82,932.33,7637.51,7637
,8091 // 83,987.77,8091.66,8091
,8572 // 84,1046.50,8572.82,8572
,9082 // 85,1108.73,9082.58,9082
,9622 // 86,1174.66,9622.66,9622
,10194 // 87,1244.51,10194.85,10194
,10801 // 88,1318.51,10801.07,10801
,11443 // 89,1396.91,11443.33,11443
,12123 // 90,1479.98,12123.79,12123
,12844 // 91,1567.98,12844.71,12844
,13608 // 92,1661.22,13608.50,13608
,14417 // 93,1760.00,14417.70,14417
,15275 // 94,1864.65,15275.02,15275
,16183 // 95,1975.53,16183.31,16183
,17145 // 96,2093.00,17145.63,17145
,18165 // 97,2217.46,18165.16,18165
,19245 // 98,2349.32,19245.31,19245
,20389 // 99,2489.01,20389.70,20389
,21602 // 100,2637.02,21602.14,21602
,22886 // 101,2793.83,22886.67,22886
,24247 // 102,2959.95,24247.58,24247
,25689 // 103,3135.96,25689.42,25689
,27216 // 104,3322.44,27216.99,27216
,28835 // 105,3520.00,28835.39,28835
,30550 // 106,3729.31,30550.04,30550
,32366 // 107,3951.06,32366.63,32366
,34291 // 108,4186.01,34291.26,34291
,36330 // 109,4434.92,36330.32,36330
,38490 // 110,4698.64,38490.65,38490
,40779 // 111,4978.03,40779.41,40779
,43204 // 112,5274.04,43204.25,43204
,45773 // 113,5587.65,45773.32,45773
,48495 // 114,5919.91,48495.14,48495
,51378 // 115,6271.92,51378.79,51378
,54433 // 116,6644.87,54433.96,54433
,57670 // 117,7040.00,57670.76,57670
,61100 // 118,7458.62,61100.07,61100
,64733 // 119,7902.13,64733.26,64733
,3046 // 120,8372.02,68582.53,3046
,7124 // 121,8869.84,72660.64,7124
,11445 // 122,9397.27,76981.30,11445
,16022 // 123,9956.06,81558.77,16022
,20872 // 124,10548.07,86408.50,20872
,26010 // 125,11175.30,91546.65,26010
,31454 // 126,11839.81,96990.28,31454
};

class CSynth
{
public:
  volatile static unsigned char* m_pWaveForm1;
  volatile static unsigned char* m_pWaveForm2;
  volatile static unsigned char* m_pLFOShape1;
  volatile static unsigned char* m_pLFOShape2;
  volatile static unsigned int m_nWaveForm1Accumulator;
  volatile static unsigned int m_nWaveForm1Increment;
  volatile static unsigned int m_nWaveForm2Accumulator;
  volatile static unsigned int m_nWaveForm2Increment;
  volatile static unsigned int m_nLFOAccumulator1;
  volatile static unsigned int m_nLFOIncrement1;
  volatile static unsigned int m_nLFOAccumulator2;
  volatile static unsigned int m_nLFOIncrement2;
  volatile static unsigned char m_sLFOType;
  volatile static unsigned char m_sMix;
  static void startAudio()
  {
     TCCR1A=0x0;          // set the timer prescaler to 8 = 16/8 = 2MHz
     TCCR1B=0x02;          // set the timer prescaler to 8 = 16/8 = 2MHz
     TIMSK1 |= (1<<OCIE1A);   // Enable output compare match interrupt on OCR1A
    
     TCCR0A=0B10110011;                                    //-8 bit audio PWM
    //TCCR0A=0x83;          // Set timer waveform generation mode to FAST PWM, clear OC0A On match, set at bottom - OC0A = digital pin 6.
    TCCR0B=0x01;          // Set to clock frequency, no prescaler
    OCR0A=127;            // set in the middle - do we need this ? probably not.
    DDRD|=1<<6;          // Set digital pin 6 to output - channels 2 and 3
  }
  static void triggerMidiNote(unsigned char sNote)
  {
    if(sNote < MIDI_NOTE_MAX)
    {
      m_nWaveForm1Increment = (pgm_read_word(midiNoteToWavePhaseIncrement + (sNote)));
    }
  }
};

volatile unsigned char* CSynth::m_pWaveForm1;
volatile unsigned char* CSynth::m_pWaveForm2;
volatile unsigned char* CSynth::m_pLFOShape1;
volatile unsigned char* CSynth::m_pLFOShape2;
volatile unsigned int CSynth::m_nWaveForm1Accumulator;
volatile unsigned int CSynth::m_nWaveForm1Increment;
volatile unsigned int CSynth::m_nWaveForm2Accumulator;
volatile unsigned int CSynth::m_nWaveForm2Increment;
volatile unsigned int CSynth::m_nLFOAccumulator1;
volatile unsigned int CSynth::m_nLFOIncrement1;
volatile unsigned int CSynth::m_nLFOAccumulator2;
volatile unsigned int CSynth::m_nLFOIncrement2;
volatile unsigned char CSynth::m_sLFOType;
volatile unsigned char CSynth::m_sMix;
 
CSynth synth;

// iterate the grains and LFO
SIGNAL (TIMER1_COMPA_vect)
{
  OCR1A += (TIMER1_FREQUENCY/UPDATE_RATE);

   // cross fade between the two channels
  unsigned char sAmplitude1 = 0;
  unsigned char sAmplitude2 = 0;
 
  if(synth.m_sMix < 127)
  {
    sAmplitude1 = synth.m_sMix << 1;
    sAmplitude2 = 255;
  }
  else if(synth.m_sMix > 127)
  {
    sAmplitude2 = (255-synth.m_sMix) << 1;
    sAmplitude1 = 255;
  }
 
  OCR0B = OCR0A =
  (((((
    (*(synth.m_pWaveForm1 + ((synth.m_nWaveForm1Accumulator+=synth.m_nWaveForm1Increment)>>8))) *
    (*(synth.m_pLFOShape1 + ((synth.m_nLFOAccumulator1+=synth.m_nLFOIncrement1) >> 8)))) >> 8) *
    sAmplitude1) >> 8) +
   ((((
    (*(synth.m_pWaveForm2 + ((synth.m_nWaveForm2Accumulator+=synth.m_nWaveForm2Increment)>>8))) *
    (*(synth.m_pLFOShape2 + ((synth.m_nLFOAccumulator2+=synth.m_nLFOIncrement2) >> 8)))) >> 8) *
    sAmplitude2) >> 8)) >> 1;   

}

void setup()
{
 pinMode(GRAIN1_SHAPE_INPUT,INPUT);
 pinMode(GRAIN2_SHAPE_INPUT,INPUT);
 pinMode(LFO_SHAPE_INPUT,INPUT);

 digitalWrite(GRAIN1_SHAPE_INPUT,HIGH);
 digitalWrite(GRAIN2_SHAPE_INPUT,HIGH);
 digitalWrite(LFO_SHAPE_INPUT,HIGH);

 Serial.begin(9600);

  for(int nIndex = 0;nIndex < 256;nIndex ++)
 {
   rampWave[nIndex] = 255 - nIndex;
   squareWave[nIndex] = (nIndex < 127) ? 0 : 255;
 }

 synth.m_pWaveForm1 = sineWave;
 synth.m_pWaveForm2 = sineWave;
 synth.m_pLFOShape1 = rampWave;
 synth.m_pLFOShape2 = squareWave;
 synth.m_sMix = MIX_MIDDLE;
 synth.startAudio();
}
void loop()
{
 synth.triggerMidiNote(analogRead(1)>>3);
 synth.m_nWaveForm2Increment = analogRead(2)<<5;

 // the LFO Oscilators control the volume of the two waveforms, a low value
 synth.m_nLFOIncrement1 = analogRead(3) >> 2;
 synth.m_nLFOIncrement2 = analogRead(4) >> 2;

 // this mixes the two channels a low value will shut off one channel, a high value will shut off the other
 // anywhere in between will generate a proportional mix of the two channels
 synth.m_sMix = analogRead(5) >> 2;
}