thedreiskre
Active member
whenever a note is pressed down there is a noticeable, but short, crackling noise. This same noise occurs whenever the note is taken off as well.
An example: https://drive.google.com/file/d/1Mj9zz4CayzUUcvzXVu26WvS21D-KUJvl/view?usp=sharing
Code seems to have been formatted weirdly on here so i have provided a pastebin link: https://pastebin.com/UznjFHG4
My Code:
An example: https://drive.google.com/file/d/1Mj9zz4CayzUUcvzXVu26WvS21D-KUJvl/view?usp=sharing
Code seems to have been formatted weirdly on here so i have provided a pastebin link: https://pastebin.com/UznjFHG4
My Code:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioSynthWaveform waveform3; //xy=284,1255
AudioSynthWaveform waveform1; //xy=288,1103
AudioSynthWaveform waveform4; //xy=289,1320
AudioSynthWaveform waveform2; //xy=290,1153
AudioFilterStateVariable filter1; //xy=475,1289
AudioFilterStateVariable filter2; //xy=477,1239
AudioFilterStateVariable filter4; //xy=479,1139
AudioFilterStateVariable filter3; //xy=479,1188
AudioMixer4 mixer1; //xy=802,1208
AudioOutputI2S i2s1; //xy=1263,1210
AudioConnection patchCord1(waveform3, 0, filter2, 0);
AudioConnection patchCord2(waveform1, 0, filter4, 0);
AudioConnection patchCord3(waveform4, 0, filter1, 0);
AudioConnection patchCord4(waveform2, 0, filter3, 0);
AudioConnection patchCord5(filter1, 0, mixer1, 3);
AudioConnection patchCord6(filter2, 0, mixer1, 2);
AudioConnection patchCord7(filter4, 0, mixer1, 0);
AudioConnection patchCord8(filter3, 0, mixer1, 1);
AudioConnection patchCord9(mixer1, 0, i2s1, 0);
AudioConnection patchCord10(mixer1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=1255,1166
// GUItool: end automatically generated code
//globals
//buffer stuff
byte noteBuffer[24];
byte velocityBuffer[24];
int bufferLen = 0;
int bufferMaxLen = 24;
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, 3951.066, 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};
//for avoiding division
const float div127 = (1.0 / 127.0);
//waveform type 1
int waveformType = WAVEFORM_SAWTOOTH;
float waveformAmplitude = 0.5;
float waveformPulseWidth = 0.15;
//filter
float filterFrequency = (0.1 * 10000);
float filterResonance = (0.8 * 4.3);
void setup() {
//main setup
AudioMemory(128);
usbMIDI.setHandleNoteOff(myNoteOff);
usbMIDI.setHandleNoteOn(myNoteOn);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
mixer1.gain(0,0.1);
mixer1.gain(1,0.1);
mixer1.gain(2,0.1);
mixer1.gain(3,0.1);
//initialize oscilators
waveform1.begin(waveformType);
waveform1.frequency(440);
waveform1.amplitude(0.5);
waveform1.pulseWidth(waveformPulseWidth);
waveform2.begin(waveformType);
waveform2.frequency(0);
waveform2.amplitude(0);
waveform2.pulseWidth(waveformPulseWidth);
waveform3.begin(waveformType);
waveform3.frequency(0);
waveform3.amplitude(0);
waveform3.pulseWidth(waveformPulseWidth);
waveform4.begin(waveformType);
waveform4.frequency(0);
waveform4.amplitude(0);
waveform4.pulseWidth(waveformPulseWidth);
}
void loop()
{
usbMIDI.read();
filter1.frequency(filterFrequency);
filter1.resonance(filterResonance);
filter2.frequency(filterFrequency);
filter2.resonance(filterResonance);
filter3.frequency(filterFrequency);
filter3.resonance(filterResonance);
filter4.frequency(filterFrequency);
filter4.resonance(filterResonance);
}
//called whenever a note is pressed down
void myNoteOn(byte channel,byte note, byte velocity)
{
BufferHandler(note,velocity,true);
}
//called whenever a note is taken off
void myNoteOff(byte channel,byte note, byte velocity)
{
BufferHandler(note,velocity,false);
}
void BufferHandler(byte note, byte velocity, bool playNote)
{
int notePos = 0;
if(playNote == true && bufferLen < bufferMaxLen)
{
noteBuffer[bufferLen] = note;
velocityBuffer[bufferLen] = velocity;
bufferLen++;
}
else if(playNote == false && bufferLen < bufferMaxLen)
{
for (int i=0; i<bufferLen; i++)//finds the note in the buffer
{
if(note == noteBuffer[i])
{
notePos = i;
break;
}
}
for(int i=notePos; i<bufferLen; i++)//removes the note and budges everything along
{
noteBuffer[i] = noteBuffer[i+1];
velocityBuffer[i] = velocityBuffer[i+1];
}
bufferLen--;
}
oscManager();
}
void oscManager()
{
AudioNoInterrupts();
waveform1.frequency(noteFreqs[noteBuffer[bufferLen - 1]]);
waveform2.frequency(noteFreqs[noteBuffer[bufferLen - 2]]);
waveform3.frequency(noteFreqs[noteBuffer[bufferLen - 3]]);
waveform4.frequency(noteFreqs[noteBuffer[bufferLen - 4]]);
waveform1.amplitude(((velocityBuffer[bufferLen - 1]) * div127) * waveformAmplitude);
waveform2.amplitude(((velocityBuffer[bufferLen - 2]) * div127) * waveformAmplitude);
waveform3.amplitude(((velocityBuffer[bufferLen - 3]) * div127) * waveformAmplitude);
waveform4.amplitude(((velocityBuffer[bufferLen - 4]) * div127) * waveformAmplitude);
AudioInterrupts();
}
Last edited by a moderator: