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
00001 // USBAudio speaker example 00002 00003 #include "mbed.h" 00004 #include "USBAudio.h" 00005 00006 // frequency: 48 kHz 00007 #define FREQ 48000 00008 00009 // 1 channel: mono 00010 #define NB_CHA 1 00011 00012 // 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 00013 #define AUDIO_LENGTH_PACKET 48 * 2 * 1 00014 00015 // USBAudio (we just use audio packets received, we don't send audio packets to the computer in this example) 00016 USBAudio audio(FREQ, NB_CHA, 8000, 1, 0x7180, 0x7500); 00017 00018 // speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker 00019 AnalogOut speaker(p18); 00020 00021 // ticker to send data to the speaker at the good frequency 00022 Ticker tic; 00023 00024 // buffer where will be store one audio packet (LENGTH_AUDIO_PACKET/2 because we are storing int16 and not uint8) 00025 int16_t buf[AUDIO_LENGTH_PACKET/2]; 00026 00027 // show if an audio packet is available 00028 volatile bool available = false; 00029 00030 // index of the value which will be send to the speaker 00031 int index_buf = 0; 00032 00033 // previous value sent to the speaker 00034 uint16_t p_val = 0; 00035 00036 // function executed each 1/FREQ s 00037 void tic_handler() { 00038 float speaker_value; 00039 00040 if (available) { 00041 //convert 2 bytes in float 00042 speaker_value = (float)(buf[index_buf]); 00043 00044 // speaker_value between 0 and 65535 00045 speaker_value += 32768.0; 00046 00047 // adjust according to current volume 00048 speaker_value *= audio.getVolume(); 00049 00050 // as two bytes has been read, we move the index of two bytes 00051 index_buf++; 00052 00053 // if we have read all the buffer, no more data available 00054 if (index_buf == AUDIO_LENGTH_PACKET/2) { 00055 index_buf = 0; 00056 available = false; 00057 } 00058 } else { 00059 speaker_value = p_val; 00060 } 00061 00062 p_val = speaker_value; 00063 00064 // send value to the speaker 00065 speaker.write_u16((uint16_t)speaker_value); 00066 } 00067 00068 int main() { 00069 00070 // attach a function executed each 1/FREQ s 00071 tic.attach_us(tic_handler, 1000000.0/(float)FREQ); 00072 00073 while (1) { 00074 // read an audio packet 00075 audio.read((uint8_t *)buf); 00076 available = true; 00077 } 00078 }
Generated on Wed Jul 13 2022 21:44:43 by 1.7.2