Merge pull request #463 from strawd/concurrent-requests
[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 using System;
30 using System.IO;
31 using System.Runtime.InteropServices;
32
33 namespace Mono.Audio
34 {
35         internal class Win32SoundPlayer : IDisposable
36         {
37                 public Win32SoundPlayer (Stream s)
38                 {
39                         if (s != null) {
40                                 _buffer = new byte [s.Length];
41                                 s.Read (_buffer, 0, _buffer.Length);
42                         } else {
43                                 _buffer = new byte [0];
44                         }
45                 }
46
47                 [DllImport ("winmm.dll", SetLastError = true)]
48                 static extern bool PlaySound (
49                         byte [] ptrToSound,
50                         UIntPtr hmod,
51                         SoundFlags flags);
52
53                 public Stream Stream {
54                         set {
55                                 Stop ();
56                                 if (value != null) {
57                                         _buffer = new byte [value.Length];
58                                         value.Read (_buffer, 0, _buffer.Length);
59                                 } else {
60                                         _buffer = new byte [0];
61                                 }
62                         }
63                 }
64
65                 public void Dispose ()
66                 {
67                         Dispose (true);
68                         GC.SuppressFinalize (this);
69                 }
70
71                 ~Win32SoundPlayer ()
72                 {
73                         Dispose (false);
74                 }
75
76                 protected virtual void Dispose (bool disposing)
77                 {
78                         if (!_disposed) {
79                                 Stop ();
80                                 _disposed = true;
81                         }
82                 }
83
84                 public void Play ()
85                 {
86                         PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_ASYNC |
87                                 SoundFlags.SND_MEMORY);
88                 }
89
90                 public void PlayLooping ()
91                 {
92                         PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_MEMORY |
93                                 SoundFlags.SND_LOOP | SoundFlags.SND_ASYNC);
94                 }
95
96                 public void PlaySync ()
97                 {
98                         PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_SYNC |
99                                 SoundFlags.SND_MEMORY | SoundFlags.SND_NODEFAULT);
100                 }
101
102                 public void Stop ()
103                 {
104                         PlaySound ((byte []) null, UIntPtr.Zero, SoundFlags.SND_SYNC);
105                 }
106
107                 enum SoundFlags : uint
108                 {
109                         SND_SYNC = 0x0000,              // play synchronously
110                         SND_ASYNC = 0x0001,             // play asynchronously
111                         SND_NODEFAULT = 0x0002,         // do not play default sound if sound not found
112                         SND_MEMORY = 0x0004,            // pszSound is loaded in memory
113                         SND_LOOP = 0x0008,              // play repeatedly until next PlaySound
114                         SND_FILENAME = 0x00020000,      // pszSound is a file name
115                 }
116
117                 private byte [] _buffer;
118                 private bool _disposed;
119         }
120 }
121