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