[NUI] Change all CallingConvention to `Cdecl`
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Utility / 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.Cdecl)]
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 StateChangedSignalType StateChangedSignal()
224         {
225             StateChangedSignalType ret = new StateChangedSignalType(Interop.TtsPlayer.StateChangedSignal(SwigCPtr), false);
226             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
227             return ret;
228         }
229
230         internal TTSPlayer Assign(TTSPlayer rhs)
231         {
232             TTSPlayer ret = new TTSPlayer(Interop.TtsPlayer.Assign(SwigCPtr, TTSPlayer.getCPtr(rhs)), false);
233             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
234             return ret;
235         }
236
237         private void OnStateChanged(TTSState prevState, TTSState nextState)
238         {
239             if (stateChangedEventHandler != null)
240             {
241                 StateChangedEventArgs e = new StateChangedEventArgs();
242
243                 e.PrevState = prevState;
244                 e.NextState = nextState;
245                 stateChangedEventHandler(this, e);
246             }
247         }
248
249         /// <summary>
250         /// State changed argument.
251         /// </summary>
252         /// <since_tizen> 3 </since_tizen>
253         public class StateChangedEventArgs : EventArgs
254         {
255             /// <summary>
256             /// PrevState.
257             /// </summary>
258             /// <since_tizen> 3 </since_tizen>
259             public TTSState PrevState
260             {
261                 get;
262                 set;
263             }
264
265             /// <summary>
266             /// NextState.
267             /// </summary>
268             /// <since_tizen> 3 </since_tizen>
269             public TTSState NextState
270             {
271                 get;
272                 set;
273             }
274         }
275     }
276 }