Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / System / System.Media / AudioDevice.cs
1
2 using System;
3 using System.IO;
4 using System.Runtime.InteropServices;
5
6 namespace Mono.Audio {
7
8         /* these are the values used by alsa */
9 #if PUBLIC_API
10         public
11 #else
12         internal
13 #endif
14         enum AudioFormat {
15                 S8,
16                 U8,
17                 S16_LE,
18                 S16_BE,
19                 U16_LE,
20                 U16_BE,
21                 S24_LE,
22                 S24_BE,
23                 U24_LE,
24                 U24_BE,
25                 S32_LE,
26                 S32_BE,
27                 U32_LE,
28                 U32_BE,
29                 FLOAT_LE,
30                 FLOAT_BE,
31                 FLOAT64_LE,
32                 FLOAT64_BE,
33                 IEC958_SUBFRAME_LE,
34                 IEC958_SUBFRAME_BE,
35                 MU_LAW,
36                 A_LAW,
37                 IMA_ADPCM,
38                 MPEG,
39                 GSM
40         }
41
42 #if PUBLIC_API
43         public
44 #else
45         internal
46 #endif
47         class AudioDevice {
48
49                 static AudioDevice TryAlsa (string name) {
50                         AudioDevice dev;
51                         try {
52                                 dev = new AlsaDevice (name);
53                                 return dev;
54                         } catch {
55                                 return null;
56                         }
57                 }
58
59                 public static AudioDevice CreateDevice (string name) {
60                         AudioDevice dev;
61
62                         dev = TryAlsa (name);
63                         /* if no option is found, return a silent device */
64                         if (dev == null)
65                                 dev = new AudioDevice ();
66                         return dev;
67                 }
68
69                 public virtual bool SetFormat (AudioFormat format, int channels, int rate) {
70                         return true;
71                 }
72
73                 public virtual int PlaySample (byte[] buffer, int num_frames) {
74                         return num_frames;
75                 }
76                 
77                 public virtual void Wait () {
78                 }
79         }
80
81         class AlsaDevice: AudioDevice, IDisposable {
82                 IntPtr handle;
83
84                 [DllImport ("libasound.so.2")]
85                 static extern int snd_pcm_open (ref IntPtr handle, string pcm_name, int stream, int mode);
86
87                 [DllImport ("libasound.so.2")]
88                 static extern int snd_pcm_close (IntPtr handle);
89
90                 [DllImport ("libasound.so.2")]
91                 static extern int snd_pcm_drain (IntPtr handle);
92
93                 [DllImport ("libasound.so.2")]
94                 static extern int snd_pcm_writei (IntPtr handle, byte[] buf, int size);
95
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);
98
99                 public AlsaDevice (string name) {
100                         if (name == null)
101                                 name = "default";
102                         int err = snd_pcm_open (ref handle, name, 0, 0);
103                         if (err < 0)
104                                 throw new Exception ("no open " + err);
105                 }
106
107                 ~AlsaDevice () {
108                         Dispose (false);
109                 }
110
111                 public void Dispose () {
112                         Dispose (true);
113                         GC.SuppressFinalize (this);
114                 }
115
116                 protected virtual void Dispose (bool disposing) {
117                         if (disposing) {
118                                 
119                         }
120                         if (handle != IntPtr.Zero)
121                                 snd_pcm_close (handle);
122                         handle = IntPtr.Zero;
123                 }
124
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);
127                         return err == 0;
128                 }
129
130                 public override int PlaySample (byte[] buffer, int num_frames) {
131                         int frames = snd_pcm_writei (handle, buffer, num_frames);
132                         return frames;
133                 }
134                 
135                 public override void Wait () {
136                         snd_pcm_drain (handle);
137                 }
138         }
139
140 }
141