1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/sound.cpp
4 // Author: Julian Smart
5 // Modified by: 2005-07-29: Vadim Zeitlin: redesign
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #if defined(__BORLANDC__)
29 #include "wx/msw/private.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // ABC for different sound data representations
43 // return true if we had been successfully initialized
44 virtual bool IsOk() const = 0;
46 // get the flag corresponding to our content for PlaySound()
47 virtual DWORD
GetSoundFlag() const = 0;
49 // get the data to be passed to PlaySound()
50 virtual LPCTSTR
GetSoundData() const = 0;
52 virtual ~wxSoundData() { }
55 // class for in-memory sound data
56 class wxSoundDataMemory
: public wxSoundData
60 wxSoundDataMemory(size_t size
, const void* buf
);
62 void *GetPtr() const { return m_waveDataPtr
; }
64 virtual bool IsOk() const { return GetPtr() != NULL
; }
65 virtual DWORD
GetSoundFlag() const { return SND_MEMORY
; }
66 virtual LPCTSTR
GetSoundData() const { return (LPCTSTR
)GetPtr(); }
70 GlobalPtrLock m_waveDataPtr
;
72 wxDECLARE_NO_COPY_CLASS(wxSoundDataMemory
);
75 // class for sound files and resources
76 class wxSoundDataFile
: public wxSoundData
79 wxSoundDataFile(const wxString
& filename
, bool isResource
);
81 virtual bool IsOk() const { return !m_name
.empty(); }
82 virtual DWORD
GetSoundFlag() const
84 return m_isResource
? SND_RESOURCE
: SND_FILENAME
;
86 virtual LPCTSTR
GetSoundData() const { return m_name
.c_str(); }
89 const wxString m_name
;
90 const bool m_isResource
;
92 wxDECLARE_NO_COPY_CLASS(wxSoundDataFile
);
95 // ============================================================================
97 // ============================================================================
99 // ----------------------------------------------------------------------------
100 // wxSoundData-derived classes
101 // ----------------------------------------------------------------------------
103 wxSoundDataMemory::wxSoundDataMemory(size_t size
, const void* buf
)
105 m_waveDataPtr(m_waveData
)
108 ::CopyMemory(m_waveDataPtr
, buf
, size
);
111 wxSoundDataFile::wxSoundDataFile(const wxString
& filename
, bool isResource
)
113 m_isResource(isResource
)
115 // check for file/resource existence?
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
127 wxSound::wxSound(const wxString
& filename
, bool isResource
)
130 Create(filename
, isResource
);
133 wxSound::wxSound(size_t size
, const void* data
)
149 bool wxSound::CheckCreatedOk()
151 if ( m_data
&& !m_data
->IsOk() )
154 return m_data
!= NULL
;
157 bool wxSound::Create(const wxString
& filename
, bool isResource
)
161 m_data
= new wxSoundDataFile(filename
, isResource
);
163 return CheckCreatedOk();
166 bool wxSound::Create(size_t size
, const void* data
)
170 m_data
= new wxSoundDataMemory(size
, data
);
172 return CheckCreatedOk();
175 bool wxSound::DoPlay(unsigned flags
) const
177 if ( !IsOk() || !m_data
->IsOk() )
180 DWORD flagsMSW
= m_data
->GetSoundFlag();
181 HMODULE hmod
= flagsMSW
== SND_RESOURCE
? wxGetInstance() : NULL
;
183 // we don't want replacement default sound
184 flagsMSW
|= SND_NODEFAULT
;
186 // NB: wxSOUND_SYNC is 0, don't test for it
187 flagsMSW
|= (flags
& wxSOUND_ASYNC
) ? SND_ASYNC
: SND_SYNC
;
188 if ( flags
& wxSOUND_LOOP
)
190 // looping only works with async flag
191 flagsMSW
|= SND_LOOP
| SND_ASYNC
;
194 return ::PlaySound(m_data
->GetSoundData(), hmod
, flagsMSW
) != FALSE
;
200 ::PlaySound(NULL
, NULL
, 0);
203 #endif // wxUSE_SOUND