2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "tts-player-impl.h"
25 #include <dali/public-api/object/type-registry.h>
37 namespace // unnamed namespace
40 Dali::BaseHandle Create()
42 return Dali::TtsPlayer::Get() ;
45 Dali::TypeRegistration mType( typeid(Dali::TtsPlayer), typeid(Dali::BaseHandle), Create ) ;
46 } // unnamed namespace
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* TtsPlayer::gLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TTS_PLAYER");
52 Dali::TtsPlayer TtsPlayer::New(Dali::TtsPlayer::Mode mode)
54 Dali::TtsPlayer player = Dali::TtsPlayer(new TtsPlayer(mode));
59 TtsPlayer::TtsPlayer(Dali::TtsPlayer::Mode mode)
60 : mInitialized(false),
68 TtsPlayer::~TtsPlayer()
70 // If it is playing, stop it
73 // Unset the callback funtion for TTS state change
74 int retVal = tts_unset_state_changed_cb(mTtsHandle);
75 if( retVal != TTS_ERROR_NONE )
77 LogErrorCode(static_cast<tts_error_e>(retVal));
80 // Destroy the TTS handle and disconnects the daemon
81 retVal = tts_destroy(mTtsHandle);
82 if( retVal != TTS_ERROR_NONE )
84 LogErrorCode(static_cast<tts_error_e>(retVal));
88 void TtsPlayer::Initialize()
90 // Create the TTS handle
91 int retVal = tts_create(&mTtsHandle);
93 if( retVal != TTS_ERROR_NONE )
95 LogErrorCode(static_cast<tts_error_e>(retVal));
99 // Set the callback funtion for TTS state change
100 retVal = tts_set_state_changed_cb(mTtsHandle, &StateChangedCallback, this);
101 if( retVal != TTS_ERROR_NONE )
103 LogErrorCode(static_cast<tts_error_e>(retVal));
107 tts_mode_e ttsMode = TTS_MODE_DEFAULT;
110 case Dali::TtsPlayer::DEFAULT:
111 ttsMode = TTS_MODE_DEFAULT;
113 case Dali::TtsPlayer::NOTIFICATION:
114 ttsMode = TTS_MODE_NOTIFICATION;
116 case Dali::TtsPlayer::SCREEN_READER:
117 ttsMode = TTS_MODE_SCREEN_READER;
124 retVal = tts_set_mode(mTtsHandle, ttsMode);
125 if(retVal != TTS_ERROR_NONE)
127 LogErrorCode(static_cast<tts_error_e>(retVal));
130 // Connect the TTS daemon asynchronously
131 retVal = tts_prepare(mTtsHandle);
132 if(retVal != TTS_ERROR_NONE)
134 LogErrorCode(static_cast<tts_error_e>(retVal));
139 void TtsPlayer::Play(const std::string& text)
145 // Add text to the queue, and use normal speed, default language and default voice set by the user
146 int retVal = tts_add_text(mTtsHandle, text.c_str(), NULL, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &mUtteranceId);
147 if(retVal != TTS_ERROR_NONE)
149 LogErrorCode(static_cast<tts_error_e>(retVal));
153 // Start synthesizing voice from text in the queue and play synthesized audio data
154 retVal = tts_play(mTtsHandle);
155 if(retVal != TTS_ERROR_NONE)
157 LogErrorCode(static_cast<tts_error_e>(retVal));
163 mUnplayedString = text;
167 void TtsPlayer::Stop()
171 // Check the current TTS state
173 int retVal = tts_get_state(mTtsHandle, &state);
174 if(retVal != TTS_ERROR_NONE)
176 LogErrorCode(static_cast<tts_error_e>(retVal));
178 else if(state == TTS_STATE_PLAYING || state == TTS_STATE_PAUSED)
180 // If it is playing or paused, stop playing and clear the queue
181 retVal = tts_stop(mTtsHandle);
182 if( retVal != TTS_ERROR_NONE )
184 LogErrorCode(static_cast<tts_error_e>(retVal));
190 void TtsPlayer::Pause()
194 // Check the current TTS state
196 int retVal = tts_get_state(mTtsHandle, &state);
197 if(retVal != TTS_ERROR_NONE)
199 LogErrorCode(static_cast<tts_error_e>(retVal));
201 else if(state == TTS_STATE_PLAYING)
203 // If the player is playing, pause it.
204 retVal = tts_pause(mTtsHandle);
205 if( retVal != TTS_ERROR_NONE )
207 LogErrorCode(static_cast<tts_error_e>(retVal));
213 void TtsPlayer::Resume()
217 // Check the current TTS state
219 int retVal = tts_get_state(mTtsHandle, &state);
220 if(retVal != TTS_ERROR_NONE)
222 LogErrorCode(static_cast<tts_error_e>(retVal));
224 else if(state == TTS_STATE_PAUSED)
226 // If the player is paused, resume it.
227 retVal = tts_play(mTtsHandle);
228 if( retVal != TTS_ERROR_NONE )
230 LogErrorCode(static_cast<tts_error_e>(retVal));
236 void TtsPlayer::StateChangedCallback(tts_h tts, tts_state_e previous, tts_state_e current, void *userData)
238 TtsPlayer* obj = static_cast<TtsPlayer*>(userData);
239 if(!obj->mInitialized && current == TTS_STATE_READY)
241 obj->mInitialized = true;
243 // if there is queued text before initialization, play it
244 if(obj->mUnplayedString != "")
246 obj->Play(obj->mUnplayedString);
247 obj->mUnplayedString = "";
252 void TtsPlayer::LogErrorCode(tts_error_e reason)
254 std::string error_string;
262 case TTS_ERROR_OUT_OF_MEMORY:
264 error_string = "TTS: Out of Memory\n";
267 case TTS_ERROR_IO_ERROR:
269 error_string = "TTS: I/O error\n";
272 case TTS_ERROR_INVALID_PARAMETER:
274 error_string = "TTS: Invalid parameter\n";
277 case TTS_ERROR_OUT_OF_NETWORK:
279 error_string = "TTS: Out of network\n";
282 case TTS_ERROR_INVALID_STATE:
284 error_string = "TTS: Invalid state\n";
287 case TTS_ERROR_INVALID_VOICE:
289 error_string = "TTS: Invalid voice\n";
292 case TTS_ERROR_ENGINE_NOT_FOUND:
294 error_string = "TTS: No available engine\n";
297 case TTS_ERROR_TIMED_OUT:
299 error_string = "TTS: No answer from the daemon\n";
302 case TTS_ERROR_OPERATION_FAILED:
304 error_string = "TTS: Operation failed\n";
309 error_string = "Invalid TTS error code\n";
314 if(reason != TTS_ERROR_NONE)
316 DALI_LOG_WARNING("[%s:%d] tts error : %s\n", __FUNCTION__, __LINE__, error_string.c_str());
320 } // namespace Adaptor
322 } // namespace Internal