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