i don't understand why my code is doing a dirty sound!?
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#define BAUDRATE 115200 // vitesse du port serie
#define SYNTH 4 // Synthesizers
AudioSynthWaveform Waveform0, Waveform1, Waveform2, Waveform3;
AudioEffectEnvelope Envelope0, Envelope1, Envelope2, Envelope3;
AudioMixer4 mixer1;
AudioOutputI2S audioOut;
AudioConnection patchCord1(Waveform0, Envelope0);
AudioConnection patchCord2(Waveform1, Envelope1);
AudioConnection patchCord3(Waveform2, Envelope2);
AudioConnection patchCord4(Waveform3, Envelope3);
AudioConnection patchCord5(Envelope0, 0, mixer1, 0);
AudioConnection patchCord6(Envelope1, 0, mixer1, 1);
AudioConnection patchCord7(Envelope2, 0, mixer1, 2);
AudioConnection patchCord8(Envelope3, 0, mixer1, 3);
AudioConnection patchCord9(mixer1, 0, audioOut, 0);
AudioConnection patchCord10(mixer1, 0, audioOut, 1);
AudioControlSGTL5000 audioShield;
typedef struct synth {
boolean state; // true:free - false:buisy
byte sensorID; // 1 to 64 (8_8 matrix)
int metro;
int timeON;
unsigned long lastPlayTime;
AudioSynthWaveform *Waveform;
AudioEffectEnvelope *Envelope;
};
synth allSynth[SYNTH] = {
{ false, -1, 2000, 100, 0, &Waveform0, &Envelope0 },
{ true, -1, 2000, 100, 0, &Waveform1, &Envelope1 },
{ true, -1, 2000, 100, 0, &Waveform2, &Envelope2 },
{ true, -1, 2000, 100, 0, &Waveform3, &Envelope3 },
};
int synthIndex = 0;
/////////////////////// INITIALISATION
void setup(){
// Serial.begin(BAUDRATE); // initialize serial
// analogReadRes(16);
// analogReadRes(12);
// Setup Teensy audio shield
AudioMemory(18);
audioShield.enable();
audioShield.volume(0.45);
mixer1.gain(1, 0.8);
mixer1.gain(2, 0.8);
mixer1.gain(3, 0.8);
mixer1.gain(4, 0.8);
// Init all synthetizers
for( int i=0; i<SYNTH; i++ ) {
allSynth[i].Waveform -> pulseWidth( 0.5 );
allSynth[i].Waveform -> begin( 0.5, 440, WAVEFORM_SINE );
allSynth[i].Envelope -> attack( 5 );
allSynth[i].Envelope -> decay( 100 );
allSynth[i].Envelope -> release( 200 );
}
}
/////////////////////// BOUCLE PRINCIPALE
void loop(){
if( allSynth[synthIndex].state == false ){ // false == BUSY_SYNTH
if( ( millis() - allSynth[synthIndex].lastPlayTime ) >= allSynth[synthIndex].metro ){
allSynth[synthIndex].Envelope -> noteOn();
}
if( ( millis() - allSynth[synthIndex].lastPlayTime ) >= ( allSynth[synthIndex].metro + allSynth[synthIndex].timeON ) ){
allSynth[synthIndex].Envelope -> noteOff();
allSynth[synthIndex].lastPlayTime = millis();
}
}
synthIndex++;
synthIndex = synthIndex % SYNTH;
}