Just finished the first version of my sampler project. It is already lots of fun to play with it! Here is a little demo:


No I need to get rid of some glitches. One thing is that when I freeze a sound with the granular object and record a loop at the same time you can hear some audio glitches + the record/loop function itself does not record 100% clean. There are glitches here and there...

Here is the code if someone is interested to have a look:
Code:
////////**********\\\\\\\\
//////// SamplOMat \\\\\\\\
 //////**  V 1.0  **\\\\\\
 // BY CHRISTIAN GROTHE \\
   // www.kryhse.com \\
////////**********\\\\\\\\


#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Mux.h>

using namespace admux;

Mux mux1(Pin(A11, INPUT, PinType::Analog), Pinset(1, 2, 3));
Mux mux2(Pin(A12, INPUT, PinType::Analog), Pinset(1, 2, 3));
Mux mux3(Pin(A13, INPUT, PinType::Analog), Pinset(1, 2, 3));

// GUItool: begin automatically generated code
AudioInputI2S              in;          
AudioEffectGranular        granular1;
AudioEffectGranular        granular2;
AudioEffectGranular        granular3;
AudioEffectMultiply        multiply0;
AudioEffectMultiply        multiply1;
AudioEffectMultiply        multiply2;
AudioEffectMultiply        multiplyA;

AudioFilterLadder          filterL;
AudioFilterLadder          filterR;
AudioEffectWaveFolder      shaperL;
AudioEffectWaveFolder      shaperR;
AudioEffectReverb          reverbL;
AudioEffectReverb          reverbR;
AudioFilterStateVariable   reverbFilterL;
AudioFilterStateVariable   reverbFilterR;
AudioFilterStateVariable   inputFilter;
AudioFilterStateVariable   inputFilter2;
AudioFilterStateVariable   outputFilterL;
AudioFilterStateVariable   outputFilterR;


AudioSynthWaveformSine     waveformA;
AudioSynthWaveform         waveform0;
AudioSynthWaveformDc       dc0;
AudioSynthWaveform         waveform1;
AudioSynthWaveformDc       dc1;
AudioSynthWaveform         waveform2;
AudioSynthWaveformDc       dc2;
AudioSynthWaveform         wavefolder;


AudioMixer4                mixer1;
AudioMixer4                mixerL;
AudioMixer4                mixerR;
AudioMixer4                mixer2grain2;
AudioMixer4                mixer2grain3;
AudioMixer4                mixerDc0;
AudioMixer4                mixerDc1;
AudioMixer4                mixerDc2;
AudioMixer4                mixerFoldL;
AudioMixer4                mixerFoldR;
AudioMixer4                reverbSendL;
AudioMixer4                reverbSendR;
AudioMixer4                reverbReturnL;
AudioMixer4                reverbReturnR;
AudioMixer4                mixerLoopDI;


AudioAmplifier             amp1;
AudioAmplifier             revAmpL;
AudioAmplifier             revAmpR;
AudioAmplifier             outAmpL;
AudioAmplifier             outAmpR;

AudioAnalyzePeak           peakGain;
AudioAnalyzePeak           peakOut;
AudioAnalyzePeak           peakWave0;         
AudioAnalyzePeak           peakWave1;         
AudioAnalyzePeak           peakWave2;    
      
AudioRecordQueue           queue1;        
AudioPlaySdRaw             playRaw1;      
AudioOutputI2S             out;          

AudioConnection            patchCord0(in, 0, inputFilter2, 0);
AudioConnection            toFilter(inputFilter2, 0, inputFilter, 0);
AudioConnection            fromFilter(inputFilter, 2, amp1, 0);
AudioConnection            patchCord1(amp1, 0, queue1, 0);
AudioConnection            patchCord2(amp1, 0, peakGain, 0);

AudioConnection            playToMixer(playRaw1, 0, granular1, 0);

AudioConnection            patchCord11(multiply0, 0, mixerL, 0);
AudioConnection            patchCord12(multiply0, 0, mixerR, 0);

