Code:
// For Guitar Distortion using Teensy 4.0 with Audio Shield
// Up to 8 x Sin function for guitar distortion effect
// Re-Applied sin function for WaveShape
// Thanks to Kenny Peng for making the waveshape example that I could build from
// NOTE: THIS IS A PROCESSING PROGRAM DO NOT SAVE OR COMPILE IN ARDUINO
// TO CONVERT TO ARDUINO FROM PROCESSING PROGRAM:
// change array syntax [32769] and no (float [] lg ;)
// remove path and PShape commands
// add library links and Audio connections
// remove secondary array definition from setup
// add Serial.begin(115200); and AudioMemory(12); to setup
// remove all "size" commands.
// remove all "path" commands.
// remove "draw()" subroutine.
// add waveshape1.shape(myWaveshape2, 32769); to end of setup
// add waveshape2.shape(myWaveshape2, 32769); to end of setup
// add sgtl5000_1.enable(); to end of setup
// add sgtl5000_1.lineInLevel(15); to end of setup
// add sgtl5000_1.lineOutLevel(25); to end of setup
// add main loop
/**
* PathPShape
*
* A simple path using PShape
*/
// A PShape object
PShape path;
float [] myWaveshape2 ; // array
float fi;
float ds16 = 8;
float d16;
float dgain = 1;
float pi = 3.141592654; // pi
float [] lg ; // array
int smode = 7;
float fv;
void setup() {
size(640, 360, P2D);
myWaveshape2 = new float[32769]; // further define array
path = createShape();
NewShape();
} // setup
void draw() {
delay(2000);
background(0,0,0); // background color
path.setStroke(color(0,255,0));// red, green, blue // line color
translate(50, 150); // location of shape
shape(path);// draw shape
textSize(24);
fill(255, 191, 0);// amber text // text color
switch (smode){
case 0 :
NewShape();
text("Full sin function",110, -125);
break;
case 1 :
NewShape();
text("Double sin function",110, -125);
break;
case 2 :
NewShape();
text("Triple sin function",110, -125);
break;
case 3 :
NewShape();
text("Quad sin function",110, -125);
break;
case 4 :
NewShape();
text("x5 sin function",110, -125);
break;
case 5 :
NewShape();
text("x6 sin function",110, -125);
break;
case 6 :
NewShape();
text("x7 sin function",110, -125);
break;
case 7 :
NewShape();
text("x8 sin function",110, -125);
break;
}// switch
textSize(18);
text("First in array 0", 350, 50);
text(lg[0], 350, 70);
text("Last in array 32768",350, 110);
text(lg[32768], 350, 130);
}// draw
void NewShape(){
smode++;
if (smode == 8){
smode=0;
}// smode==8
path.setStroke(color(0,255,0));// red, green, blue // line color
lg = new float[32769];// further define array
for (int i=0; i<32769; i++) {
fi = i + 16384; // float needed for math to be correct // 16384 is starting point at top of sine
d16 = sin( pi * ( fi / 32768 )) * ( ds16 * 1000 ); // fi/32768 to give middle 1/2 cycle starting the peak of the sine.
myWaveshape2[i] = round( d16 * dgain ); // round and gain
myWaveshape2[i]= -(myWaveshape2[i]/8000); // needed math to divide back to -1.0 to 1.0
} //for i
for (int i=0; i<32769; i++) {
fv=myWaveshape2[i]*16384; // set fv for re-aplication of sin function
d16 = sin( pi * ( fv / 32768 )) * ( ds16 * 1000 );// sin function
lg[i] = round( d16 * dgain ); // round and gain
lg[i]= lg[i]/8000; // needed math to divide back to -1.0 to 1.0
} // for
switch (smode){
case 0 : // Full sin function
path = createShape();
path.beginShape();
path.noFill();
path.stroke(255); // brightness of line
path.strokeWeight(1); // thickness of line
for (int i=0; i<32769; i++) {
lg[i]=(myWaveshape2[i]);// just transfer full sin fuction shape
path.vertex(i/100,lg[i]*100);// put array into shape named path
//if (( i & 63 )== 63) {// print every 64th loop
// print(lg[i]);
// print(",");
// if (( i & 511 )== 511)println();
//}// i & 63
} // for
path.endShape();
break;
case 1 : // Double sin function
// 3rd re-aplication of sin function
path = createShape();
path.beginShape();
path.noFill();
path.stroke(255); // brightness of line
path.strokeWeight(1); // thickness of line
for (int i=0; i<32769; i++) {
fv=myWaveshape2[i]*16384; // set fv for re-aplication of sin function
d16 = sin( pi * ( fv / 32768 )) * ( ds16 * 1000 );// sin function
lg[i] = round( d16 * dgain ); // round and gain
lg[i]= lg[i]/8000; // needed math to divide back to -1.0 to 1.0
path.vertex(i/100,lg[i]*100);// put array into shape named path
//if (( i & 63 )== 63) {// print every 64th loop
//print(lg[i]);
//print(",");
//if (( i & 511 )== 511)println();
//}// i & 63
}// for
path.endShape();
break;
case 2 : // Triple sin function
SinReAp();// 3rd re-aplication of sin function
break;
case 3 : // Quad sin function
SinReAp();// 3rd re-aplication of sin function
SinReAp();// 4th re-aplication of sin function
break;
case 4 : // 5x sin function
SinReAp();// 3rd re-aplication of sin function
SinReAp();// 4th re-aplication of sin function
SinReAp();// 5th re-aplication of sin function
break;
case 5 : // 6x sin function
SinReAp();// 3rd re-aplication of sin function
SinReAp();// 4th re-aplication of sin function
SinReAp();// 5th re-aplication of sin function
SinReAp();// 6th re-aplication of sin function
break;
case 6 : // 7x sin function
SinReAp();// 3rd re-aplication of sin function
SinReAp();// 4th re-aplication of sin function
SinReAp();// 5th re-aplication of sin function
SinReAp();// 6th re-aplication of sin function
SinReAp();// 7th re-aplication of sin function
break;
case 7 : // 8x sin function
SinReAp();// 3rd re-aplication of sin function
SinReAp();// 4th re-aplication of sin function
SinReAp();// 5th re-aplication of sin function
SinReAp();// 6th re-aplication of sin function
SinReAp();// 7th re-aplication of sin function
SinReAp();// 8th re-aplication of sin function
break;
}// switch
//waveshape1.shape(lg, 32769);// shape implementation L
//waveshape2.shape(lg, 32769);// shape implementation R
} // NewShape
void SinReAp(){
path = createShape();
path.setStroke(color(0,255,0));// red, green, blue // line color
path.beginShape();
path.strokeWeight(1); // thickness of line
path.noFill();
for (int i=0; i<32769; i++) {
fv=lg[i]*16384; // set fv re-aplication of sin function as many times as needed
d16 = sin( pi * ( fv / 32768 )) * ( ds16 * 1000 );// sin function
lg[i] = round( d16 * dgain ); // round and gain
lg[i]= lg[i]/8000; // needed math to divide back to -1.0 to 1.0
path.vertex(i/100,lg[i]*100);// put array into shape named path
//if (( i & 63 )== 63) {// print every 64th loop
//print(lg[i]);
//print(",");
//if (( i & 511 )== 511)println();
//}// i & 63
path.vertex(i/100,lg[i]*100);// put array into shape named path
} // for
path.endShape();
} // SinReAp