this stuff is pretty amazing
it basically takes no time to process
/*
karplus.pde
guest openmusiclabs 4.3.12
*/
// setup codec parameters
// must be done before #includes
// see readme file in libraries folder for explanations
#define SAMPLE_RATE 44 // 44.1Khz
#define ADCS 1 // no ADCs are being used
// include necessary libraries
#include <Wire.h>
#include <SPI.h>
#include <AudioCodec.h>
// create data variables for audio transfer
int left_in = 0x0000;
int left_out = 0x0000;
int right_in = 0x0000;
int right_out = 0x0000;
int last_value = 0x0000;
int curr_value = 0x0000;
unsigned int decay = 0x0000;
#define DECAY 65000
unsigned int mod0_value =0;
// create a delay buffer in memory
#define SIZE 800 // buffer size is limited by microcontroller SRAM size
int delaymem[SIZE]; // 800 positions x 2 bytes = 1600 bytes of SRAM
unsigned int location = 0; // buffer location to read/write from
unsigned int boundary = SIZE;
void setup() {
for (int i = 0; i < SIZE; i++) {
delaymem[i] = random(65535);
}
AudioCodec_init(); // setup codec registers
// call this last if setting up other parts
}
void loop() {
while (1); // reduces clock jitter
}
// timer1 interrupt routine - all data processed here
ISR(TIMER1_COMPA_vect, ISR_NAKED) { // dont store any registers
// &'s are necessary on data_in variables
AudioCodec_data(&left_in, &right_in, left_out, right_out);
// & is required before adc variable
AudioCodec_ADC(&mod0_value);
if (decay++ < DECAY) {
delaymem[location++] = left_out;
// check if location has gotten bigger than buffer size
if (location >= boundary) {
location = 0; // reset location
}
curr_value = delaymem[location];
left_out = (last_value>>1) + (curr_value>>1);
last_value = curr_value;
}
else {
for (int i = 0; i < SIZE; i++) {
delaymem[i] = random(65535);
}
decay = 0;
unsigned int scale = SIZE;
MultiU16X16toH16(boundary, mod0_value, scale);
}
// dont forget to return from interrupt
reti();
}