A reverse delay to help with the study of reverse speech

Dependencies:   mbed

Fork of Reverseguitar20kcutoff by Philips Philips

Files at this revision

API Documentation at this revision

Comitter:
mbed2f
Date:
Mon May 30 15:31:21 2011 +0000
Commit message:

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 8eeac881ada0 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon May 30 15:31:21 2011 +0000
@@ -0,0 +1,73 @@
+#include <mbed.h>
+ 
+// Boolean types
+#define TRUE 1
+#define FALSE 0
+
+/* ADC for the microphone/input, DAC for the speaker/output */
+AnalogIn mic(p19);
+AnalogOut speaker(p18);
+ 
+// Allocate a buffer to be used for the audio recording
+static const size_t   BufferSize = 15 * 1024;
+static unsigned short Buffer[BufferSize];
+ 
+ 
+int main(void)
+{
+    unsigned short        ReadSample = 0xFFFF;
+    // Indices to track where the playback and recording should take place in the
+    // audio buffer.  The recording can occur one sample behind the current playback
+    // index since it is no longer required.
+    int                  Index = 0;
+    // Reverse the direction the buffer is walked between each iteration to save memory
+    int                  Direction = 1;
+    // Have audio to playback
+    int                  Playback = FALSE;
+    // The amount of data to be recorded before starting reverse playback
+    // NOTE: Probably want this to be configured at runtime via a knob, etc.
+    int                  ChunkSize = BufferSize;
+    
+    // Infinite loop of recording and reverse playback
+    for (;;)
+    {
+        unsigned short PlaySample;
+ 
+        // Slow down the sampling rate so that it doesn't exceed 20kHz
+        wait_us(50);
+        
+        // Read out the sample from the buffer to be played back
+        if (Playback)
+        {
+            PlaySample = Buffer[Index];
+            speaker.write_u16(PlaySample);
+        }
+        
+        // Obtain current audio sample from the A/D converter.
+        ReadSample = mic.read_u16();    
+        
+        // Record the sample into the buffer right where a space was freed up from the PlaySample read above
+        Buffer[Index] = ReadSample;
+        
+         // Increment the buffer pointer
+        Index += Direction;
+        
+        // Check to see if the chunk has been filled
+        if (Index < 0)
+        {
+            // Now have a chunk to be played back
+            Playback = TRUE;
+            // Reverse the direction of playback and recording
+            Direction *= -1;
+            Index = 0;
+        }
+        else if (Index >= ChunkSize)
+        {
+            // Now have a chunk to be played back
+            Playback = TRUE;
+            // Reverse the direction of playback and recording
+            Direction *= -1;
+            Index = ChunkSize - 1;
+        }
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 8eeac881ada0 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon May 30 15:31:21 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e