System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System / System.Media / Win32SoundPlayer.cs
1 //
2 // Mono.Audio.Win32SoundPlayer
3 //
4 // Authors:
5 //    Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (C) 2007 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.IO;
33 using System.Runtime.InteropServices;
34
35 namespace Mono.Audio
36 {
37         internal class Win32SoundPlayer : IDisposable
38         {
39                 public Win32SoundPlayer (Stream s)
40                 {
41                         if (s != null) {
42                                 _buffer = new byte [s.Length];
43                                 s.Read (_buffer, 0, _buffer.Length);
44                         } else {
45                                 _buffer = new byte [0];
46                         }
47                 }
48
49                 [DllImport ("winmm.dll", SetLastError = true)]
50                 static extern bool PlaySound (
51                         byte [] ptrToSound,
52                         UIntPtr hmod,
53                         SoundFlags flags);
54
55                 public Stream Stream {
56                         set {
57                                 Stop ();
58                                 if (value != null) {
59                                         _buffer = new byte [value.Length];
60                                         value.Read (_buffer, 0, _buffer.Length);
61                                 } else {
62                                         _buffer = new byte [0];
63                                 }
64                         }
65                 }
66
67                 public void Dispose ()
68                 {
69                         Dispose (true);
70                         GC.SuppressFinalize (this);
71                 }
72
73                 ~Win32SoundPlayer ()
74                 {
75                         Dispose (false);
76                 }
77
78                 protected virtual void Dispose (bool disposing)
79                 {
80                         if (!_disposed) {
81                                 Stop ();
82                                 _disposed = true;
83                         }
84                 }
85
86                 public void Play ()
87                 {
88                         PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_ASYNC |
89                                 SoundFlags.SND_MEMORY);
90                 }
91
92                 public void PlayLooping ()
93                 {
94                         PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_MEMORY |
95                                 SoundFlags.SND_LOOP | SoundFlags.SND_ASYNC);
96                 }
97
98                 public void PlaySync ()
99                 {
100                         PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_SYNC |
101                                 SoundFlags.SND_MEMORY | SoundFlags.SND_NODEFAULT);
102                 }
103
104                 public void Stop ()
105                 {
106                         PlaySound ((byte []) null, UIntPtr.Zero, SoundFlags.SND_SYNC);
107                 }
108
109                 enum SoundFlags : uint
110                 {
111                         SND_SYNC = 0x0000,              // play synchronously
112                         SND_ASYNC = 0x0001,             // play asynchronously
113                         SND_NODEFAULT = 0x0002,         // do not play default sound if sound not found
114                         SND_MEMORY = 0x0004,            // pszSound is loaded in memory
115                         SND_LOOP = 0x0008,              // play repeatedly until next PlaySound
116                         SND_FILENAME = 0x00020000,      // pszSound is a file name
117                 }
118
119                 private byte [] _buffer;
120                 private bool _disposed;
121         }
122 }
123
124 #endif