To call Azure Marketplace Translation (and Speech) service
Dependencies: EthernetInterface-FRDM HTTPClient-SSL R_BSP SDFileSystem TLV320_RBSP USBHost mbed-rtos mbed-src
main.cpp
00001 //////////////////////////////////////////////////////////////////////////// 00002 // Licensed under the Apache License, Version 2.0 (the "License"); 00003 // you may not use this file except in compliance with the License. 00004 // You may obtain a copy of the License at 00005 // 00006 // http://www.apache.org/licenses/LICENSE-2.0 00007 // 00008 // Unless required by applicable law or agreed to in writing, software 00009 // distributed under the License is distributed on an "AS IS" BASIS, 00010 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00011 // See the License for the specific language governing permissions and 00012 // limitations under the License. 00013 // 00014 // Copyright (c) Microsoft Corporation. All rights reserved. 00015 // Portions Copyright (c) Kentaro Sekimoto All rights reserved. 00016 // 00017 //////////////////////////////////////////////////////////////////////////// 00018 00019 #include "mbed.h" 00020 #include "EthernetInterface.h" 00021 #include "HTTPClient.h" 00022 #include "getline.h" 00023 #include "SpeechSynthesizer.h" 00024 #include "TLV320_RBSP.h" 00025 #include "dec_wav.h" 00026 #include "SDFileSystem.h" 00027 00028 //#define SPEECH_TEXT "hello" 00029 #define SPEECH_TEXT "I will explain Cloud Speech Application." 00030 00031 void wavecnv(char *fromfn, char *tofn); 00032 SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd"); 00033 00034 namespace { 00035 const char *sd_file_path = "/sd/out.txt"; 00036 const int DATA_SIZE = 256; 00037 } 00038 00039 #ifdef USBFS 00040 #include "USBHostMSD.h" 00041 #if defined(TARGET_RZ_A1H) 00042 #include "usb_host_setting.h" 00043 #else 00044 #define USB_HOST_CH 0 00045 #endif 00046 #if (USB_HOST_CH == 1) //Audio Shield USB1 00047 DigitalOut usb1en(P3_8); 00048 #endif 00049 #endif 00050 00051 #ifdef ROMRAMFS 00052 #include "RomRamFileSystem.h" 00053 #endif 00054 00055 #define AUDIO_WRITE_BUFF_SIZE (4096) 00056 #define AUDIO_WRITE_BUFF_NUM (9) 00057 #define FILE_NAME_LEN (64) 00058 #define TEXT_SIZE (64 + 1) //null-terminated 00059 00060 #define CLIENT_ID "xxxxxx" // need to register Azure 00061 #define CLIENT_SECRET "yyyyyyyyyyyyyyyyyyyyyyyyyyyy" // need to register Azure 00062 #ifdef ROMRAMFS 00063 #define WAVE_MEM_FN "/romram/speech.wav" 00064 #endif 00065 #ifdef USBFS 00066 #define WAVE_USB_FN "/usb/speech.wav" 00067 #endif 00068 #define WAVE_SD_FN "/sd/speech8.wav" 00069 #define WAVE_SD_FN1 "/sd/speech16.wav" 00070 00071 EthernetInterface eth; 00072 #ifdef USBFS 00073 USBHostMSD msd("usb"); 00074 #endif 00075 #ifdef ROMRAMFS 00076 RomRamFileSystem romramfs("romram"); 00077 #endif 00078 HTTPClient http; 00079 //char recvBuff[1024*20]; 00080 00081 #ifdef ROMRAMFS 00082 static void mount_romramfs(void) { 00083 FILE * fp; 00084 romramfs.format(); 00085 } 00086 #endif 00087 00088 static void callback_audio_write_end(void * p_data, int32_t result, void * p_app_data) 00089 { 00090 if (result < 0) { 00091 //printf("audio write callback error %d\n", result); 00092 } 00093 } 00094 00095 void wav_play(TLV320_RBSP *audio, char *filename) 00096 { 00097 //4 bytes aligned! No cache memory 00098 static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE] __attribute((section("NC_BSS"),aligned(4))); 00099 static uint8_t title_buf[TEXT_SIZE]; 00100 static uint8_t artist_buf[TEXT_SIZE]; 00101 static uint8_t album_buf[TEXT_SIZE]; 00102 00103 dec_wav wav_file; 00104 size_t audio_data_size; 00105 rbsp_data_conf_t audio_write_async_ctl = {&callback_audio_write_end, NULL}; 00106 00107 FILE *fp = fopen(filename, "r"); 00108 if (fp == NULL) { 00109 printf("Can't open %s\r\n", filename); 00110 return; 00111 } 00112 if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf, TEXT_SIZE, fp) == false) { 00113 printf("WAV format not supported. (%s)\r\n", filename); 00114 printf("Error File :%s\r\n", filename); 00115 printf("Audio Info :%dch, %dbit, %dHz\r\n", 00116 wav_file.GetChannel(), wav_file.GetBlockSize(), wav_file.GetSamplingRate()); 00117 fclose(fp); 00118 } else if ((wav_file.GetChannel() != 2) 00119 || (audio->format(wav_file.GetBlockSize()) == false) 00120 || (audio->frequency(wav_file.GetSamplingRate()) == false)) { 00121 printf("WAV format not supported. (%s)\r\n", filename); 00122 printf("Audio Info :%dch, %dbit, %dHz\r\n", 00123 wav_file.GetChannel(), wav_file.GetBlockSize(), wav_file.GetSamplingRate()); 00124 fclose(fp); 00125 } else { 00126 printf("File :%s\r\n", filename); 00127 printf("Audio Info :%dch, %dbit, %dHz\r\n", 00128 wav_file.GetChannel(), wav_file.GetBlockSize(), wav_file.GetSamplingRate()); 00129 //printf("Title :%s\r\n", title_buf); 00130 //printf("Artist :%s\r\n", artist_buf); 00131 //printf("Album :%s\r\n", album_buf); 00132 00133 int buff_index = 0; 00134 while (1) { 00135 uint8_t *p_buf = audio_write_buff[buff_index]; 00136 audio_data_size = wav_file.GetNextData(p_buf, AUDIO_WRITE_BUFF_SIZE); 00137 if (audio_data_size > 0) { 00138 audio->write(p_buf, audio_data_size, &audio_write_async_ctl); 00139 buff_index++; 00140 } 00141 if (buff_index >= AUDIO_WRITE_BUFF_NUM) { 00142 buff_index = 0; 00143 } 00144 if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE)) { 00145 break; 00146 } 00147 } 00148 fclose(fp); 00149 } 00150 } 00151 00152 void net_main(void const *av) 00153 { 00154 int ret ; 00155 char key[100] ; 00156 char *translated; 00157 00158 eth.init(); //Use DHCP 00159 printf("HTTP Client, Starting,...\r\n") ; 00160 while(1) { 00161 if(eth.connect() == 0)break ; 00162 printf("Retry\n") ; 00163 } 00164 SpeechSynthesizer speech = SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET); 00165 speech.UpdateToken(); 00166 translated = speech.Translate(SPEECH_TEXT, "ja"); 00167 speech.GetSpeakStream(WAVE_SD_FN, translated, "ja"); 00168 getline("Hit any key to start!\r\n", key, sizeof(key)); 00169 wavecnv(WAVE_SD_FN, WAVE_SD_FN1); 00170 00171 TLV320_RBSP audio(P10_13, I2C_SDA, I2C_SCL, P4_4, P4_5, P4_7, P4_6, 0x80, (AUDIO_WRITE_BUFF_NUM - 1), 0); 00172 audio.power(0x02); // mic off 00173 audio.inputVolume(0.7, 0.7); 00174 while(1) { 00175 char key[10]; 00176 wav_play(&audio, WAVE_SD_FN1); 00177 getline("Hit any key to start!\r\n", key, sizeof(key)); 00178 } 00179 } 00180 00181 main() 00182 { 00183 #ifdef USBFS 00184 #if (USB_HOST_CH == 1) //Audio Shield USB1 00185 //Audio Shield USB1 enable 00186 usb1en = 1; //Outputs high level 00187 Thread::wait(5); 00188 usb1en = 0; //Outputs low level 00189 #endif 00190 USBHostMSD msd("usb"); 00191 #endif 00192 00193 #ifdef ROMRAMFS 00194 mount_romramfs(); 00195 #endif 00196 #define STACK_SIZE 24000 00197 Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE); 00198 while (true) { 00199 Thread::wait(1000); 00200 } 00201 }
Generated on Fri Jul 15 2022 04:07:23 by 1.7.2