I am so cool I did it all by myself lol (.)(.)

Dependencies:   mbed

Committer:
mbed2f
Date:
Sun May 29 19:08:48 2011 +0000
Revision:
0:597fa7722bb6
Play guitar or speech in reverse

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed2f 0:597fa7722bb6 1 #include <mbed.h>
mbed2f 0:597fa7722bb6 2
mbed2f 0:597fa7722bb6 3 // Boolean types
mbed2f 0:597fa7722bb6 4 #define TRUE 1
mbed2f 0:597fa7722bb6 5 #define FALSE 0
mbed2f 0:597fa7722bb6 6
mbed2f 0:597fa7722bb6 7 //#define NUM_SAMPLES 48000
mbed2f 0:597fa7722bb6 8 /* ADC for the microphone/input, DAC for the speaker/output */
mbed2f 0:597fa7722bb6 9 AnalogIn mic(p19);
mbed2f 0:597fa7722bb6 10 AnalogOut speaker(p18);
mbed2f 0:597fa7722bb6 11
mbed2f 0:597fa7722bb6 12 // Allocate a buffer to be used for the audio recording
mbed2f 0:597fa7722bb6 13 static const size_t BufferSize = 10 * 1024;
mbed2f 0:597fa7722bb6 14 static unsigned short Buffer[BufferSize];
mbed2f 0:597fa7722bb6 15 // int16_t Buffer[5000];
mbed2f 0:597fa7722bb6 16 //unsigned short Buffer[NUM_SAMPLES];
mbed2f 0:597fa7722bb6 17
mbed2f 0:597fa7722bb6 18
mbed2f 0:597fa7722bb6 19 int main(void)
mbed2f 0:597fa7722bb6 20 {
mbed2f 0:597fa7722bb6 21
mbed2f 0:597fa7722bb6 22 //int i;
mbed2f 0:597fa7722bb6 23 // for (i = 0; ; )
mbed2f 0:597fa7722bb6 24
mbed2f 0:597fa7722bb6 25 { unsigned short ReadSample = 0xFFFF;
mbed2f 0:597fa7722bb6 26 // Indices to track where the playback and recording should take place in the
mbed2f 0:597fa7722bb6 27 // audio buffer. The recording can occur one sample behind the current playback
mbed2f 0:597fa7722bb6 28 // index since it is no longer required.
mbed2f 0:597fa7722bb6 29 int Index = 0;
mbed2f 0:597fa7722bb6 30 // Reverse the direction the buffer is walked between each iteration to save memory
mbed2f 0:597fa7722bb6 31 int Direction = 1;
mbed2f 0:597fa7722bb6 32 // Have audio to playback
mbed2f 0:597fa7722bb6 33 int Playback = FALSE;
mbed2f 0:597fa7722bb6 34 // The amount of data to be recorded before starting reverse playback
mbed2f 0:597fa7722bb6 35 // NOTE: Probably want this to be configured at runtime via a knob, etc.
mbed2f 0:597fa7722bb6 36 int ChunkSize = 6024;
mbed2f 0:597fa7722bb6 37
mbed2f 0:597fa7722bb6 38 // Infinite loop of recording and reverse playback
mbed2f 0:597fa7722bb6 39 for (;;)
mbed2f 0:597fa7722bb6 40 {
mbed2f 0:597fa7722bb6 41 unsigned short PlaySample;
mbed2f 0:597fa7722bb6 42
mbed2f 0:597fa7722bb6 43 // Read out the sample from the buffer to be played back
mbed2f 0:597fa7722bb6 44 if (Playback)
mbed2f 0:597fa7722bb6 45 {
mbed2f 0:597fa7722bb6 46 PlaySample = Buffer[Index];
mbed2f 0:597fa7722bb6 47 speaker.write_u16(PlaySample);
mbed2f 0:597fa7722bb6 48 // i = (i+0) % NUM_SAMPLES;
mbed2f 0:597fa7722bb6 49
mbed2f 0:597fa7722bb6 50 //wait(0.2f);
mbed2f 0:597fa7722bb6 51 }
mbed2f 0:597fa7722bb6 52
mbed2f 0:597fa7722bb6 53 // Obtain current audio sample from the A/D converter.
mbed2f 0:597fa7722bb6 54 // NOTE: I am just faking these values in this sample with an incrementing value
mbed2f 0:597fa7722bb6 55 ReadSample = mic.read_u16();
mbed2f 0:597fa7722bb6 56
mbed2f 0:597fa7722bb6 57 // Record the sample into the buffer right where a space was freed up from the PlaySample read above
mbed2f 0:597fa7722bb6 58 Buffer[Index] = ReadSample += mic.read_u16();
mbed2f 0:597fa7722bb6 59
mbed2f 0:597fa7722bb6 60 // Increment the buffer pointer
mbed2f 0:597fa7722bb6 61 Index += Direction;
mbed2f 0:597fa7722bb6 62
mbed2f 0:597fa7722bb6 63 // Check to see if the chunk has been filled
mbed2f 0:597fa7722bb6 64 if (Index < 0)
mbed2f 0:597fa7722bb6 65 {
mbed2f 0:597fa7722bb6 66 // Now have a chunk to be played back
mbed2f 0:597fa7722bb6 67 Playback = TRUE;
mbed2f 0:597fa7722bb6 68 // Reverse the direction of playback and recording
mbed2f 0:597fa7722bb6 69 Direction *= -1;
mbed2f 0:597fa7722bb6 70 Index = 0;
mbed2f 0:597fa7722bb6 71 }
mbed2f 0:597fa7722bb6 72 else if (Index >= ChunkSize)
mbed2f 0:597fa7722bb6 73 {
mbed2f 0:597fa7722bb6 74 // Now have a chunk to be played back
mbed2f 0:597fa7722bb6 75 Playback = TRUE;
mbed2f 0:597fa7722bb6 76 // Reverse the direction of playback and recording
mbed2f 0:597fa7722bb6 77 Direction *= -1;
mbed2f 0:597fa7722bb6 78 Index = ChunkSize - 1;
mbed2f 0:597fa7722bb6 79 }
mbed2f 0:597fa7722bb6 80 }
mbed2f 0:597fa7722bb6 81 }
mbed2f 0:597fa7722bb6 82 }