Polyphonic Synth not working

Status
Not open for further replies.

Elekris

Member
So I'm following the Teensy Synth tutorial by Dave (https://www.youtube.com/watch?v=UJcZxyB5rVc), but with a few differences:
- I'm using PureData solely as a MIDI passthrough
- I'm using an ILI9341 touchscreen for controlling parameters instead
- I have a Teensy 3.5 with Audio Shield
- I want my Synth to be 8 tone polyphonic

Here is my code:
Code:
#include <ILI9341_t3.h>
#include <font_Arial.h>
#include <XPT2046_Touchscreen.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform00;      //xy=425,109
AudioSynthWaveform       waveform40; //xy=422.8571472167969,715.7142944335938
AudioSynthWaveform       waveform20; //xy=424.2857360839844,378.5714416503906
AudioSynthWaveform       waveform60; //xy=422.14288330078125,985.2857360839844
AudioSynthWaveform       waveform30; //xy=424.2857360839844,507.1428527832031
AudioSynthWaveform       waveform70; //xy=422.14288330078125,1113.8571472167969
AudioSynthWaveform       waveform10; //xy=425.71429443359375,245.7142677307129
AudioSynthWaveform       waveform50; //xy=423.5714416503906,852.4285621643066
AudioSynthWaveform       waveform01;      //xy=428.7142791748047,149.28571701049805
AudioSynthWaveform       waveform41; //xy=426.57142639160156,756.0000114440918
AudioSynthWaveform       waveform21; //xy=428.00001525878906,418.8571586608887
AudioSynthWaveform       waveform61; //xy=425.85716247558594,1025.5714530944824
AudioSynthWaveform       waveform31; //xy=428.00001525878906,547.4285697937012
AudioSynthWaveform       waveform71;  //xy=425.85716247558594,1154.142864227295
AudioSynthWaveform       waveform11; //xy=429.42857360839844,285.99998474121094
AudioSynthWaveform       waveform51; //xy=427.2857208251953,892.7142791748047
AudioSynthNoisePink      noise0;          //xy=435.00000762939453,189.2857265472412
AudioSynthNoisePink      noise4; //xy=432.8571548461914,796.000020980835
AudioSynthNoisePink      noise2; //xy=434.2857437133789,458.85716819763184
AudioSynthNoisePink      noise6; //xy=432.1428909301758,1065.5714626312256
AudioSynthNoisePink      noise3; //xy=434.2857437133789,587.4285793304443
AudioSynthNoisePink      noise7; //xy=432.1428909301758,1194.142873764038
AudioSynthNoisePink      noise1; //xy=435.7143020629883,325.9999942779541
AudioSynthNoisePink      noise5; //xy=433.57144927978516,932.7142887115479
AudioMixer4              mixer0;         //xy=621.2500076293945,169.25000190734863
AudioMixer4              mixer4; //xy=622.8571472167969,759.7142944335938
AudioMixer4              mixer2; //xy=624.2857360839844,422.5714416503906
AudioMixer4              mixer6; //xy=622.1428833007812,1029.2857360839844
AudioMixer4              mixer3; //xy=624.2857360839844,551.1428527832031
AudioMixer4              mixer7; //xy=622.1428833007812,1157.8571472167969
AudioMixer4              mixer1; //xy=625.7142944335938,289.7142677307129
AudioMixer4              mixer5; //xy=623.5714416503906,896.4285621643066
AudioAnalyzePeak         peak1;          //xy=801.4285714285713,85.71428571428571
AudioMixer4              mixer8;         //xy=808.5713958740234,361.4285831451416
AudioMixer4              mixer9;         //xy=832.857120513916,970.000036239624
AudioAnalyzePeak         peak2;          //xy=909.9999999999999,158.57142857142856
AudioMixer4              mixer10;         //xy=1127.142857142857,617.1428571428571
AudioAnalyzePeak         peak3;          //xy=1154.2857142857142,281.4285714285714
AudioOutputI2S           i2s1;           //xy=1375.5713539123535,615.285722732544
AudioAnalyzePeak         peak4;          //xy=1395.7142857142856,499.99999999999994
AudioConnection          patchCord1(waveform00, 0, mixer0, 0);
AudioConnection          patchCord2(waveform00, peak1);
AudioConnection          patchCord3(waveform40, 0, mixer4, 0);
AudioConnection          patchCord4(waveform20, 0, mixer2, 0);
AudioConnection          patchCord5(waveform60, 0, mixer6, 0);
AudioConnection          patchCord6(waveform30, 0, mixer3, 0);
AudioConnection          patchCord7(waveform70, 0, mixer7, 0);
AudioConnection          patchCord8(waveform10, 0, mixer1, 0);
AudioConnection          patchCord9(waveform50, 0, mixer5, 0);
AudioConnection          patchCord10(waveform01, 0, mixer0, 1);
AudioConnection          patchCord11(waveform41, 0, mixer4, 1);
AudioConnection          patchCord12(waveform21, 0, mixer2, 1);
AudioConnection          patchCord13(waveform61, 0, mixer6, 1);
AudioConnection          patchCord14(waveform31, 0, mixer3, 1);
AudioConnection          patchCord15(waveform71, 0, mixer7, 1);
AudioConnection          patchCord16(waveform11, 0, mixer1, 1);
AudioConnection          patchCord17(waveform51, 0, mixer5, 1);
AudioConnection          patchCord18(noise0, 0, mixer0, 2);
AudioConnection          patchCord19(noise4, 0, mixer4, 2);
AudioConnection          patchCord20(noise2, 0, mixer2, 2);
AudioConnection          patchCord21(noise6, 0, mixer6, 2);
AudioConnection          patchCord22(noise3, 0, mixer3, 2);
AudioConnection          patchCord23(noise7, 0, mixer7, 2);
AudioConnection          patchCord24(noise1, 0, mixer1, 2);
AudioConnection          patchCord25(noise5, 0, mixer5, 2);
AudioConnection          patchCord26(mixer0, 0, mixer8, 0);
AudioConnection          patchCord27(mixer0, peak2);
AudioConnection          patchCord28(mixer4, 0, mixer9, 0);
AudioConnection          patchCord29(mixer2, 0, mixer8, 2);
AudioConnection          patchCord30(mixer6, 0, mixer9, 2);
AudioConnection          patchCord31(mixer3, 0, mixer8, 3);
AudioConnection          patchCord32(mixer7, 0, mixer9, 3);
AudioConnection          patchCord33(mixer1, 0, mixer8, 1);
AudioConnection          patchCord34(mixer5, 0, mixer9, 1);
AudioConnection          patchCord35(mixer8, 0, mixer10, 0);
AudioConnection          patchCord36(mixer8, peak3);
AudioConnection          patchCord37(mixer9, 0, mixer10, 1);
AudioConnection          patchCord38(mixer10, 0, i2s1, 0);
AudioConnection          patchCord39(mixer10, 0, i2s1, 1);
AudioConnection          patchCord40(mixer10, peak4);
AudioControlSGTL5000     audioShield;     //xy=1126.5357818603516,419.28571128845215
// GUItool: end automatically generated code

