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 // 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 // Convert wave file from 8bit mono to 16 bit stereo
ksekimoto 0:40a09c55e5be 18 //
ksekimoto 0:40a09c55e5be 19 ////////////////////////////////////////////////////////////////////////////
ksekimoto 0:40a09c55e5be 20
ksekimoto 0:40a09c55e5be 21 #include "mbed.h"
ksekimoto 0:40a09c55e5be 22
ksekimoto 0:40a09c55e5be 23 #define HEADER_SIZE 0x2e
ksekimoto 0:40a09c55e5be 24 #define BUF_SIZE 1024
ksekimoto 0:40a09c55e5be 25 #define TO_BUF_SIZE (FROM_BUF_SIZE * 2)
ksekimoto 0:40a09c55e5be 26 #define MUL 20
ksekimoto 0:40a09c55e5be 27
ksekimoto 0:40a09c55e5be 28 unsigned char fromHeader[HEADER_SIZE];
ksekimoto 0:40a09c55e5be 29 unsigned char toHeader[HEADER_SIZE];
ksekimoto 0:40a09c55e5be 30
ksekimoto 0:40a09c55e5be 31 unsigned char fromBuf[BUF_SIZE];
ksekimoto 0:40a09c55e5be 32 short toBuf[BUF_SIZE*2];
ksekimoto 0:40a09c55e5be 33
ksekimoto 0:40a09c55e5be 34 // 0x25c8 + 0x2e = 0x25f6
ksekimoto 0:40a09c55e5be 35 void wavecnv(char *fromfn, char *tofn)
ksekimoto 0:40a09c55e5be 36 {
ksekimoto 0:40a09c55e5be 37 FILE *fromfp;
ksekimoto 0:40a09c55e5be 38 FILE *tofp;
ksekimoto 0:40a09c55e5be 39 int fromSize;
ksekimoto 0:40a09c55e5be 40 int toSize;
ksekimoto 0:40a09c55e5be 41 unsigned int size;
ksekimoto 0:40a09c55e5be 42 int i;
ksekimoto 0:40a09c55e5be 43 unsigned int fromChunkSize;
ksekimoto 0:40a09c55e5be 44 unsigned int toChunkSize;
ksekimoto 0:40a09c55e5be 45 unsigned int sampleRate;
ksekimoto 0:40a09c55e5be 46 unsigned short uval;
ksekimoto 0:40a09c55e5be 47 short sval;
ksekimoto 0:40a09c55e5be 48 if ((fromfp = fopen(fromfn, "rb")) == NULL)
ksekimoto 0:40a09c55e5be 49 return;
ksekimoto 0:40a09c55e5be 50 if ((tofp = fopen(tofn, "wb")) == NULL)
ksekimoto 0:40a09c55e5be 51 return;
ksekimoto 0:40a09c55e5be 52 fromSize = fread(fromHeader, 1, HEADER_SIZE, fromfp);
ksekimoto 0:40a09c55e5be 53 memcpy(toHeader, fromHeader, HEADER_SIZE);
ksekimoto 0:40a09c55e5be 54 sampleRate = (unsigned int)fromHeader[0x18] +
ksekimoto 0:40a09c55e5be 55 ((unsigned int)fromHeader[0x19] << 8) +
ksekimoto 0:40a09c55e5be 56 ((unsigned int)fromHeader[0x1a] << 16) +
ksekimoto 0:40a09c55e5be 57 ((unsigned int)fromHeader[0x1b] << 24) ;
ksekimoto 0:40a09c55e5be 58 sampleRate *= 4;
ksekimoto 0:40a09c55e5be 59 toHeader[0x1c] = (unsigned char)(sampleRate & 0xff);
ksekimoto 0:40a09c55e5be 60 toHeader[0x1d] = (unsigned char)((sampleRate >> 8) & 0xff);
ksekimoto 0:40a09c55e5be 61 toHeader[0x1e] = (unsigned char)((sampleRate >> 16) & 0xff);
ksekimoto 0:40a09c55e5be 62 toHeader[0x1f] = (unsigned char)((sampleRate >> 24) & 0xff);
ksekimoto 0:40a09c55e5be 63 toHeader[0x10] = 0x10;
ksekimoto 0:40a09c55e5be 64 toHeader[0x16] = 0x02;
ksekimoto 0:40a09c55e5be 65 toHeader[0x20] = 0x04;
ksekimoto 0:40a09c55e5be 66 toHeader[0x22] = 0x10;
ksekimoto 0:40a09c55e5be 67 fromChunkSize = (unsigned int)fromHeader[0x2a] +
ksekimoto 0:40a09c55e5be 68 ((unsigned int)fromHeader[0x2b] << 8) +
ksekimoto 0:40a09c55e5be 69 ((unsigned int)fromHeader[0x2c] << 16) +
ksekimoto 0:40a09c55e5be 70 ((unsigned int)fromHeader[0x2d] << 24) ;
ksekimoto 0:40a09c55e5be 71 toChunkSize = fromChunkSize * 4;
ksekimoto 0:40a09c55e5be 72 toHeader[0x24] = 'd';
ksekimoto 0:40a09c55e5be 73 toHeader[0x25] = 'a';
ksekimoto 0:40a09c55e5be 74 toHeader[0x26] = 't';
ksekimoto 0:40a09c55e5be 75 toHeader[0x27] = 'a';
ksekimoto 0:40a09c55e5be 76 toHeader[0x28] = (unsigned char)(toChunkSize & 0xff);
ksekimoto 0:40a09c55e5be 77 toHeader[0x29] = (unsigned char)((toChunkSize >> 8) & 0xff);
ksekimoto 0:40a09c55e5be 78 toHeader[0x2a] = (unsigned char)((toChunkSize >> 16) & 0xff);
ksekimoto 0:40a09c55e5be 79 toHeader[0x2b] = (unsigned char)((toChunkSize >> 24) & 0xff);
ksekimoto 0:40a09c55e5be 80 size = toChunkSize + 0x2c - 8;
ksekimoto 0:40a09c55e5be 81 toHeader[0x04] = (unsigned char)(size & 0xff);
ksekimoto 0:40a09c55e5be 82 toHeader[0x05] = (unsigned char)((size >> 8) & 0xff);
ksekimoto 0:40a09c55e5be 83 toHeader[0x06] = (unsigned char)((size >> 16) & 0xff);
ksekimoto 0:40a09c55e5be 84 toHeader[0x07] = (unsigned char)((size >> 24) & 0xff);
ksekimoto 0:40a09c55e5be 85 toSize = fwrite(toHeader, 1, HEADER_SIZE, tofp);
ksekimoto 0:40a09c55e5be 86 while (fromChunkSize > 0) {
ksekimoto 0:40a09c55e5be 87 if (fromChunkSize > BUF_SIZE)
ksekimoto 0:40a09c55e5be 88 size = BUF_SIZE;
ksekimoto 0:40a09c55e5be 89 else
ksekimoto 0:40a09c55e5be 90 size = fromChunkSize;
ksekimoto 0:40a09c55e5be 91 fromSize = fread(fromBuf, 1, size, fromfp);
ksekimoto 0:40a09c55e5be 92 for (i = 0; i < size; i++) {
ksekimoto 0:40a09c55e5be 93 if (fromBuf[i] == 0x80) {
ksekimoto 0:40a09c55e5be 94 toBuf[i*2] = 0;
ksekimoto 0:40a09c55e5be 95 toBuf[i*2+1] = 0;
ksekimoto 0:40a09c55e5be 96 } else {
ksekimoto 0:40a09c55e5be 97 uval = (unsigned short)fromBuf[i];
ksekimoto 0:40a09c55e5be 98 if (uval > 0x80) {
ksekimoto 0:40a09c55e5be 99 uval = (uval - 0x80) * 256;
ksekimoto 0:40a09c55e5be 100 sval = (short)uval;
ksekimoto 0:40a09c55e5be 101 } else {
ksekimoto 0:40a09c55e5be 102 uval = (0x80 - uval) * 256;
ksekimoto 0:40a09c55e5be 103 sval = ((short)uval) * (-1);
ksekimoto 0:40a09c55e5be 104 }
ksekimoto 0:40a09c55e5be 105 toBuf[i*2] = sval;
ksekimoto 0:40a09c55e5be 106 toBuf[i*2+1] = sval;
ksekimoto 0:40a09c55e5be 107 }
ksekimoto 0:40a09c55e5be 108 }
ksekimoto 0:40a09c55e5be 109 toSize = fwrite(toBuf, 4, size, tofp);
ksekimoto 0:40a09c55e5be 110 fromChunkSize -= size;
ksekimoto 0:40a09c55e5be 111 }
ksekimoto 0:40a09c55e5be 112 fclose(tofp);
ksekimoto 0:40a09c55e5be 113 fclose(fromfp);
ksekimoto 0:40a09c55e5be 114 }