5 using System.Runtime.InteropServices;
9 /* these are the values used by alsa */
50 static AudioDevice TryAlsa (string name) {
53 dev = new AlsaDevice (name);
60 public static AudioDevice CreateDevice (string name) {
64 /* if no option is found, return a silent device */
66 dev = new AudioDevice ();
70 public virtual bool SetFormat (AudioFormat format, int channels, int rate) {
74 public virtual int PlaySample (byte[] buffer, int num_frames) {
78 public virtual void Wait () {
82 class AlsaDevice: AudioDevice, IDisposable {
85 [DllImport ("libasound.so.2")]
86 static extern int snd_pcm_open (ref IntPtr handle, string pcm_name, int stream, int mode);
88 [DllImport ("libasound.so.2")]
89 static extern int snd_pcm_close (IntPtr handle);
91 [DllImport ("libasound.so.2")]
92 static extern int snd_pcm_drain (IntPtr handle);
94 [DllImport ("libasound.so.2")]
95 static extern int snd_pcm_writei (IntPtr handle, byte[] buf, int size);
97 [DllImport ("libasound.so.2")]
98 static extern int snd_pcm_set_params (IntPtr handle, int format, int access, int channels, int rate, int soft_resample, int latency);
100 public AlsaDevice (string name) {
103 int err = snd_pcm_open (ref handle, name, 0, 0);
105 throw new Exception ("no open " + err);
112 public void Dispose () {
114 GC.SuppressFinalize (this);
117 protected virtual void Dispose (bool disposing) {
121 if (handle != IntPtr.Zero)
122 snd_pcm_close (handle);
123 handle = IntPtr.Zero;
126 public override bool SetFormat (AudioFormat format, int channels, int rate) {
127 int err = snd_pcm_set_params (handle, (int)format, 3, channels, rate, 1, 500000);
131 public override int PlaySample (byte[] buffer, int num_frames) {
132 int frames = snd_pcm_writei (handle, buffer, num_frames);
136 public override void Wait () {
137 snd_pcm_drain (handle);