DIY Synth, question 1 of 10000.

Status
Not open for further replies.

halogravity

Well-known member
Hello, my name is Chris. I just started the complex yet satisfying journey of building my own synth. I have some programming experience, but I am quite unfamiliar with C+. I figured for my first test I would line up four oscillators, one knob each for tuning, and viola! My first drone synth works! Step two is to add a button. Success! Step three is where my problem lies, Trying to add ADSR envelope to the button press. There's not a ton of info specifically for this out there, so I was hoping you guys can take a look at my supernoob code and tell me what I'm doing wrong? My heartfelt thanks is advance, I am honored to join such an accomplished and talented forum!

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

Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15);  // 15 = 15 ms debounce time
Bounce button2 = Bounce(2, 15);

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform3;      //xy=163,288
AudioSynthWaveform       waveform2;      //xy=166,236
AudioSynthWaveform       waveform1;      //xy=167,174
AudioSynthWaveform       waveform4;      //xy=168,343
AudioMixer4              patch_mixer;         //xy=446,281
AudioEffectEnvelope      envelope;      //xy=628,285
AudioOutputI2S           audioOut;           //xy=1028,320
AudioConnection          patchCord1(waveform3, 0, patch_mixer, 2);
AudioConnection          patchCord2(waveform2, 0, patch_mixer, 1);
AudioConnection          patchCord3(waveform1, 0, patch_mixer, 0);
AudioConnection          patchCord4(waveform4, 0, patch_mixer, 3);
AudioConnection          patchCord5(patch_mixer, envelope);
AudioConnection          patchCord6(envelope, 0, audioOut, 0);
AudioConnection          patchCord7(envelope, 0, audioOut, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=566,404
// GUItool: end automatically generated code

void setup() {

  
  Serial.begin(9600);
  AudioMemory(64);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.3);

  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  
  patch_mixer.gain(0, 0.70);
  patch_mixer.gain(1, 0.70);
  patch_mixer.gain(2, 0.70);
  patch_mixer.gain(3, 0.70);


    
  waveform1.begin(WAVEFORM_TRIANGLE);
  waveform1.amplitude(.75);
  waveform1.frequency(0);
//  waveform1.pulseWidth(0.15);
  
  waveform2.begin(WAVEFORM_TRIANGLE);
  waveform2.amplitude(.75);
  waveform2.frequency(0);
//  waveform2.pulseWidth(0.15);
  
  waveform3.begin(WAVEFORM_TRIANGLE);
  waveform3.amplitude(.75);
  waveform3.frequency(0);
 // waveform3.pulseWidth(0.15);
  
  waveform1.begin(WAVEFORM_TRIANGLE);
  waveform4.amplitude(.75);
  waveform4.frequency(0);
 // waveform4.pulseWidth(0.15);


}

void loop() {

  button0.update();
  button1.update();
  button2.update();
  
  // use the knobs to adjust parameters
  //float knob1 = (float)analogRead(A1) / 1023.0;
  
  float knob2 = (float)analogRead(A2) / 1023.0;
  float knob3 = (float)analogRead(A3) / 1023.0;
  float knob4 = (float)analogRead(A4) / 1023.0;
  float knob5 = (float)analogRead(A5) / 1023.0;
  
  waveform1.frequency(1650 * knob2 + 0.25);
  waveform2.frequency(1650 * knob3 + 0.25);
  waveform3.frequency(1650 * knob4 + 0.25);
  waveform4.frequency(1650 * knob5 + 0.25);
  
  if (button0.fallingEdge()) {
    
    envelope.noteOn();
  }
    if (button0.risingEdge()) {
    envelope.noteOff();
  }
}
 
I got it working :D ! I hope the title of this thread isn't too vague, I'll certainly be more specific next time. I am not 100% sure what the difference between the working and not working code is, except I moved the envelope before the mixer and each waveform has its own envelope. There must be a much more efficient way to do this. If someone could explain the multiply usage of an envelope I would be forever grateful! Anywaaaays...

