AP mode
Dependencies: NetworkSocketAPI WizFi310Interface mbed
Fork of WizFi310_TCP_Echo_Server_Example by
tts.cpp
- Committer:
- maru536
- Date:
- 2017-10-02
- Revision:
- 3:4b4eb90875bd
- Parent:
- 2:8d119e9b8f5a
- Child:
- 7:e5500683781b
File content as of revision 3:4b4eb90875bd:
#include "tts.h" void playTTS(VS1053 *player, TCPSocket *socket, char* buf) { int content_length; char text[200] = ""; char c; socket->connect("api.voicerss.org", 80); url_encode(text, buf); 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); socket->send(buf, MAX_BUF_SIZE); extractVoiceData(socket, content_length); printf("play start! : %d\n", content_length); //player->sendDataBlock(socket, content_length); int sizeSent = 0; while (socket->recv(&c, sizeof(c)) >= 0 && sizeSent < content_length) { //printf("%c", c); sizeSent++; } printf("\nplay size: %d\n", sizeSent); } void playTTS(TCPSocket *socket, char* buf) { int content_length; char text[200] = ""; char c; socket->connect("api.voicerss.org", 80); url_encode(text, buf); 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); socket->send(buf, MAX_BUF_SIZE); extractVoiceData(socket, content_length); printf("play start! : %d\n", content_length); int sizeSent = 0; while (sizeSent < content_length) { if (socket->recv(&c, sizeof(c)) >= 0) { //printf("%d: %c\n", sizeSent, c); sizeSent++; } } printf("\nplay size: %d\n", sizeSent); } void url_encode(char *dst, const char* src ) { int i, j = 0, len; char* tmp; len = strlen( src ); tmp = (char*) malloc( (sizeof(char) * 3 * len) +1 ); for( i = 0 ; i < len ; i++ ){ if( IS_ALNUM( src[i] ) ) { tmp[j] = src[i]; } else{ snprintf( &tmp[j], 4, "%%%02X\n", (unsigned char)src[i] ); j += 2; } j++; } tmp[j] = 0; strcpy(dst, tmp); } uint16_t strToUint16(char* str, int size) { uint16_t result = 0; for (int i = 0; i < size; i++) { if (str[i] > '9' || str[i] < '0') { break; } result *= 10; result += str[i] - '0'; } return result; } bool extractVoiceData(TCPSocket *socket, int &content_length) { bool isGetLength = false; char c; content_length = 0; char buf[MAX_NUMBER_SIZE] = ""; while (socket->recv(&c, sizeof(c)) >= 0) { if (isGetLength == false && c == '\n') { for (int i = 0; i < MAX_HEADER_SIZE; i++) { 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' && socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == 'e' && socket->recv(&c, sizeof(c)) >= 0 && c == 'n' && socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == '-' && socket->recv(&c, sizeof(c)) >= 0 && c == 'L' && socket->recv(&c, sizeof(c)) >= 0 && c == 'e' && socket->recv(&c, sizeof(c)) >= 0 && c == 'n' && socket->recv(&c, sizeof(c)) >= 0 && c == 'g' && socket->recv(&c, sizeof(c)) >= 0 && c == 't' && socket->recv(&c, sizeof(c)) >= 0 && c == 'h' && socket->recv(&c, sizeof(c)) >= 0 && c == ':' && socket->recv(&c, sizeof(c)) >= 0 && c == ' ') { for(int j = 0; j < MAX_NUMBER_SIZE; j++) { if (socket->recv(&c, sizeof(c)) >= 0 && c == '\n') { buf[j] = 0; content_length = strToUint16(buf, sizeof(buf)); isGetLength = true; break; } buf[j] = c; } } else { break; } } } if (isGetLength == true && c == '\n') { for (int i = 0; i < 3; i++) { if (socket->recv(&c, sizeof(c)) >= 0 && c == '\n') { printf("content_length: %d\n", content_length); return true; } } } } return false; } void strcpy(unsigned char *dst, unsigned char *src) { int idx = 0; for (idx = 0; src[idx] != '\0'; idx++) { dst[idx] = src[idx]; } dst[idx] = src[idx]; }