#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <MIDI.h>
#include "tone2freq.h"
#include "Synth.h"
AudioSynthWaveform * pWaveform1;
AudioEffectEnvelope * pEnvelope1;
AudioOutputAnalogStereo * pDacs1;
AudioEffectChorus * pChorus;
AudioConnection * pPatchCord1;
AudioConnection * pPatchCord2;
AudioConnection * pPatchCord3;
AudioConnection * pPatchCord4;
AudioConnection * pPatchCord5;
AudioConnection * pPatchCord6;
AudioConnection * pPatchCord7;
#define CHORUS_DELAY_LENGTH (16*AUDIO_BLOCK_SAMPLES)
// Allocate the delay lines for left and right channels
short chorus_delay[CHORUS_DELAY_LENGTH];
int numNotes =0;
int numVoices = 2;
static void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
{
if (inNote > 128) { inNote = 128; }
Serial.printf("NoteOn: %d\n", inNote);
pWaveform1->frequency(tone2freq[inNote]);
pEnvelope1->noteOn();
numNotes++;
}
static void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
{
numNotes--;
if (numNotes < 1){
pEnvelope1->noteOff();
numNotes = 0;
}
Serial.printf("NoteOff: %d\n", inNote);
}
Synth::Synth(void)
{
Serial.begin(115200);
AudioMemory(20);
pWaveform1 = new AudioSynthWaveform();
pEnvelope1 = new AudioEffectEnvelope();
pChorus = new AudioEffectChorus();
pDacs1 = new AudioOutputAnalogStereo();
pPatchCord1 = new AudioConnection(*pWaveform1,*pEnvelope1);
pPatchCord2 = new AudioConnection(*pEnvelope1, *pChorus);
pPatchCord4 = new AudioConnection(*pChorus, 0, *pDacs1, 0);
pPatchCord5 = new AudioConnection(*pChorus, 0, *pDacs1, 1);
pPatchCord6 = new AudioConnection(*pEnvelope1, 0, *pDacs1, 0);
pPatchCord7 = new AudioConnection(*pEnvelope1, 0, *pDacs1, 1);
MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.begin(1);
pWaveform1->amplitude(0.11);
pWaveform1->frequency(660);
pWaveform1->begin(WAVEFORM_SAWTOOTH);
pEnvelope1->attack(2.0);
pEnvelope1->decay(2.0);
pEnvelope1->sustain(0.5);
pEnvelope1->release(500.0);
pChorus->begin(chorus_delay,CHORUS_DELAY_LENGTH,numVoices);
}
void Synth::loop(void)
{
MIDI.read();
delay(10);
if (Serial.available())
{
char c = Serial.read();
if (c == 'a'){
pChorus->voices(numVoices++);
Serial.printf("Num Voices: %d", numVoices);
}
else if (c == 'z'){
pChorus->voices(numVoices--);
Serial.printf("Num Voices: %d", numVoices);
}
else if (c == 'd'){
Serial.printf("disconnect chorus\n");
pPatchCord2->disconnect();
pPatchCord4->disconnect();
pPatchCord5->disconnect();
pPatchCord6->connect();
pPatchCord7->connect();
if (pChorus->isActive()) {
Serial.printf("chorus active\n");
} else {
Serial.printf("chorus inactive\n");
}
} else if (c == 'e'){
Serial.printf("connect chorus\n");
pPatchCord6->disconnect();
pPatchCord7->disconnect();
pPatchCord2->connect();
pPatchCord4->connect();
pPatchCord5->connect();
if (pChorus->isActive()) {
Serial.printf("chorus active\n");
} else {
Serial.printf("chorus inactive\n");
}
}
}
}