I have 4 pots and 4 buttons hooked up, here's the working code:

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

Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15);  // 15 = 15 ms debounce time
Bounce button2 = Bounce(2, 15);
Bounce button3 = Bounce(3, 15);

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=396,267
AudioSynthWaveform       waveform2;      //xy=398,312
AudioSynthWaveform       waveform4;      //xy=398,398
AudioSynthWaveform       waveform3;      //xy=399,356
AudioEffectEnvelope      envelope2;      //xy=576,336
AudioEffectEnvelope      envelope1;      //xy=582,273
AudioEffectEnvelope      envelope4;      //xy=592,454
AudioEffectEnvelope      envelope3;      //xy=597,392
AudioMixer4              patch_mixer;    //xy=777,326
AudioOutputI2S           audioOut; //xy=1079,332
AudioConnection          patchCord1(waveform1, envelope1);
AudioConnection          patchCord2(waveform2, envelope2);
AudioConnection          patchCord3(waveform4, envelope4);
AudioConnection          patchCord4(waveform3, envelope3);
AudioConnection          patchCord5(envelope2, 0, patch_mixer, 1);
AudioConnection          patchCord6(envelope1, 0, patch_mixer, 0);
AudioConnection          patchCord7(envelope4, 0, patch_mixer, 3);
AudioConnection          patchCord8(envelope3, 0, patch_mixer, 2);
AudioConnection          patchCord9(patch_mixer, 0, audioOut, 0);
AudioConnection          patchCord10(patch_mixer, 0, audioOut, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=797,498
// GUItool: end automatically generated code

void setup() {


  Serial.begin(9600);
  AudioMemory(64);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.3);

  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  
  patch_mixer.gain(0, 1);
  patch_mixer.gain(1, 1);
  patch_mixer.gain(2, 1);
  patch_mixer.gain(3, 1);
  
  waveform1.begin(0.5, 440, WAVEFORM_TRIANGLE);
  waveform2.begin(0.5, 440, WAVEFORM_TRIANGLE);
  waveform3.begin(0.5, 440, WAVEFORM_TRIANGLE);
  waveform4.begin(0.5, 440, WAVEFORM_TRIANGLE);
  
  envelope1.attack(250);
  envelope1.decay(350);
  envelope1.sustain(0.6);
  envelope1.release(350);

  envelope2.attack(250);
  envelope2.decay(350);
  envelope2.sustain(0.6);
  envelope2.release(350);

  envelope3.attack(250);
  envelope3.decay(350);
  envelope3.sustain(0.6);
  envelope3.release(350);
 
  envelope4.attack(250);
  envelope4.decay(350);
  envelope4.sustain(0.6);
  envelope4.release(350);
}

void loop() {

  button0.update();
  button1.update();
  button2.update();
  button3.update();

  // use the knobs to adjust parameters
  //float knob1 = (float)analogRead(A1) / 1023.0;

  float knob2 = (float)analogRead(A2) / 1023.0;
  float knob3 = (float)analogRead(A3) / 1023.0;
  float knob4 = (float)analogRead(A4) / 1023.0;
  float knob5 = (float)analogRead(A5) / 1023.0;

  waveform1.frequency(1650 * knob2 + 0.25);
  waveform2.frequency(1650 * knob3 + 0.25);
  waveform3.frequency(1650 * knob4 + 0.25);
  waveform4.frequency(1650 * knob5 + 0.25);

  if (button0.fallingEdge()) {
    envelope1.noteOn();
  }
  if (button0.risingEdge()) {
    envelope1.noteOff();
  }
  if (button1.fallingEdge()) {
    envelope2.noteOn();
  }
  if (button1.risingEdge()) {
    envelope2.noteOff();
  }
  if (button2.fallingEdge()) {
    envelope3.noteOn();
  }
  if (button2.risingEdge()) {
    envelope3.noteOff();
  }
  if (button3.fallingEdge()) {
    envelope4.noteOn();
  }
  if (button3.risingEdge()) {
    envelope4.noteOff();
  }
}
 
Status
Not open for further replies.
Back
Top