//FX
AudioConnection            patchCord15(mixerL, 0, shaperL, 0);
AudioConnection            patchCord16(mixerR, 0, shaperR, 0);
AudioConnection            toShaperModL(wavefolder, 0, shaperL, 1);
AudioConnection            toSahperModR(wavefolder, 0, shaperR, 1);
AudioConnection            mixerL2mixFoldL(mixerL, 0, mixerFoldL, 0);
AudioConnection            mixerR2mixFoldR(mixerR, 0, mixerFoldR, 0);
AudioConnection            fold2mixfoldL(shaperL, 0, mixerFoldL, 1);
AudioConnection            fold2mixfoldR(shaperR, 0, mixerFoldR, 1);

AudioConnection            toFilterL(mixerFoldL, 0, filterL, 0);
AudioConnection            toFilterR(mixerFoldR, 0, filterR, 0);

AudioConnection            reverbSendL1(filterL, 0, reverbSendL, 0);
AudioConnection            reverbSendR1(filterR, 0, reverbSendR, 0);
AudioConnection            toReverb1(reverbSendL, 0, reverbL, 0);
AudioConnection            toReverb2(reverbSendR, 0, reverbR, 0);

AudioConnection            revtoAmp1(reverbL, 0, revAmpL, 0);
AudioConnection            revtoAmp2(reverbR, 0, revAmpR, 0);
AudioConnection            revtoFilter1(revAmpL, 0, reverbFilterL, 0);
AudioConnection            revtoFilter2(revAmpR, 0, reverbFilterR, 0);
AudioConnection            wetL(reverbFilterL, 0, reverbReturnL, 0);
AudioConnection            wetR(reverbFilterR, 0, reverbReturnR, 0);


AudioConnection            dryL(filterL, 0, reverbReturnL, 1);
AudioConnection            dryR(filterR, 0, reverbReturnR, 1);
AudioConnection            toOutL(reverbReturnL, 0, outAmpL, 0);
AudioConnection            toOutR(reverbReturnR, 0, outAmpR, 0);
AudioConnection            OutputFilterL(outAmpL, 0, outputFilterL, 0);
AudioConnection            OutputFilterR(outAmpR, 0, outputFilterR, 0);
AudioConnection            OutL(outputFilterL, 2, out, 0);
AudioConnection            OutR(outputFilterR, 2, out, 1);

AudioConnection            PeakOut(outAmpL, 0, peakOut, 0);



// Granular Mix

AudioConnection            LoopDI1(granular1, 0, mixerLoopDI, 0);
AudioConnection            LoopDI2(amp1, 0, mixerLoopDI, 1);

AudioConnection            patchCord17(mixerLoopDI, 0, mixer2grain2, 0);
AudioConnection            patchCord18(mixerLoopDI, 0, mixer2grain3, 0);
AudioConnection            patchCord19(mixer2grain2, 0, granular2, 0);
AudioConnection            patchCord20(mixer2grain3, 0, granular3, 0);

AudioConnection            patchCord21(multiply1, 0, mixerL, 1);
AudioConnection            patchCord22(multiply1, 0, mixerR, 1);
AudioConnection            patchCord23(multiply2, 0, mixerL, 2);
AudioConnection            patchCord24(multiply2, 0, mixerR, 2);


// VCA

AudioConnection            patchCord27(granular3, 0, multiply1, 0);
AudioConnection            patchCord28(waveform1, 0, mixerDc1, 0);
AudioConnection            patchPeak(waveform1, 0, peakWave1, 0);
AudioConnection            patchCord29(dc1, 0, mixerDc1, 1);
AudioConnection            patchCord30(mixerDc1, 0, multiply1, 1);

AudioConnection            patchCord31(granular2, 0, multiply2, 0);
AudioConnection            patchCordP0(waveform2, 0, peakWave2, 0);
AudioConnection            patchCord32(waveform2, 0, mixerDc2, 0);
AudioConnection            patchCord33(dc2, 0, mixerDc2, 1);
AudioConnection            patchCord34(mixerDc2, 0, multiply2, 1);

AudioConnection            grainStableA1(granular1, 0, multiplyA, 0);
AudioConnection            grainStableA2(waveformA, 0, multiplyA, 1);


