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
Fork of USBAUDIO_speaker by
main.cpp@6:c44d017f93de, 2011-11-30 (annotated)
- Committer:
- samux
- Date:
- Wed Nov 30 16:20:38 2011 +0000
- Revision:
- 6:c44d017f93de
- Parent:
- 5:6afa90233635
- Child:
- 7:ba4f65ce69f2
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:5176b3dfccd6 | 15 | // USBAudio |
samux | 0:5176b3dfccd6 | 16 | USBAudio audio(FREQ, NB_CHA, 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 | } |