haha Eggsperde,
I've got the code running, with synchronized background adc function, using Pedvides adc library: https://github.com/pedvide/ADC
You can choose resolution and sampleRate you want, lowering sampleRate will give you more cpu time, ofcourse..
I havent tested it with actual guitar yet, but hey, it runs, gives vital information about performance over serial, so you can tweak it fast.
Have fun!
-You'll have to connect your guitar to A0 via C and dividernetwork. protective Diodes strongly recommended!
-output A14 via C
Code:
/*
---------------------------------------------------------------
C r u d e S t o m p b o x w i t h T e e n s y 3 . 1
---------------------------------------------------------------
Version 0.1 "Fuzzzzyy.."
Paul Driessen - 27 March 2014
ADC library used https://github.com/pedvide/ADC
This code represents the shortes way a sample can travel through a Teensy 3.1.
It uses Pedvide's ADC lib, for the benefit of adc usage in the background, the standard Arduino calls dont support that ( why not? )
Once setup correctly, the audiolatency of this code is 2 samples.
Features:
-low latency 2 sample latency throughput, output 12 bits via internal ADC
-A serial performance indicator will show you how much CPU you have left for coding cool stuff.
-An example fuzz routine with simple LP filter
Connections needed:
Input circuitry:
[TODO] ** OWN RISK **
Output circuitry:
-A condensator on pin A14
*/
#include <ADC.h>
ADC adc; // adc object
int rawAudioIn;
int dcAudio;
int audioIn;
int audioOut;
int sampleRate;
int microsPerSample;
elapsedMicros microCount;
elapsedMillis updateDisplay;
int accu=0;
void setup()
{
Serial.begin(38400);
delay(1000);
adc.setAveraging(1); // where not into astrophysics here, no averaging required
adc.setResolution(16); // valid resolutions for singelended adc like this are 8, 10, 12 or 16 bit.
// each resolution comes with it own max speed.
// Note: 16 bit will distort the output easy, I've added no conversion between bit depth buildin ( but it's easy: code it yourself!)
sampleRate=50000 ; // requested samplerate, will result however in 1 uS steppable frequencies!
microsPerSample= 1000000/sampleRate; // thats how we keep pace
analogWriteResolution(12); // max resolution on the output
for(int t=0; t<40000; t++)
{
accu += adc.analogRead(A0); // get the dc level..
}
dcAudio = accu / 40000;
adc.enableInterrupts();
adc.startSingleRead(A0); //get the systems started,
//adc.startContinuous (A0);
while(!adc.isComplete()) {}; //gets the first rawAudio..
microCount=0;
}
volatile byte readySample=0;
int adcUsage=0;
int originalAudio;
void loop()
{
audioIn += (rawAudioIn - dcAudio); // get rid of DC values the easiest way..
readySample=0; //reset flag
adc.startSingleRead(A0); // prepare the sample for next
originalAudio=audioIn;
//rawAudioIn= adc.analogReadContinuous ();
analogWrite(A14, (audioOut+2048)); // 1uS: write the value from previous calculation
//
//
// S T A R T O F C O D I N G A R E A ( you'll have about 80% power at your dispo @ 44,1k )
//
// Let's try a fuzz to start with..
int posTreshold=50; //higher than this will be set to Outlevel
int negTreshold=-50; //lower than this will be set to -1* Outlevel
int outLevel=1000; //
int IR_filter=8; // LP: the higher the number, the more the squarewave becomes smooth
if(audioIn>posTreshold)
{
audioIn=outLevel;
}else{
if(audioIn<negTreshold)
{
audioIn=-1*outLevel;
}else{
audioIn=0; // dead silence below treshold, typicall for fuzz..
}
}
// now apply a simple IR filter, to smooth the edges (LP)
audioIn = audioOut + ( (audioIn - (audioOut)) / IR_filter );
// Output is ready, copy it to the output.
audioOut = audioIn; //
//
// E N D O F C O D I N G A R E A
//
// Display Performance in %
int MC= microCount;
int Performance= (MC*100)/(microsPerSample);
int actFramerate =(1000000/microsPerSample);
if(updateDisplay>1000)
{
Serial.println("");
Serial.print("Time between samples:");
Serial.println(microsPerSample);
Serial.print("Real samplerate:");
Serial.print(actFramerate);
Serial.println(" uS");
Serial.print("CPU usage:");
Serial.print(Performance);
Serial.println("%");
Serial.print("ADC usage:");
Serial.print((adcUsage*100)/(microsPerSample));
Serial.println("% ");
Serial.print("originalAudio:");
Serial.println(originalAudio);
Serial.print("audioOut:");
Serial.println(audioOut);
updateDisplay=0;
}
while(microCount<microsPerSample){} // fill our timing budget
microCount-=microsPerSample; // keep pace
}
void adc0_isr(void) {
// Low-level code
rawAudioIn=ADC0_RA;
//GPIOC_PTOR = 1<<5; */
readySample=1;
adcUsage=microCount;
}