Voice input at ADC (p20) is sampled at 16kHz and delayed 0.1-0.5s and output at DAC (p18).
Dependencies: mbed
Diff: voiceecho.cpp
- Revision:
- 3:8ed0851755d7
- Parent:
- 2:718d996170c2
diff -r 718d996170c2 -r 8ed0851755d7 voiceecho.cpp --- a/voiceecho.cpp Fri Apr 19 04:11:04 2019 +0000 +++ b/voiceecho.cpp Mon Jul 26 01:25:31 2021 +0000 @@ -13,12 +13,12 @@ AnalogOut spk(p18); Ticker voicetick; -#define WAVMAX 7000 /*buffer size; SAMPFREQ*DEALY<WAVMAX*/ +#define WAVMAX 10000 /*buffer size; SAMPFREQ*DEALY<WAVMAX*/ #define SAMPFREQ 16000.0 -#define DELAY 0.25 /*s*/ +#define DELAY 0.3 /*s*/ unsigned short wave[WAVMAX]; -int readindex=0, writeindex=0; +int readindex=0, writeindex=0, wavmax=WAVMAX; class KBline : public Serial { @@ -81,12 +81,12 @@ } ; int newindex(int ix) -{ if (ix<0) ix += WAVMAX; - return(ix % WAVMAX); } +{ if (ix<0) ix += wavmax; + return(ix % wavmax); } void voice_io() /*timer driven*/ { unsigned short val; - wave[readindex]= wave[readindex]*0.4+mic.read_u16()*0.6; //mic.read()*30000; + wave[readindex]= wave[readindex]*0.45+mic.read_u16()*0.55; //mic.read()*30000; val=wave[writeindex]; pwm.write((float)val/65536.0); spk.write_u16(val); // (val/30000.0 ); @@ -103,6 +103,7 @@ pwm.period_us(32); /* 62.5us*/ delay=DELAY; readindex=0; + wavmax=delay*SAMPFREQ+1; writeindex=newindex(readindex-delay*SAMPFREQ); voicetick.attach(voice_io, 1.0/SAMPFREQ); pc.printf("echo machine delay=%fs\n\r", delay); @@ -113,6 +114,8 @@ wait(0.2); } sscanf(pc.line,"%f", &delay); /* multiple of 0.1s */ + readindex=0; + wavmax=delay*SAMPFREQ+1; writeindex=newindex(readindex-delay*SAMPFREQ); pc.printf("new delay %5.2fs r=%d w=%d val=%d\n\r", delay, readindex, writeindex, wave[writeindex]); }