To call Azure Marketplace Translation (and Speech) service

Dependencies:   EthernetInterface-FRDM HTTPClient-SSL R_BSP SDFileSystem TLV320_RBSP USBHost mbed-rtos mbed-src

The program calls Azure Marketplace Translation (and Speech) service.

How to use - Create an account for Microsoft Azure Marketplace - Get client ID and client secret. - Change client ID and client secret in main.cpp

The program was created based on the following libraries.

CodePlex Text-To-Speech with Microsoft Translator Service http://translatorservice.codeplex.com/

MBED HTTPClient-SSL Library https://developer.mbed.org/teams/MultiTech/code/HTTPClient-SSL/

/media/uploads/ksekimoto/gr-peach_cloud_speech_01.jpg

Committer:
ksekimoto
Date:
Sat Nov 07 12:29:06 2015 +0000
Revision:
1:a2bd45c3b373
Parent:
0:40a09c55e5be
To call Azure Marketplace Translation (and Speech) service.; The first version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ksekimoto 0:40a09c55e5be 1 /**************************************************************************//**
ksekimoto 0:40a09c55e5be 2 * @file dec_wav.h
ksekimoto 0:40a09c55e5be 3 * @brief wav
ksekimoto 0:40a09c55e5be 4 ******************************************************************************/
ksekimoto 0:40a09c55e5be 5 #include "mbed.h"
ksekimoto 0:40a09c55e5be 6
ksekimoto 0:40a09c55e5be 7 /** A class to communicate a dec_wav
ksekimoto 0:40a09c55e5be 8 *
ksekimoto 0:40a09c55e5be 9 */
ksekimoto 0:40a09c55e5be 10 class dec_wav {
ksekimoto 0:40a09c55e5be 11 public:
ksekimoto 0:40a09c55e5be 12
ksekimoto 0:40a09c55e5be 13 /** analyze header
ksekimoto 0:40a09c55e5be 14 *
ksekimoto 0:40a09c55e5be 15 * @param p_title title tag buffer
ksekimoto 0:40a09c55e5be 16 * @param p_artist artist tag buffer
ksekimoto 0:40a09c55e5be 17 * @param p_album album tag buffer
ksekimoto 0:40a09c55e5be 18 * @param tag_size tag buffer size
ksekimoto 0:40a09c55e5be 19 * @param fp file pointer
ksekimoto 0:40a09c55e5be 20 * @return true = success, false = failure
ksekimoto 0:40a09c55e5be 21 */
ksekimoto 0:40a09c55e5be 22 bool AnalyzeHeder(uint8_t * p_title, uint8_t * p_artist, uint8_t * p_album, uint16_t tag_size, FILE * fp) {
ksekimoto 0:40a09c55e5be 23 bool result = false;
ksekimoto 0:40a09c55e5be 24 size_t read_size;
ksekimoto 0:40a09c55e5be 25 uint8_t wk_read_buff[36];
ksekimoto 0:40a09c55e5be 26 uint8_t *data;
ksekimoto 0:40a09c55e5be 27 uint32_t chunk_size;
ksekimoto 0:40a09c55e5be 28 uint32_t sub_chunk_size;
ksekimoto 0:40a09c55e5be 29 uint32_t list_index_max;
ksekimoto 0:40a09c55e5be 30 bool list_ok = false;
ksekimoto 0:40a09c55e5be 31 uint32_t read_index = 0;
ksekimoto 0:40a09c55e5be 32 uint32_t data_index = 0;
ksekimoto 0:40a09c55e5be 33 uint16_t wk_len;
ksekimoto 0:40a09c55e5be 34
ksekimoto 0:40a09c55e5be 35 if (fp == NULL) {
ksekimoto 0:40a09c55e5be 36 return false;
ksekimoto 0:40a09c55e5be 37 }
ksekimoto 0:40a09c55e5be 38 music_data_size = 0;
ksekimoto 0:40a09c55e5be 39 music_data_index = 0;
ksekimoto 0:40a09c55e5be 40 wav_fp = fp;
ksekimoto 0:40a09c55e5be 41 if (p_title != NULL) {
ksekimoto 0:40a09c55e5be 42 p_title[0] = '\0';
ksekimoto 0:40a09c55e5be 43 }
ksekimoto 0:40a09c55e5be 44 if (p_artist != NULL) {
ksekimoto 0:40a09c55e5be 45 p_artist[0] = '\0';
ksekimoto 0:40a09c55e5be 46 }
ksekimoto 0:40a09c55e5be 47 if (p_album != NULL) {
ksekimoto 0:40a09c55e5be 48 p_album[0] = '\0';
ksekimoto 0:40a09c55e5be 49 }
ksekimoto 0:40a09c55e5be 50
ksekimoto 0:40a09c55e5be 51 read_size = fread(&wk_read_buff[0], sizeof(char), 36, wav_fp);
ksekimoto 0:40a09c55e5be 52 if (read_size < 36) {
ksekimoto 0:40a09c55e5be 53 // do nothing
ksekimoto 0:40a09c55e5be 54 } else if (memcmp(&wk_read_buff[0], "RIFF", 4) != 0) {
ksekimoto 0:40a09c55e5be 55 // do nothing
ksekimoto 0:40a09c55e5be 56 } else if (memcmp(&wk_read_buff[8], "WAVE", 4) != 0) {
ksekimoto 0:40a09c55e5be 57 // do nothing
ksekimoto 0:40a09c55e5be 58 } else if (memcmp(&wk_read_buff[12], "fmt ", 4) != 0) {
ksekimoto 0:40a09c55e5be 59 // do nothing
ksekimoto 0:40a09c55e5be 60 } else {
ksekimoto 0:40a09c55e5be 61 read_index += 36;
ksekimoto 0:40a09c55e5be 62 channel = ((uint32_t)wk_read_buff[22] << 0) + ((uint32_t)wk_read_buff[23] << 8);
ksekimoto 0:40a09c55e5be 63 sampling_rate = ((uint32_t)wk_read_buff[24] << 0)
ksekimoto 0:40a09c55e5be 64 + ((uint32_t)wk_read_buff[25] << 8)
ksekimoto 0:40a09c55e5be 65 + ((uint32_t)wk_read_buff[26] << 16)
ksekimoto 0:40a09c55e5be 66 + ((uint32_t)wk_read_buff[27] << 24);
ksekimoto 0:40a09c55e5be 67 block_size = ((uint32_t)wk_read_buff[34] << 0) + ((uint32_t)wk_read_buff[35] << 8);
ksekimoto 0:40a09c55e5be 68 while (1) {
ksekimoto 0:40a09c55e5be 69 //printf("dev_wav1\r\n");
ksekimoto 0:40a09c55e5be 70 read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp);
ksekimoto 0:40a09c55e5be 71 read_index += 8;
ksekimoto 0:40a09c55e5be 72 if (read_size < 8) {
ksekimoto 0:40a09c55e5be 73 break;
ksekimoto 0:40a09c55e5be 74 } else {
ksekimoto 0:40a09c55e5be 75 chunk_size = ((uint32_t)wk_read_buff[4] << 0)
ksekimoto 0:40a09c55e5be 76 + ((uint32_t)wk_read_buff[5] << 8)
ksekimoto 0:40a09c55e5be 77 + ((uint32_t)wk_read_buff[6] << 16)
ksekimoto 0:40a09c55e5be 78 + ((uint32_t)wk_read_buff[7] << 24);
ksekimoto 0:40a09c55e5be 79 if (memcmp(&wk_read_buff[0], "data", 4) == 0) {
ksekimoto 0:40a09c55e5be 80 result = true;
ksekimoto 0:40a09c55e5be 81 music_data_size = chunk_size;
ksekimoto 0:40a09c55e5be 82 if (list_ok == true) {
ksekimoto 0:40a09c55e5be 83 break;
ksekimoto 0:40a09c55e5be 84 } else {
ksekimoto 0:40a09c55e5be 85 data_index = read_index;
ksekimoto 0:40a09c55e5be 86 fseek(wav_fp, chunk_size, SEEK_CUR);
ksekimoto 0:40a09c55e5be 87 read_index += chunk_size;
ksekimoto 0:40a09c55e5be 88 }
ksekimoto 0:40a09c55e5be 89 //printf("dev_wav2\r\n");
ksekimoto 0:40a09c55e5be 90 } else if (memcmp(&wk_read_buff[2], "data", 4) == 0) {
ksekimoto 0:40a09c55e5be 91 read_index += 2;
ksekimoto 0:40a09c55e5be 92 chunk_size = ((uint32_t)wk_read_buff[6] << 0)
ksekimoto 0:40a09c55e5be 93 + ((uint32_t)wk_read_buff[7] << 8);
ksekimoto 0:40a09c55e5be 94 result = true;
ksekimoto 0:40a09c55e5be 95 music_data_size = chunk_size;
ksekimoto 0:40a09c55e5be 96 if (list_ok == true) {
ksekimoto 0:40a09c55e5be 97 break;
ksekimoto 0:40a09c55e5be 98 } else {
ksekimoto 0:40a09c55e5be 99 data_index = read_index;
ksekimoto 0:40a09c55e5be 100 fseek(wav_fp, chunk_size, SEEK_CUR);
ksekimoto 0:40a09c55e5be 101 read_index += chunk_size;
ksekimoto 0:40a09c55e5be 102 }
ksekimoto 0:40a09c55e5be 103 } else if (memcmp(&wk_read_buff[0], "LIST", 4) == 0) {
ksekimoto 0:40a09c55e5be 104 list_ok = true;
ksekimoto 0:40a09c55e5be 105 list_index_max = read_index + chunk_size;
ksekimoto 0:40a09c55e5be 106 read_size = fread(&wk_read_buff[0], sizeof(char), 4, wav_fp);
ksekimoto 0:40a09c55e5be 107 read_index += 4;
ksekimoto 0:40a09c55e5be 108 while (read_index < list_index_max) {
ksekimoto 0:40a09c55e5be 109 read_size = fread(&wk_read_buff[0], sizeof(char), 8, wav_fp);
ksekimoto 0:40a09c55e5be 110 read_index += 8;
ksekimoto 0:40a09c55e5be 111 if (read_size < 8) {
ksekimoto 0:40a09c55e5be 112 break;
ksekimoto 0:40a09c55e5be 113 } else if (memcmp(&wk_read_buff[0], "INAM", 4) == 0) {
ksekimoto 0:40a09c55e5be 114 data = p_title;
ksekimoto 0:40a09c55e5be 115 } else if (memcmp(&wk_read_buff[0], "IART", 4) == 0) {
ksekimoto 0:40a09c55e5be 116 data = p_artist;
ksekimoto 0:40a09c55e5be 117 } else if (memcmp(&wk_read_buff[0], "IPRD", 4) == 0) {
ksekimoto 0:40a09c55e5be 118 data = p_album;
ksekimoto 0:40a09c55e5be 119 } else {
ksekimoto 0:40a09c55e5be 120 data = NULL;
ksekimoto 0:40a09c55e5be 121 }
ksekimoto 0:40a09c55e5be 122 if ((data != NULL) && (tag_size != 0)) {
ksekimoto 0:40a09c55e5be 123 sub_chunk_size = ((uint32_t)wk_read_buff[4] << 0)
ksekimoto 0:40a09c55e5be 124 + ((uint32_t)wk_read_buff[5] << 8)
ksekimoto 0:40a09c55e5be 125 + ((uint32_t)wk_read_buff[6] << 16)
ksekimoto 0:40a09c55e5be 126 + ((uint32_t)wk_read_buff[7] << 24);
ksekimoto 0:40a09c55e5be 127 if (sub_chunk_size > (tag_size - 1)) {
ksekimoto 0:40a09c55e5be 128 wk_len = (tag_size - 1);
ksekimoto 0:40a09c55e5be 129 } else {
ksekimoto 0:40a09c55e5be 130 wk_len = sub_chunk_size;
ksekimoto 0:40a09c55e5be 131 }
ksekimoto 0:40a09c55e5be 132 read_size = fread(data, sizeof(char), wk_len, wav_fp);
ksekimoto 0:40a09c55e5be 133 read_index += sub_chunk_size;
ksekimoto 0:40a09c55e5be 134 fseek(wav_fp, read_index, SEEK_SET);
ksekimoto 0:40a09c55e5be 135 data[wk_len] = '\0';
ksekimoto 0:40a09c55e5be 136 }
ksekimoto 0:40a09c55e5be 137 }
ksekimoto 0:40a09c55e5be 138 if (data_index != 0) {
ksekimoto 0:40a09c55e5be 139 break;
ksekimoto 0:40a09c55e5be 140 } else {
ksekimoto 0:40a09c55e5be 141 fseek(wav_fp, list_index_max, SEEK_SET);
ksekimoto 0:40a09c55e5be 142 }
ksekimoto 0:40a09c55e5be 143 //printf("dev_wav3\r\n");
ksekimoto 0:40a09c55e5be 144 } else {
ksekimoto 0:40a09c55e5be 145 fseek(wav_fp, chunk_size, SEEK_CUR);
ksekimoto 0:40a09c55e5be 146 read_index += chunk_size;
ksekimoto 0:40a09c55e5be 147 //printf("dev_wav4\r\n");
ksekimoto 0:40a09c55e5be 148 }
ksekimoto 0:40a09c55e5be 149 }
ksekimoto 0:40a09c55e5be 150 }
ksekimoto 0:40a09c55e5be 151
ksekimoto 0:40a09c55e5be 152 if (data_index != 0) {
ksekimoto 0:40a09c55e5be 153 fseek(wav_fp, data_index, SEEK_SET);
ksekimoto 0:40a09c55e5be 154 //printf("dev_wav5\r\n");
ksekimoto 0:40a09c55e5be 155 }
ksekimoto 0:40a09c55e5be 156 }
ksekimoto 0:40a09c55e5be 157
ksekimoto 0:40a09c55e5be 158 return result;
ksekimoto 0:40a09c55e5be 159 };
ksekimoto 0:40a09c55e5be 160
ksekimoto 0:40a09c55e5be 161 /** get next data
ksekimoto 0:40a09c55e5be 162 *
ksekimoto 0:40a09c55e5be 163 * @param buf data buffer address
ksekimoto 0:40a09c55e5be 164 * @param len data buffer length
ksekimoto 0:40a09c55e5be 165 * @return get data size
ksekimoto 0:40a09c55e5be 166 */
ksekimoto 0:40a09c55e5be 167 size_t GetNextData(void *buf, size_t len) {
ksekimoto 0:40a09c55e5be 168 if (block_size == 24) {
ksekimoto 0:40a09c55e5be 169 // Add padding
ksekimoto 0:40a09c55e5be 170 int write_index = 0;
ksekimoto 0:40a09c55e5be 171 int wavfile_index;
ksekimoto 0:40a09c55e5be 172 int read_len;
ksekimoto 0:40a09c55e5be 173 int pading_index = 0;
ksekimoto 0:40a09c55e5be 174 uint8_t * p_buf = (uint8_t *)buf;
ksekimoto 0:40a09c55e5be 175 size_t ret;
ksekimoto 0:40a09c55e5be 176
ksekimoto 0:40a09c55e5be 177 if ((music_data_index + len) > music_data_size) {
ksekimoto 0:40a09c55e5be 178 len = music_data_size - music_data_index;
ksekimoto 0:40a09c55e5be 179 }
ksekimoto 0:40a09c55e5be 180 while (write_index < len) {
ksekimoto 0:40a09c55e5be 181 read_len = (len - write_index) * 3 / 4;
ksekimoto 0:40a09c55e5be 182 if (read_len > sizeof(wk_wavfile_buff)) {
ksekimoto 0:40a09c55e5be 183 read_len = sizeof(wk_wavfile_buff);
ksekimoto 0:40a09c55e5be 184 }
ksekimoto 0:40a09c55e5be 185 music_data_index += read_len;
ksekimoto 0:40a09c55e5be 186 ret = fread(wk_wavfile_buff, sizeof(char), read_len, wav_fp);
ksekimoto 0:40a09c55e5be 187 if (ret < read_len) {
ksekimoto 0:40a09c55e5be 188 break;
ksekimoto 0:40a09c55e5be 189 }
ksekimoto 0:40a09c55e5be 190 wavfile_index = 0;
ksekimoto 0:40a09c55e5be 191 while ((write_index < len) && (wavfile_index < read_len)) {
ksekimoto 0:40a09c55e5be 192 if (pading_index == 0) {
ksekimoto 0:40a09c55e5be 193 p_buf[write_index] = 0;
ksekimoto 0:40a09c55e5be 194 } else {
ksekimoto 0:40a09c55e5be 195 p_buf[write_index] = wk_wavfile_buff[wavfile_index];
ksekimoto 0:40a09c55e5be 196 wavfile_index++;
ksekimoto 0:40a09c55e5be 197 }
ksekimoto 0:40a09c55e5be 198 if (pading_index < 3) {
ksekimoto 0:40a09c55e5be 199 pading_index++;
ksekimoto 0:40a09c55e5be 200 } else {
ksekimoto 0:40a09c55e5be 201 pading_index = 0;
ksekimoto 0:40a09c55e5be 202 }
ksekimoto 0:40a09c55e5be 203 write_index++;
ksekimoto 0:40a09c55e5be 204 }
ksekimoto 0:40a09c55e5be 205 }
ksekimoto 0:40a09c55e5be 206
ksekimoto 0:40a09c55e5be 207 return write_index;
ksekimoto 0:40a09c55e5be 208 } else {
ksekimoto 0:40a09c55e5be 209 if ((music_data_index + len) > music_data_size) {
ksekimoto 0:40a09c55e5be 210 len = music_data_size - music_data_index;
ksekimoto 0:40a09c55e5be 211 }
ksekimoto 0:40a09c55e5be 212 music_data_index += len;
ksekimoto 0:40a09c55e5be 213
ksekimoto 0:40a09c55e5be 214 return fread(buf, sizeof(char), len, wav_fp);
ksekimoto 0:40a09c55e5be 215 }
ksekimoto 0:40a09c55e5be 216 };
ksekimoto 0:40a09c55e5be 217
ksekimoto 0:40a09c55e5be 218 /** get channel
ksekimoto 0:40a09c55e5be 219 *
ksekimoto 0:40a09c55e5be 220 * @return channel
ksekimoto 0:40a09c55e5be 221 */
ksekimoto 0:40a09c55e5be 222 uint16_t GetChannel() {
ksekimoto 0:40a09c55e5be 223 return channel;
ksekimoto 0:40a09c55e5be 224 };
ksekimoto 0:40a09c55e5be 225
ksekimoto 0:40a09c55e5be 226 /** get block size
ksekimoto 0:40a09c55e5be 227 *
ksekimoto 0:40a09c55e5be 228 * @return block size
ksekimoto 0:40a09c55e5be 229 */
ksekimoto 0:40a09c55e5be 230 uint16_t GetBlockSize() {
ksekimoto 0:40a09c55e5be 231 return block_size;
ksekimoto 0:40a09c55e5be 232 };
ksekimoto 0:40a09c55e5be 233
ksekimoto 0:40a09c55e5be 234 /** get sampling rate
ksekimoto 0:40a09c55e5be 235 *
ksekimoto 0:40a09c55e5be 236 * @return sampling rate
ksekimoto 0:40a09c55e5be 237 */
ksekimoto 0:40a09c55e5be 238 uint32_t GetSamplingRate() {
ksekimoto 0:40a09c55e5be 239 return sampling_rate;
ksekimoto 0:40a09c55e5be 240 };
ksekimoto 0:40a09c55e5be 241
ksekimoto 0:40a09c55e5be 242 private:
ksekimoto 0:40a09c55e5be 243 #define FILE_READ_BUFF_SIZE (3072)
ksekimoto 0:40a09c55e5be 244
ksekimoto 0:40a09c55e5be 245 FILE * wav_fp;
ksekimoto 0:40a09c55e5be 246 uint32_t music_data_size;
ksekimoto 0:40a09c55e5be 247 uint32_t music_data_index;
ksekimoto 0:40a09c55e5be 248 uint16_t channel;
ksekimoto 0:40a09c55e5be 249 uint16_t block_size;
ksekimoto 0:40a09c55e5be 250 uint32_t sampling_rate;
ksekimoto 0:40a09c55e5be 251 uint8_t wk_wavfile_buff[FILE_READ_BUFF_SIZE];
ksekimoto 0:40a09c55e5be 252 };