/* sine_generator.pde guest openmusiclabs 7.7.11 this program creates a sinewave of variable frequency and amplitude, presented at both left and right outputs. there isnt any interpolation, so you only get 256 discrete frequencies across the span of 44Hz to 10kHz. */ // setup codec parameters // must be done before #includes // see readme file in libraries folder for explanations #define SAMPLE_RATE 44 // 44.1kHz sample rate #define ADCS 0 // use both ADCs // include necessary libraries #include #include #include // create data variables for audio transfer // even though there is no input needed, the codec requires stereo data int left_in = 0; // in from codec (LINE_IN) int right_in = 0; int left_out = 0; // out to codec (HP_OUT) int right_out = 0; // create variables for ADC results // it only has positive values -> unsigned unsigned int mod0_value = 0; unsigned int mod1_value = 0; // create sinewave lookup table // PROGMEM stores the values in the program memory // it is automatically included with AudioCodec.h PROGMEM prog_int16_t sinewave[] = { // this file is stored in AudioCodec.h and is a 1024 value // sinewave lookup table of signed 16bit integers // you can replace it with your own waveform if you like #include }; unsigned int location; // lookup table value location void setup() { // call this last if you are setting up other things Serial.begin(115200); // AudioCodec_init(); // setup codec and microcontroller registers // TIMSK0 = 1; Serial.println("hello"); } #define SIZE 20 // number of samples to collect // SIZE >= (BINS + 1) #define BINS 6 // number of samples to slide the window by // total number is +/- BINS + the 0 bin (2*BINS + 1). volatile byte flag = 1; byte count = 0; int buffer1[SIZE]; int buffer2[SIZE]; long curr_max = 0; char curr_bin = 0; int var = 0; void loop() { while (1) { while (flag); // wait till samples are collected // do correlation curr_max = 0; for (char j = 0; j <= BINS; j++) { long result = 0; byte k = 0; for (char i = j; i < SIZE; i++) { long temp; MultiS16X16to32(temp, buffer1[k], buffer2[i]); result += temp >> 8; k++; } if (result > curr_max) { curr_bin = -j; curr_max = result; } } for (char j = 1; j <= BINS; j++) { long result = 0; byte k = 0; for (char i = j; i < SIZE; i++) { long temp; MultiS16X16to32(temp, buffer1[i], buffer2[k]); result += temp >> 8; k++; } if (result > curr_max) { curr_bin = j; curr_max = result; } } Serial.println(curr_bin, DEC); flag = 1; // gather more samples } } // timer1 interrupt routine - all data processed here ISR(TIMER1_COMPA_vect) { // dont store any registers // &'s are necessary on data_in variables AudioCodec_data(&left_in, &right_in, left_out, right_out); if (flag) { // collect samples buffer1[count] = left_in; buffer2[count] = right_in; count++; if (count >= SIZE) flag = 0; } }