AudioConnection            patchCord35(multiplyA, 0, multiply0, 0);
AudioConnection            patchCord36(waveform0, 0, mixerDc0, 0);
AudioConnection            patchCord37(waveform0, 0, peakWave0, 0);
AudioConnection            patchCord38(dc0, 0, mixerDc0, 1);
AudioConnection            patchCord39(mixerDc0, 0, multiply0, 1);

AudioControlSGTL5000       sgtl5000_1;    
// GUItool: end automatically generated code



#define GRANULAR_MEMORY_SIZE 12800  // enough for 290 ms at 44.1 kHz
int16_t granularMemory[GRANULAR_MEMORY_SIZE];

#define GRANULAR_MEMORY_SIZE2 25000  // enough for 290 ms at 44.1 kHz
int16_t granularMemory2[GRANULAR_MEMORY_SIZE2];

#define GRANULAR_MEMORY_SIZE3 25000  // enough for 290 ms at 44.1 kHz
int16_t granularMemory3[GRANULAR_MEMORY_SIZE3];


// which input on the audio shield will be used?
const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;


// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14


// Remember which mode we're doing
enum Mode {
  stopped,
  recording,
  playing,
};

Mode mode = stopped;
Mode mode1 = stopped;

// The file where data is recorded
File frec;

bool State = false;
//bool Stateenv = false;
Mode playmode = playing;
unsigned long previousMillis = 0;
unsigned long interval = 500;
//unsigned long interval1 = interval - 50;
//unsigned long intervalenv = 500;

int current_waveform0 = 0;
int current_waveform1 = 0;
int current_waveform2 = 0;

int env0LED = 0;
int env1LED = 0;
int env2LED = 0;

int startGrain1 = 31;
int startGrain2 = 30;
int changeLFO0 = 32;
int changeLFO1 = 34;
int changeLFO2 = 35;
int router = 39;

int startRec = 0;

int wave0LED = 37;
int wave1LED = 36;
int wave2LED = 33;

int peakLED = 28;
int peakBright = 0;
int clipLED = 29;
int lengthLED = 38;



// Bounce objects to easily and reliably read the buttons
Bounce buttonRecord =   Bounce(startRec, 3);

Bounce buttonGrain1 =   Bounce(startGrain1, 3);
Bounce buttonGrain2 =   Bounce(startGrain2, 3);

Bounce buttonLoopDI =    Bounce(router, 3);

Bounce changeWaveform0 = Bounce(changeLFO0, 15);
Bounce changeWaveform1 = Bounce(changeLFO1, 15);
Bounce changeWaveform2 = Bounce(changeLFO2, 15);

