4 using System.Runtime.InteropServices;
8 /* these are the values used by alsa */
49 static AudioDevice TryAlsa (string name) {
52 dev = new AlsaDevice (name);
59 public static AudioDevice CreateDevice (string name) {
63 /* if no option is found, return a silent device */
65 dev = new AudioDevice ();
69 public virtual bool SetFormat (AudioFormat format, int channels, int rate) {
73 public virtual int PlaySample (byte[] buffer, int num_frames) {
77 public virtual void Wait () {
81 class AlsaDevice: AudioDevice, IDisposable {
84 [DllImport ("libasound.so.2")]
85 static extern int snd_pcm_open (ref IntPtr handle, string pcm_name, int stream, int mode);
87 [DllImport ("libasound.so.2")]
88 static extern int snd_pcm_close (IntPtr handle);
90 [DllImport ("libasound.so.2")]
91 static extern int snd_pcm_drain (IntPtr handle);
93 [DllImport ("libasound.so.2")]
94 static extern int snd_pcm_writei (IntPtr handle, byte[] buf, int size);
96 [DllImport ("libasound.so.2")]
97 static extern int snd_pcm_set_params (IntPtr handle, int format, int access, int channels, int rate, int soft_resample, int latency);
99 public AlsaDevice (string name) {
102 int err = snd_pcm_open (ref handle, name, 0, 0);
104 throw new Exception ("no open " + err);
111 public void Dispose () {
113 GC.SuppressFinalize (this);
116 protected virtual void Dispose (bool disposing) {
120 if (handle != IntPtr.Zero)
121 snd_pcm_close (handle);
122 handle = IntPtr.Zero;
125 public override bool SetFormat (AudioFormat format, int channels, int rate) {
126 int err = snd_pcm_set_params (handle, (int)format, 3, channels, rate, 1, 500000);
130 public override int PlaySample (byte[] buffer, int num_frames) {
131 int frames = snd_pcm_writei (handle, buffer, num_frames);
135 public override void Wait () {
136 snd_pcm_drain (handle);