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