void setup() {

  granular1.begin(granularMemory, GRANULAR_MEMORY_SIZE);
  granular2.begin(granularMemory2, GRANULAR_MEMORY_SIZE2);
  granular3.begin(granularMemory3, GRANULAR_MEMORY_SIZE3);

  granular1.beginPitchShift(100);

  amp1.gain(0);
  revAmpL.gain(3);
  revAmpR.gain(3);
  outAmpL.gain(1);
  outAmpR.gain(1);
 
  mixer1.gain(0, 1);
  mixer1.gain(1, 1);
  
  mixerL.gain(0, 0);
  mixerR.gain(0, 0);
  mixerL.gain(1, 0);
  mixerR.gain(1, 0);
  mixerL.gain(2, 0);
  mixerR.gain(2, 0);

  mixer2grain2.gain(0, 0);
  mixer2grain3.gain(0, 0);
 

  mixerDc0.gain(0, 1);
  mixerDc0.gain(1, 1);
  mixerDc1.gain(0, 1);
  mixerDc1.gain(1, 1);
  mixerDc2.gain(0, 1);
  mixerDc2.gain(1, 1);

  reverbReturnL.gain(0, 1);
  reverbReturnR.gain(0, 1);
  reverbReturnL.gain(1, 1);
  reverbReturnR.gain(1, 1);

  inputFilter.frequency(80);
  inputFilter.resonance(0.7);
  inputFilter2.frequency(15000);
  inputFilter2.resonance(0.7);
  outputFilterL.frequency(80);
  outputFilterL.resonance(0.7);
  outputFilterR.frequency(80);
  outputFilterR.resonance(0.7);

  // Configure the pushbutton pins
  pinMode(startRec, INPUT_PULLUP);
  pinMode(startGrain1, INPUT_PULLUP);
  pinMode(startGrain2, INPUT_PULLUP);
  pinMode(changeLFO0, INPUT_PULLUP);
  pinMode(changeLFO1, INPUT_PULLUP);
  pinMode(changeLFO2, INPUT_PULLUP);
  pinMode(router, INPUT_PULLUP);

  pinMode(wave0LED, OUTPUT);
  pinMode(wave1LED, OUTPUT);
  pinMode(wave2LED, OUTPUT);
  pinMode(peakLED, OUTPUT);
  pinMode(clipLED, OUTPUT);
  pinMode(lengthLED, OUTPUT);


  // Audio connections require memory, and the record queue
  // uses this memory to buffer incoming audio.
  AudioMemory(50);

  // Enable the audio shield, select input, and enable output
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(myInput);
  sgtl5000_1.volume(1);

 // sgtl5000_1.audioPostProcessorEnable();
 // sgtl5000_1.eqBands(50, 16000);

  // Initialize the SD card
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    // stop here if no SD card, but print a message
    while (1) {
     // Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
  if (SD.exists("RECORD.RAW")) {
    // The SD library writes new data to the end of the
    // file, so to start a new recording, the old file
    // must be deleted before new data is written.
    SD.remove("RECORD.RAW");
  }

  current_waveform0 = WAVEFORM_SINE;
  current_waveform1 = WAVEFORM_SINE;
  current_waveform2 = WAVEFORM_SINE;
  waveform0.begin(current_waveform0);
  waveform1.begin(current_waveform1);
  waveform2.begin(current_waveform2);
  waveform0.offset(1);
  waveform1.offset(1);
  waveform2.offset(1);

  waveformA.amplitude(1);
  waveformA.frequency(10);

}



void loop() {
//Serial.println(AudioMemoryUsageMax());


  
  grain();
  grainfreeze();
  effects();
  vca0();
  vca1();
  vca2();
  looplength();


// Multiplexing

  // for (byte i = 0; i < mux1.channelCount(); i++) {
  //  mux1.channel(i);
  //  mux2.channel(i);
  //  mux3.channel(i);
    // allow 50 us for signals to stablize
  //  delayMicroseconds(50);
 // }

// Volumes & Pan


// Input Gain

float gainInput = mux1.read(0) / 1023.0 * 2 + 1.0;
   amp1.gain(gainInput);
   //Serial.println(gainInput);

// LOOP
int LoopValueL = map(mux1.read(4), 0, 1023, 1023, 0);

float pan0L = LoopValueL / 1023.0;
float pan0R = mux1.read(4) / 1023.0;

float LoopVlL = mux1.read(2) / 1023.0 * pan0L;
float LoopVlR = mux1.read(2) / 1023.0 * pan0R;

  mixerL.gain(0, LoopVlL);
  mixerR.gain(0, LoopVlR);

// Grains
int pan1ValueL = map(mux2.read(1), 0, 1023, 1023, 0);

float pan1L = pan1ValueL / 1023.0;
float pan1R = mux2.read(1) / 1023.0;
//
int pan2ValueL = map(mux2.read(7), 0, 1023, 1023, 0);

float pan2L = pan2ValueL / 1023.0;
float pan2R = mux2.read(7) / 1023.0;

//

float grain1VlL = mux1.read(7) / 1023.0 * pan1L;
float grain1VlR = mux1.read(7) / 1023.0 * pan1R;
float grain2VlL = mux2.read(5) / 1023.0 * pan2L;
float grain2VlR = mux2.read(5) / 1023.0 * pan2R;

  mixerL.gain(1, grain1VlL);
  mixerR.gain(1, grain1VlR);
  mixerL.gain(2, grain2VlL);
  mixerR.gain(2, grain2VlR);
  
    if (peakGain.available()){

  peakBright = peakGain.read() * 255;

  analogWrite(peakLED, peakBright);
    }

      if (peakBright >= 240 ){
  digitalWrite(clipLED, HIGH);
    } else {
  digitalWrite(clipLED, LOW);
    }

  
  ///////////////// Record
   buttonRecord.update();
  // Respond to button presses
  if (buttonRecord.fallingEdge()) {
   
    stopPlaying1();
    stopPlaying2();
    startRecording();
    playmode = recording;

  }

  if (buttonRecord.risingEdge()) {

    stopRecording();
    playmode = playing;
  }


  if (playmode == playing) {

    //play1

    if (State == true) {

      stopPlaying2();

      if (mode == stopped) {
        startPlaying1();

      }
    }

    if (State == false) {

      stopPlaying1();

      if (mode1 == stopped) {
        startPlaying2();
      }

    }
  }


  // If we're playing or recording, carry on...
  if (mode == recording) {

    continueRecording();
  }
  if (mode == playing) {
    
    continuePlaying1();

  }
  if (mode1 == playing) {
 
    continuePlaying2();
  }


}


void startRecording() {
//  Serial.println("startRecording");

  if (SD.exists("RECORD.RAW")) {
    // The SD library writes new data to the end of the
    // file, so to start a new recording, the old file
    // must be deleted before new data is written.
    SD.remove("RECORD.RAW");
  }
  frec = SD.open("RECORD.RAW", FILE_WRITE);
  if (frec) {
    
    queue1.begin();
    mode = recording;
   
  }
}

void continueRecording() {

  if (queue1.available() >= 2) {


    byte buffer[512];

    // Fetch 2 blocks from the audio library and copy
    // into a 512 byte buffer.  The Arduino SD library
    // is most efficient when full 512 byte sector size
    // writes are used.
    
    memcpy(buffer, queue1.readBuffer(), 256);
    queue1.freeBuffer();
    memcpy(buffer + 256, queue1.readBuffer(), 256);
    queue1.freeBuffer();
    
    // write all 512 bytes to the SD card
    frec.write(buffer, 512);
    
  }
}

void stopRecording() {

 // Serial.println("stopRecording");
  
  queue1.end();
  if (mode == 1) {
    while (queue1.available() > 0) {
      frec.write((byte*)queue1.readBuffer(), 256);
      queue1.freeBuffer();
    }
    frec.close();
  }
  mode = stopped;
}

////////// Playing1

void startPlaying1() {

  playRaw1.play("RECORD.RAW");
  mode = playing;
}

void continuePlaying1() {
  if (!playRaw1.isPlaying()) {
    playRaw1.stop();
    mode = stopped;
  }
}

void stopPlaying1() {


  if (mode == 2) playRaw1.stop();
  mode = stopped;
}

////////// playing2

void startPlaying2() {

  playRaw1.play("RECORD.RAW");
  mode1 = playing;
}

void continuePlaying2() {
  if (!playRaw1.isPlaying()) {
    playRaw1.stop();
    mode1 = stopped;
  }
}

void stopPlaying2() {

  if (mode1 == 2) playRaw1.stop();
  mode1 = stopped;
}


/////////// Grain for main loop
void grain() {

  float FaderPitch = mux1.read(3) / 1023.0;

  // Continuously adjust the speed, based on the A3 pot
  float ratio;
  //ratio = powf(2.0, knobA11 * 6.0 - 3.0); // 0.5 to 2.0
  ratio = powf(2.0, FaderPitch * 2.0 - 1.0); // 0.125 to 8.0 -- uncomment for far too much range!
  granular1.setSpeed(ratio);
  delayMicroseconds(30);

}


//////////// Grainfreeze

void grainfreeze() {

  buttonGrain1.update();
  buttonGrain2.update();
  buttonLoopDI.update();
 
  float FaderFreezePitch1 = mux2.read(0) / 1023.0;
  float FaderFreezePitch2 = mux2.read(6) / 1023.0;
  float FaderFreezeLength = mux2.read(4) / 1023.0;

  float msec = 20.0 + (FaderFreezeLength * 530.0);

//LOOP DI

if (buttonLoopDI.fallingEdge()){
  mixerLoopDI.gain(0, 1);
  mixerLoopDI.gain(1, 0);
}

if (buttonLoopDI.risingEdge()){
  mixerLoopDI.gain(0, 0);
  mixerLoopDI.gain(1, 1);
}

  // Button 1 starts Freeze effect
  if (buttonGrain1.fallingEdge()){
    mixer2grain2.gain(0, 1);
    granular2.beginFreeze(msec);
    
  }


  if (buttonGrain1.risingEdge()) {
    mixer2grain2.gain(0, 0);
    granular2.stop();
  }

    // Button 2 starts Freeze effect
  if (buttonGrain2.fallingEdge()){
    mixer2grain3.gain(0, 1);
    
    granular3.beginFreeze(msec);
  }

  if (buttonGrain2.risingEdge()) {
    mixer2grain3.gain(0, 0);
    granular3.stop();
  }

  float ratio1;
  //ratio2 = powf(2.0, knobA10 * 2.0 - 1.0); // 0.5 to 2.0
  ratio1 = powf(2.0, FaderFreezePitch2 * 2.0 - 1.0); // 0.125 to 8.0 -- uncomment for far too much range!
  granular2.setSpeed(ratio1);

  float ratio2;
  //ratio2 = powf(2.0, knobA10 * 2.0 - 1.0); // 0.5 to 2.0
  ratio2 = powf(2.0, FaderFreezePitch1 * 2.0 - 1.0); // 0.125 to 8.0 -- uncomment for far too much range!
  granular3.setSpeed(ratio2);

}


void effects(){

//WaveFolder
float gain1 = mux3.read(2) / 8000.0;
int reversed = map(mux3.read(2), 0, 1023, 1023, 0);
float gain0 = reversed / 1023.0;
float folderfreq = mux3.read(3) * 0.8 + 0.1;

wavefolder.amplitude(1);
wavefolder.begin(WAVEFORM_SAWTOOTH);
wavefolder.frequency(folderfreq);

mixerFoldL.gain(0, gain0);
mixerFoldR.gain(0, gain0);
mixerFoldL.gain(1, gain1);
mixerFoldR.gain(1, gain1);

//LP
float lin = mux3.read(4) / 1023.0;
float loga = 0.0;
float resValue  = mux3.read(5) / 1136.0;

if (lin == 0){
  loga = lin;
}

// 0.01 - 0.8
if (lin <= 0.8 && lin > 0) {
  loga = lin * 0.25;
}

// 0.81 - 0.9
if (lin <= 0.9 && lin > 0.8) {
  loga = lin * 2.25 - 1.57;
}

// 0.91 - 1
if (lin <= 1.0 && lin > 0.9) {
  loga = lin * 5.9 - 4.9;
}

float filterFQ = loga * 15000.0;

filterL.frequency(filterFQ);
filterR.frequency(filterFQ);
filterL.resonance(resValue);
filterR.resonance(resValue);


//Reverb
float reverbSize = mux3.read(7) / 1023.0 * 15.0;
float reverbAmount = mux3.read(6) / 1023.0 * 0.4; 

reverbSendL.gain(0, reverbAmount);
reverbSendR.gain(0, reverbAmount); 

reverbL.reverbTime(reverbSize);
reverbR.reverbTime(reverbSize);
//reverb.damping(0.9);

reverbFilterL.frequency(15000);
reverbFilterR.frequency(15000);
reverbFilterL.resonance(0.7);
reverbFilterR.resonance(0.7);
reverbFilterL.octaveControl(5);
reverbFilterR.octaveControl(5);


}


///////////// VCA

void vca0() {

  float fq0 = mux1.read(6) / 1023.0;
  changeWaveform0.update();

  waveform0.amplitude(1);
  waveform0.frequency(fq0 * 15 + 0.1);

  int depthValue = map(mux1.read(5), 0, 1023, 1023, 0);
  float Depth = depthValue / 1023.0;
  
  dc0.amplitude(Depth);
   
    if (peakWave0.available()){
  env0LED = peakWave0.read() * 255;

  analogWrite(wave0LED, env0LED);
    }


  if (changeWaveform0.fallingEdge()) {
    switch (current_waveform0) {
      case WAVEFORM_SINE:
        current_waveform0 = WAVEFORM_SQUARE;
       // Serial.println("SQUARE");
        break;
      case WAVEFORM_SQUARE:
        current_waveform0 = WAVEFORM_SAWTOOTH_REVERSE;
       // Serial.println("SAW");
        break;  
      case WAVEFORM_SAWTOOTH_REVERSE:
        current_waveform0 = WAVEFORM_SAWTOOTH;
      //  Serial.println("SAW");
        break;        
      case WAVEFORM_SAWTOOTH:
        current_waveform0 = WAVEFORM_SAMPLE_HOLD;
      //  Serial.println("S&H");
        break;
      case WAVEFORM_SAMPLE_HOLD:
        current_waveform0 = WAVEFORM_SINE;
      //  Serial.println("Sine");
        break;
    }
   // AudioNoInterrupts();
    waveform0.begin(current_waveform0);
  //  AudioInterrupts();
  }
}

void vca1() {

  float fq1 = mux2.read(3) / 1023.0;
  changeWaveform1.update();

  waveform1.amplitude(1);
  waveform1.frequency(fq1 * 15 + 0.1);

  int depthValue = map(mux2.read(2), 0, 1023, 1023, 0);
  float Depth = depthValue / 1023.0;
  
  dc1.amplitude(Depth);
   
    if (peakWave1.available()){
  env1LED = peakWave1.read() * 255;

  analogWrite(wave1LED, env1LED);
    }


  if (changeWaveform1.fallingEdge()) {
    switch (current_waveform1) {
      case WAVEFORM_SINE:
        current_waveform1 = WAVEFORM_SQUARE;
       // Serial.println("SQUARE");
        break;
      case WAVEFORM_SQUARE:
        current_waveform1 = WAVEFORM_SAWTOOTH_REVERSE;
       // Serial.println("SAW");
        break;  
      case WAVEFORM_SAWTOOTH_REVERSE:
        current_waveform1 = WAVEFORM_SAWTOOTH;
      //  Serial.println("SAW");
        break;        
      case WAVEFORM_SAWTOOTH:
        current_waveform1 = WAVEFORM_SAMPLE_HOLD;
      //  Serial.println("S&H");
        break;
      case WAVEFORM_SAMPLE_HOLD:
        current_waveform1 = WAVEFORM_SINE;
      //  Serial.println("Sine");
        break;
    }
   // AudioNoInterrupts();
    waveform1.begin(current_waveform1);
  //  AudioInterrupts();
  }
}

///////////

void vca2() {

  float fq2 = mux3.read(1) / 1023.0;
  changeWaveform2.update();

  waveform2.amplitude(1);
  waveform2.frequency(fq2 * 15 + 0.1);

  int depthValue = map(mux3.read(0), 0, 1023, 1023, 0);
  float Depth = depthValue / 1023.0;
  
  dc2.amplitude(Depth);
   
    if (peakWave2.available()){
  env2LED = peakWave2.read() * 255;

  analogWrite(wave2LED, env2LED);
    }


  if (changeWaveform2.fallingEdge()) {
    switch (current_waveform2) {
      case WAVEFORM_SINE:
        current_waveform2 = WAVEFORM_SQUARE;
       // Serial.println("SQUARE");
        break;
      case WAVEFORM_SQUARE:
        current_waveform2 = WAVEFORM_SAWTOOTH_REVERSE;
       // Serial.println("SAW");
        break;  
      case WAVEFORM_SAWTOOTH_REVERSE:
        current_waveform2 = WAVEFORM_SAWTOOTH;
      //  Serial.println("SAW");
        break;        
      case WAVEFORM_SAWTOOTH:
        current_waveform2 = WAVEFORM_SAMPLE_HOLD;
      //  Serial.println("S&H");
        break;
      case WAVEFORM_SAMPLE_HOLD:
        current_waveform2 = WAVEFORM_SINE;
      //  Serial.println("Sine");
        break;
    }
   // AudioNoInterrupts();
    waveform2.begin(current_waveform2);
  //  AudioInterrupts();
  }
}


void looplength() {
  unsigned long currentMillis = millis();
  interval = map (mux1.read(1), 0, 1023, 50, playRaw1.lengthMillis()); 
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    State = !State;
  }

//  if (currentMillis - previousMillis >= interval1) {
//    Stateenv = !Stateenv;
//  }


  if (State == HIGH){
    digitalWrite(lengthLED, HIGH);
    
  } else {
    digitalWrite(lengthLED,LOW);
  }
}