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 );
48 * Helper function to convert Tizen-specific TTS state to external state.
49 * @param state The Tizen TTS state.
50 * @return The external TTS state.
52 Dali::TtsPlayer::State InternalToExternalState( tts_state_e state )
56 case TTS_STATE_CREATED:
58 return Dali::TtsPlayer::UNAVAILABLE;
62 return Dali::TtsPlayer::READY;
64 case TTS_STATE_PLAYING:
66 return Dali::TtsPlayer::PLAYING;
68 case TTS_STATE_PAUSED:
70 return Dali::TtsPlayer::PAUSED;
74 return Dali::TtsPlayer::UNAVAILABLE;
77 } // unnamed namespace
79 #if defined(DEBUG_ENABLED)
80 Debug::Filter* TtsPlayer::gLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TTS_PLAYER");
83 Dali::TtsPlayer TtsPlayer::New(Dali::TtsPlayer::Mode mode)
85 Dali::TtsPlayer player = Dali::TtsPlayer(new TtsPlayer(mode));
90 TtsPlayer::TtsPlayer(Dali::TtsPlayer::Mode mode)
91 : mInitialized(false),
99 TtsPlayer::~TtsPlayer()
101 // If it is playing, stop it
104 // Unset the callback funtion for TTS state change
105 int retVal = tts_unset_state_changed_cb(mTtsHandle);
106 if( retVal != TTS_ERROR_NONE )
108 LogErrorCode(static_cast<tts_error_e>(retVal));
111 // Destroy the TTS handle and disconnects the daemon
112 retVal = tts_destroy(mTtsHandle);
113 if( retVal != TTS_ERROR_NONE )
115 LogErrorCode(static_cast<tts_error_e>(retVal));
119 void TtsPlayer::Initialize()
121 // Create the TTS handle
122 int retVal = tts_create(&mTtsHandle);
124 if( retVal != TTS_ERROR_NONE )
126 LogErrorCode(static_cast<tts_error_e>(retVal));
130 // Set the callback funtion for TTS state change
131 retVal = tts_set_state_changed_cb(mTtsHandle, &StateChangedCallback, this);
132 if( retVal != TTS_ERROR_NONE )
134 LogErrorCode(static_cast<tts_error_e>(retVal));
138 tts_mode_e ttsMode = TTS_MODE_DEFAULT;
141 case Dali::TtsPlayer::DEFAULT:
142 ttsMode = TTS_MODE_DEFAULT;
144 case Dali::TtsPlayer::NOTIFICATION:
145 ttsMode = TTS_MODE_NOTIFICATION;
147 case Dali::TtsPlayer::SCREEN_READER:
148 ttsMode = TTS_MODE_SCREEN_READER;
155 retVal = tts_set_mode(mTtsHandle, ttsMode);
156 if(retVal != TTS_ERROR_NONE)
158 LogErrorCode(static_cast<tts_error_e>(retVal));
161 // Connect the TTS daemon asynchronously
162 retVal = tts_prepare(mTtsHandle);
163 if(retVal != TTS_ERROR_NONE)
165 LogErrorCode(static_cast<tts_error_e>(retVal));
170 void TtsPlayer::Play(const std::string& text)
176 // Add text to the queue, and use normal speed, default language and default voice set by the user
177 int retVal = tts_add_text(mTtsHandle, text.c_str(), NULL, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &mUtteranceId);
178 if(retVal != TTS_ERROR_NONE)
180 LogErrorCode(static_cast<tts_error_e>(retVal));
184 // Start synthesizing voice from text in the queue and play synthesized audio data
185 retVal = tts_play(mTtsHandle);
186 if(retVal != TTS_ERROR_NONE)
188 LogErrorCode(static_cast<tts_error_e>(retVal));
194 mUnplayedString = text;
198 void TtsPlayer::Stop()
202 // Check the current TTS state
204 int retVal = tts_get_state(mTtsHandle, &state);
205 if(retVal != TTS_ERROR_NONE)
207 LogErrorCode(static_cast<tts_error_e>(retVal));
209 else if(state == TTS_STATE_PLAYING || state == TTS_STATE_PAUSED)
211 // If it is playing or paused, stop playing and clear the queue
212 retVal = tts_stop(mTtsHandle);
213 if( retVal != TTS_ERROR_NONE )
215 LogErrorCode(static_cast<tts_error_e>(retVal));
221 void TtsPlayer::Pause()
225 // Check the current TTS state
227 int retVal = tts_get_state(mTtsHandle, &state);
228 if(retVal != TTS_ERROR_NONE)
230 LogErrorCode(static_cast<tts_error_e>(retVal));
232 else if(state == TTS_STATE_PLAYING)
234 // If the player is playing, pause it.
235 retVal = tts_pause(mTtsHandle);
236 if( retVal != TTS_ERROR_NONE )
238 LogErrorCode(static_cast<tts_error_e>(retVal));
244 void TtsPlayer::Resume()
248 // Check the current TTS state
250 int retVal = tts_get_state(mTtsHandle, &state);
251 if(retVal != TTS_ERROR_NONE)
253 LogErrorCode(static_cast<tts_error_e>(retVal));
255 else if(state == TTS_STATE_PAUSED)
257 // If the player is paused, resume it.
258 retVal = tts_play(mTtsHandle);
259 if( retVal != TTS_ERROR_NONE )
261 LogErrorCode(static_cast<tts_error_e>(retVal));
267 Dali::TtsPlayer::State TtsPlayer::GetState()
269 Dali::TtsPlayer::State ttsState = Dali::TtsPlayer::UNAVAILABLE;
273 // Check the current TTS state
275 int retVal = tts_get_state(mTtsHandle, &state);
276 if(retVal != TTS_ERROR_NONE)
278 LogErrorCode(static_cast<tts_error_e>(retVal));
282 ttsState = InternalToExternalState( state );
289 Dali::TtsPlayer::StateChangedSignalType& TtsPlayer::StateChangedSignal()
291 return mStateChangedSignal;
294 void TtsPlayer::EmitStateChangedSignal( tts_state_e previous, tts_state_e current )
296 // Convert the previous and current states to external states and emit them as a signal.
297 if( !mStateChangedSignal.Empty() )
299 mStateChangedSignal.Emit( InternalToExternalState( previous ), InternalToExternalState( current ) );
303 void TtsPlayer::StateChangedCallback(tts_h tts, tts_state_e previous, tts_state_e current, void *userData)
305 // Get the implementation (this is a static function).
306 TtsPlayer* obj = static_cast<TtsPlayer*>(userData);
309 obj->EmitStateChangedSignal( previous, current );
311 if(!obj->mInitialized && current == TTS_STATE_READY)
313 obj->mInitialized = true;
315 // if there is queued text before initialization, play it
316 if(obj->mUnplayedString != "")
318 obj->Play(obj->mUnplayedString);
319 obj->mUnplayedString = "";
324 void TtsPlayer::LogErrorCode(tts_error_e reason)
326 std::string error_string;
334 case TTS_ERROR_OUT_OF_MEMORY:
336 error_string = "TTS: Out of Memory\n";
339 case TTS_ERROR_IO_ERROR:
341 error_string = "TTS: I/O error\n";
344 case TTS_ERROR_INVALID_PARAMETER:
346 error_string = "TTS: Invalid parameter\n";
349 case TTS_ERROR_OUT_OF_NETWORK:
351 error_string = "TTS: Out of network\n";
354 case TTS_ERROR_INVALID_STATE:
356 error_string = "TTS: Invalid state\n";
359 case TTS_ERROR_INVALID_VOICE:
361 error_string = "TTS: Invalid voice\n";
364 case TTS_ERROR_ENGINE_NOT_FOUND:
366 error_string = "TTS: No available engine\n";
369 case TTS_ERROR_TIMED_OUT:
371 error_string = "TTS: No answer from the daemon\n";
374 case TTS_ERROR_OPERATION_FAILED:
376 error_string = "TTS: Operation failed\n";
381 error_string = "Invalid TTS error code\n";
386 if(reason != TTS_ERROR_NONE)
388 DALI_LOG_WARNING("[%s:%d] tts error : %s\n", __FUNCTION__, __LINE__, error_string.c_str());
392 } // namespace Adaptor
394 } // namespace Internal