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();
}
}