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