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