Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Polyphonic Synth - Issue With Button Presses Giving Different Waveform Volume Levels

  1. #1
    Junior Member PJWD's Avatar
    Join Date
    Apr 2019
    Posts
    2

    Polyphonic Synth - Issue With Button Presses Giving Different Waveform Volume Levels

    I'm aiming to make a polyphonic synth with arcade buttons for the keyboard wired directly to the pins of a Teensy 3.2 with an Audio Shield attached.

    I'm using the code below, with a Waveform object for each individual button. At present I'm touching a cable connected to Ground to each pin to act as the button press. When the first pin (0) is pressed the audio sounds loud, the next ones, (1, 2, 3, 4) are less loud, then the next (5, 8, 16) are even quieter. 17 sound a similar level to 1, 2, 3 and 4. 20 is quieter and 21, 24, 25 make no sound.

    I have tried the pins in a different order and the same thing happens in the same sequence (eg 24 loud, 25, 26, 27, 28 less loud, 29, 30 ,31 quiet etc...)

    I have also rebuilt the patch in the Audio System Design Tool several times, creating the objects in different orders and the result is the same.

    Code:
    #include <Bounce.h>
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioSynthWaveform       waveform12;     //xy=68,570
    AudioSynthWaveform       waveform2;      //xy=71,82
    AudioSynthWaveform       waveform11;     //xy=71,527
    AudioSynthWaveform       waveform10;     //xy=78,474
    AudioSynthWaveform       waveform13;     //xy=79,612
    AudioSynthWaveform       waveform1;      //xy=82,36
    AudioSynthWaveform       waveform8;      //xy=83,388
    AudioSynthWaveform       waveform9;      //xy=83,429
    AudioSynthWaveform       waveform7;      //xy=85,344
    AudioSynthWaveform       waveform3;      //xy=87,125
    AudioSynthWaveform       waveform4;      //xy=92,175
    AudioSynthWaveform       waveform6;      //xy=95,293
    AudioSynthWaveform       waveform5;      //xy=96,239
    AudioEffectEnvelope      envelope1;      //xy=229.00000381469727,39
    AudioEffectEnvelope      envelope5;      //xy=231,243
    AudioEffectEnvelope      envelope10;     //xy=231,476
    AudioEffectEnvelope      envelope6;      //xy=235,292
    AudioEffectEnvelope      envelope13;     //xy=234,617
    AudioEffectEnvelope      envelope9;      //xy=238,437
    AudioEffectEnvelope      envelope11;     //xy=238.00000381469727,522.0000076293945
    AudioEffectEnvelope      envelope3;      //xy=240,134
    AudioEffectEnvelope      envelope2;      //xy=243,84
    AudioEffectEnvelope      envelope7;      //xy=243,342
    AudioEffectEnvelope      envelope4;      //xy=246,177
    AudioEffectEnvelope      envelope12;     //xy=247,576
    AudioEffectEnvelope      envelope8;      //xy=257,388
    AudioMixer4              mixer1;         //xy=427,145
    AudioMixer4              mixer3;         //xy=438,488
    AudioMixer4              mixer2;         //xy=439,336
    AudioMixer4              mixer4;         //xy=640,314
    AudioOutputI2S           i2s1;           //xy=912,300
    AudioConnection          patchCord1(waveform12, envelope12);
    AudioConnection          patchCord2(waveform2, envelope2);
    AudioConnection          patchCord3(waveform11, envelope11);
    AudioConnection          patchCord4(waveform10, envelope10);
    AudioConnection          patchCord5(waveform13, envelope13);
    AudioConnection          patchCord6(waveform1, envelope1);
    AudioConnection          patchCord7(waveform8, envelope8);
    AudioConnection          patchCord8(waveform9, envelope9);
    AudioConnection          patchCord9(waveform7, envelope7);
    AudioConnection          patchCord10(waveform3, envelope3);
    AudioConnection          patchCord11(waveform4, envelope4);
    AudioConnection          patchCord12(waveform6, envelope6);
    AudioConnection          patchCord13(waveform5, envelope5);
    AudioConnection          patchCord14(envelope1, 0, mixer1, 0);
    AudioConnection          patchCord15(envelope5, 0, mixer2, 0);
    AudioConnection          patchCord16(envelope10, 0, mixer3, 1);
    AudioConnection          patchCord17(envelope6, 0, mixer2, 1);
    AudioConnection          patchCord18(envelope13, 0, mixer4, 3);
    AudioConnection          patchCord19(envelope9, 0, mixer3, 0);
    AudioConnection          patchCord20(envelope11, 0, mixer3, 2);
    AudioConnection          patchCord21(envelope3, 0, mixer1, 2);
    AudioConnection          patchCord22(envelope2, 0, mixer1, 1);
    AudioConnection          patchCord23(envelope7, 0, mixer2, 2);
    AudioConnection          patchCord24(envelope4, 0, mixer1, 3);
    AudioConnection          patchCord25(envelope12, 0, mixer3, 3);
    AudioConnection          patchCord26(envelope8, 0, mixer2, 3);
    AudioConnection          patchCord27(mixer1, 0, mixer4, 0);
    AudioConnection          patchCord28(mixer3, 0, mixer4, 2);
    AudioConnection          patchCord29(mixer2, 0, mixer4, 1);
    AudioConnection          patchCord30(mixer4, 0, i2s1, 0);
    AudioConnection          patchCord31(mixer4, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=912,367
    // GUItool: end automatically generated code
    
    Bounce button0 = Bounce(0, 15); //(Pin number, ms debounce time)
    Bounce button1 = Bounce(1, 15);
    Bounce button2 = Bounce(2, 15);
    Bounce button3 = Bounce(3, 15);
    Bounce button4 = Bounce(4, 15);
    Bounce button5 = Bounce(5, 15);
    Bounce button6 = Bounce(8, 15);
    Bounce button7 = Bounce(16, 15);
    Bounce button8 = Bounce(17, 15);
    Bounce button9 = Bounce(20, 15);
    Bounce button10 = Bounce(21, 15);
    Bounce button11 = Bounce(24, 15);
    Bounce button12 = Bounce(25, 15);
    
    int C2 = 65.41; // declaring the notes as values in Hz
    int CShp2 = 69.30;
    int D2 = 73.42;
    int DShp2 = 77.78;
    int E2 = 82.41;
    int F2 = 87.31;
    int FShp2 = 92.50;
    int G2 = 98.00;
    int GShp2 = 103.83;
    int Aa2 = 110.00;
    int AShp2 = 116.54;
    int B2 = 123.47;
    int C3 = 130.82;
    
    void setup() {
      Serial.begin(9600);
      AudioMemory(64);
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5); // master volume level
    
      pinMode(0, INPUT_PULLUP);
      pinMode(1, INPUT_PULLUP);
      pinMode(2, INPUT_PULLUP);
      pinMode(3, INPUT_PULLUP);
      pinMode(4, INPUT_PULLUP);
      pinMode(5, INPUT_PULLUP);
      pinMode(8, INPUT_PULLUP);
      pinMode(16, INPUT_PULLUP);
      pinMode(17, INPUT_PULLUP);
      pinMode(20, INPUT_PULLUP);
      pinMode(21, INPUT_PULLUP);
      pinMode(24, INPUT_PULLUP);
      pinMode(25, INPUT_PULLUP);
    
      waveform1.begin(0.2, 0, WAVEFORM_SAWTOOTH);
      waveform2.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform3.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform4.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform5.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform6.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform7.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform8.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform9.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform10.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform11.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform12.begin(0.2, 440, WAVEFORM_SAWTOOTH);
      waveform13.begin(0.2, 440, WAVEFORM_SAWTOOTH);
    
      mixer1.gain(1, 0.2);
      mixer1.gain(2, 0.2);
      mixer1.gain(3, 0.2);
      mixer1.gain(4, 0.2);
      mixer2.gain(1, 0.2);
      mixer2.gain(2, 0.2);
      mixer2.gain(3, 0.2);
      mixer2.gain(4, 0.2);
      mixer3.gain(1, 0.2);
      mixer3.gain(2, 0.2);
      mixer3.gain(3, 0.2);
      mixer3.gain(4, 0.2);
      mixer4.gain(1, 0.2);
      mixer4.gain(2, 0.2);
      mixer4.gain(3, 0.2);
      mixer4.gain(4, 0.2);
    
      envelope1.attack(10);
      envelope1.decay(350);
      envelope1.sustain(0.8);
      envelope1.release(500);
      envelope2.attack(10);
      envelope2.decay(350);
      envelope2.sustain(0.8);
      envelope2.release(500);
      envelope3.attack(10);
      envelope3.decay(350);
      envelope3.sustain(0.8);
      envelope3.release(500);
      envelope4.attack(10);
      envelope4.decay(350);
      envelope4.sustain(0.8);
      envelope4.release(500);
      envelope5.attack(10);
      envelope5.decay(350);
      envelope5.sustain(0.8);
      envelope5.release(500);
      envelope6.attack(10);
      envelope6.decay(350);
      envelope6.sustain(0.8);
      envelope6.release(500);
      envelope7.attack(10);
      envelope7.decay(350);
      envelope7.sustain(0.8);
      envelope7.release(500);
      envelope8.attack(10);
      envelope8.decay(350);
      envelope8.sustain(0.8);
      envelope8.release(500);
      envelope9.attack(10);
      envelope9.decay(350);
      envelope9.sustain(0.8);
      envelope9.release(500);
      envelope10.attack(10);
      envelope10.decay(350);
      envelope10.sustain(0.8);
      envelope10.release(500);
      envelope11.attack(10);
      envelope11.decay(350);
      envelope11.sustain(0.8);
      envelope11.release(500);
      envelope12.attack(10);
      envelope12.decay(350);
      envelope12.sustain(0.8);
      envelope12.release(500);
      envelope13.attack(10);
      envelope13.decay(350);
      envelope13.sustain(0.8);
      envelope13.release(500);
    }
    
    void loop() {
    
      button0.update();
      button1.update();
      button2.update();
      button3.update();
      button4.update();
      button5.update();
      button6.update();
      button7.update();
      button8.update();
      button9.update();
    
    
      // key press for notes
      if (button0.fallingEdge()) {
        envelope1.noteOn();
        waveform1.frequency(C2);
      }
      if (button0.risingEdge()) {
        envelope1.noteOff();
      }
      if (button1.fallingEdge()) {
        envelope2.noteOn();
        waveform2.frequency(CShp2);
      }
      if (button1.risingEdge()) {
        envelope2.noteOff();
      }
      if (button2.fallingEdge()) {
        envelope3.noteOn();
        waveform3.frequency(D2);
      }
      if (button2.risingEdge()) {
        envelope3.noteOff();
      }
      if (button3.fallingEdge()) {
        envelope4.noteOn();
        waveform4.frequency(DShp2);
      }
      if (button3.risingEdge()) {
        envelope4.noteOff();
      }
      if (button4.fallingEdge()) {
        envelope5.noteOn();
        waveform5.frequency(E2);
      }
      if (button4.risingEdge()) {
        envelope5.noteOff();
      }
      if (button5.fallingEdge()) {
        envelope6.noteOn();
        waveform6.frequency(F2);
      }
      if (button5.risingEdge()) {
        envelope6.noteOff();
      }
      if (button6.fallingEdge()) {
        envelope7.noteOn();
        waveform7.frequency(FShp2);
      }
      if (button6.risingEdge()) {
        envelope7.noteOff();
      }
      if (button7.fallingEdge()) {
        envelope8.noteOn();
        waveform8.frequency(G2);
      }
      if (button7.risingEdge()) {
        envelope8.noteOff();
      }
      if (button8.fallingEdge()) {
        envelope9.noteOn();
        waveform9.frequency(GShp2);
      }
      if (button8.risingEdge()) {
        envelope9.noteOff();
      }
      if (button9.fallingEdge()) {
        envelope10.noteOn();
        waveform10.frequency(Aa2);
      }
      if (button9.risingEdge()) {
        envelope10.noteOff();
      }
      if (button10.fallingEdge()) {
        envelope11.noteOn();
        waveform11.frequency(AShp2);
      }
      if (button10.risingEdge()) {
        envelope11.noteOff();
      }
      if (button11.fallingEdge()) {
        envelope12.noteOn();
        waveform12.frequency(B2);
      }
      if (button11.risingEdge()) {
        envelope12.noteOff();
      }
      if (button12.fallingEdge()) {
        envelope13.noteOn();
        waveform13.frequency(C3);
      }
      if (button12.risingEdge()) {
        envelope13.noteOff();
      }
    }
    The patch in the Audio System Design Tool looks like this:
    Click image for larger version. 

