2 using System.Runtime.InteropServices;
\r
4 namespace Tizen.Multimedia
\r
7 /// The Audio Output class provides a set of functions to directly manage the system audio output devices.
\r
9 public class AudioOutput : BaseAudio
\r
12 /// Gets the sample rate of the audio output data stream.
\r
14 public override int SampleRate
\r
19 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetSampleRate(_handle, out sampleRate));
\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
27 public override AudioChannel Channel
\r
32 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetChannel(_handle, out channel));
\r
33 return (AudioChannel)channel;
\r
37 /// Gets the sample audio format (8-bit or 16-bit) of the audio output data stream.
\r
39 public override AudioSampleType SampleType
\r
44 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetSampleType(_handle, out type));
\r
45 return (AudioSampleType)type;
\r
49 /// Gets the size to be allocated for the audio output buffer.
\r
51 public override int BufferSize
\r
56 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetBufferSize(_handle, out size));
\r
61 /// Gets the sound type supported by the audio output device.
\r
63 public AudioStreamType SoundStreamType
\r
68 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.GetSoundType(_handle, out audioType));
\r
69 return (AudioStreamType)audioType;
\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
79 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Drain(_handle));
\r
81 object _lock = new object();
\r
83 /// Starts writing the audio data to the device.
\r
85 /// <param name="buffer"></param>
\r
86 public void Write(byte[] buffer)
\r
88 if (buffer.Length <= 0)
\r
90 throw new IndexOutOfRangeException("Bed File Length");
\r
92 IntPtr ptrByteArray = Marshal.AllocHGlobal(buffer.Length);
\r
95 Marshal.Copy(buffer, 0, ptrByteArray, buffer.Length);
\r
96 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Write(_handle, ptrByteArray, (uint)buffer.Length));
\r
100 if (ptrByteArray != IntPtr.Zero)
\r
102 Marshal.FreeHGlobal(ptrByteArray);
\r
103 ptrByteArray = IntPtr.Zero;
\r
108 /// Sets an asynchronous(event) callback function to handle playing PCM (pulse-code modulation) data.
\r
110 protected override void RegisterAudioStreamLengthChanged()
\r
112 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.SetAudioOutputStreamChangedCallback(_handle, OnStream, IntPtr.Zero));
\r
115 /// Unregisters the callback function.
\r
117 protected override void UnregisterAudioStreamLengthChanged()
\r
119 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.UnsetAudioOutputStreamChangedCallback(_handle));
\r
122 /// Sets the state changed callback function to the audio output handle.
\r
124 protected override void RegisterAudioStateChangedCallback()
\r
126 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.SetAudioOutputStateChangedCallback(_handle, OnStateChanged, IntPtr.Zero));
\r
129 /// Unregisters the state changed callback function of the audio output handle.
\r
131 protected override void UnregisterAudioStateChangedmCallback()
\r
133 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.UnsetAudioOutputStateChangedCallback(_handle));
\r
136 /// Releases the audio output handle, along with all its resources.
\r
138 protected override void Destroy()
\r
140 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Destroy(_handle));
\r
143 /// Prepares the audio output for playback, this must be called before audio_out_write().
\r
145 public override void Prepare()
\r
147 if (_bReady == false)
\r
149 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Prepare(_handle));
\r
154 /// Unprepares the audio output.
\r
156 public override void Unprepare()
\r
160 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Unprepare(_handle));
\r
165 /// Pauses feeding of audio data to the device.
\r
167 public override void Pause()
\r
169 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Pause(_handle));
\r
173 /// Resumes feeding of audio data to the device.
\r
175 public override void Resume()
\r
177 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Resume(_handle));
\r
180 /// Flushes and discards buffered audio data from the output stream.
\r
182 public override void Flush()
\r
184 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Flush(_handle));
\r
187 /// Sets the sound stream information to the audio output.
\r
189 /// <param name="stream_info"></param>
\r
190 /// <returns></returns>
\r
191 public override void SetStreamInfo(AudioStreamPolicy streamPolicy)
\r
193 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.SetStreamInfo(_handle, streamPolicy.Handle));
\r
196 /// Initializes the instance of the AudioOutput class with the SafeAudioInputHandle.
\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
203 AudioErrorHelper.Try(Interop.AudioIO.AudioOutput.Create(sample_rate, (int)channel, (int)type, out _handle));
\r
205 private AudioOutput() { }
\r