Samuel Mokrani
/
USBAudio_micro
USBAudio example using a microphone
main.cpp@9:9de252a14cde, 2012-09-27 (annotated)
- Committer:
- samux
- Date:
- Thu Sep 27 20:55:04 2012 +0000
- Revision:
- 9:9de252a14cde
- Parent:
- 8:caede7b4c444
- Child:
- 10:3eab670ff01f
USB audio with microphone
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux |
0:539ec61e1fbb | 1 | #include "mbed.h" |
samux |
8:caede7b4c444 | 2 | #include "USBAudio.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 | 9:9de252a14cde | 13 | // as we don't use audio in, 48000 and 1 are dummy |
samux | 9:9de252a14cde | 14 | USBAudio audio(48000, 1, FREQ, NB_CHA, 0x1007, 0xa455); |
samux |
6:be128039be16 | 15 | |
samux |
3:e6a29c83ac52 | 16 | AnalogIn mic(p20); |
samux |
0:539ec61e1fbb | 17 | |
samux |
5:b49b6a8ca111 | 18 | int16_t buf[AUDIO_LENGTH_PACKET/2]; |
samux |
4:bef3b485f22e | 19 | |
samux |
0:539ec61e1fbb | 20 | int main() { |
samux | 9:9de252a14cde | 21 | double mic_mean = 0.0; |
samux |
5:b49b6a8ca111 | 22 | double mic_value; |
samux |
5:b49b6a8ca111 | 23 | |
samux |
5:b49b6a8ca111 | 24 | // compute average value of the microphone. We can then center the audio signal sent to the computer |
samux |
5:b49b6a8ca111 | 25 | for (int j = 0; j < 1000; j++) { |
samux |
5:b49b6a8ca111 | 26 | mic_value = (mic.read_u16() >> 3); |
samux |
5:b49b6a8ca111 | 27 | mic_mean = (mic_mean*j + mic_value)/(j+1); |
samux |
5:b49b6a8ca111 | 28 | } |
samux |
3:e6a29c83ac52 | 29 | |
samux |
0:539ec61e1fbb | 30 | while (1) { |
samux |
5:b49b6a8ca111 | 31 | for (int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) { |
samux |
5:b49b6a8ca111 | 32 | buf[i] = (mic.read_u16() >> 3) - mic_mean; |
samux |
5:b49b6a8ca111 | 33 | if (i != AUDIO_LENGTH_PACKET/2) { |
samux |
6:be128039be16 | 34 | wait_us(80); |
samux |
3:e6a29c83ac52 | 35 | } |
samux |
3:e6a29c83ac52 | 36 | } |
samux |
0:539ec61e1fbb | 37 | audio.write((uint8_t *)buf); |
samux | 9:9de252a14cde | 38 | } |
samux |
0:539ec61e1fbb | 39 | } |