AudioAnalyzePeak peaks[4] = {peak1, peak2, peak3, peak4};
AudioSynthWaveform primOscs[8] = {waveform00, waveform10, waveform20, waveform30, waveform40, waveform50, waveform60, waveform70};
AudioSynthWaveform secondOscs[8] = {waveform01, waveform11, waveform21, waveform31, waveform41, waveform51, waveform61, waveform71};
AudioSynthNoisePink noises[8] = {noise0, noise1, noise2, noise3, noise4, noise5, noise6, noise7};
AudioMixer4 mixers[11] = {mixer0, mixer1, mixer2, mixer3, mixer4, mixer5, mixer6, mixer7, mixer8, mixer9, mixer10};

ILI9341_t3 tft = ILI9341_t3(21, 20, 255, 7, 14, 12);
XPT2046_Touchscreen ts(8);

bool wasTouched = false;
int lastbtn = 0;

bool isNewMenu = false;
int currentMenu = 0;
const String menu0[8] = {"Waveform", "Button 2", "Button 3", "Button 4", "Button 5", "Button 6", "Button 7", "Button 8"};
const String menu1[8] = {"Go Back", "Sine", "Saw", "Saw Rev", "Square", "Triangle", "", ""};
const String menuArray[2][8] = {menu0, menu1};

