AP mode

Dependencies:   NetworkSocketAPI WizFi310Interface mbed

Fork of WizFi310_TCP_Echo_Server_Example by WIZnet

Committer:
maru536
Date:
Tue Oct 03 03:43:34 2017 +0000
Revision:
7:e5500683781b
Parent:
3:4b4eb90875bd
voice enable

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maru536 2:8d119e9b8f5a 1 #include "tts.h"
maru536 2:8d119e9b8f5a 2
maru536 2:8d119e9b8f5a 3
maru536 2:8d119e9b8f5a 4 void playTTS(VS1053 *player, TCPSocket *socket, char* buf) {
maru536 2:8d119e9b8f5a 5 int content_length;
maru536 2:8d119e9b8f5a 6 char text[200] = "";
maru536 2:8d119e9b8f5a 7 char c;
maru536 2:8d119e9b8f5a 8 socket->connect("api.voicerss.org", 80);
maru536 2:8d119e9b8f5a 9 url_encode(text, buf);
maru536 3:4b4eb90875bd 10 sprintf(buf, "GET /?key=fb82492d22b541ce8983bbea7d0d984b&hl=en-us&src=%s&c=mp3&f=8khz_8bit_mono&r=1 HTTP/1.1\nHost: api.voicerss.org\n\n", text);
maru536 2:8d119e9b8f5a 11 socket->send(buf, MAX_BUF_SIZE);
maru536 2:8d119e9b8f5a 12 extractVoiceData(socket, content_length);
maru536 2:8d119e9b8f5a 13 printf("play start! : %d\n", content_length);
maru536 7:e5500683781b 14 player->sendDataBlock(socket, content_length);
maru536 2:8d119e9b8f5a 15 }
maru536 2:8d119e9b8f5a 16
maru536 2:8d119e9b8f5a 17 void playTTS(TCPSocket *socket, char* buf) {
maru536 2:8d119e9b8f5a 18 int content_length;
maru536 2:8d119e9b8f5a 19 char text[200] = "";
maru536 2:8d119e9b8f5a 20 char c;
maru536 2:8d119e9b8f5a 21 socket->connect("api.voicerss.org", 80);
maru536 2:8d119e9b8f5a 22 url_encode(text, buf);
maru536 3:4b4eb90875bd 23 sprintf(buf, "GET /?key=fb82492d22b541ce8983bbea7d0d984b&hl=en-us&src=%s&c=mp3&f=8khz_8bit_mono&r=1 HTTP/1.1\nHost: api.voicerss.org\n\n", text);
maru536 2:8d119e9b8f5a 24 socket->send(buf, MAX_BUF_SIZE);
maru536 2:8d119e9b8f5a 25 extractVoiceData(socket, content_length);
maru536 2:8d119e9b8f5a 26 printf("play start! : %d\n", content_length);
maru536 2:8d119e9b8f5a 27 int sizeSent = 0;
maru536 2:8d119e9b8f5a 28 while (sizeSent < content_length) {
maru536 2:8d119e9b8f5a 29 if (socket->recv(&c, sizeof(c)) >= 0) {
maru536 2:8d119e9b8f5a 30 //printf("%d: %c\n", sizeSent, c);
maru536 2:8d119e9b8f5a 31 sizeSent++;
maru536 2:8d119e9b8f5a 32 }
maru536 2:8d119e9b8f5a 33 }
maru536 2:8d119e9b8f5a 34 printf("\nplay size: %d\n", sizeSent);
maru536 2:8d119e9b8f5a 35 }
maru536 2:8d119e9b8f5a 36
maru536 2:8d119e9b8f5a 37 void url_encode(char *dst, const char* src ) {
maru536 2:8d119e9b8f5a 38 int i, j = 0, len;
maru536 2:8d119e9b8f5a 39 char* tmp;
maru536 2:8d119e9b8f5a 40
maru536 2:8d119e9b8f5a 41 len = strlen( src );
maru536 2:8d119e9b8f5a 42 tmp = (char*) malloc( (sizeof(char) * 3 * len) +1 );
maru536 2:8d119e9b8f5a 43
maru536 2:8d119e9b8f5a 44 for( i = 0 ; i < len ; i++ ){
maru536 2:8d119e9b8f5a 45 if( IS_ALNUM( src[i] ) ) {
maru536 2:8d119e9b8f5a 46 tmp[j] = src[i];
maru536 2:8d119e9b8f5a 47 }
maru536 2:8d119e9b8f5a 48 else{
maru536 2:8d119e9b8f5a 49 snprintf( &tmp[j], 4, "%%%02X\n", (unsigned char)src[i] );
maru536 2:8d119e9b8f5a 50 j += 2;
maru536 2:8d119e9b8f5a 51 }
maru536 2:8d119e9b8f5a 52 j++;
maru536 2:8d119e9b8f5a 53 }
maru536 2:8d119e9b8f5a 54
maru536 2:8d119e9b8f5a 55 tmp[j] = 0;
maru536 2:8d119e9b8f5a 56 strcpy(dst, tmp);
maru536 2:8d119e9b8f5a 57 }
maru536 2:8d119e9b8f5a 58
maru536 2:8d119e9b8f5a 59 uint16_t strToUint16(char* str, int size)
maru536 2:8d119e9b8f5a 60 {
maru536 2:8d119e9b8f5a 61 uint16_t result = 0;
maru536 2:8d119e9b8f5a 62 for (int i = 0; i < size; i++) {
maru536 2:8d119e9b8f5a 63 if (str[i] > '9' || str[i] < '0') {
maru536 2:8d119e9b8f5a 64 break;
maru536 2:8d119e9b8f5a 65 }
maru536 2:8d119e9b8f5a 66
maru536 2:8d119e9b8f5a 67 result *= 10;
maru536 2:8d119e9b8f5a 68 result += str[i] - '0';
maru536 2:8d119e9b8f5a 69 }
maru536 2:8d119e9b8f5a 70
maru536 2:8d119e9b8f5a 71 return result;
maru536 2:8d119e9b8f5a 72 }
maru536 2:8d119e9b8f5a 73
maru536 2:8d119e9b8f5a 74 bool extractVoiceData(TCPSocket *socket, int &content_length) {
maru536 2:8d119e9b8f5a 75 bool isGetLength = false;
maru536 2:8d119e9b8f5a 76 char c;
maru536 2:8d119e9b8f5a 77 content_length = 0;
maru536 2:8d119e9b8f5a 78 char buf[MAX_NUMBER_SIZE] = "";
maru536 2:8d119e9b8f5a 79 while (socket->recv(&c, sizeof(c)) >= 0) {
maru536 2:8d119e9b8f5a 80 if (isGetLength == false && c == '\n') {
maru536 2:8d119e9b8f5a 81 for (int i = 0; i < MAX_HEADER_SIZE; i++) {
maru536 2:8d119e9b8f5a 82 if (socket->recv(&c, sizeof(c)) >= 0 && c == 'C' && socket->recv(&c, sizeof(c)) >= 0 && c == 'o' && socket->recv(&c, sizeof(c)) >= 0 && c == 'n' &&
maru536 2:8d119e9b8f5a 83 socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == 'e' && socket->recv(&c, sizeof(c)) >= 0 && c == 'n' &&
maru536 2:8d119e9b8f5a 84 socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == '-' && socket->recv(&c, sizeof(c)) >= 0 && c == 'L' &&
maru536 2:8d119e9b8f5a 85 socket->recv(&c, sizeof(c)) >= 0 && c == 'e' && socket->recv(&c, sizeof(c)) >= 0 && c == 'n' && socket->recv(&c, sizeof(c)) >= 0 && c == 'g' &&
maru536 2:8d119e9b8f5a 86 socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == 'h' && socket->recv(&c, sizeof(c)) >= 0 && c == ':' &&
maru536 2:8d119e9b8f5a 87 socket->recv(&c, sizeof(c)) >= 0 && c == ' ') {
maru536 2:8d119e9b8f5a 88 for(int j = 0; j < MAX_NUMBER_SIZE; j++) {
maru536 2:8d119e9b8f5a 89 if (socket->recv(&c, sizeof(c)) >= 0 && c == '\n') {
maru536 2:8d119e9b8f5a 90 buf[j] = 0;
maru536 2:8d119e9b8f5a 91 content_length = strToUint16(buf, sizeof(buf));
maru536 2:8d119e9b8f5a 92 isGetLength = true;
maru536 2:8d119e9b8f5a 93 break;
maru536 2:8d119e9b8f5a 94 }
maru536 2:8d119e9b8f5a 95 buf[j] = c;
maru536 2:8d119e9b8f5a 96 }
maru536 2:8d119e9b8f5a 97 }
maru536 2:8d119e9b8f5a 98 else {
maru536 2:8d119e9b8f5a 99 break;
maru536 2:8d119e9b8f5a 100 }
maru536 2:8d119e9b8f5a 101 }
maru536 2:8d119e9b8f5a 102 }
maru536 2:8d119e9b8f5a 103
maru536 2:8d119e9b8f5a 104 if (isGetLength == true && c == '\n') {
maru536 2:8d119e9b8f5a 105 for (int i = 0; i < 3; i++) {
maru536 2:8d119e9b8f5a 106 if (socket->recv(&c, sizeof(c)) >= 0 && c == '\n') {
maru536 2:8d119e9b8f5a 107 printf("content_length: %d\n", content_length);
maru536 2:8d119e9b8f5a 108 return true;
maru536 2:8d119e9b8f5a 109 }
maru536 2:8d119e9b8f5a 110 }
maru536 2:8d119e9b8f5a 111 }
maru536 2:8d119e9b8f5a 112 }
maru536 2:8d119e9b8f5a 113
maru536 2:8d119e9b8f5a 114 return false;
maru536 2:8d119e9b8f5a 115 }
maru536 2:8d119e9b8f5a 116
maru536 2:8d119e9b8f5a 117 void strcpy(unsigned char *dst, unsigned char *src)
maru536 2:8d119e9b8f5a 118 {
maru536 2:8d119e9b8f5a 119 int idx = 0;
maru536 2:8d119e9b8f5a 120 for (idx = 0; src[idx] != '\0'; idx++) {
maru536 2:8d119e9b8f5a 121 dst[idx] = src[idx];
maru536 2:8d119e9b8f5a 122 }
maru536 2:8d119e9b8f5a 123 dst[idx] = src[idx];
maru536 2:8d119e9b8f5a 124 }