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),
93 TtsPlayer::~TtsPlayer()
95 // If it is playing, stop it
98 // Unset the callback funtion for TTS state change
99 int retVal = tts_unset_state_changed_cb(mTtsHandle);
100 if( retVal != TTS_ERROR_NONE )
102 LogErrorCode(static_cast<tts_error_e>(retVal));
105 // Destroy the TTS handle and disconnects the daemon
106 retVal = tts_destroy(mTtsHandle);
107 if( retVal != TTS_ERROR_NONE )
109 LogErrorCode(static_cast<tts_error_e>(retVal));
113 void TtsPlayer::Initialize()
115 // Create the TTS handle
116 int retVal = tts_create(&mTtsHandle);
118 if( retVal != TTS_ERROR_NONE )
120 LogErrorCode(static_cast<tts_error_e>(retVal));
124 // Set the callback funtion for TTS state change
125 retVal = tts_set_state_changed_cb(mTtsHandle, &StateChangedCallback, this);
126 if( retVal != TTS_ERROR_NONE )
128 LogErrorCode(static_cast<tts_error_e>(retVal));
132 tts_mode_e ttsMode = TTS_MODE_DEFAULT;
135 case Dali::TtsPlayer::DEFAULT:
136 ttsMode = TTS_MODE_DEFAULT;
138 case Dali::TtsPlayer::NOTIFICATION:
139 ttsMode = TTS_MODE_NOTIFICATION;
141 case Dali::TtsPlayer::SCREEN_READER:
142 ttsMode = TTS_MODE_SCREEN_READER;
149 retVal = tts_set_mode(mTtsHandle, ttsMode);
150 if(retVal != TTS_ERROR_NONE)
152 LogErrorCode(static_cast<tts_error_e>(retVal));
155 // Connect the TTS daemon asynchronously
156 retVal = tts_prepare(mTtsHandle);
157 if(retVal != TTS_ERROR_NONE)
159 LogErrorCode(static_cast<tts_error_e>(retVal));
164 void TtsPlayer::Play(const std::string& text)
170 // Add text to the queue, and use normal speed, default language and default voice set by the user
171 int retVal = tts_add_text(mTtsHandle, text.c_str(), NULL, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &mUtteranceId);
172 if(retVal != TTS_ERROR_NONE)
174 LogErrorCode(static_cast<tts_error_e>(retVal));
178 // Start synthesizing voice from text in the queue and play synthesized audio data
179 retVal = tts_play(mTtsHandle);
180 if(retVal != TTS_ERROR_NONE)
182 LogErrorCode(static_cast<tts_error_e>(retVal));
188 mUnplayedString = text;
192 void TtsPlayer::Stop()
196 // Check the current TTS state
198 int retVal = tts_get_state(mTtsHandle, &state);
199 if(retVal != TTS_ERROR_NONE)
201 LogErrorCode(static_cast<tts_error_e>(retVal));
203 else if(state == TTS_STATE_PLAYING || state == TTS_STATE_PAUSED)
205 // If it is playing or paused, stop playing and clear the queue
206 retVal = tts_stop(mTtsHandle);
207 if( retVal != TTS_ERROR_NONE )
209 LogErrorCode(static_cast<tts_error_e>(retVal));
215 void TtsPlayer::Pause()
219 // Check the current TTS state
221 int retVal = tts_get_state(mTtsHandle, &state);
222 if(retVal != TTS_ERROR_NONE)
224 LogErrorCode(static_cast<tts_error_e>(retVal));
226 else if(state == TTS_STATE_PLAYING)
228 // If the player is playing, pause it.
229 retVal = tts_pause(mTtsHandle);
230 if( retVal != TTS_ERROR_NONE )
232 LogErrorCode(static_cast<tts_error_e>(retVal));
238 void TtsPlayer::Resume()
242 // Check the current TTS state
244 int retVal = tts_get_state(mTtsHandle, &state);
245 if(retVal != TTS_ERROR_NONE)
247 LogErrorCode(static_cast<tts_error_e>(retVal));
249 else if(state == TTS_STATE_PAUSED)
251 // If the player is paused, resume it.
252 retVal = tts_play(mTtsHandle);
253 if( retVal != TTS_ERROR_NONE )
255 LogErrorCode(static_cast<tts_error_e>(retVal));
261 Dali::TtsPlayer::State TtsPlayer::GetState()
263 Dali::TtsPlayer::State ttsState = Dali::TtsPlayer::UNAVAILABLE;
267 // Check the current TTS state
269 int retVal = tts_get_state(mTtsHandle, &state);
270 if(retVal != TTS_ERROR_NONE)
272 LogErrorCode(static_cast<tts_error_e>(retVal));
276 ttsState = InternalToExternalState( state );
283 Dali::TtsPlayer::StateChangedSignalType& TtsPlayer::StateChangedSignal()
285 return mStateChangedSignal;
288 void TtsPlayer::EmitStateChangedSignal( tts_state_e previous, tts_state_e current )
290 // Convert the previous and current states to external states and emit them as a signal.
291 if( !mStateChangedSignal.Empty() )
293 mStateChangedSignal.Emit( InternalToExternalState( previous ), InternalToExternalState( current ) );
297 void TtsPlayer::StateChangedCallback(tts_h tts, tts_state_e previous, tts_state_e current, void *userData)
299 // Get the implementation (this is a static function).
300 TtsPlayer* obj = static_cast<TtsPlayer*>(userData);
303 obj->EmitStateChangedSignal( previous, current );
305 if(!obj->mInitialized && current == TTS_STATE_READY)
307 obj->mInitialized = true;
309 // if there is queued text before initialization, play it
310 if(obj->mUnplayedString != "")
312 obj->Play(obj->mUnplayedString);
313 obj->mUnplayedString = "";
318 void TtsPlayer::LogErrorCode(tts_error_e reason)
320 std::string error_string;
328 case TTS_ERROR_OUT_OF_MEMORY:
330 error_string = "TTS: Out of Memory\n";
333 case TTS_ERROR_IO_ERROR:
335 error_string = "TTS: I/O error\n";
338 case TTS_ERROR_INVALID_PARAMETER:
340 error_string = "TTS: Invalid parameter\n";
343 case TTS_ERROR_OUT_OF_NETWORK:
345 error_string = "TTS: Out of network\n";
348 case TTS_ERROR_INVALID_STATE:
350 error_string = "TTS: Invalid state\n";
353 case TTS_ERROR_INVALID_VOICE:
355 error_string = "TTS: Invalid voice\n";
358 case TTS_ERROR_ENGINE_NOT_FOUND:
360 error_string = "TTS: No available engine\n";
363 case TTS_ERROR_TIMED_OUT:
365 error_string = "TTS: No answer from the daemon\n";
368 case TTS_ERROR_OPERATION_FAILED:
370 error_string = "TTS: Operation failed\n";
375 error_string = "Invalid TTS error code\n";
380 if(reason != TTS_ERROR_NONE)
382 DALI_LOG_WARNING("[%s:%d] tts error : %s\n", __FUNCTION__, __LINE__, error_string.c_str());
386 } // namespace Adaptor
388 } // namespace Internal