/////////////////////////////////////////////////////////////////////////////////////////////// // // AndroTTSSpeech.cpp // Android AndroTTS c example running on windows and linux // // On linux to complie use: // gcc AndroTTSSpeech.cpp -oAndroTTSSpeech // then use the following command for execution (where 172.16.9.179 is the ip displyed o Android //./AndroTTSSpeech 172.16.9.179 3383 "Hello World!" EN hello.wav #include #include #include #ifdef WIN32 #include #include #include #include #include #define S_IWUSR 0777 #define S_IRUSR 0 #define S_IRGRP 0 #define S_IWGRP 0 #define S_IROTH 0 #define S_IWOTH 0 #define O_NOCTTY _O_TRUNC #define O_NDELAY _O_CREAT #else #include #include #include #include #include #define O_BINARY 0 #define closesocket close #define _write write #define _close close #define _open open #endif struct androtts_server { char host[80]; int port; char lang[20]; int TTSsocket; }; static int ttsconnect(struct androtts_server *cur) { struct sockaddr_in client; int k; cur->TTSsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (cur->TTSsocket<0) { return 0; } memset(&client, 0, sizeof(client)); client.sin_family = AF_INET; client.sin_addr.s_addr = inet_addr(cur->host); client.sin_port = htons(cur->port); k = connect(cur->TTSsocket, (struct sockaddr*)&client, sizeof(client)); if (k != 0) { closesocket(cur->TTSsocket); cur->TTSsocket = 0; return 0; } return 1; } static int ttsdisconnect(struct androtts_server *cur) { if (cur->TTSsocket >= 0) closesocket(cur->TTSsocket); return 1; } static int PlayText(char *text, struct androtts_server *as, char *fileName) { char Buffer[1024]; int r = 0; char cLen[8]; send(as->TTSsocket, text, strlen(text)+1, 0); send(as->TTSsocket, "\t", 1, 0); send(as->TTSsocket, as->lang, strlen(as->lang), 0); send(as->TTSsocket, "\n", 1, 0); long readbytes = 0; readbytes = recv(as->TTSsocket, cLen, 8, 0); long FileSize = (FileSize = 0xFF & cLen[7]) | ((cLen[6] << 8) & 0x0000FF00) | ((cLen[5] << 16) & 0x00FF0000) | ((cLen[4] << 24) & 0xFF000000); readbytes = 0; int f = _open(fileName, O_CREAT | O_TRUNC | O_RDWR| O_BINARY, S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (f < 0) { fprintf(stderr, "Can't open output file"); return 0; } while (readbytes != FileSize) { int read = 0; read = recv(as->TTSsocket, (char *)Buffer, 1024, 0); if (read == 0) break; readbytes += read; if (read != _write(f, (void *)Buffer, read)) { fprintf(stderr, "Socket error"); return 0; } } _close(f); return 1; } int main(int argc, char* argv[]) { if (argc != 6) { fprintf(stderr,"Usage:\nAndroTTSSpeech [IP] [Port] [Text] [Lang] [OutFile]\nExample AndroTTSSpeech 192.168.42.1 3383 \"Hello World!\" EN c:\\tmp\\hello.wav"); exit(-1); } #ifdef WIN32 WORD wVersionRequested; WSADATA wsaData; int err; /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { /* Tell the user that we could not find a usable */ /* Winsock DLL. */ printf("WSAStartup failed with error: %d\n", err); exit(-1); return -1; } #endif struct androtts_server *as; if (!(as = (struct androtts_server *)malloc(sizeof(struct androtts_server)))) { fprintf(stderr, "Alloc error. Aborting.\n"); exit(-1); return -1; } memset(as, 0, sizeof(struct androtts_server)); strcpy(as->host, argv[1]); as->port = atoi(argv[2]); strcpy(as->lang, argv[4]); if (0 == ttsconnect(as)) { fprintf(stderr, "Connection error. Aborting.\n"); return -1; } if( 0==PlayText(argv[3], as, argv[5])) { ttsdisconnect(as); free(as); exit(-1); return -1; } ttsdisconnect(as); free(as); exit(0); return 0; }