ok put into USB device mode attach headset to analog out use control panel to set MBED as default output adjtst speaker volume to get sound play music

Dependencies:   USBDevice mbed

Fork of USBAUDIO_speaker by Samuel Mokrani

Committer:
samux
Date:
Wed Nov 30 16:20:19 2011 +0000
Revision:
5:6afa90233635
Parent:
3:41b10e311100
Child:
6:c44d017f93de

        

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