]> git.saurik.com Git - wxWidgets.git/blame - src/msw/sound.cpp
Unicode build fix
[wxWidgets.git] / src / msw / sound.cpp
CommitLineData
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
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
ddc5c471 21 #pragma implementation "sound.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
ddc5c471 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
315ebf68 31#if wxUSE_SOUND
1e6feb95 32
315ebf68 33#include "wx/sound.h"
ee4f8c2a 34#include "wx/msw/private.h"
2bda0e17 35
2cef71bc 36#include <mmsystem.h>
2bda0e17 37
ddc5c471
VZ
38// ----------------------------------------------------------------------------
39// wxSoundData
40// ----------------------------------------------------------------------------
41
42// ABC for different sound data representations
43class wxSoundData
2bda0e17 44{
ddc5c471
VZ
45public:
46 wxSoundData() { }
2bda0e17 47
ddc5c471
VZ
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
61class wxSoundDataMemory : public wxSoundData
2bda0e17 62{
ddc5c471
VZ
63public:
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(); }
2bda0e17 72
ddc5c471
VZ
73private:
74 GlobalPtr m_waveData;
75 GlobalPtrLock m_waveDataPtr;
76
77 DECLARE_NO_COPY_CLASS(wxSoundDataMemory)
78};
79
80// class for sound files and resources
81class wxSoundDataFile : public wxSoundData
82{
83public:
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
93private:
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
108wxSoundDataMemory::wxSoundDataMemory(int size, const wxByte *buf)
109 : m_waveData(size),
110 m_waveDataPtr(m_waveData)
321db4b6 111{
ddc5c471
VZ
112 if ( IsOk() )
113 ::CopyMemory(m_waveDataPtr, buf, size);
321db4b6 114}
2bda0e17 115
ddc5c471
VZ
116wxSoundDataFile::wxSoundDataFile(const wxString& filename, bool isResource)
117 : m_name(filename),
118 m_isResource(isResource)
2bda0e17 119{
ddc5c471 120 // check for file/resource existence?
2bda0e17
KB
121}
122
ddc5c471
VZ
123// ----------------------------------------------------------------------------
124// wxSound
125// ----------------------------------------------------------------------------
126
127wxSound::wxSound()
2bda0e17 128{
ddc5c471
VZ
129 Init();
130}
2bda0e17 131
ddc5c471
VZ
132wxSound::wxSound(const wxString& filename, bool isResource)
133{
134 Init();
135 Create(filename, isResource);
136}
2bda0e17 137
ddc5c471
VZ
138wxSound::wxSound(int size, const wxByte *data)
139{
140 Init();
141 Create(size, data);
142}
2bda0e17 143
ddc5c471
VZ
144wxSound::~wxSound()
145{
146 Free();
147}
2bda0e17 148
ddc5c471
VZ
149void wxSound::Free()
150{
151 if ( m_data )
2bda0e17 152 {
ddc5c471
VZ
153 delete m_data;
154 m_data = NULL;
2bda0e17 155 }
ddc5c471 156}
2bda0e17 157
ddc5c471
VZ
158bool wxSound::CheckCreatedOk()
159{
160 if ( m_data && !m_data->IsOk() )
161 Free();
2bda0e17 162
ddc5c471
VZ
163 return m_data != NULL;
164}
2bda0e17 165
ddc5c471
VZ
166bool wxSound::Create(const wxString& filename, bool isResource)
167{
168 Free();
2bda0e17 169
ddc5c471 170 m_data = new wxSoundDataFile(filename, isResource);
2bda0e17 171
ddc5c471 172 return CheckCreatedOk();
2bda0e17
KB
173}
174
315ebf68 175bool wxSound::Create(int size, const wxByte* data)
321db4b6 176{
ddc5c471
VZ
177 Free();
178
179 m_data = new wxSoundDataMemory(size, data);
180
181 return CheckCreatedOk();
321db4b6
JS
182}
183
315ebf68 184bool wxSound::DoPlay(unsigned flags) const
2bda0e17 185{
ddc5c471
VZ
186 if ( !IsOk() || !m_data->IsOk() )
187 return false;
2bda0e17 188
ddc5c471
VZ
189 DWORD flagsMSW = m_data->GetSoundFlag();
190 HMODULE hmod = flagsMSW == SND_RESOURCE ? wxGetInstance() : NULL;
2bda0e17 191
ddc5c471
VZ
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;
2bda0e17 201 }
ddc5c471
VZ
202
203 return ::PlaySound(m_data->GetSoundData(), hmod, flagsMSW) != FALSE;
315ebf68
VS
204}
205
ddc5c471
VZ
206/* static */
207void wxSound::Stop()
315ebf68
VS
208{
209 ::PlaySound(NULL, NULL, 0);
2bda0e17
KB
210}
211
315ebf68 212#endif // wxUSE_SOUND
ddc5c471 213