Wednesday, 3 April 2013

Build a groove box with Arduino Due, Post3 : Let's code a simple synth

After explaining the project in posts 1 and 2, now let's code into the Arduino Due.
Please refer to the main post : http://groovuino.blogspot.fr/2013/03/the-goal-is-to-build-groovebox-using.html
You can use the USB cable with Programming Port. For now, no need to external power supply. all components used have very low power needs. When we'll add LCD, it will be necessary.

First of all, download the groovuino library on guithub :
https://github.com/Gaetino/Groovuino

Then copy the folder into the library of your arduino folder.

If you haven't got yet, download the latest arduino environment here :
http://arduino.cc/en/Main/Software

Be careful to take the one working with Arduino Due.


Now, we'll code a synth which is working with the groovuino hardware.

Libraries

First, before making any code, import all the libraries you need :


#include <arduino.h>
#include <osc.h>
#include <env.h>
#include <filter.h>
#include <sequencer.h>

#define POLIPHONY 1
int notes[POLIPHONY];

#include <interface.h>
#include <arp.h> 
#include <timer.h> 



Now we instantiate the voices and their volume envelopes, and the filter. For the moment, there will be just 1 voice, we'll make a mono synth.

Osc oscA[POLIPHONY];
Env env1[POLIPHONY];

LowPassFilter LP;

We need some status :


boolean play = false;
boolean mono = true;


We have to include the analogread.h function AFTER having instantiated envelopes, oscillators and filter, because it's using these objects.

#include <analogread.h>


Initialization (setup function)

void setup() 


// Defining pots and sensors  
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
  pinMode(A6, INPUT);

// Loading osc tables
  createSineTable();
  createSawTable();  
  createSqTable();  

// Start the audio engine
  startTimer(TC1, 1, TC4_IRQn, 44100);
  analogWrite(DAC1,0); 
  
// Set filter default values
  LP.setCutoffFreq(255);
  LP.setResonance(1);
  
  for(int i=0; i<POLIPHONY; i++)
  {
    oscA[i].init();
    env1[i].init();
    notes[i]=0;
  }

  play=false;
  
  analogcontroller_init();

// Set the glide time at 2 ms
  oscA[0].setGlideTime(2);
  
  init_interface();
}   

The loop function will be only used to read sensor and potentiometers data.


void loop() 

  read_input();
  analogcontroller_read();
}



Finally, the 44 kHz loop, where we have to read oscillators values


void TC4_Handler() 


 TC_GetStatus(TC1, 1); 

 int32_t ulOutput=2048;

// load oscillator value  
 oscA[0].next();
 ulOutput += oscA[0].output();

// apply filter effect
  if(ulOutput>10) ulOutput = LP.next(ulOutput);

// apply envelope 
  ulOutput = (ulOutput * env1[0].amount())>>19;

// apply global volume
  ulOutput = (ulOutput*volsynth)>>7;

  
  if(ulOutput>4095) ulOutput=4095;
   
  dacc_write_conversion_data(DACC_INTERFACE, ulOutput); 
}



To be continued...



No comments:

Post a Comment