The sound input from a microphone is output from a speaker. Check device : BSHSAU01BK
Dependencies: USBHostDac USBHost_custom_Addiso
main.cpp
00001 #include "mbed.h" 00002 #include "USBHostDac.h" 00003 00004 #if defined(TARGET_RZ_A1H) 00005 #include "usb_host_setting.h" 00006 #else 00007 #define USB_HOST_CH 0 00008 #endif 00009 00010 #if (USB_HOST_CH == 1) //Audio Shield USB1 00011 DigitalOut usb1en(P3_8); 00012 #endif 00013 DigitalIn button(USER_BUTTON0); 00014 00015 #define AUDIO_READ_BUFF_SIZE (2048) 00016 #define AUDIO_WRITE_BUFF_SIZE (AUDIO_READ_BUFF_SIZE * 2) 00017 00018 #define AUDIO_WRITE_BUFF_NUM (2) 00019 00020 static uint16_t audio_read_buff[AUDIO_READ_BUFF_SIZE/2]; 00021 static uint16_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE/2]; 00022 00023 Semaphore sem(0); 00024 00025 void test_task(void const* arg) { 00026 int buf_index = 0; 00027 USBHostDac * p_usbdac = (USBHostDac *)arg; 00028 00029 while (1) { 00030 sem.wait(); 00031 00032 p_usbdac->send((uint8_t *)audio_write_buff[buf_index], AUDIO_WRITE_BUFF_SIZE, false); 00033 00034 buf_index++; 00035 if (buf_index >= AUDIO_WRITE_BUFF_NUM) { 00036 buf_index = 0; 00037 } 00038 } 00039 } 00040 00041 int main() { 00042 int buf_index = 0; 00043 00044 #if (USB_HOST_CH == 1) //Audio Shield USB1 00045 //Audio Shield USB1 enable 00046 usb1en = 1; //Outputs high level 00047 Thread::wait(5); 00048 usb1en = 0; //Outputs low level 00049 #endif 00050 00051 USBHostDac usbdac; 00052 Thread TestTask(test_task, &usbdac, osPriorityNormal, 1024 * 8); 00053 00054 while(1) { 00055 // try to connect a usbdac device 00056 while(!usbdac.connect()) { 00057 Thread::wait(500); 00058 } 00059 00060 while (1) { 00061 // if device disconnected, try to connect again 00062 if (!usbdac.connected()) { 00063 break; 00064 } 00065 int ret = usbdac.receive((uint8_t *)audio_read_buff, AUDIO_READ_BUFF_SIZE); 00066 if (ret > 0) { 00067 //1ch -> 2ch 00068 uint16_t * p_write = audio_write_buff[buf_index]; 00069 uint16_t * p_read = audio_read_buff; 00070 00071 for (int cnt = 0; cnt < (ret / 2); cnt++) { 00072 *p_write = *p_read; 00073 p_write++; 00074 *p_write = *p_read; 00075 p_write++; 00076 p_read++; 00077 } 00078 00079 sem.release(); 00080 00081 buf_index++; 00082 if (buf_index >= AUDIO_WRITE_BUFF_NUM) { 00083 buf_index = 0; 00084 } 00085 } 00086 } 00087 } 00088 }
Generated on Fri Jul 15 2022 20:53:42 by 1.7.2