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
41 * Helper function to convert Tizen-specific TTS state to external state.
42 * @param state The Tizen TTS state.
43 * @return The external TTS state.
45 Dali::TtsPlayer::State InternalToExternalState( tts_state_e state )
49 case TTS_STATE_CREATED:
51 return Dali::TtsPlayer::UNAVAILABLE;
55 return Dali::TtsPlayer::READY;
57 case TTS_STATE_PLAYING:
59 return Dali::TtsPlayer::PLAYING;
61 case TTS_STATE_PAUSED:
63 return Dali::TtsPlayer::PAUSED;
67 return Dali::TtsPlayer::UNAVAILABLE;
70 } // unnamed namespace
72 #if defined(DEBUG_ENABLED)
73 Debug::Filter* TtsPlayer::gLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TTS_PLAYER");
76 Dali::TtsPlayer TtsPlayer::New(Dali::TtsPlayer::Mode mode)
78 Dali::TtsPlayer player = Dali::TtsPlayer(new TtsPlayer(mode));
83 TtsPlayer::TtsPlayer(Dali::TtsPlayer::Mode mode)
84 : mInitialized(false),
92 TtsPlayer::~TtsPlayer()
94 // If it is playing, stop it
97 // Unset the callback funtion for TTS state change
98 int retVal = tts_unset_state_changed_cb(mTtsHandle);
99 if( retVal != TTS_ERROR_NONE )
101 LogErrorCode(static_cast<tts_error_e>(retVal));
104 // Destroy the TTS handle and disconnects the daemon
105 retVal = tts_destroy(mTtsHandle);
106 if( retVal != TTS_ERROR_NONE )
108 LogErrorCode(static_cast<tts_error_e>(retVal));
112 void TtsPlayer::Initialize()
114 // Create the TTS handle
115 int retVal = tts_create(&mTtsHandle);
117 if( retVal != TTS_ERROR_NONE )
119 LogErrorCode(static_cast<tts_error_e>(retVal));
123 // Set the callback funtion for TTS state change
124 retVal = tts_set_state_changed_cb(mTtsHandle, &StateChangedCallback, this);
125 if( retVal != TTS_ERROR_NONE )
127 LogErrorCode(static_cast<tts_error_e>(retVal));
131 tts_mode_e ttsMode = TTS_MODE_DEFAULT;
134 case Dali::TtsPlayer::DEFAULT:
135 ttsMode = TTS_MODE_DEFAULT;
137 case Dali::TtsPlayer::NOTIFICATION:
138 ttsMode = TTS_MODE_NOTIFICATION;
140 case Dali::TtsPlayer::SCREEN_READER:
141 ttsMode = TTS_MODE_SCREEN_READER;
148 retVal = tts_set_mode(mTtsHandle, ttsMode);
149 if(retVal != TTS_ERROR_NONE)
151 LogErrorCode(static_cast<tts_error_e>(retVal));
154 // Connect the TTS daemon asynchronously
155 retVal = tts_prepare(mTtsHandle);
156 if(retVal != TTS_ERROR_NONE)
158 LogErrorCode(static_cast<tts_error_e>(retVal));
163 void TtsPlayer::Play(const std::string& text)
169 // Add text to the queue, and use normal speed, default language and default voice set by the user
170 int retVal = tts_add_text(mTtsHandle, text.c_str(), NULL, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &mUtteranceId);
171 if(retVal != TTS_ERROR_NONE)
173 LogErrorCode(static_cast<tts_error_e>(retVal));
177 // Start synthesizing voice from text in the queue and play synthesized audio data
178 retVal = tts_play(mTtsHandle);
179 if(retVal != TTS_ERROR_NONE)
181 LogErrorCode(static_cast<tts_error_e>(retVal));
187 mUnplayedString = text;
191 void TtsPlayer::Stop()
195 // Check the current TTS state
197 int retVal = tts_get_state(mTtsHandle, &state);
198 if(retVal != TTS_ERROR_NONE)
200 LogErrorCode(static_cast<tts_error_e>(retVal));
202 else if(state == TTS_STATE_PLAYING || state == TTS_STATE_PAUSED)
204 // If it is playing or paused, stop playing and clear the queue
205 retVal = tts_stop(mTtsHandle);
206 if( retVal != TTS_ERROR_NONE )
208 LogErrorCode(static_cast<tts_error_e>(retVal));
214 void TtsPlayer::Pause()
218 // Check the current TTS state
220 int retVal = tts_get_state(mTtsHandle, &state);
221 if(retVal != TTS_ERROR_NONE)
223 LogErrorCode(static_cast<tts_error_e>(retVal));
225 else if(state == TTS_STATE_PLAYING)
227 // If the player is playing, pause it.
228 retVal = tts_pause(mTtsHandle);
229 if( retVal != TTS_ERROR_NONE )
231 LogErrorCode(static_cast<tts_error_e>(retVal));
237 void TtsPlayer::Resume()
241 // Check the current TTS state
243 int retVal = tts_get_state(mTtsHandle, &state);
244 if(retVal != TTS_ERROR_NONE)
246 LogErrorCode(static_cast<tts_error_e>(retVal));
248 else if(state == TTS_STATE_PAUSED)
250 // If the player is paused, resume it.
251 retVal = tts_play(mTtsHandle);
252 if( retVal != TTS_ERROR_NONE )
254 LogErrorCode(static_cast<tts_error_e>(retVal));
260 Dali::TtsPlayer::State TtsPlayer::GetState()
262 Dali::TtsPlayer::State ttsState = Dali::TtsPlayer::UNAVAILABLE;
266 // Check the current TTS state
268 int retVal = tts_get_state(mTtsHandle, &state);
269 if(retVal != TTS_ERROR_NONE)
271 LogErrorCode(static_cast<tts_error_e>(retVal));
275 ttsState = InternalToExternalState( state );
282 Dali::TtsPlayer::StateChangedSignalType& TtsPlayer::StateChangedSignal()
284 return mStateChangedSignal;
287 void TtsPlayer::EmitStateChangedSignal( tts_state_e previous, tts_state_e current )
289 // Convert the previous and current states to external states and emit them as a signal.
290 if( !mStateChangedSignal.Empty() )
292 mStateChangedSignal.Emit( InternalToExternalState( previous ), InternalToExternalState( current ) );
296 void TtsPlayer::StateChangedCallback(tts_h tts, tts_state_e previous, tts_state_e current, void *userData)
298 // Get the implementation (this is a static function).
299 TtsPlayer* obj = static_cast<TtsPlayer*>(userData);
302 obj->EmitStateChangedSignal( previous, current );
304 if(!obj->mInitialized && current == TTS_STATE_READY)
306 obj->mInitialized = true;
308 // if there is queued text before initialization, play it
309 if(obj->mUnplayedString != "")
311 obj->Play(obj->mUnplayedString);
312 obj->mUnplayedString = "";
317 void TtsPlayer::LogErrorCode(tts_error_e reason)
319 std::string error_string;
327 case TTS_ERROR_OUT_OF_MEMORY:
329 error_string = "TTS: Out of Memory\n";
332 case TTS_ERROR_IO_ERROR:
334 error_string = "TTS: I/O error\n";
337 case TTS_ERROR_INVALID_PARAMETER:
339 error_string = "TTS: Invalid parameter\n";
342 case TTS_ERROR_OUT_OF_NETWORK:
344 error_string = "TTS: Out of network\n";
347 case TTS_ERROR_INVALID_STATE:
349 error_string = "TTS: Invalid state\n";
352 case TTS_ERROR_INVALID_VOICE:
354 error_string = "TTS: Invalid voice\n";
357 case TTS_ERROR_ENGINE_NOT_FOUND:
359 error_string = "TTS: No available engine\n";
362 case TTS_ERROR_TIMED_OUT:
364 error_string = "TTS: No answer from the daemon\n";
367 case TTS_ERROR_OPERATION_FAILED:
369 error_string = "TTS: Operation failed\n";
374 error_string = "Invalid TTS error code\n";
379 if(reason != TTS_ERROR_NONE)
381 DALI_LOG_WARNING("[%s:%d] tts error : %s\n", __FUNCTION__, __LINE__, error_string.c_str());
385 } // namespace Adaptor
387 } // namespace Internal