Microphone Record and play. using Nucleo STM32F303RE STM32 Hands On Makers Workshop

Dependencies:   mbed

Committer:
STM32Workshop
Date:
Mon Jul 25 09:16:58 2016 +0000
Revision:
2:1dbe3a1af47a
Parent:
1:5f34e59f97f7
Updated version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
STM32Workshop 0:ff0b6f6ddb23 1 #include "mbed.h"
STM32Workshop 0:ff0b6f6ddb23 2
STM32Workshop 0:ff0b6f6ddb23 3 // Serial pc(SERIAL_TX, SERIAL_RX); // for debbug
STM32Workshop 0:ff0b6f6ddb23 4
STM32Workshop 0:ff0b6f6ddb23 5 // Peripherials
STM32Workshop 0:ff0b6f6ddb23 6 DigitalIn mybutton(USER_BUTTON);
STM32Workshop 0:ff0b6f6ddb23 7 DigitalOut myled(LED1);
STM32Workshop 0:ff0b6f6ddb23 8 AnalogIn AudioIn(A0);
STM32Workshop 0:ff0b6f6ddb23 9 AnalogOut AudioOut(A2);
STM32Workshop 0:ff0b6f6ddb23 10
STM32Workshop 0:ff0b6f6ddb23 11 // Define Ticker
STM32Workshop 0:ff0b6f6ddb23 12 Ticker Audio_Sample_Ticker;
STM32Workshop 0:ff0b6f6ddb23 13
STM32Workshop 0:ff0b6f6ddb23 14 // Audio Buffer
STM32Workshop 1:5f34e59f97f7 15 #define BUFFERSIZE 30000 // Sample rate 5 Khz, 3 sec record 30*10^3 samples
STM32Workshop 0:ff0b6f6ddb23 16 uint16_t ABuffer[BUFFERSIZE]={0};
STM32Workshop 0:ff0b6f6ddb23 17 // variables to store buffer position and lenght
STM32Workshop 0:ff0b6f6ddb23 18 uint32_t BufferEnd=0;
STM32Workshop 0:ff0b6f6ddb23 19 uint32_t Buffer_i=0;
STM32Workshop 0:ff0b6f6ddb23 20 bool RecordFull=0;
STM32Workshop 0:ff0b6f6ddb23 21 // Variable to store button state
STM32Workshop 0:ff0b6f6ddb23 22 bool ButtonState=0;
STM32Workshop 0:ff0b6f6ddb23 23 bool LastButtonState=0;
STM32Workshop 0:ff0b6f6ddb23 24
STM32Workshop 0:ff0b6f6ddb23 25 volatile bool Audio_Sample_Flag=0;
STM32Workshop 0:ff0b6f6ddb23 26
STM32Workshop 0:ff0b6f6ddb23 27 // Filter sound HPF to remove dc for amplification
STM32Workshop 0:ff0b6f6ddb23 28 // Simple First order filter
STM32Workshop 0:ff0b6f6ddb23 29 float AudioSignal=0;
STM32Workshop 0:ff0b6f6ddb23 30 float AHPF=0.99f;
STM32Workshop 0:ff0b6f6ddb23 31
STM32Workshop 0:ff0b6f6ddb23 32 float LastY=0;
STM32Workshop 0:ff0b6f6ddb23 33 float CurY=0;
STM32Workshop 0:ff0b6f6ddb23 34 float LastU=0;
STM32Workshop 0:ff0b6f6ddb23 35 float CurU=0;
STM32Workshop 0:ff0b6f6ddb23 36
STM32Workshop 0:ff0b6f6ddb23 37 // Ticker function
STM32Workshop 0:ff0b6f6ddb23 38 void Audio_Sample() {
STM32Workshop 0:ff0b6f6ddb23 39 Audio_Sample_Flag=1;;
STM32Workshop 0:ff0b6f6ddb23 40 }
STM32Workshop 0:ff0b6f6ddb23 41
STM32Workshop 0:ff0b6f6ddb23 42 int main() { // setup loop
STM32Workshop 0:ff0b6f6ddb23 43
STM32Workshop 0:ff0b6f6ddb23 44 // attach Ticker object
STM32Workshop 0:ff0b6f6ddb23 45 Audio_Sample_Ticker.attach_us(&Audio_Sample, 100); // create 10Khz Ticker
STM32Workshop 0:ff0b6f6ddb23 46
STM32Workshop 0:ff0b6f6ddb23 47 while(1) { // infinite loop
STM32Workshop 0:ff0b6f6ddb23 48 if (Audio_Sample_Flag){
STM32Workshop 0:ff0b6f6ddb23 49 Audio_Sample_Flag=0;
STM32Workshop 0:ff0b6f6ddb23 50 LastButtonState=ButtonState;
STM32Workshop 0:ff0b6f6ddb23 51 ButtonState=mybutton.read();
STM32Workshop 0:ff0b6f6ddb23 52
STM32Workshop 0:ff0b6f6ddb23 53 // Record sample and apply filter
STM32Workshop 0:ff0b6f6ddb23 54 // Buffer variables
STM32Workshop 0:ff0b6f6ddb23 55 LastU=CurU;
STM32Workshop 0:ff0b6f6ddb23 56 CurU=AudioIn.read();
STM32Workshop 0:ff0b6f6ddb23 57 LastY=CurY;
STM32Workshop 0:ff0b6f6ddb23 58
STM32Workshop 0:ff0b6f6ddb23 59 // Simple Filter HPF
STM32Workshop 0:ff0b6f6ddb23 60 CurY=AHPF*(LastY+CurU-LastU);
STM32Workshop 0:ff0b6f6ddb23 61
STM32Workshop 0:ff0b6f6ddb23 62 // Amplify, add dc offset and add to buffer;
STM32Workshop 1:5f34e59f97f7 63 AudioSignal=CurY*2.0f+0.5f;//*10.0f+0.5f);
STM32Workshop 0:ff0b6f6ddb23 64 if (AudioSignal>1){
STM32Workshop 0:ff0b6f6ddb23 65 AudioSignal=1;
STM32Workshop 0:ff0b6f6ddb23 66 }else if (AudioSignal<0){
STM32Workshop 0:ff0b6f6ddb23 67 AudioSignal=0;
STM32Workshop 0:ff0b6f6ddb23 68 }
STM32Workshop 0:ff0b6f6ddb23 69
STM32Workshop 0:ff0b6f6ddb23 70 if ((!ButtonState)&&(!RecordFull) ){ // Button is pressed and record is not full
STM32Workshop 0:ff0b6f6ddb23 71 myled.write(1); // LED On
STM32Workshop 0:ff0b6f6ddb23 72 if (LastButtonState){ // first sample
STM32Workshop 0:ff0b6f6ddb23 73 Buffer_i=0;
STM32Workshop 0:ff0b6f6ddb23 74 // Re-set Ticker object
STM32Workshop 0:ff0b6f6ddb23 75 Audio_Sample_Ticker.attach_us(&Audio_Sample, 100); // create 10Khz Ticker
STM32Workshop 0:ff0b6f6ddb23 76 }
STM32Workshop 0:ff0b6f6ddb23 77 // Record samples
STM32Workshop 0:ff0b6f6ddb23 78 //ABuffer[Buffer_i]=AudioIn.read_u16();
STM32Workshop 0:ff0b6f6ddb23 79
STM32Workshop 0:ff0b6f6ddb23 80 AudioSignal=AudioSignal*0xFFFF; // scale to 16 bit (aligned to left apparently, wasted an hour on that one)
STM32Workshop 0:ff0b6f6ddb23 81 ABuffer[Buffer_i]=(uint16_t)AudioSignal; // Store to buffer;
STM32Workshop 0:ff0b6f6ddb23 82
STM32Workshop 0:ff0b6f6ddb23 83 //pc.printf("%d \r\n",ABuffer[Buffer_i]); // debug check/
STM32Workshop 0:ff0b6f6ddb23 84 //AudioOut.write_u16(ABuffer[Buffer_i]); // Debug output
STM32Workshop 0:ff0b6f6ddb23 85
STM32Workshop 0:ff0b6f6ddb23 86 BufferEnd=Buffer_i;
STM32Workshop 0:ff0b6f6ddb23 87 Buffer_i++;
STM32Workshop 0:ff0b6f6ddb23 88 if (Buffer_i==BUFFERSIZE){ // buffer full
STM32Workshop 0:ff0b6f6ddb23 89 RecordFull=1;
STM32Workshop 0:ff0b6f6ddb23 90 Buffer_i=0;
STM32Workshop 0:ff0b6f6ddb23 91 myled.write(0); // LED Off // record off
STM32Workshop 0:ff0b6f6ddb23 92 }
STM32Workshop 0:ff0b6f6ddb23 93
STM32Workshop 0:ff0b6f6ddb23 94 }else if (ButtonState) { //Button is unpressed
STM32Workshop 0:ff0b6f6ddb23 95 if (!LastButtonState){ // first sample
STM32Workshop 0:ff0b6f6ddb23 96 // Re-set Ticker object
STM32Workshop 1:5f34e59f97f7 97 Audio_Sample_Ticker.attach_us(&Audio_Sample, 50); // create 10Khz Ticker
STM32Workshop 0:ff0b6f6ddb23 98 // Buffer_i=0;
STM32Workshop 0:ff0b6f6ddb23 99 }
STM32Workshop 0:ff0b6f6ddb23 100 myled.write(0); // LED Off
STM32Workshop 0:ff0b6f6ddb23 101 // Play Recorded audio
STM32Workshop 0:ff0b6f6ddb23 102 RecordFull=0;
STM32Workshop 0:ff0b6f6ddb23 103 if (Buffer_i>BufferEnd){ // Replay record
STM32Workshop 0:ff0b6f6ddb23 104 Buffer_i=0;
STM32Workshop 0:ff0b6f6ddb23 105 }
STM32Workshop 0:ff0b6f6ddb23 106 AudioOut.write_u16(ABuffer[Buffer_i]);
STM32Workshop 0:ff0b6f6ddb23 107 Buffer_i++;
STM32Workshop 0:ff0b6f6ddb23 108 }
STM32Workshop 0:ff0b6f6ddb23 109 }
STM32Workshop 0:ff0b6f6ddb23 110 }
STM32Workshop 0:ff0b6f6ddb23 111 }
STM32Workshop 0:ff0b6f6ddb23 112