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