Samuel Mokrani
/
USBAudio_micro
USBAudio example using a microphone
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "USBAudio.h" 00003 00004 // frequency: 8 kHz 00005 #define FREQ 8000 00006 00007 // 1 channel: mono 00008 #define NB_CHA 1 00009 00010 // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1 00011 #define AUDIO_LENGTH_PACKET (FREQ/500) * NB_CHA 00012 00013 // as we don't use audio in, 48000 and 1 are dummy 00014 USBAudio audio(8000, 1, FREQ, NB_CHA, 0x0103, 0x4521); 00015 00016 AnalogIn mic(p20); 00017 00018 int16_t buf[AUDIO_LENGTH_PACKET/2]; 00019 00020 int main() { 00021 double mic_mean = 0.0; 00022 double mic_value; 00023 00024 // compute average value of the microphone. We can then center the audio signal sent to the computer 00025 for (int j = 0; j < 1000; j++) { 00026 mic_value = (mic.read_u16() >> 3); 00027 mic_mean = (mic_mean*j + mic_value)/(j+1); 00028 } 00029 00030 while (1) { 00031 for (int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) { 00032 buf[i] = (mic.read_u16() >> 3) - mic_mean; 00033 if (i != AUDIO_LENGTH_PACKET/2) { 00034 wait_us(80); 00035 } 00036 } 00037 audio.write((uint8_t *)buf); 00038 } 00039 }
Generated on Thu Jul 14 2022 02:13:17 by 1.7.2