cfredisded
Active member
Hey all,
I am currently trying to set up an 11 detent potentiometer to cycle through the waveform shapes. Is it possible to do without restarting the waveform? I wanted to set up some if statements to determine the wave shape, but if i use waveform.begin it just resets the wave form every time it goes through the if statement. Any help/suggestions are appreciated and thanks for the cool library paul.
Cheers,
cfredisdead
*the first pass at the 'if statements' are commented out under '//LFO shape"
I am currently trying to set up an 11 detent potentiometer to cycle through the waveform shapes. Is it possible to do without restarting the waveform? I wanted to set up some if statements to determine the wave shape, but if i use waveform.begin it just resets the wave form every time it goes through the if statement. Any help/suggestions are appreciated and thanks for the cool library paul.
Cheers,
cfredisdead
*the first pass at the 'if statements' are commented out under '//LFO shape"
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_SSD1306.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioSynthWaveform waveform1; //xy=515,193
AudioSynthWaveform waveform2; //xy=553,278
AudioOutputPT8211 pt8211_1; //xy=1073,250
AudioConnection patchCord1(waveform1, 0, pt8211_1, 0);
AudioConnection patchCord2(waveform2, 0, pt8211_1, 1);
// GUItool: end automatically generated code
// Set up external controls
const int analogPin0 = A0; // the pin that the vco waveform1 potentiometer is attached to
const int analogPin1 = A1; // the pin that the lfo waveform2 potentiometer is attached to
const int analogPin3 = A3; // LFO voltage back from DAC
const int analogPin13 = A13; // LFO Depth controll
const int analogPin7 = A7; // LFO shape controll
const int analogPin6 = A6; // VCO shape controll
void setup() {
// Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial
Serial.begin(9600);
// Set up audio shield
AudioMemory(8); //Allocate the memory for all audio connections. The numberBlocks input specifies how much memory to reserve for audio data. Each block holds 128 audio samples, or approx 2.9 ms of sound. Usually an initial guess is made for numberBlocks and the actual usage is checked with AudioMemoryUsageMax().
// Set up waveform 1
waveform1.pulseWidth(0.5); // Change the width (duty cycle) of the pulse.
waveform1.begin(1, 220, WAVEFORM_SQUARE); // begin(level, frequency, waveform) Output a waveform, and set the amplitude and frequency.
waveform2.pulseWidth(0.5); // Change the width (duty cycle) of the pulse.
waveform2.begin(1, 220, WAVEFORM_SINE); // begin(level, frequency, waveform) Output a waveform, and set the amplitude and frequency. wave 2 is used as LFO modulator
// WAVEFORM_SINE
// WAVEFORM_SAWTOOTH
// WAVEFORM_SAWTOOTH_REVERSE
// WAVEFORM_SQUARE
// WAVEFORM_TRIANGLE
// WAVEFORM_ARBITRARY
// WAVEFORM_PULSE
// WAVEFORM_SAMPLE_HOLD
}
void loop() {
// LFO speed
int lfoSPEED1 = analogRead(analogPin1); //Read the value of A1_lfoSPEED
float lfoSPEED2 = fscale(0, 1023, 0.01, 20, lfoSPEED1, -4); // Adjust the lfoSPEED range with fscale
waveform2.frequency(lfoSPEED2); //Set the value of Waveform2 lfo speed
// VCO freq
int vcoFREQ1 = analogRead(analogPin0); //Read the value of A0_vcoFREQ
float vcoFREQ2 = fscale(0, 1023, 20, 3000, vcoFREQ1, -4); // Adjust the vfoFreq range with fscale
//LFO Depth Read
int lfoDEPTH1 = analogRead(analogPin13); //Read the value of A33_lfoDEPTH
float lfoDEPTH2 = fscale(0, 1023, 0, 1000, lfoDEPTH1, 0); // Adjust the lfoSPEED range with fscale
// LFO READ
int lfoREAD1 = analogRead(analogPin3); //Read the value of A3_lfoREAD
float lfoREAD2 = fscale(0, 1023, -lfoDEPTH2, lfoDEPTH2, lfoREAD1, 0); // Adjust the lfoSPEED range with fscale range of lfodepth
//Modulate vco waveform1.frequency
float vcoFREQ3 = (lfoREAD2+vcoFREQ2); //modulate vcoFREQ with lfoREAD
waveform1.frequency(vcoFREQ3); //Set the value of VcoFreq modulate with lfo speed
//LFO Shape
int lfoSHAPE1 = analogRead(analogPin7); //Read the value of A7_lfoSHAPE
int lfoSHAPE2 = fscale(0, 1023, 0, 22, lfoSHAPE1, 0);
//if (lfoSHAPE1 >= 0, lfoSHAPE1<= 12){
//waveform2.begin(1, vcoFREQ3, WAVEFORM_SINE);
//}
//if (lfoSHAPE1 >= 13, lfoSHAPE1<= 30){
//waveform2.begin(1, vcoFREQ3, WAVEFORM_SAWTOOTH);
//}
//if (lfoSHAPE1 >= 31, lfoSHAPE1<= 150){
//waveform2.begin(1, vcoFREQ3, WAVEFORM_SAWTOOTH_REVERSE);
//}
Serial.println(lfoSHAPE2);
}
//fscale code
float fscale( float originalMin, float originalMax, float newBegin, float
newEnd, float inputValue, float curve){
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
/*
Serial.println(curve * 100, DEC); // multply by 100 to preserve resolution
Serial.println();
*/
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
// Zero Refference the values
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin){
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
/*
Serial.print(OriginalRange, DEC);
Serial.print(" ");
Serial.print(NewRange, DEC);
Serial.print(" ");
Serial.println(zeroRefCurVal, DEC);
Serial.println();
*/
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax ) {
return 0;
}
if (invFlag == 0){
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
}
else // invert the ranges
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
}