const float div127 = (1.0 / 127.0);
const float noteFreqs[128] = {8.176, 8.662, 9.177, 9.723, 10.301, 10.913, 11.562, 12.25, 12.978, 13.75, 14.568, 15.434, 16.352, 17.324, 18.354, 19.445, 20.602, 21.827, 23.125, 24.5, 25.957, 27.5, 29.135, 30.868, 32.703, 34.648, 36.708, 38.891, 41.203, 43.654, 46.249, 48.999, 51.913, 55, 58.27, 61.735, 65.406, 69.296, 73.416, 77.782, 82.407, 87.307, 92.499, 97.999, 103.826, 110, 116.541, 123.471, 130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220, 233.082, 246.942, 261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164, 493.883, 523.251, 554.365, 587.33, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880, 932.328, 987.767, 1046.502, 1108.731, 1174.659, 1244.508, 1318.51, 1396.913, 1479.978, 1567.982, 1661.219, 1760, 1864.655, 1975.533, 2093.005, 2217.461, 2349.318, 2489.016, 2637.02, 2793.826, 2959.955, 3135.963, 3322.438, 3520, 3729.31, 3950.566, 4186.009, 4434.922, 4698.636, 4978.032, 5274.041, 5587.652, 5919.911, 6271.927, 6644.875, 7040, 7458.62, 7902.133, 8372.018, 8869.844, 9397.273, 9956.063, 10548.08, 11175.3, 11839.82, 12543.85};
const int octave = 12;
bool isPlaying[8];
byte oscNotes[8];

unsigned long timestamp = 0;

void setup(){
  Serial.begin(115200);
  setupAudio();
  usbMIDI.setHandleNoteOff(myNoteOff);
  usbMIDI.setHandleNoteOn(myNoteOn);
  
  tft.begin();
  tft.setRotation(0);
  ts.begin();
  ts.setRotation(0);
  tft.setFont(Arial_16);
  resetAllButtons();
}

void setupAudio(){
  AudioMemory(400);
  audioShield.enable();
  audioShield.volume(0.7);

  for(int i = 0; i < 8; i++){
    primOscs[i].begin(WAVEFORM_SAWTOOTH);
    secondOscs[i].begin(WAVEFORM_SAWTOOTH);
    primOscs[i].amplitude(0.0);
    secondOscs[i].amplitude(0.0);
    primOscs[i].pulseWidth(0.15);
    secondOscs[i].pulseWidth(0.15);
  }

  for(int i = 0; i < 8; i++){
    mixers[i].gain(0, 0.3);
    mixers[i].gain(1, 0.3);
    mixers[i].gain(2, 0.3);
  }

  for(int i = 0; i < 4; i++){
    mixers[8].gain(i, 0.25);
    mixers[9].gain(i, 0.25);
  }

  mixers[10].gain(0, 0.5);
  mixers[10].gain(1, 0.5);
}

void loop() {
  usbMIDI.read();

  if(millis() - timestamp >= 250){
    timestamp = millis();
    Serial.print("wave00 peak: ");
    Serial.print(peak1.read());
    Serial.print(", mixer0 peak: ");
    Serial.print(peak2.read());
    Serial.print(", mixer8 peak: ");
    Serial.print(peak3.read());
    Serial.print(", mixer10 peak: ");
    Serial.println(peak4.read());
  }
  
  if(ts.touched() == true && wasTouched == false){
    wasTouched = true;
    TS_Point p = ts.getPoint();
    int y = p.y - 220;
    int x = p.x - 300;
    if(y <= 920){
      if(x >= 1700) button4();
      else button8();
    }
    else if(y <= 1840){
      if(x >= 1700) button3();
      else button7();
    }
    else if(y <= 2760){
      if(x >= 1700) button2();
      else button6();
    }
    else{
      if(x >= 1700) button1();
      else button5();
    }
  }else if(ts.touched() == false && wasTouched == true){
    wasTouched = false;
    if(isNewMenu) resetAllButtons();
    else resetButton(lastbtn);
  }
}

void myNoteOn(byte channel, byte note, byte velocity){
  if ( note > 23 && note < 108 ){
    for(int i = 0; i < 8; i++){
      if(!isPlaying[i]){
        primOscs[i].frequency(noteFreqs[note]);
        float vel = (velocity * div127);
        primOscs[i].amplitude(vel);
        isPlaying[i] = true;
        oscNotes[i] = note;
        Serial.print("Playing oscillator ");
        Serial.print(i);
        Serial.print(" with amplitude ");
        Serial.println(vel);
        break;
      }
    }
  }
}

void myNoteOff(byte channel, byte note, byte velocity){
  Serial.println("key released");
  if ( note > 23 && note < 108 ){
    for(int i = 0; i < 8; i++){
      if(oscNotes[i] == note){
        primOscs[i].amplitude(0.0);
        isPlaying[i] = false;
        break;
      }
    }
  }
}

void setMenu(int m){
  currentMenu = m;
  isNewMenu = true;
}

void button1(){
  lastbtn = 1;
  Serial.println("You pressed BUTTON 1");
  tft.fillRect(0, 0, 120, 80, ILI9341_BLACK);
  tft.setCursor(10, 20);
  tft.setTextColor(ILI9341_RED);
  tft.print(menuArray[currentMenu][0]);

  if(currentMenu == 0) setMenu(1);
  else if(currentMenu == 1) setMenu(0);
}

void button2(){
  lastbtn = 2;
  Serial.println("You pressed BUTTON 2");
  tft.fillRect(0, 80, 120, 80, ILI9341_BLACK);
  tft.setCursor(10, 100);
  tft.setTextColor(ILI9341_GREEN);
  tft.print(menuArray[currentMenu][1]);

  //if(currentMenu == 1) waveform.begin(WAVEFORM_SINE);
}

void button3(){
  lastbtn = 3;
  Serial.println("You pressed BUTTON 3");
  tft.fillRect(0, 160, 120, 80, ILI9341_BLACK);
  tft.setCursor(10, 180);
  tft.setTextColor(ILI9341_BLUE);
  tft.print(menuArray[currentMenu][2]);

  //if(currentMenu == 1) waveform.begin(WAVEFORM_SAWTOOTH);
}

void button4(){
  lastbtn = 4;
  Serial.println("You pressed BUTTON 4");
  tft.fillRect(0, 240, 120, 80, ILI9341_WHITE);
  tft.setCursor(10, 260);
  tft.setTextColor(ILI9341_BLACK);
  tft.print(menuArray[currentMenu][3]);

  //if(currentMenu == 1) waveform.begin(WAVEFORM_SAWTOOTH_REVERSE);
}

void button5(){
  lastbtn = 5;
  Serial.println("You pressed BUTTON 5");
  tft.fillRect(120, 0, 120, 80, ILI9341_BLACK);
  tft.setCursor(130, 20);
  tft.setTextColor(ILI9341_YELLOW);
  tft.print(menuArray[currentMenu][4]);

  //if(currentMenu == 1) waveform.begin(WAVEFORM_SQUARE);
}

void button6(){
  lastbtn = 6;
  Serial.println("You pressed BUTTON 6");
  tft.fillRect(120, 80, 120, 80, ILI9341_BLACK);
  tft.setCursor(130, 100);
  tft.setTextColor(ILI9341_CYAN);
  tft.print(menuArray[currentMenu][5]);

  //if(currentMenu == 1) waveform.begin(WAVEFORM_TRIANGLE);
}

void button7(){
  lastbtn = 7;
  Serial.println("You pressed BUTTON 7");
  tft.fillRect(120, 160, 120, 80, ILI9341_BLACK);
  tft.setCursor(130, 180);
  tft.setTextColor(ILI9341_MAGENTA);
  tft.print(menuArray[currentMenu][6]);

  //                                                   PASTE BUTTON 7 FUNCTION HERE
}

void button8(){
  lastbtn = 8;
  Serial.println("You pressed BUTTON 8");
  tft.fillRect(120, 240, 120, 80, ILI9341_BLACK);
  tft.setCursor(130, 260);
  tft.setTextColor(ILI9341_WHITE);
  tft.print(menuArray[currentMenu][7]);

  //                                                   PASTE BUTTON 8 FUNCTION HERE
}

void resetAllButtons(){
  for(int i = 1; i < 9; i++){
    resetButton(i);
  }
}

void resetButton(int btn){
  switch(btn){
    case 1:
      tft.fillRect(0, 0, 120, 80, ILI9341_RED);
      tft.setCursor(10, 20);
      tft.setTextColor(ILI9341_WHITE);
      tft.print(menuArray[currentMenu][0]);
      break;
    case 2:
      tft.fillRect(0, 80, 120, 80, ILI9341_GREEN);
      tft.setCursor(10, 100);
      tft.setTextColor(ILI9341_WHITE);
      tft.print(menuArray[currentMenu][1]);
      break;
    case 3:
      tft.fillRect(0, 160, 120, 80, ILI9341_BLUE);
      tft.setCursor(10, 180);
      tft.setTextColor(ILI9341_WHITE);
      tft.print(menuArray[currentMenu][2]);
      break;
    case 4:
      tft.fillRect(0, 240, 120, 80, ILI9341_BLACK);
      tft.setCursor(10, 260);
      tft.setTextColor(ILI9341_WHITE);
      tft.print(menuArray[currentMenu][3]);
      break;
    case 5:
      tft.fillRect(120, 0, 120, 80, ILI9341_YELLOW);
      tft.setCursor(130, 20);
      tft.setTextColor(ILI9341_BLACK);
      tft.print(menuArray[currentMenu][4]);
      break;
    case 6:
      tft.fillRect(120, 80, 120, 80, ILI9341_CYAN);
      tft.setCursor(130, 100);
      tft.setTextColor(ILI9341_BLACK);
      tft.print(menuArray[currentMenu][5]);
      break;
    case 7:
      tft.fillRect(120, 160, 120, 80, ILI9341_MAGENTA);
      tft.setCursor(130, 180);
      tft.setTextColor(ILI9341_BLACK);
      tft.print(menuArray[currentMenu][6]);
      break;
    case 8:
      tft.fillRect(120, 240, 120, 80, ILI9341_WHITE);
      tft.setCursor(130, 260);
      tft.setTextColor(ILI9341_BLACK);
      tft.print(menuArray[currentMenu][7]);
      break;
  }
}