Name:	PolyLayout.jpg 
Views:	12 
Size:	75.2 KB 
ID:	16444

    Any help or suggestions gratefully received!

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,068
    You aren't setting the mixer gains correctly which might affect the problem. E.g. in mixer1
    Code:
      mixer1.gain(1, 0.2);
      mixer1.gain(2, 0.2);
      mixer1.gain(3, 0.2);
      mixer1.gain(4, 0.2);
    The inputs to each mixer are numbered from zero. This should be:
    Code:
      mixer1.gain(0, 0.2);
      mixer1.gain(1, 0.2);
      mixer1.gain(2, 0.2);
      mixer1.gain(3, 0.2);
    and similarly for the other three mixers.

    Two other minor tweaks.
    mixer4 has the same problem as above but it also only has three inputs (currently). I'd suggest that you set it like this:
    Code:
      mixer4.gain(0, 0.3);
      mixer4.gain(1, 0.3);
      mixer4.gain(2, 0.3);
      mixer4.gain(3, 0);
    Finally, storing the frequencies in integers will truncate the floating point values that you've used which will make most of the notes sound a little flat. I suggest you change the note declarations to use #define so that the floating point value will be used.
    Code:
    // declaring the notes as values in Hz
    #define C2     65.41
    #define CShp2  69.30
    #define D2     73.42
    #define DShp2  77.78
    #define E2     82.41
    #define F2     87.31
    #define FShp2  92.50
    #define G2     98.00
    #define GShp2 103.83
    #define Aa2   110.00
    #define AShp2 116.54
    #define B2    123.47
    #define C3    130.82
    Pete

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,068
    P.S. Another way to handle the floating point constants would be to change the "int" in each declaration to "const float". E.g.
    Code:
    const float C2 = 65.41;
    etc.

    Pete

  4. #4
    Junior Member PJWD's Avatar
    Join Date
    Apr 2019
    Posts
    2
    Thank you. I'm a bit embarrassed not to have spotted the problem with the mixer, having spent so long checking through everything, but i'm very happy to be able to progress with the project.

    I'll implement your advice about using "#define" or "const float" to handle the floating point numbers in the code too.

    Huge thanks and best wishes,
    Phil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •