]> git.saurik.com Git - wxWidgets.git/blob - src/msw/sound.cpp
Doc fix
[wxWidgets.git] / src / msw / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound.cpp
3 // Purpose: wxSound
4 // Author: Julian Smart
5 // Modified by: 2005-07-29: Vadim Zeitlin: redesign
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "sound.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #if defined(__BORLANDC__)
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_SOUND
32
33 #include "wx/sound.h"
34 #include "wx/msw/private.h"
35
36 #include <mmsystem.h>
37
38 // ----------------------------------------------------------------------------
39 // wxSoundData
40 // ----------------------------------------------------------------------------
41
42 // ABC for different sound data representations
43 class wxSoundData
44 {
45 public:
46 wxSoundData() { }
47
48 // return true if we had been successfully initialized
49 virtual bool IsOk() const = 0;
50
51 // get the flag corresponding to our content for PlaySound()
52 virtual DWORD GetSoundFlag() const = 0;
53
54 // get the data to be passed to PlaySound()
55 virtual LPCTSTR GetSoundData() const = 0;
56
57 virtual ~wxSoundData() { }
58 };
59
60 // class for in-memory sound data
61 class wxSoundDataMemory : public wxSoundData
62 {
63 public:
64 // we copy the data
65 wxSoundDataMemory(int size, const wxByte *buf);
66
67 void *GetPtr() const { return m_waveDataPtr; }
68
69 virtual bool IsOk() const { return GetPtr() != NULL; }
70 virtual DWORD GetSoundFlag() const { return SND_MEMORY; }
71 virtual LPCTSTR GetSoundData() const { return (LPCTSTR)GetPtr(); }
72
73 private:
74 GlobalPtr m_waveData;
75 GlobalPtrLock m_waveDataPtr;
76
77 DECLARE_NO_COPY_CLASS(wxSoundDataMemory)
78 };
79
80 // class for sound files and resources
81 class wxSoundDataFile : public wxSoundData
82 {
83 public:
84 wxSoundDataFile(const wxString& filename, bool isResource);
85
86 virtual bool IsOk() const { return !m_name.empty(); }
87 virtual DWORD GetSoundFlag() const
88 {
89 return m_isResource ? SND_RESOURCE : SND_FILENAME;
90 }
91 virtual LPCTSTR GetSoundData() const { return m_name.c_str(); }
92
93 private:
94 const wxString m_name;
95 const bool m_isResource;
96
97 DECLARE_NO_COPY_CLASS(wxSoundDataFile)
98 };
99
100 // ============================================================================
101 // implementation
102 // ============================================================================
103
104 // ----------------------------------------------------------------------------
105 // wxSoundData-derived classes
106 // ----------------------------------------------------------------------------
107
108 wxSoundDataMemory::wxSoundDataMemory(int size, const wxByte *buf)
109 : m_waveData(size),
110 m_waveDataPtr(m_waveData)
111 {
112 if ( IsOk() )
113 ::CopyMemory(m_waveDataPtr, buf, size);
114 }
115
116 wxSoundDataFile::wxSoundDataFile(const wxString& filename, bool isResource)
117 : m_name(filename),
118 m_isResource(isResource)
119 {
120 // check for file/resource existence?
121 }
122
123 // ----------------------------------------------------------------------------
124 // wxSound
125 // ----------------------------------------------------------------------------
126
127 wxSound::wxSound()
128 {
129 Init();
130 }
131
132 wxSound::wxSound(const wxString& filename, bool isResource)
133 {
134 Init();
135 Create(filename, isResource);
136 }
137
138 wxSound::wxSound(int size, const wxByte *data)
139 {
140 Init();
141 Create(size, data);
142 }
143
144 wxSound::~wxSound()
145 {
146 Free();
147 }
148
149 void wxSound::Free()
150 {
151 if ( m_data )
152 {
153 delete m_data;
154 m_data = NULL;
155 }
156 }
157
158 bool wxSound::CheckCreatedOk()
159 {
160 if ( m_data && !m_data->IsOk() )
161 Free();
162
163 return m_data != NULL;
164 }
165
166 bool wxSound::Create(const wxString& filename, bool isResource)
167 {
168 Free();
169
170 m_data = new wxSoundDataFile(filename, isResource);
171
172 return CheckCreatedOk();
173 }
174
175 bool wxSound::Create(int size, const wxByte* data)
176 {
177 Free();
178
179 m_data = new wxSoundDataMemory(size, data);
180
181 return CheckCreatedOk();
182 }
183
184 bool wxSound::DoPlay(unsigned flags) const
185 {
186 if ( !IsOk() || !m_data->IsOk() )
187 return false;
188
189 DWORD flagsMSW = m_data->GetSoundFlag();
190 HMODULE hmod = flagsMSW == SND_RESOURCE ? wxGetInstance() : NULL;
191
192 // we don't want replacement default sound
193 flagsMSW |= SND_NODEFAULT;
194
195 // NB: wxSOUND_SYNC is 0, don't test for it
196 flagsMSW |= (flags & wxSOUND_ASYNC) ? SND_ASYNC : SND_SYNC;
197 if ( flags & wxSOUND_LOOP )
198 {
199 // looping only works with async flag
200 flagsMSW |= SND_LOOP | SND_ASYNC;
201 }
202
203 return ::PlaySound(m_data->GetSoundData(), hmod, flagsMSW) != FALSE;
204 }
205
206 /* static */
207 void wxSound::Stop()
208 {
209 ::PlaySound(NULL, NULL, 0);
210 }
211
212 #endif // wxUSE_SOUND
213