And here is what the audio system looks like:
audioscreenshot.png

And here is my problem: Everything works just fine, but there won't be any audio output coming from the Teensy.
The MIDI works, the audio objects are connected properly, the oscillators are supposed to generate sound, but they don't.
And for some unknown reason, the peak objects all return a value of 1.0, even if the oscillators aren't playing.

EDIT: Also the hardware is fine. If I upload the sketch Dave provided in part 3 of his tutorial, I get audio output.
 
I found the problem.

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=930.7142868041992,549.0000305175781
AudioSynthWaveform       waveform2;      //xy=934.4285659790039,589.2857475280762
AudioSynthNoisePink      noise1;          //xy=940.7142944335938,629.2857570648193
AudioSynthWaveform       waveform3; //xy=944.2857055664062,691.4285888671875
AudioSynthWaveform       waveform4; //xy=947.9999847412109,731.7143058776855
AudioSynthNoisePink      noise2; //xy=954.2857131958008,771.7143154144287
AudioMixer4              mixer1;         //xy=1126.9642944335938,609.2500324249268
AudioMixer4              mixer2;  //xy=1140.5357131958008,751.6785907745361
AudioMixer4              mixer3;         //xy=1369.9999999999998,681.4285714285713
AudioOutputI2S           i2s1;           //xy=1567.0000114440918,683.8571147918701
AudioConnection          patchCord1(waveform1, 0, mixer1, 0);
AudioConnection          patchCord2(waveform2, 0, mixer1, 1);
AudioConnection          patchCord3(noise1, 0, mixer1, 2);
AudioConnection          patchCord4(waveform3, 0, mixer2, 0);
AudioConnection          patchCord5(waveform4, 0, mixer2, 1);
AudioConnection          patchCord6(noise2, 0, mixer2, 2);
AudioConnection          patchCord7(mixer1, 0, mixer3, 0);
AudioConnection          patchCord8(mixer2, 0, mixer3, 1);
AudioConnection          patchCord9(mixer3, 0, i2s1, 0);
AudioConnection          patchCord10(mixer3, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=1126.5357818603516,419.28571128845215
// GUItool: end automatically generated code

AudioSynthWaveform primOscs[4] = {waveform1, waveform2, waveform3, waveform4};

void setup(){
  AudioMemory(100);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.7);

  primOscs[0].begin(1.0, 100, WAVEFORM_SINE);
}
This won't output any audio. I'm not sure why, but my guess is that the array isn't actually pointing to the waveform objects.

But if I write the code like this, it works:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveform       primOscs[4];
AudioSynthNoisePink      noise1;          //xy=940.7142944335938,629.2857570648193
AudioSynthNoisePink      noise2; //xy=954.2857131958008,771.7143154144287
AudioMixer4              mixer1;         //xy=1126.9642944335938,609.2500324249268
AudioMixer4              mixer2;  //xy=1140.5357131958008,751.6785907745361
AudioMixer4              mixer3;         //xy=1369.9999999999998,681.4285714285713
AudioOutputI2S           i2s1;           //xy=1567.0000114440918,683.8571147918701
AudioConnection          patchCord1(primOscs[0], 0, mixer1, 0);
AudioConnection          patchCord2(primOscs[1], 0, mixer1, 1);
AudioConnection          patchCord3(noise1, 0, mixer1, 2);
AudioConnection          patchCord4(primOscs[2], 0, mixer2, 0);
AudioConnection          patchCord5(primOscs[3], 0, mixer2, 1);
AudioConnection          patchCord6(noise2, 0, mixer2, 2);
AudioConnection          patchCord7(mixer1, 0, mixer3, 0);
AudioConnection          patchCord8(mixer2, 0, mixer3, 1);
AudioConnection          patchCord9(mixer3, 0, i2s1, 0);
AudioConnection          patchCord10(mixer3, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=1126.5357818603516,419.28571128845215
// GUItool: end automatically generated code

void setup(){
  AudioMemory(100);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.7);

  primOscs[0].begin(1.0, 100, WAVEFORM_SINE);
}
 
Status
Not open for further replies.
Back
Top