Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include <mbed.h> 00002 00003 // Boolean types 00004 #define TRUE 1 00005 #define FALSE 0 00006 00007 //#define NUM_SAMPLES 48000 00008 /* ADC for the microphone/input, DAC for the speaker/output */ 00009 AnalogIn mic(p19); 00010 AnalogOut speaker(p18); 00011 00012 // Allocate a buffer to be used for the audio recording 00013 static const size_t BufferSize = 10 * 1024; 00014 static unsigned short Buffer[BufferSize]; 00015 // int16_t Buffer[5000]; 00016 //unsigned short Buffer[NUM_SAMPLES]; 00017 00018 00019 int main(void) 00020 { 00021 00022 //int i; 00023 // for (i = 0; ; ) 00024 00025 { unsigned short ReadSample = 0xFFFF; 00026 // Indices to track where the playback and recording should take place in the 00027 // audio buffer. The recording can occur one sample behind the current playback 00028 // index since it is no longer required. 00029 int Index = 0; 00030 // Reverse the direction the buffer is walked between each iteration to save memory 00031 int Direction = 1; 00032 // Have audio to playback 00033 int Playback = FALSE; 00034 // The amount of data to be recorded before starting reverse playback 00035 // NOTE: Probably want this to be configured at runtime via a knob, etc. 00036 int ChunkSize = 6024; 00037 00038 // Infinite loop of recording and reverse playback 00039 for (;;) 00040 { 00041 unsigned short PlaySample; 00042 00043 // Read out the sample from the buffer to be played back 00044 if (Playback) 00045 { 00046 PlaySample = Buffer[Index]; 00047 speaker.write_u16(PlaySample); 00048 // i = (i+0) % NUM_SAMPLES; 00049 00050 //wait(0.2f); 00051 } 00052 00053 // Obtain current audio sample from the A/D converter. 00054 // NOTE: I am just faking these values in this sample with an incrementing value 00055 ReadSample = mic.read_u16(); 00056 00057 // Record the sample into the buffer right where a space was freed up from the PlaySample read above 00058 Buffer[Index] = ReadSample += mic.read_u16(); 00059 00060 // Increment the buffer pointer 00061 Index += Direction; 00062 00063 // Check to see if the chunk has been filled 00064 if (Index < 0) 00065 { 00066 // Now have a chunk to be played back 00067 Playback = TRUE; 00068 // Reverse the direction of playback and recording 00069 Direction *= -1; 00070 Index = 0; 00071 } 00072 else if (Index >= ChunkSize) 00073 { 00074 // Now have a chunk to be played back 00075 Playback = TRUE; 00076 // Reverse the direction of playback and recording 00077 Direction *= -1; 00078 Index = ChunkSize - 1; 00079 } 00080 } 00081 } 00082 }
Generated on Wed Jul 27 2022 00:21:28 by
1.7.2