251f8796289ee855be1d0ea6cb72b2f1f12a7bfd
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / TTSPlayer.cs
1 /*
2  * Copyright(c) 2017 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 using System;
19 using System.Runtime.InteropServices;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// The Text-to-speech (TTS) player.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class TTSPlayer : BaseHandle
29     {
30         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
31
32         internal TTSPlayer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicManualPINVOKE.TtsPlayer_SWIGUpcast(cPtr), cMemoryOwn)
33         {
34             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
35         }
36
37         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(TTSPlayer obj)
38         {
39             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
40         }
41
42         private static readonly TTSPlayer instance = TTSPlayer.Get();
43
44         /// <summary>
45         /// Gets the singleton of the TTSPlayer object.
46         /// </summary>
47         /// <since_tizen> 5 </since_tizen>
48         public static TTSPlayer Instance
49         {
50             get
51             {
52                 return instance;
53             }
54         }
55
56         /// <summary>
57         /// Dispose.
58         /// </summary>
59         /// <param name="type">The dispose type</param>
60         /// <since_tizen> 3 </since_tizen>
61         /// Please DO NOT use! This will be deprecated!
62         /// Dispose() method in Singletone classes (ex: FocusManager, StyleManager, VisualFactory, IMFManager, TtsPlayer, Window) is not required.
63         /// Because it is Sigletone, so it is alive for one thread until the NUI is terminated, so it never be disposed.
64         [EditorBrowsable(EditorBrowsableState.Never)]
65         protected override void Dispose(DisposeTypes type)
66         {
67             if (disposed)
68             {
69                 return;
70             }
71
72             if (type == DisposeTypes.Explicit)
73             {
74                 //Called by User
75                 //Release your own managed resources here.
76                 //You should release all of your own disposable objects here.
77             }
78
79             //Release your own unmanaged resources here.
80             //You should not access any managed member here except static instance.
81             //because the execution order of Finalizes is non-deterministic.
82
83             if (_stateChangedEventCallback != null)
84             {
85                 StateChangedSignal().Disconnect(_stateChangedEventCallback);
86             }
87
88             if (swigCPtr.Handle != global::System.IntPtr.Zero)
89             {
90                 if (swigCMemOwn)
91                 {
92                     swigCMemOwn = false;
93                     NDalicManualPINVOKE.delete_TtsPlayer(swigCPtr);
94                 }
95                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
96             }
97
98             base.Dispose(type);
99         }
100
101         internal TTSPlayer() : this(NDalicManualPINVOKE.new_TtsPlayer__SWIG_0(), true)
102         {
103             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
104         }
105
106         /// <summary>
107         /// Gets the singleton of the TTS player for the given mode.
108         /// </summary>
109         /// <param name="mode"> The mode of TTS player.</param>
110         /// <returns> A handle of the TTS player for the given mode.</returns>
111         /// <since_tizen> 3 </since_tizen>
112         public static TTSPlayer Get(TTSMode mode)
113         {
114             TTSPlayer ret = new TTSPlayer(NDalicManualPINVOKE.TtsPlayer_Get__SWIG_0((int)mode), true);
115             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116             return ret;
117         }
118
119         /// <summary>
120         /// Gets the singleton of the TTS player for the default mode.
121         /// </summary>
122         /// <returns> A handle of the TTS player for the default mode.</returns>
123         /// <since_tizen> 3 </since_tizen>
124         public static TTSPlayer Get()
125         {
126             TTSPlayer ret = new TTSPlayer(NDalicManualPINVOKE.TtsPlayer_Get__SWIG_1(), true);
127             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
128             return ret;
129         }
130
131         internal TTSPlayer(TTSPlayer handle) : this(NDalicManualPINVOKE.new_TtsPlayer__SWIG_1(TTSPlayer.getCPtr(handle)), true)
132         {
133             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
134         }
135
136         internal TTSPlayer Assign(TTSPlayer rhs)
137         {
138             TTSPlayer ret = new TTSPlayer(NDalicManualPINVOKE.TtsPlayer_Assign(swigCPtr, TTSPlayer.getCPtr(rhs)), false);
139             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
140             return ret;
141         }
142
143         /// <summary>
144         /// Starts playing the audio data synthesized from the specified text.
145         /// </summary>
146         /// <param name="text"> The text to play.</param>
147         /// <remarks>The TTS player needs to be initialized.</remarks>
148         /// <since_tizen> 3 </since_tizen>
149         public void Play(string text)
150         {
151             NDalicManualPINVOKE.TtsPlayer_Play(swigCPtr, text);
152             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
153         }
154
155         /// <summary>
156         /// Stops playing the utterance.
157         /// </summary>
158         /// <remarks>The TTS player needs to be initialized.</remarks>
159         /// <since_tizen> 3 </since_tizen>
160         public void Stop()
161         {
162             NDalicManualPINVOKE.TtsPlayer_Stop(swigCPtr);
163             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
164         }
165
166         /// <summary>
167         /// Pauses the currently playing utterance.
168         /// </summary>
169         /// <remarks>The TTS player needs to be initialized.</remarks>
170         /// <since_tizen> 3 </since_tizen>
171         public void Pause()
172         {
173             NDalicManualPINVOKE.TtsPlayer_Pause(swigCPtr);
174             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
175         }
176
177         /// <summary>
178         /// Resumes the previously paused utterance.
179         /// </summary>
180         /// <remarks>The TTS player needs to be initialized.</remarks>
181         /// <since_tizen> 3 </since_tizen>
182         public void Resume()
183         {
184             NDalicManualPINVOKE.TtsPlayer_Resume(swigCPtr);
185             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
186         }
187
188         /// <summary>
189         /// Gets the current state of the player.
190         /// </summary>
191         /// <returns> The current TTS state. </returns>
192         /// <remarks>The TTS player needs to be initialized.</remarks>
193         /// <since_tizen> 3 </since_tizen>
194         public TTSState GetState()
195         {
196             TTSState ret = (TTSState)NDalicManualPINVOKE.TtsPlayer_GetState(swigCPtr);
197             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
198             return ret;
199         }
200
201         /// <summary>
202         /// State changed argument.
203         /// </summary>
204         /// <since_tizen> 3 </since_tizen>
205         public class StateChangedEventArgs : EventArgs
206         {
207             /// <summary>
208             /// PrevState.
209             /// </summary>
210             /// <since_tizen> 3 </since_tizen>
211             public TTSState PrevState
212             {
213                 get;
214                 set;
215             }
216
217             /// <summary>
218             /// NextState.
219             /// </summary>
220             /// <since_tizen> 3 </since_tizen>
221             public TTSState NextState
222             {
223                 get;
224                 set;
225             }
226         }
227
228         private StateChangedEventCallbackType _stateChangedEventCallback;
229         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
230         private delegate void StateChangedEventCallbackType(TTSState prevState, TTSState nextState);
231         private event EventHandler<StateChangedEventArgs> _stateChangedEventHandler;
232
233         /// <summary>
234         /// State changed event.
235         /// </summary>
236         /// <since_tizen> 3 </since_tizen>
237         public event EventHandler<StateChangedEventArgs> StateChanged
238         {
239             add
240             {
241                 if (_stateChangedEventHandler == null)
242                 {
243                     _stateChangedEventCallback = OnStateChanged;
244                     StateChangedSignal().Connect(_stateChangedEventCallback);
245                 }
246
247                 _stateChangedEventHandler += value;
248             }
249             remove
250             {
251                 _stateChangedEventHandler -= value;
252
253                 if (_stateChangedEventHandler == null && StateChangedSignal().Empty() == false && _stateChangedEventCallback != null)
254                 {
255                     StateChangedSignal().Disconnect(_stateChangedEventCallback);
256                 }
257             }
258         }
259
260         private void OnStateChanged(TTSState prevState, TTSState nextState)
261         {
262             StateChangedEventArgs e = new StateChangedEventArgs();
263
264             e.PrevState = prevState;
265             e.NextState = nextState;
266
267             if (_stateChangedEventHandler != null)
268             {
269                 _stateChangedEventHandler(this, e);
270             }
271         }
272
273         internal StateChangedSignalType StateChangedSignal()
274         {
275             StateChangedSignalType ret = new StateChangedSignalType(NDalicManualPINVOKE.TtsPlayer_StateChangedSignal(swigCPtr), false);
276             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
277             return ret;
278         }
279
280         /// <summary>
281         /// Enumeration for the instance of TTS mode.
282         /// </summary>
283         /// <since_tizen> 3 </since_tizen>
284         public enum TTSMode
285         {
286             /// <summary>
287             /// Default mode for normal application.
288             /// </summary>
289             Default = 0,
290             /// <summary>
291             /// Notification mode, such as playing utterance is started or completed.
292             /// </summary>
293             Notification,
294             /// <summary>
295             /// Screen reader mode. <br />
296             /// To help visually impaired users interact with their devices,<br />
297             /// screen reader reads text or graphic elements on the screen using the TTS engine.
298             /// </summary>
299             ScreenReader,
300             /// <summary>
301             /// Number of mode.
302             /// </summary>
303             ModeNum
304         }
305
306         /// <summary>
307         /// Enumeration for the instance of TTS state.
308         /// </summary>
309         /// <since_tizen> 3 </since_tizen>
310         public enum TTSState
311         {
312             /// <summary>
313             /// Player is not available.
314             /// </summary>
315             Unavailable = 0,
316             /// <summary>
317             /// Player is ready to play.
318             /// </summary>
319             Ready,
320             /// <summary>
321             /// Player is playing.
322             /// </summary>
323             Playing,
324             /// <summary>
325             /// Player is paused.
326             /// </summary>
327             Paused
328         }
329
330     }
331
332 }