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