[AudioIO] Fixed AudioIOEnumerations file
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / AudioIO / AudioOutput.cs
1 using System;\r
2 using System.Runtime.InteropServices;\r
3 \r
4 namespace Tizen.Multimedia\r
5 {\r
6     /// <summary>\r
7     /// The Audio Output class provides a set of functions to directly manage the system audio output devices. \r
8     /// </summary>\r
9     public class AudioOutput : BaseAudio\r
10     {\r
11         /// <summary>\r
12         /// Gets the sample rate of the audio output data stream. \r
13         /// </summary>\r
14         public override int SampleRate\r
15         {\r
16             get\r
17             {\r
18                 int sampleRate;\r
19                 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetSampleRate(_handle, out sampleRate));\r
20                 return sampleRate;\r
21             }\r
22         }\r
23         /// <summary>\r
24         /// Gets the channel type of the audio output data stream. \r
25         /// The audio channel type defines whether the audio is mono or stereo.\r
26         /// </summary>\r
27         public override AudioChannel Channel\r
28         {\r
29             get\r
30             {\r
31                 int channel;\r
32                 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetChannel(_handle, out channel));\r
33                 return (AudioChannel)channel;\r
34             }\r
35         }\r
36         /// <summary>\r
37         /// Gets the sample audio format (8-bit or 16-bit) of the audio output data stream. \r
38         /// </summary>\r
39         public override AudioSampleType SampleType\r
40         {\r
41             get\r
42             {\r
43                 int type;\r
44                 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetSampleType(_handle, out type));\r
45                 return (AudioSampleType)type;\r
46             }\r
47         }\r
48         /// <summary>\r
49         /// Gets the size to be allocated for the audio output buffer. \r
50         /// </summary>\r
51         public override int BufferSize\r
52         {\r
53             get\r
54             {\r
55                 int size = 0;\r
56                 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetBufferSize(_handle, out size));\r
57                 return size;\r
58             }\r
59         }\r
60         /// <summary>\r
61         /// Gets the sound type supported by the audio output device. \r
62         /// </summary>\r
63         public AudioStreamType SoundStreamType\r
64         {\r
65             get\r
66             {\r
67                 int audioType = 0;\r
68                 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetSoundType(_handle, out audioType));\r
69                 return (AudioStreamType)audioType;\r
70             }\r
71         }\r
72 \r
73         /// <summary>\r
74         /// Drains buffered audio data from the output stream. \r
75         /// This function waits until drains stream buffer completely. (e.g end of playback)\r
76         /// </summary>\r
77         public void Drain()\r
78         {\r
79             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Drain(_handle));\r
80         }\r
81         object _lock = new object();\r
82         /// <summary>\r
83         /// Starts writing the audio data to the device. \r
84         /// </summary>\r
85         /// <param name="buffer"></param>\r
86         public void Write(byte[] buffer)\r
87         {\r
88             if (buffer.Length <= 0)\r
89             {\r
90                 throw new IndexOutOfRangeException("Bed File Length");\r
91             }\r
92             IntPtr ptrByteArray = Marshal.AllocHGlobal(buffer.Length);\r
93             try\r
94             {\r
95                 Marshal.Copy(buffer, 0, ptrByteArray, buffer.Length);\r
96                 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Write(_handle, ptrByteArray, (uint)buffer.Length));\r
97             }\r
98             finally\r
99             {\r
100                 if (ptrByteArray != IntPtr.Zero)\r
101                 {\r
102                     Marshal.FreeHGlobal(ptrByteArray);\r
103                     ptrByteArray = IntPtr.Zero;\r
104                 }\r
105             }\r
106         }\r
107         /// <summary>\r
108         /// Sets an asynchronous(event) callback function to handle playing PCM (pulse-code modulation) data. \r
109         /// </summary>\r
110         protected override void RegisterAudioStreamLengthChanged()\r
111         {\r
112             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.SetAudioOutputStreamChangedCallback(_handle, OnStream, IntPtr.Zero));\r
113         }\r
114         /// <summary>\r
115         /// Unregisters the callback function. \r
116         /// </summary>\r
117         protected override void UnregisterAudioStreamLengthChanged()\r
118         {\r
119             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.UnsetAudioOutputStreamChangedCallback(_handle));\r
120         }\r
121         /// <summary>\r
122         /// Sets the state changed callback function to the audio output handle. \r
123         /// </summary>\r
124         protected override void RegisterAudioStateChangedCallback()\r
125         {\r
126             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.SetAudioOutputStateChangedCallback(_handle, OnStateChanged, IntPtr.Zero));\r
127         }\r
128         /// <summary>\r
129         /// Unregisters the state changed callback function of the audio output handle. \r
130         /// </summary>\r
131         protected override void UnregisterAudioStateChangedmCallback()\r
132         {\r
133             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.UnsetAudioOutputStateChangedCallback(_handle));\r
134         }\r
135         /// <summary>\r
136         /// Releases the audio output handle, along with all its resources. \r
137         /// </summary>\r
138         protected override void Destroy()\r
139         {\r
140             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Destroy(_handle));\r
141         }\r
142         /// <summary>\r
143         /// Prepares the audio output for playback, this must be called before audio_out_write(). \r
144         /// </summary>\r
145         public override void Prepare()\r
146         {\r
147             if (_bReady == false)\r
148             {\r
149                 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Prepare(_handle));\r
150                 _bReady = true;\r
151             }\r
152         }\r
153         /// <summary>\r
154         /// Unprepares the audio output. \r
155         /// </summary>\r
156         public override void Unprepare()\r
157         {\r
158             if (_bReady)\r
159             {\r
160                 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Unprepare(_handle));\r
161                 _bReady = false;\r
162             }\r
163         }\r
164         /// <summary>\r
165         /// Pauses feeding of audio data to the device. \r
166         /// </summary>\r
167         public override void Pause()\r
168         {\r
169             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Pause(_handle));\r
170 \r
171         }\r
172         /// <summary>\r
173         /// Resumes feeding of audio data to the device. \r
174         /// </summary>\r
175         public override void Resume()\r
176         {\r
177             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Resume(_handle));\r
178         }\r
179         /// <summary>\r
180         /// Flushes and discards buffered audio data from the output stream. \r
181         /// </summary>\r
182         public override void Flush()\r
183         {\r
184             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Flush(_handle));\r
185         }\r
186         /// <summary>\r
187         /// Sets the sound stream information to the audio output. \r
188         /// </summary>\r
189         /// <param name="stream_info"></param>\r
190         /// <returns></returns>\r
191         public override void SetStreamInfo(AudioStreamPolicy streamPolicy)\r
192         {\r
193             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.SetStreamInfo(_handle, streamPolicy.Handle));\r
194         }\r
195         /// <summary>\r
196         /// Initializes the instance of the AudioOutput class with the SafeAudioInputHandle.\r
197         /// </summary>\r
198         /// <param name="_sample_rate"></param>\r
199         /// <param name="_channel"></param>\r
200         /// <param name="_type"></param>\r
201         public AudioOutput(int sample_rate, AudioChannel channel, AudioSampleType type)\r
202         {\r
203             AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Create(sample_rate, (int)channel, (int)type, out _handle));\r
204         }\r
205         private AudioOutput() { }\r
206     }\r
207 }\r