USBAudio example using a microphone

Dependencies:   USBDevice mbed

Committer:
samux
Date:
Tue Dec 20 11:41:31 2011 +0000
Revision:
7:6b0012b8fd01
Parent:
6:be128039be16
Child:
8:caede7b4c444
create USBAudioOUT class. all is working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:539ec61e1fbb 1 #include "mbed.h"
samux 7:6b0012b8fd01 2 #include "USBAudioOUT.h"
samux 6:be128039be16 3
samux 5:b49b6a8ca111 4 // frequency: 8 kHz
samux 5:b49b6a8ca111 5 #define FREQ 8000
samux 5:b49b6a8ca111 6
samux 5:b49b6a8ca111 7 // 1 channel: mono
samux 5:b49b6a8ca111 8 #define NB_CHA 1
samux 0:539ec61e1fbb 9
samux 5:b49b6a8ca111 10 // 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
samux 5:b49b6a8ca111 11 #define AUDIO_LENGTH_PACKET (FREQ/500) * NB_CHA
samux 5:b49b6a8ca111 12
samux 7:6b0012b8fd01 13 USBAudioOUT audio(FREQ, NB_CHA, 0x1111, 0x78ab);
samux 6:be128039be16 14
samux 3:e6a29c83ac52 15 AnalogIn mic(p20);
samux 0:539ec61e1fbb 16
samux 5:b49b6a8ca111 17 int16_t buf[AUDIO_LENGTH_PACKET/2];
samux 4:bef3b485f22e 18
samux 0:539ec61e1fbb 19 int main() {
samux 5:b49b6a8ca111 20 double mic_mean = 0.0;
samux 5:b49b6a8ca111 21 double mic_value;
samux 5:b49b6a8ca111 22
samux 5:b49b6a8ca111 23 // compute average value of the microphone. We can then center the audio signal sent to the computer
samux 5:b49b6a8ca111 24 for (int j = 0; j < 1000; j++) {
samux 5:b49b6a8ca111 25 mic_value = (mic.read_u16() >> 3);
samux 5:b49b6a8ca111 26 mic_mean = (mic_mean*j + mic_value)/(j+1);
samux 5:b49b6a8ca111 27 }
samux 3:e6a29c83ac52 28
samux 0:539ec61e1fbb 29 while (1) {
samux 5:b49b6a8ca111 30 for (int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
samux 5:b49b6a8ca111 31 buf[i] = (mic.read_u16() >> 3) - mic_mean;
samux 5:b49b6a8ca111 32 if (i != AUDIO_LENGTH_PACKET/2) {
samux 6:be128039be16 33 wait_us(80);
samux 3:e6a29c83ac52 34 }
samux 3:e6a29c83ac52 35 }
samux 0:539ec61e1fbb 36 audio.write((uint8_t *)buf);
samux 0:539ec61e1fbb 37 }
samux 0:539ec61e1fbb 38 }