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:21:19 2015 +0000
Revision:
0:40a09c55e5be
The frist version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ksekimoto 0:40a09c55e5be 1 ////////////////////////////////////////////////////////////////////////////
ksekimoto 0:40a09c55e5be 2 // Licensed under the Apache License, Version 2.0 (the "License");
ksekimoto 0:40a09c55e5be 3 // you may not use this file except in compliance with the License.
ksekimoto 0:40a09c55e5be 4 // You may obtain a copy of the License at
ksekimoto 0:40a09c55e5be 5 //
ksekimoto 0:40a09c55e5be 6 // http://www.apache.org/licenses/LICENSE-2.0
ksekimoto 0:40a09c55e5be 7 //
ksekimoto 0:40a09c55e5be 8 // Unless required by applicable law or agreed to in writing, software
ksekimoto 0:40a09c55e5be 9 // distributed under the License is distributed on an "AS IS" BASIS,
ksekimoto 0:40a09c55e5be 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ksekimoto 0:40a09c55e5be 11 // See the License for the specific language governing permissions and
ksekimoto 0:40a09c55e5be 12 // limitations under the License.
ksekimoto 0:40a09c55e5be 13 //
ksekimoto 0:40a09c55e5be 14 // Copyright (c) Microsoft Corporation. All rights reserved.
ksekimoto 0:40a09c55e5be 15 // Portions Copyright (c) Kentaro Sekimoto All rights reserved.
ksekimoto 0:40a09c55e5be 16 //
ksekimoto 0:40a09c55e5be 17 ////////////////////////////////////////////////////////////////////////////
ksekimoto 0:40a09c55e5be 18
ksekimoto 0:40a09c55e5be 19 #include "mbed.h"
ksekimoto 0:40a09c55e5be 20 #include "EthernetInterface.h"
ksekimoto 0:40a09c55e5be 21 #include "HTTPClient.h"
ksekimoto 0:40a09c55e5be 22 #include "getline.h"
ksekimoto 0:40a09c55e5be 23 #include "SpeechSynthesizer.h"
ksekimoto 0:40a09c55e5be 24 #include "TLV320_RBSP.h"
ksekimoto 0:40a09c55e5be 25 #include "dec_wav.h"
ksekimoto 0:40a09c55e5be 26 #include "SDFileSystem.h"
ksekimoto 0:40a09c55e5be 27
ksekimoto 0:40a09c55e5be 28 //#define SPEECH_TEXT "hello"
ksekimoto 0:40a09c55e5be 29 #define SPEECH_TEXT "I will explain Cloud Speech Application."
ksekimoto 0:40a09c55e5be 30
ksekimoto 0:40a09c55e5be 31 void wavecnv(char *fromfn, char *tofn);
ksekimoto 0:40a09c55e5be 32 SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
ksekimoto 0:40a09c55e5be 33
ksekimoto 0:40a09c55e5be 34 namespace {
ksekimoto 0:40a09c55e5be 35 const char *sd_file_path = "/sd/out.txt";
ksekimoto 0:40a09c55e5be 36 const int DATA_SIZE = 256;
ksekimoto 0:40a09c55e5be 37 }
ksekimoto 0:40a09c55e5be 38
ksekimoto 0:40a09c55e5be 39 #ifdef USBFS
ksekimoto 0:40a09c55e5be 40 #include "USBHostMSD.h"
ksekimoto 0:40a09c55e5be 41 #if defined(TARGET_RZ_A1H)
ksekimoto 0:40a09c55e5be 42 #include "usb_host_setting.h"
ksekimoto 0:40a09c55e5be 43 #else
ksekimoto 0:40a09c55e5be 44 #define USB_HOST_CH 0
ksekimoto 0:40a09c55e5be 45 #endif
ksekimoto 0:40a09c55e5be 46 #if (USB_HOST_CH == 1) //Audio Shield USB1
ksekimoto 0:40a09c55e5be 47 DigitalOut usb1en(P3_8);
ksekimoto 0:40a09c55e5be 48 #endif
ksekimoto 0:40a09c55e5be 49 #endif
ksekimoto 0:40a09c55e5be 50
ksekimoto 0:40a09c55e5be 51 #ifdef ROMRAMFS
ksekimoto 0:40a09c55e5be 52 #include "RomRamFileSystem.h"
ksekimoto 0:40a09c55e5be 53 #endif
ksekimoto 0:40a09c55e5be 54
ksekimoto 0:40a09c55e5be 55 #define AUDIO_WRITE_BUFF_SIZE (4096)
ksekimoto 0:40a09c55e5be 56 #define AUDIO_WRITE_BUFF_NUM (9)
ksekimoto 0:40a09c55e5be 57 #define FILE_NAME_LEN (64)
ksekimoto 0:40a09c55e5be 58 #define TEXT_SIZE (64 + 1) //null-terminated
ksekimoto 0:40a09c55e5be 59
ksekimoto 0:40a09c55e5be 60 #define CLIENT_ID "xxxxxx" // need to register Azure
ksekimoto 0:40a09c55e5be 61 #define CLIENT_SECRET "yyyyyyyyyyyyyyyyyyyyyyyyyyyy" // need to register Azure
ksekimoto 0:40a09c55e5be 62 #ifdef ROMRAMFS
ksekimoto 0:40a09c55e5be 63 #define WAVE_MEM_FN "/romram/speech.wav"
ksekimoto 0:40a09c55e5be 64 #endif
ksekimoto 0:40a09c55e5be 65 #ifdef USBFS
ksekimoto 0:40a09c55e5be 66 #define WAVE_USB_FN "/usb/speech.wav"
ksekimoto 0:40a09c55e5be 67 #endif
ksekimoto 0:40a09c55e5be 68 #define WAVE_SD_FN "/sd/speech8.wav"
ksekimoto 0:40a09c55e5be 69 #define WAVE_SD_FN1 "/sd/speech16.wav"
ksekimoto 0:40a09c55e5be 70
ksekimoto 0:40a09c55e5be 71 EthernetInterface eth;
ksekimoto 0:40a09c55e5be 72 #ifdef USBFS
ksekimoto 0:40a09c55e5be 73 USBHostMSD msd("usb");
ksekimoto 0:40a09c55e5be 74 #endif
ksekimoto 0:40a09c55e5be 75 #ifdef ROMRAMFS
ksekimoto 0:40a09c55e5be 76 RomRamFileSystem romramfs("romram");
ksekimoto 0:40a09c55e5be 77 #endif
ksekimoto 0:40a09c55e5be 78 HTTPClient http;
ksekimoto 0:40a09c55e5be 79 //char recvBuff[1024*20];
ksekimoto 0:40a09c55e5be 80
ksekimoto 0:40a09c55e5be 81 #ifdef ROMRAMFS
ksekimoto 0:40a09c55e5be 82 static void mount_romramfs(void) {
ksekimoto 0:40a09c55e5be 83 FILE * fp;
ksekimoto 0:40a09c55e5be 84 romramfs.format();
ksekimoto 0:40a09c55e5be 85 }
ksekimoto 0:40a09c55e5be 86 #endif
ksekimoto 0:40a09c55e5be 87
ksekimoto 0:40a09c55e5be 88 static void callback_audio_write_end(void * p_data, int32_t result, void * p_app_data)
ksekimoto 0:40a09c55e5be 89 {
ksekimoto 0:40a09c55e5be 90 if (result < 0) {
ksekimoto 0:40a09c55e5be 91 //printf("audio write callback error %d\n", result);
ksekimoto 0:40a09c55e5be 92 }
ksekimoto 0:40a09c55e5be 93 }
ksekimoto 0:40a09c55e5be 94
ksekimoto 0:40a09c55e5be 95 void wav_play(TLV320_RBSP *audio, char *filename)
ksekimoto 0:40a09c55e5be 96 {
ksekimoto 0:40a09c55e5be 97 //4 bytes aligned! No cache memory
ksekimoto 0:40a09c55e5be 98 static uint8_t audio_write_buff[AUDIO_WRITE_BUFF_NUM][AUDIO_WRITE_BUFF_SIZE] __attribute((section("NC_BSS"),aligned(4)));
ksekimoto 0:40a09c55e5be 99 static uint8_t title_buf[TEXT_SIZE];
ksekimoto 0:40a09c55e5be 100 static uint8_t artist_buf[TEXT_SIZE];
ksekimoto 0:40a09c55e5be 101 static uint8_t album_buf[TEXT_SIZE];
ksekimoto 0:40a09c55e5be 102
ksekimoto 0:40a09c55e5be 103 dec_wav wav_file;
ksekimoto 0:40a09c55e5be 104 size_t audio_data_size;
ksekimoto 0:40a09c55e5be 105 rbsp_data_conf_t audio_write_async_ctl = {&callback_audio_write_end, NULL};
ksekimoto 0:40a09c55e5be 106
ksekimoto 0:40a09c55e5be 107 FILE *fp = fopen(filename, "r");
ksekimoto 0:40a09c55e5be 108 if (fp == NULL) {
ksekimoto 0:40a09c55e5be 109 printf("Can't open %s\r\n", filename);
ksekimoto 0:40a09c55e5be 110 return;
ksekimoto 0:40a09c55e5be 111 }
ksekimoto 0:40a09c55e5be 112 if (wav_file.AnalyzeHeder(title_buf, artist_buf, album_buf, TEXT_SIZE, fp) == false) {
ksekimoto 0:40a09c55e5be 113 printf("WAV format not supported. (%s)\r\n", filename);
ksekimoto 0:40a09c55e5be 114 printf("Error File :%s\r\n", filename);
ksekimoto 0:40a09c55e5be 115 printf("Audio Info :%dch, %dbit, %dHz\r\n",
ksekimoto 0:40a09c55e5be 116 wav_file.GetChannel(), wav_file.GetBlockSize(), wav_file.GetSamplingRate());
ksekimoto 0:40a09c55e5be 117 fclose(fp);
ksekimoto 0:40a09c55e5be 118 } else if ((wav_file.GetChannel() != 2)
ksekimoto 0:40a09c55e5be 119 || (audio->format(wav_file.GetBlockSize()) == false)
ksekimoto 0:40a09c55e5be 120 || (audio->frequency(wav_file.GetSamplingRate()) == false)) {
ksekimoto 0:40a09c55e5be 121 printf("WAV format not supported. (%s)\r\n", filename);
ksekimoto 0:40a09c55e5be 122 printf("Audio Info :%dch, %dbit, %dHz\r\n",
ksekimoto 0:40a09c55e5be 123 wav_file.GetChannel(), wav_file.GetBlockSize(), wav_file.GetSamplingRate());
ksekimoto 0:40a09c55e5be 124 fclose(fp);
ksekimoto 0:40a09c55e5be 125 } else {
ksekimoto 0:40a09c55e5be 126 printf("File :%s\r\n", filename);
ksekimoto 0:40a09c55e5be 127 printf("Audio Info :%dch, %dbit, %dHz\r\n",
ksekimoto 0:40a09c55e5be 128 wav_file.GetChannel(), wav_file.GetBlockSize(), wav_file.GetSamplingRate());
ksekimoto 0:40a09c55e5be 129 //printf("Title :%s\r\n", title_buf);
ksekimoto 0:40a09c55e5be 130 //printf("Artist :%s\r\n", artist_buf);
ksekimoto 0:40a09c55e5be 131 //printf("Album :%s\r\n", album_buf);
ksekimoto 0:40a09c55e5be 132
ksekimoto 0:40a09c55e5be 133 int buff_index = 0;
ksekimoto 0:40a09c55e5be 134 while (1) {
ksekimoto 0:40a09c55e5be 135 uint8_t *p_buf = audio_write_buff[buff_index];
ksekimoto 0:40a09c55e5be 136 audio_data_size = wav_file.GetNextData(p_buf, AUDIO_WRITE_BUFF_SIZE);
ksekimoto 0:40a09c55e5be 137 if (audio_data_size > 0) {
ksekimoto 0:40a09c55e5be 138 audio->write(p_buf, audio_data_size, &audio_write_async_ctl);
ksekimoto 0:40a09c55e5be 139 buff_index++;
ksekimoto 0:40a09c55e5be 140 }
ksekimoto 0:40a09c55e5be 141 if (buff_index >= AUDIO_WRITE_BUFF_NUM) {
ksekimoto 0:40a09c55e5be 142 buff_index = 0;
ksekimoto 0:40a09c55e5be 143 }
ksekimoto 0:40a09c55e5be 144 if ((audio_data_size < AUDIO_WRITE_BUFF_SIZE)) {
ksekimoto 0:40a09c55e5be 145 break;
ksekimoto 0:40a09c55e5be 146 }
ksekimoto 0:40a09c55e5be 147 }
ksekimoto 0:40a09c55e5be 148 fclose(fp);
ksekimoto 0:40a09c55e5be 149 }
ksekimoto 0:40a09c55e5be 150 }
ksekimoto 0:40a09c55e5be 151
ksekimoto 0:40a09c55e5be 152 void net_main(void const *av)
ksekimoto 0:40a09c55e5be 153 {
ksekimoto 0:40a09c55e5be 154 int ret ;
ksekimoto 0:40a09c55e5be 155 char key[100] ;
ksekimoto 0:40a09c55e5be 156 char *translated;
ksekimoto 0:40a09c55e5be 157
ksekimoto 0:40a09c55e5be 158 eth.init(); //Use DHCP
ksekimoto 0:40a09c55e5be 159 printf("HTTP Client, Starting,...\r\n") ;
ksekimoto 0:40a09c55e5be 160 while(1) {
ksekimoto 0:40a09c55e5be 161 if(eth.connect() == 0)break ;
ksekimoto 0:40a09c55e5be 162 printf("Retry\n") ;
ksekimoto 0:40a09c55e5be 163 }
ksekimoto 0:40a09c55e5be 164 SpeechSynthesizer speech = SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET);
ksekimoto 0:40a09c55e5be 165 speech.UpdateToken();
ksekimoto 0:40a09c55e5be 166 translated = speech.Translate(SPEECH_TEXT, "ja");
ksekimoto 0:40a09c55e5be 167 speech.GetSpeakStream(WAVE_SD_FN, translated, "ja");
ksekimoto 0:40a09c55e5be 168 getline("Hit any key to start!\r\n", key, sizeof(key));
ksekimoto 0:40a09c55e5be 169 wavecnv(WAVE_SD_FN, WAVE_SD_FN1);
ksekimoto 0:40a09c55e5be 170
ksekimoto 0:40a09c55e5be 171 TLV320_RBSP audio(P10_13, I2C_SDA, I2C_SCL, P4_4, P4_5, P4_7, P4_6, 0x80, (AUDIO_WRITE_BUFF_NUM - 1), 0);
ksekimoto 0:40a09c55e5be 172 audio.power(0x02); // mic off
ksekimoto 0:40a09c55e5be 173 audio.inputVolume(0.7, 0.7);
ksekimoto 0:40a09c55e5be 174 while(1) {
ksekimoto 0:40a09c55e5be 175 char key[10];
ksekimoto 0:40a09c55e5be 176 wav_play(&audio, WAVE_SD_FN1);
ksekimoto 0:40a09c55e5be 177 getline("Hit any key to start!\r\n", key, sizeof(key));
ksekimoto 0:40a09c55e5be 178 }
ksekimoto 0:40a09c55e5be 179 }
ksekimoto 0:40a09c55e5be 180
ksekimoto 0:40a09c55e5be 181 main()
ksekimoto 0:40a09c55e5be 182 {
ksekimoto 0:40a09c55e5be 183 #ifdef USBFS
ksekimoto 0:40a09c55e5be 184 #if (USB_HOST_CH == 1) //Audio Shield USB1
ksekimoto 0:40a09c55e5be 185 //Audio Shield USB1 enable
ksekimoto 0:40a09c55e5be 186 usb1en = 1; //Outputs high level
ksekimoto 0:40a09c55e5be 187 Thread::wait(5);
ksekimoto 0:40a09c55e5be 188 usb1en = 0; //Outputs low level
ksekimoto 0:40a09c55e5be 189 #endif
ksekimoto 0:40a09c55e5be 190 USBHostMSD msd("usb");
ksekimoto 0:40a09c55e5be 191 #endif
ksekimoto 0:40a09c55e5be 192
ksekimoto 0:40a09c55e5be 193 #ifdef ROMRAMFS
ksekimoto 0:40a09c55e5be 194 mount_romramfs();
ksekimoto 0:40a09c55e5be 195 #endif
ksekimoto 0:40a09c55e5be 196 #define STACK_SIZE 24000
ksekimoto 0:40a09c55e5be 197 Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE);
ksekimoto 0:40a09c55e5be 198 while (true) {
ksekimoto 0:40a09c55e5be 199 Thread::wait(1000);
ksekimoto 0:40a09c55e5be 200 }
ksekimoto 0:40a09c55e5be 201 }