To call Azure Marketplace Translation (and Speech) service

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SpeechSynthesizer.cpp Source File

SpeechSynthesizer.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 //  Based on Text-To-Speech for .NET Micro Framework with Microsoft 
00018 //  Translator Service
00019 //  https://code.msdn.microsoft.com/Text-To-Speech-for-NET-9cea4462
00020 //
00021 ////////////////////////////////////////////////////////////////////////////
00022 
00023 #include "mbed.h"
00024 #include "SpeechSynthesizer.h"
00025 #include "AdmAuthentication.h"
00026 #include "HTTPClient.h"
00027 #include "HTTPFile.h"
00028 #include "USBHostMSD.h"
00029 #include "string.h"
00030 
00031 #define MAX_STREAM  (256*256*16)
00032 #define BASE_URL    "http://api.microsofttranslator.com/v2/Http.svc/"
00033 #define AUTHORIZATION_HEADER    "Authorization"
00034 #define AUDIOFORMAT     "audio/wav"
00035 #define AUDIOQUALITY    "MinSize"       // "MaxQuality"
00036 #define LANGUAGE    "en"
00037 //#define TEST_TOKEN  "Bearer http%3a%2f%2fschemas.xmlsoap.org%2fws%2f2005%2f05%2fidentity%2fclaims%2fnameidentifier=netmf_speech&http%3a%2f%2fschemas.microsoft.com%2faccesscontrolservice%2f2010%2f07%2fclaims%2fidentityprovider=https%3a%2f%2fdatamarket.accesscontrol.windows.net%2f&Audience=http%3a%2f%2fapi.microsofttranslator.com&ExpiresOn=1446517938&Issuer=https%3a%2f%2fdatamarket.accesscontrol.windows.net%2f&HMACSHA256=h%2bgxg4uaVMnlB8PYzFhCn9Q2KGPtE2%2bwZHuseh2nQxY%3d"
00038 
00039 static char url[1024];
00040 static char encoded_text[1024];
00041 static char encoded_format[32];
00042 static char encoded_language[32];
00043 static char buf[MAX_STREAM];
00044 static HTTPText inText(buf, MAX_STREAM);
00045 static char header[1024];
00046 static char encoded_from[256];
00047 static char encoded_to[256];
00048 static char translated[256];
00049 
00050 void urlencode(char *src, char *dst)
00051 {
00052     int c;
00053     char *hex = "0123456789ABCDEF";
00054 
00055     while( (c = *src++) != 0 ){
00056         if( ('a' <= c && c <= 'z')
00057         || ('A' <= c && c <= 'Z')
00058         || ('0' <= c && c <= '9') ){
00059             *dst++ = c;
00060         } else {
00061             *dst++ = '%';
00062             *dst++ = hex[c >> 4];
00063             *dst++ = hex[c & 15];
00064         }
00065     }
00066 }
00067 
00068 void SpeechSynthesizer::UpdateToken()
00069 {
00070     char *ptoken;
00071     printf("UpdateToken...\r\n");
00072     AdmAuthentication auth = AdmAuthentication(id, secret);
00073     ptoken = auth.GetAccessToken();
00074     sprintf(token, "Bearer %s", ptoken);
00075 }
00076 
00077 void SpeechSynthesizer::GetSpeakStream(char *filename, char *text, char *language)
00078 {
00079     printf("GetSpeakStream...\r\n");
00080     HTTPClient hc;
00081     HTTPFile inFile(filename);
00082     urlencode(text, encoded_text);
00083     urlencode(audioFormat, encoded_format);
00084     urlencode(language, encoded_language);
00085     sprintf(url, "%sSpeak?text=%s&language=%s&format=%s&options=%s", 
00086         BASE_URL, encoded_text, encoded_language, encoded_format, audioQuality);
00087 #ifdef DEBUG_SPEECH
00088     printf("url:%s\r\n", url);
00089 #endif
00090 #ifndef TEST_TOKEN
00091     sprintf(header, "%s: %s\r\n", AUTHORIZATION_HEADER, token);
00092 #else
00093     sprintf(header, "%s: %s\r\n", AUTHORIZATION_HEADER, TEST_TOKEN);
00094 #endif
00095 #ifdef DEBUG_SPEECH
00096     printf("header:%s\r\n", header);
00097 #endif
00098     hc.setHeader(header);
00099     hc.get((const char*)url, &inFile, 10000);
00100     inFile.close();
00101 }
00102 
00103 char *SpeechSynthesizer::Translate(char *from, char *to)
00104 {
00105     printf("Translate...\r\n");
00106     HTTPClient hc;
00107     urlencode(from, encoded_from);
00108     urlencode(to, encoded_to);
00109     sprintf(url, "%sTranslate?text=%s&to=%s&contentType=text/plain", 
00110         BASE_URL, encoded_from, encoded_to);
00111 #ifdef DEBUG_SPEECH
00112     printf("url:%s\r\n", url);
00113 #endif
00114 #ifndef TEST_TOKEN
00115     sprintf(header, "%s: %s\r\n", AUTHORIZATION_HEADER, token);
00116 #else
00117     sprintf(header, "%s: %s\r\n", AUTHORIZATION_HEADER, TEST_TOKEN);
00118 #endif
00119     hc.setHeader(header);
00120     hc.get((const char*)url, &inText, 10000);
00121     char *pTokenStart = strstr(buf, ">");
00122     if (pTokenStart == NULL) {
00123         return (char *)NULL;
00124     }
00125     pTokenStart += 1;
00126     char *pTokenEnd = strstr(pTokenStart, "<");
00127     if (pTokenEnd == NULL) {
00128         return (char *)NULL;
00129     }
00130     strncpy(translated, pTokenStart, (pTokenEnd - pTokenStart));
00131     translated[pTokenEnd - pTokenStart] = 0;
00132     printf("%s -> %s\r\n", from, translated);
00133     return (char *)translated;
00134 }
00135     
00136 SpeechSynthesizer::SpeechSynthesizer(const char *clientID, const char *clientSecret)
00137 {
00138     id = (char *)clientID;
00139     secret = (char *)clientSecret; 
00140     language = (char *)LANGUAGE;
00141     audioFormat = (char *)AUDIOFORMAT;
00142     audioQuality = (char *)AUDIOQUALITY;
00143 }
00144 
00145 SpeechSynthesizer::~SpeechSynthesizer()
00146 {
00147 }
00148