[NUI] TCSACR-226 code change (#1032)
[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 static readonly TTSPlayer instance = TTSPlayer.Get();
31         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
32         private StateChangedEventCallbackType _stateChangedEventCallback;
33
34         internal TTSPlayer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.TtsPlayer.TtsPlayer_SWIGUpcast(cPtr), cMemoryOwn)
35         {
36             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
37         }
38
39         internal TTSPlayer() : this(Interop.TtsPlayer.new_TtsPlayer__SWIG_0(), true)
40         {
41             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
42         }
43
44         internal TTSPlayer(TTSPlayer handle) : this(Interop.TtsPlayer.new_TtsPlayer__SWIG_1(TTSPlayer.getCPtr(handle)), true)
45         {
46             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
47         }
48
49         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
50         private delegate void StateChangedEventCallbackType(TTSState prevState, TTSState nextState);
51         private event EventHandler<StateChangedEventArgs> _stateChangedEventHandler;
52
53         /// <summary>
54         /// State changed event.
55         /// </summary>
56         /// <since_tizen> 3 </since_tizen>
57         public event EventHandler<StateChangedEventArgs> StateChanged
58         {
59             add
60             {
61                 if (_stateChangedEventHandler == null)
62                 {
63                     _stateChangedEventCallback = OnStateChanged;
64                     StateChangedSignal().Connect(_stateChangedEventCallback);
65                 }
66
67                 _stateChangedEventHandler += value;
68             }
69             remove
70             {
71                 _stateChangedEventHandler -= value;
72
73                 if (_stateChangedEventHandler == null && StateChangedSignal().Empty() == false && _stateChangedEventCallback != null)
74                 {
75                     StateChangedSignal().Disconnect(_stateChangedEventCallback);
76                 }
77             }
78         }
79
80         /// <summary>
81         /// Enumeration for the instance of TTS mode.
82         /// </summary>
83         /// <since_tizen> 3 </since_tizen>
84         public enum TTSMode
85         {
86             /// <summary>
87             /// Default mode for normal application.
88             /// </summary>
89             Default = 0,
90             /// <summary>
91             /// Notification mode, such as playing utterance is started or completed.
92             /// </summary>
93             Notification,
94             /// <summary>
95             /// Screen reader mode. <br />
96             /// To help visually impaired users interact with their devices,<br />
97             /// screen reader reads text or graphic elements on the screen using the TTS engine.
98             /// </summary>
99             ScreenReader,
100             /// <summary>
101             /// Number of mode.
102             /// </summary>
103             ModeNum
104         }
105
106         /// <summary>
107         /// Enumeration for the instance of TTS state.
108         /// </summary>
109         /// <since_tizen> 3 </since_tizen>
110         public enum TTSState
111         {
112             /// <summary>
113             /// Player is not available.
114             /// </summary>
115             Unavailable = 0,
116             /// <summary>
117             /// Player is ready to play.
118             /// </summary>
119             Ready,
120             /// <summary>
121             /// Player is playing.
122             /// </summary>
123             Playing,
124             /// <summary>
125             /// Player is paused.
126             /// </summary>
127             Paused
128         }
129
130         /// <summary>
131         /// Gets the singleton of the TTSPlayer object.
132         /// </summary>
133         /// <since_tizen> 5 </since_tizen>
134         public static TTSPlayer Instance
135         {
136             get
137             {
138                 return instance;
139             }
140         }
141
142         /// <summary>
143         /// Gets the singleton of the TTS player for the given mode.
144         /// </summary>
145         /// <param name="mode"> The mode of TTS player.</param>
146         /// <returns> A handle of the TTS player for the given mode.</returns>
147         /// <since_tizen> 3 </since_tizen>
148         public static TTSPlayer Get(TTSMode mode)
149         {
150             TTSPlayer ret = new TTSPlayer(Interop.TtsPlayer.TtsPlayer_Get__SWIG_0((int)mode), true);
151             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
152             return ret;
153         }
154
155         /// <summary>
156         /// Gets the singleton of the TTS player for the default mode.
157         /// </summary>
158         /// <returns> A handle of the TTS player for the default mode.</returns>
159         /// <since_tizen> 3 </since_tizen>
160         public static TTSPlayer Get()
161         {
162             TTSPlayer ret = new TTSPlayer(Interop.TtsPlayer.TtsPlayer_Get__SWIG_1(), true);
163             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
164             return ret;
165         }
166
167         /// <summary>
168         /// Starts playing the audio data synthesized from the specified text.
169         /// </summary>
170         /// <param name="text"> The text to play.</param>
171         /// <remarks>The TTS player needs to be initialized.</remarks>
172         /// <since_tizen> 3 </since_tizen>
173         public void Play(string text)
174         {
175             Interop.TtsPlayer.TtsPlayer_Play(swigCPtr, text);
176             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
177         }
178
179         /// <summary>
180         /// Stops playing the utterance.
181         /// </summary>
182         /// <remarks>The TTS player needs to be initialized.</remarks>
183         /// <since_tizen> 3 </since_tizen>
184         public void Stop()
185         {
186             Interop.TtsPlayer.TtsPlayer_Stop(swigCPtr);
187             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
188         }
189
190         /// <summary>
191         /// Pauses the currently playing utterance.
192         /// </summary>
193         /// <remarks>The TTS player needs to be initialized.</remarks>
194         /// <since_tizen> 3 </since_tizen>
195         public void Pause()
196         {
197             Interop.TtsPlayer.TtsPlayer_Pause(swigCPtr);
198             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
199         }
200
201         /// <summary>
202         /// Resumes the previously paused utterance.
203         /// </summary>
204         /// <remarks>The TTS player needs to be initialized.</remarks>
205         /// <since_tizen> 3 </since_tizen>
206         public void Resume()
207         {
208             Interop.TtsPlayer.TtsPlayer_Resume(swigCPtr);
209             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
210         }
211
212         /// <summary>
213         /// Gets the current state of the player.
214         /// </summary>
215         /// <returns> The current TTS state. </returns>
216         /// <remarks>The TTS player needs to be initialized.</remarks>
217         /// <since_tizen> 3 </since_tizen>
218         public TTSState GetState()
219         {
220             TTSState ret = (TTSState)Interop.TtsPlayer.TtsPlayer_GetState(swigCPtr);
221             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
222             return ret;
223         }
224
225         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(TTSPlayer obj)
226         {
227             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
228         }
229
230         internal StateChangedSignalType StateChangedSignal()
231         {
232             StateChangedSignalType ret = new StateChangedSignalType(Interop.TtsPlayer.TtsPlayer_StateChangedSignal(swigCPtr), false);
233             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
234             return ret;
235         }
236
237         internal TTSPlayer Assign(TTSPlayer rhs)
238         {
239             TTSPlayer ret = new TTSPlayer(Interop.TtsPlayer.TtsPlayer_Assign(swigCPtr, TTSPlayer.getCPtr(rhs)), false);
240             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
241             return ret;
242         }
243
244         private void OnStateChanged(TTSState prevState, TTSState nextState)
245         {
246             StateChangedEventArgs e = new StateChangedEventArgs();
247
248             e.PrevState = prevState;
249             e.NextState = nextState;
250
251             if (_stateChangedEventHandler != null)
252             {
253                 _stateChangedEventHandler(this, e);
254             }
255         }
256
257         /// <summary>
258         /// State changed argument.
259         /// </summary>
260         /// <since_tizen> 3 </since_tizen>
261         public class StateChangedEventArgs : EventArgs
262         {
263             /// <summary>
264             /// PrevState.
265             /// </summary>
266             /// <since_tizen> 3 </since_tizen>
267             public TTSState PrevState
268             {
269                 get;
270                 set;
271             }
272
273             /// <summary>
274             /// NextState.
275             /// </summary>
276             /// <since_tizen> 3 </since_tizen>
277             public TTSState NextState
278             {
279                 get;
280                 set;
281             }
282         }
283     }
284
285 }