2 * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
20 #define TTS_STRDUP(src) ((src != NULL) ? strdup(src) : NULL )
23 static char* g_text = NULL;
30 Eina_Bool __tts_test_destroy(void *data);
32 static bool __tts_test_get_text_from_file(const char* path, char** text)
39 if((fp = fopen(path, "rb")) == NULL ) {
40 SLOG(LOG_ERROR, tts_tag(), "Fail to open file (%s)", path);
44 fseek(fp , 0 , SEEK_END);
46 int text_len = ftell(fp);
48 SLOG(LOG_ERROR, tts_tag(), "File has no contents\n");
52 SLOG(LOG_ERROR, tts_tag(), "text_len(%d)\n", text_len);
55 *text = (char*)calloc(1, text_len+1);
58 SLOG(LOG_ERROR, tts_tag(), "Fail to memory allocation\n");
65 result_len = fread(*text, sizeof(char), text_len, fp);
66 if (result_len != text_len) {
67 SLOG(LOG_ERROR, tts_tag(), "Fail to read\n");
73 *text[result_len] = '\0';
79 Eina_Bool __tts_test_play(void *data)
87 ret = tts_add_text(g_tts, g_text, lang, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &utt_id);
88 if (TTS_ERROR_NONE != ret) {
89 SLOG(LOG_ERROR, tts_tag(), "Fail to add text\n");
90 ecore_timer_add(0, __tts_test_destroy, NULL);
94 SLOG(LOG_ERROR, tts_tag(), "Play : utt id(%d)\n", utt_id);
95 ret = tts_play(g_tts);
96 if (TTS_ERROR_NONE != ret) {
97 SLOG(LOG_ERROR, tts_tag(), "Fail to play\n");
98 ecore_timer_add(0, __tts_test_destroy, NULL);
105 Eina_Bool __tts_test_destroy(void *data)
108 SLOG(LOG_ERROR, tts_tag(), "Stop\n");
109 ret = tts_stop(g_tts);
110 if (TTS_ERROR_NONE != ret) {
111 SLOG(LOG_ERROR, tts_tag(), "Fail to stop\n");
114 SLOG(LOG_ERROR, tts_tag(), "Unprepare (Disconnection)\n");
115 ret = tts_unprepare(g_tts);
116 if (TTS_ERROR_NONE != ret) {
117 SLOG(LOG_ERROR, tts_tag(), "Fail to unprepare\n");
120 SLOG(LOG_ERROR, tts_tag(), "Destory tts client\n");
121 ret = tts_destroy(g_tts);
122 if (TTS_ERROR_NONE != ret) {
123 SLOG(LOG_ERROR, tts_tag(), "Fail to destroy\n");
126 ecore_main_loop_quit();
131 static void __tts_test_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* user_data)
133 if (TTS_STATE_CREATED == previous && TTS_STATE_READY == current) {
134 SLOG(LOG_ERROR, tts_tag(), "State is ready after prepare\n");
135 ecore_timer_add(0, __tts_test_play, user_data);
141 static void __tts_test_utt_started_cb(tts_h tts, int utt_id, void* user_data)
143 SLOG(LOG_DEBUG, tts_tag(), "Utterance started : utt id(%d) \n", utt_id);
148 static void __tts_test_utt_completed_cb(tts_h tts, int utt_id, void* user_data)
150 SLOG(LOG_DEBUG, tts_tag(), "Utterance completed : utt id(%d) \n", utt_id);
151 ecore_timer_add(0, __tts_test_destroy, NULL);
156 int main (int argc, char *argv[])
158 if (1 == argc || 5 < argc) {
159 SLOG(LOG_DEBUG, tts_tag(), "Please check parameter\n");
160 SLOG(LOG_DEBUG, tts_tag(), "Ex> tts-test 'text'\n");
161 SLOG(LOG_DEBUG, tts_tag(), "Specific mode> tts-test 'text' '-sr || -noti'\n");
166 char* src_path = NULL;
170 while(NULL != argv[n]) {
172 if(!strcmp("-h", argv[n])) {
173 SLOG(LOG_DEBUG, tts_tag(), "\n");
174 SLOG(LOG_DEBUG, tts_tag(), " ==========================================\n");
175 SLOG(LOG_DEBUG, tts_tag(), " TTS test usage\n");
176 SLOG(LOG_DEBUG, tts_tag(), " ==========================================\n\n");
177 SLOG(LOG_DEBUG, tts_tag(), " -t : Synthesize text \n");
178 SLOG(LOG_DEBUG, tts_tag(), " -l : Determine langage to synthesize text, ex) en_US, ko_KR ...\n");
179 SLOG(LOG_DEBUG, tts_tag(), " -f : Determine file path which include text\n\n");
180 SLOG(LOG_DEBUG, tts_tag(), " ***************************************************\n");
181 SLOG(LOG_DEBUG, tts_tag(), " Example : #tts-test -l en_US -t \"1 2 3 4\" \n");
182 SLOG(LOG_DEBUG, tts_tag(), " ***************************************************\n");
183 SLOG(LOG_DEBUG, tts_tag(), "\n");
187 // check langage option
188 if(!strcmp("-l", argv[n])) {
189 lang = TTS_STRDUP(argv[n+1]);
190 SLOG(LOG_ERROR, tts_tag(), "Language : %s\n", lang);
192 // check text to synthesize
193 else if (!strcmp("-t", argv[n])) {
194 g_text = TTS_STRDUP(argv[n+1]);
195 SLOG(LOG_ERROR, tts_tag(), "Text : %s\n", g_text);
197 // check file path to synthesize
198 else if (!strcmp("-f", argv[n])) {
199 src_path = TTS_STRDUP(argv[n+1]);
200 SLOG(LOG_ERROR, tts_tag(), "File path : %s\n", src_path);
201 if(!__tts_test_get_text_from_file(src_path, &g_text)) {
208 if(!g_text && !src_path) {
209 SLOG(LOG_ERROR, tts_tag(), "Invalid parameter, check help with command tts-test -h");
213 //===================================
215 tts_mode_e mode = TTS_MODE_DEFAULT;
217 SLOG(LOG_DEBUG, tts_tag(), " ");
218 SLOG(LOG_DEBUG, tts_tag(), " ");
219 SLOG(LOG_DEBUG, tts_tag(), "===== TTS Sample start =====\n");
221 SLOG(LOG_DEBUG, tts_tag(), "Input text : %s\n", g_text ? g_text : "NULL");
222 SLOG(LOG_DEBUG, tts_tag(), "Input lang : %s\n", lang ? lang : "NULL");
223 SLOG(LOG_DEBUG, tts_tag(), "Input file path : %s\n", src_path ? src_path : "NULL");
226 SLOG(LOG_ERROR, tts_tag(), "[Main ERROR] Fail ecore_init()\n");
232 SLOG(LOG_DEBUG, tts_tag(), "Create tts client\n");
233 ret = tts_create(&g_tts);
234 if (TTS_ERROR_NONE != ret) {
235 SLOG(LOG_ERROR, tts_tag(), "Fail to create\n");
239 SLOG(LOG_DEBUG, tts_tag(), "Set tts mode - %d\n", mode);
240 ret = tts_set_mode(g_tts, mode);
241 if (TTS_ERROR_NONE != ret) {
242 SLOG(LOG_ERROR, tts_tag(), "Fail to set mode\n");
247 SLOG(LOG_DEBUG, tts_tag(), "Set Callback func\n");
248 ret = tts_set_state_changed_cb(g_tts, __tts_test_state_changed_cb, (void*)lang);
249 if (TTS_ERROR_NONE != ret) {
250 SLOG(LOG_ERROR, tts_tag(), "Fail to set state changed cb\n");
255 ret = tts_set_utterance_started_cb(g_tts, __tts_test_utt_started_cb, NULL);
256 if (TTS_ERROR_NONE != ret) {
257 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt started cb\n");
262 ret = tts_set_utterance_completed_cb(g_tts, __tts_test_utt_completed_cb, NULL);
263 if (TTS_ERROR_NONE != ret) {
264 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt completed cb\n");
269 SLOG(LOG_DEBUG, tts_tag(), "Prepare (Daemon Connection) asynchronously : Wait for ready state \n");
270 ret = tts_prepare(g_tts);
271 if (TTS_ERROR_NONE != ret) {
272 SLOG(LOG_ERROR, tts_tag(), "Fail to prepare\n");
277 ecore_main_loop_begin();
281 if(src_path) free(src_path);
283 if(g_text) free(g_text);
285 SLOG(LOG_DEBUG, tts_tag(), "===== TTS END =====\n\n\n");