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