Support to build against ubuntu environment.
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / tts-player-impl-tizen.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "tts-player-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <tts.h>
23 #include <stdio.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/object/type-registry.h>
27
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 namespace // unnamed namespace
39 {
40 // Type Registration
41 Dali::BaseHandle Create()
42 {
43   return Dali::TtsPlayer::Get() ;
44 }
45
46 Dali::TypeRegistration mType( typeid(Dali::TtsPlayer), typeid(Dali::BaseHandle), Create ) ;
47 } // unnamed namespace
48
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* TtsPlayer::gLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TTS_PLAYER");
51 #endif
52
53 Dali::TtsPlayer TtsPlayer::New(Dali::TtsPlayer::Mode mode)
54 {
55   Dali::TtsPlayer player = Dali::TtsPlayer(new TtsPlayer(mode));
56
57   return player;
58 }
59
60 TtsPlayer::TtsPlayer(Dali::TtsPlayer::Mode mode)
61 : mInitialized(false),
62   mUnplayedString(""),
63   mUtteranceId(0),
64   mTtsMode(mode)
65 {
66   Initialize();
67 }
68
69 TtsPlayer::~TtsPlayer()
70 {
71   // If it is playing, stop it
72   Stop();
73
74   // Unset the callback funtion for TTS state change
75   int retVal = tts_unset_state_changed_cb(mTtsHandle);
76   if( retVal != TTS_ERROR_NONE )
77   {
78     LogErrorCode(static_cast<tts_error_e>(retVal));
79   }
80
81   // Destroy the TTS handle and disconnects the daemon
82   retVal = tts_destroy(mTtsHandle);
83   if( retVal != TTS_ERROR_NONE )
84   {
85     LogErrorCode(static_cast<tts_error_e>(retVal));
86   }
87 }
88
89 void TtsPlayer::Initialize()
90 {
91   // Create the TTS handle
92   int retVal = tts_create(&mTtsHandle);
93
94   if( retVal != TTS_ERROR_NONE )
95   {
96     LogErrorCode(static_cast<tts_error_e>(retVal));
97   }
98   else
99   {
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 )
103     {
104       LogErrorCode(static_cast<tts_error_e>(retVal));
105     }
106
107     // Check tts mode
108     tts_mode_e ttsMode = TTS_MODE_DEFAULT;
109     switch (mTtsMode)
110     {
111       case Dali::TtsPlayer::DEFAULT:
112         ttsMode = TTS_MODE_DEFAULT;
113       break;
114       case Dali::TtsPlayer::NOTIFICATION:
115         ttsMode = TTS_MODE_NOTIFICATION;
116       break;
117       case Dali::TtsPlayer::SCREEN_READER:
118         ttsMode = TTS_MODE_SCREEN_READER;
119       break;
120       default:
121       break;
122     }
123
124     // Set mode
125     retVal = tts_set_mode(mTtsHandle, ttsMode);
126     if(retVal != TTS_ERROR_NONE)
127     {
128       LogErrorCode(static_cast<tts_error_e>(retVal));
129     }
130
131     // Connect the TTS daemon asynchronously
132     retVal = tts_prepare(mTtsHandle);
133     if(retVal != TTS_ERROR_NONE)
134     {
135       LogErrorCode(static_cast<tts_error_e>(retVal));
136     }
137   }
138 }
139
140 void TtsPlayer::Play(const std::string& text)
141 {
142   if(mInitialized)
143   {
144     Stop();
145
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)
149     {
150       LogErrorCode(static_cast<tts_error_e>(retVal));
151     }
152     else
153     {
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)
157       {
158         LogErrorCode(static_cast<tts_error_e>(retVal));
159       }
160     }
161   }
162   else
163   {
164     mUnplayedString = text;
165   }
166 }
167
168 void TtsPlayer::Stop()
169 {
170   if(mInitialized)
171   {
172     // Check the current TTS state
173     tts_state_e state;
174     int retVal = tts_get_state(mTtsHandle, &state);
175     if(retVal != TTS_ERROR_NONE)
176     {
177       LogErrorCode(static_cast<tts_error_e>(retVal));
178     }
179     else if(state == TTS_STATE_PLAYING || state == TTS_STATE_PAUSED)
180     {
181       // If it is playing or paused, stop playing and clear the queue
182       retVal = tts_stop(mTtsHandle);
183       if( retVal != TTS_ERROR_NONE )
184       {
185         LogErrorCode(static_cast<tts_error_e>(retVal));
186       }
187     }
188   }
189 }
190
191 void TtsPlayer::Pause()
192 {
193   if(mInitialized)
194   {
195     // Check the current TTS state
196     tts_state_e state;
197     int retVal = tts_get_state(mTtsHandle, &state);
198     if(retVal != TTS_ERROR_NONE)
199     {
200       LogErrorCode(static_cast<tts_error_e>(retVal));
201     }
202     else if(state == TTS_STATE_PLAYING)
203     {
204       // If the player is playing, pause it.
205       retVal = tts_pause(mTtsHandle);
206       if( retVal != TTS_ERROR_NONE )
207       {
208         LogErrorCode(static_cast<tts_error_e>(retVal));
209       }
210     }
211   }
212 }
213
214 void TtsPlayer::Resume()
215 {
216   if(mInitialized)
217   {
218     // Check the current TTS state
219     tts_state_e state;
220     int retVal = tts_get_state(mTtsHandle, &state);
221     if(retVal != TTS_ERROR_NONE)
222     {
223       LogErrorCode(static_cast<tts_error_e>(retVal));
224     }
225     else if(state == TTS_STATE_PAUSED)
226     {
227       // If the player is paused, resume it.
228       retVal = tts_play(mTtsHandle);
229       if( retVal != TTS_ERROR_NONE )
230       {
231         LogErrorCode(static_cast<tts_error_e>(retVal));
232       }
233     }
234   }
235 }
236
237 void TtsPlayer::StateChangedCallback(tts_h tts, tts_state_e previous, tts_state_e current, void *userData)
238 {
239   TtsPlayer* obj = static_cast<TtsPlayer*>(userData);
240   if(!obj->mInitialized && current == TTS_STATE_READY)
241   {
242     obj->mInitialized = true;
243
244     // if there is queued text before initialization, play it
245     if(obj->mUnplayedString != "")
246     {
247       obj->Play(obj->mUnplayedString);
248       obj->mUnplayedString = "";
249     }
250   }
251 }
252
253 void TtsPlayer::LogErrorCode(tts_error_e reason)
254 {
255   std::string error_string;
256
257   switch (reason)
258   {
259     case TTS_ERROR_NONE:
260     {
261       break;
262     }
263     case TTS_ERROR_OUT_OF_MEMORY:
264     {
265       error_string = "TTS: Out of Memory\n";
266       break;
267     }
268     case TTS_ERROR_IO_ERROR:
269     {
270       error_string = "TTS: I/O error\n";
271       break;
272     }
273     case TTS_ERROR_INVALID_PARAMETER:
274     {
275       error_string = "TTS: Invalid parameter\n";
276       break;
277     }
278     case TTS_ERROR_OUT_OF_NETWORK:
279     {
280       error_string = "TTS: Out of network\n";
281       break;
282     }
283     case TTS_ERROR_INVALID_STATE:
284     {
285       error_string = "TTS: Invalid state\n";
286       break;
287     }
288     case TTS_ERROR_INVALID_VOICE:
289     {
290       error_string = "TTS: Invalid voice\n";
291       break;
292     }
293     case TTS_ERROR_ENGINE_NOT_FOUND:
294     {
295       error_string = "TTS: No available engine\n";
296       break;
297     }
298     case TTS_ERROR_TIMED_OUT:
299     {
300       error_string = "TTS: No answer from the daemon\n";
301       break;
302     }
303     case TTS_ERROR_OPERATION_FAILED:
304     {
305       error_string = "TTS: Operation failed\n";
306       break;
307     }
308     default:
309     {
310       error_string = "Invalid TTS error code\n";
311       break;
312     }
313   }
314
315   if(reason != TTS_ERROR_NONE)
316   {
317     DALI_LOG_WARNING("[%s:%d] tts error : %s\n", __FUNCTION__, __LINE__, error_string.c_str());
318   }
319 }
320
321 } // namespace Adaptor
322
323 } // namespace Internal
324
325 } // namespace Dali