Audio echo effect. See http://dev.frozeneskimo.com/embedded_projects/audio_echo_effect for more info!

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define MAX_DELAY   15000
00004 #define MIN_DELAY   50
00005 
00006 #define MAX_GAIN    25
00007 #define MIN_GAIN    2
00008 
00009 /* ADC for the microphone/input, DAC for the speaker/output */
00010 AnalogIn mic(p19);
00011 AnalogOut speaker(p18);
00012 /* Two potentiometer voltage dividers for the delay/gain control knobs */
00013 AnalogIn delay_knob(p15);
00014 AnalogIn gain_knob(p16);
00015 
00016 unsigned short buffer[MAX_DELAY];
00017 
00018 /* inv_gain = 1 / gain; it's faster to avoid floating point during the main loop */
00019 int inv_gain = 3;
00020 int delay = MAX_DELAY;
00021 
00022 void read_knobs(void) {
00023     delay = delay_knob*MAX_DELAY;
00024     //gain = gain_knob*MAX_GAIN;
00025     if (delay < MIN_DELAY)
00026         delay = MIN_DELAY;
00027     /*if (gain < MIN_GAIN)
00028         gain = MIN_GAIN;
00029     if (gain == MAX_GAIN)
00030         gain -= 1;*/
00031 }
00032 
00033 int main() {
00034     int i;
00035     /* Fill up the sample buffer first */
00036     for (i = 0; i < delay; i++)
00037         buffer[i] += mic.read_u16();
00038 
00039     for (i = 0; ; ) {
00040         /* Multiply old data by the gain, add new data */
00041         buffer[i] = buffer[i]/inv_gain + mic.read_u16();
00042         /* Write to speaker */
00043         speaker.write_u16(buffer[i]);
00044         /* Increment index and wrap around, effectively only using "delay" length of the buffer */
00045         i = (i+1) % delay;
00046         /* Occasionally read the knobs */
00047         if (i == 0)
00048             read_knobs();
00049     }
00050 }