USBAudio speaker example

Dependencies:   mbed USBDevice

Committer:
samux
Date:
Fri Mar 01 13:21:37 2013 +0000
Revision:
9:a7a9d8e8a740
Parent:
7:ba4f65ce69f2
use latest USBDevice lib (FRDM-KL25Z support)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 3:41b10e311100 1 // USBAudio speaker example
samux 0:5176b3dfccd6 2
samux 0:5176b3dfccd6 3 #include "mbed.h"
samux 0:5176b3dfccd6 4 #include "USBAudio.h"
samux 0:5176b3dfccd6 5
samux 0:5176b3dfccd6 6 // frequency: 48 kHz
samux 0:5176b3dfccd6 7 #define FREQ 48000
samux 0:5176b3dfccd6 8
samux 0:5176b3dfccd6 9 // 1 channel: mono
samux 0:5176b3dfccd6 10 #define NB_CHA 1
samux 0:5176b3dfccd6 11
samux 0:5176b3dfccd6 12 // 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 2:c3edc567ae33 13 #define AUDIO_LENGTH_PACKET 48 * 2 * 1
samux 0:5176b3dfccd6 14
samux 7:ba4f65ce69f2 15 // USBAudio (we just use audio packets received, we don't send audio packets to the computer in this example)
samux 7:ba4f65ce69f2 16 USBAudio audio(FREQ, NB_CHA, 8000, 1, 0x7180, 0x7500);
samux 0:5176b3dfccd6 17
samux 0:5176b3dfccd6 18 // speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker
samux 0:5176b3dfccd6 19 AnalogOut speaker(p18);
samux 0:5176b3dfccd6 20
samux 0:5176b3dfccd6 21 // ticker to send data to the speaker at the good frequency
samux 0:5176b3dfccd6 22 Ticker tic;
samux 0:5176b3dfccd6 23
samux 1:0335b5c18618 24 // buffer where will be store one audio packet (LENGTH_AUDIO_PACKET/2 because we are storing int16 and not uint8)
samux 2:c3edc567ae33 25 int16_t buf[AUDIO_LENGTH_PACKET/2];
samux 0:5176b3dfccd6 26
samux 0:5176b3dfccd6 27 // show if an audio packet is available
samux 0:5176b3dfccd6 28 volatile bool available = false;
samux 0:5176b3dfccd6 29
samux 0:5176b3dfccd6 30 // index of the value which will be send to the speaker
samux 0:5176b3dfccd6 31 int index_buf = 0;
samux 0:5176b3dfccd6 32
samux 0:5176b3dfccd6 33 // previous value sent to the speaker
samux 0:5176b3dfccd6 34 uint16_t p_val = 0;
samux 0:5176b3dfccd6 35
samux 0:5176b3dfccd6 36 // function executed each 1/FREQ s
samux 0:5176b3dfccd6 37 void tic_handler() {
samux 0:5176b3dfccd6 38 float speaker_value;
samux 0:5176b3dfccd6 39
samux 0:5176b3dfccd6 40 if (available) {
samux 0:5176b3dfccd6 41 //convert 2 bytes in float
samux 1:0335b5c18618 42 speaker_value = (float)(buf[index_buf]);
samux 0:5176b3dfccd6 43
samux 0:5176b3dfccd6 44 // speaker_value between 0 and 65535
samux 0:5176b3dfccd6 45 speaker_value += 32768.0;
samux 0:5176b3dfccd6 46
samux 0:5176b3dfccd6 47 // adjust according to current volume
samux 0:5176b3dfccd6 48 speaker_value *= audio.getVolume();
samux 0:5176b3dfccd6 49
samux 0:5176b3dfccd6 50 // as two bytes has been read, we move the index of two bytes
samux 1:0335b5c18618 51 index_buf++;
samux 0:5176b3dfccd6 52
samux 0:5176b3dfccd6 53 // if we have read all the buffer, no more data available
samux 2:c3edc567ae33 54 if (index_buf == AUDIO_LENGTH_PACKET/2) {
samux 0:5176b3dfccd6 55 index_buf = 0;
samux 0:5176b3dfccd6 56 available = false;
samux 0:5176b3dfccd6 57 }
samux 0:5176b3dfccd6 58 } else {
samux 0:5176b3dfccd6 59 speaker_value = p_val;
samux 0:5176b3dfccd6 60 }
samux 0:5176b3dfccd6 61
samux 0:5176b3dfccd6 62 p_val = speaker_value;
samux 0:5176b3dfccd6 63
samux 0:5176b3dfccd6 64 // send value to the speaker
samux 0:5176b3dfccd6 65 speaker.write_u16((uint16_t)speaker_value);
samux 0:5176b3dfccd6 66 }
samux 0:5176b3dfccd6 67
samux 0:5176b3dfccd6 68 int main() {
samux 0:5176b3dfccd6 69
samux 0:5176b3dfccd6 70 // attach a function executed each 1/FREQ s
samux 0:5176b3dfccd6 71 tic.attach_us(tic_handler, 1000000.0/(float)FREQ);
samux 0:5176b3dfccd6 72
samux 0:5176b3dfccd6 73 while (1) {
samux 0:5176b3dfccd6 74 // read an audio packet
samux 1:0335b5c18618 75 audio.read((uint8_t *)buf);
samux 0:5176b3dfccd6 76 available = true;
samux 0:5176b3dfccd6 77 }
samux 0:5176b3dfccd6 78 }