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"
26 #include <dali/public-api/object/type-registry.h>
38 namespace // unnamed namespace
41 Dali::BaseHandle Create()
43 return Dali::TtsPlayer::Get() ;
46 Dali::TypeRegistration mType( typeid(Dali::TtsPlayer), typeid(Dali::BaseHandle), Create ) ;
47 } // unnamed namespace
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* TtsPlayer::gLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TTS_PLAYER");
53 Dali::TtsPlayer TtsPlayer::New(Dali::TtsPlayer::Mode mode)
55 Dali::TtsPlayer player = Dali::TtsPlayer(new TtsPlayer(mode));
60 TtsPlayer::TtsPlayer(Dali::TtsPlayer::Mode mode)
61 : mInitialized(false),
69 TtsPlayer::~TtsPlayer()
71 // If it is playing, stop it
74 // Unset the callback funtion for TTS state change
75 int retVal = tts_unset_state_changed_cb(mTtsHandle);
76 if( retVal != TTS_ERROR_NONE )
78 LogErrorCode(static_cast<tts_error_e>(retVal));
81 // Destroy the TTS handle and disconnects the daemon
82 retVal = tts_destroy(mTtsHandle);
83 if( retVal != TTS_ERROR_NONE )
85 LogErrorCode(static_cast<tts_error_e>(retVal));
89 void TtsPlayer::Initialize()
91 // Create the TTS handle
92 int retVal = tts_create(&mTtsHandle);
94 if( retVal != TTS_ERROR_NONE )
96 LogErrorCode(static_cast<tts_error_e>(retVal));
100 // Set the callback funtion for TTS state change
101 retVal = tts_set_state_changed_cb(mTtsHandle, &StateChangedCallback, this);
102 if( retVal != TTS_ERROR_NONE )
104 LogErrorCode(static_cast<tts_error_e>(retVal));
108 tts_mode_e ttsMode = TTS_MODE_DEFAULT;
111 case Dali::TtsPlayer::DEFAULT:
112 ttsMode = TTS_MODE_DEFAULT;
114 case Dali::TtsPlayer::NOTIFICATION:
115 ttsMode = TTS_MODE_NOTIFICATION;
117 case Dali::TtsPlayer::SCREEN_READER:
118 ttsMode = TTS_MODE_SCREEN_READER;
125 retVal = tts_set_mode(mTtsHandle, ttsMode);
126 if(retVal != TTS_ERROR_NONE)
128 LogErrorCode(static_cast<tts_error_e>(retVal));
131 // Connect the TTS daemon asynchronously
132 retVal = tts_prepare(mTtsHandle);
133 if(retVal != TTS_ERROR_NONE)
135 LogErrorCode(static_cast<tts_error_e>(retVal));
140 void TtsPlayer::Play(const std::string& text)
146 // Add text to the queue, and use normal speed, default language and default voice set by the user
147 int retVal = tts_add_text(mTtsHandle, text.c_str(), NULL, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &mUtteranceId);
148 if(retVal != TTS_ERROR_NONE)
150 LogErrorCode(static_cast<tts_error_e>(retVal));
154 // Start synthesizing voice from text in the queue and play synthesized audio data
155 retVal = tts_play(mTtsHandle);
156 if(retVal != TTS_ERROR_NONE)
158 LogErrorCode(static_cast<tts_error_e>(retVal));
164 mUnplayedString = text;
168 void TtsPlayer::Stop()
172 // Check the current TTS state
174 int retVal = tts_get_state(mTtsHandle, &state);
175 if(retVal != TTS_ERROR_NONE)
177 LogErrorCode(static_cast<tts_error_e>(retVal));
179 else if(state == TTS_STATE_PLAYING || state == TTS_STATE_PAUSED)
181 // If it is playing or paused, stop playing and clear the queue
182 retVal = tts_stop(mTtsHandle);
183 if( retVal != TTS_ERROR_NONE )
185 LogErrorCode(static_cast<tts_error_e>(retVal));
191 void TtsPlayer::Pause()
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)
204 // If the player is playing, pause it.
205 retVal = tts_pause(mTtsHandle);
206 if( retVal != TTS_ERROR_NONE )
208 LogErrorCode(static_cast<tts_error_e>(retVal));
214 void TtsPlayer::Resume()
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_PAUSED)
227 // If the player is paused, resume it.
228 retVal = tts_play(mTtsHandle);
229 if( retVal != TTS_ERROR_NONE )
231 LogErrorCode(static_cast<tts_error_e>(retVal));
237 void TtsPlayer::StateChangedCallback(tts_h tts, tts_state_e previous, tts_state_e current, void *userData)
239 TtsPlayer* obj = static_cast<TtsPlayer*>(userData);
240 if(!obj->mInitialized && current == TTS_STATE_READY)
242 obj->mInitialized = true;
244 // if there is queued text before initialization, play it
245 if(obj->mUnplayedString != "")
247 obj->Play(obj->mUnplayedString);
248 obj->mUnplayedString = "";
253 void TtsPlayer::LogErrorCode(tts_error_e reason)
255 std::string error_string;
263 case TTS_ERROR_OUT_OF_MEMORY:
265 error_string = "TTS: Out of Memory\n";
268 case TTS_ERROR_IO_ERROR:
270 error_string = "TTS: I/O error\n";
273 case TTS_ERROR_INVALID_PARAMETER:
275 error_string = "TTS: Invalid parameter\n";
278 case TTS_ERROR_OUT_OF_NETWORK:
280 error_string = "TTS: Out of network\n";
283 case TTS_ERROR_INVALID_STATE:
285 error_string = "TTS: Invalid state\n";
288 case TTS_ERROR_INVALID_VOICE:
290 error_string = "TTS: Invalid voice\n";
293 case TTS_ERROR_ENGINE_NOT_FOUND:
295 error_string = "TTS: No available engine\n";
298 case TTS_ERROR_TIMED_OUT:
300 error_string = "TTS: No answer from the daemon\n";
303 case TTS_ERROR_OPERATION_FAILED:
305 error_string = "TTS: Operation failed\n";
310 error_string = "Invalid TTS error code\n";
315 if(reason != TTS_ERROR_NONE)
317 DALI_LOG_WARNING("[%s:%d] tts error : %s\n", __FUNCTION__, __LINE__, error_string.c_str());
321 } // namespace Adaptor
323 } // namespace Internal