1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: 2005-07-29: Vadim Zeitlin: redesign
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
30 #include "wx/msw/private.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // ABC for different sound data representations
44 // return true if we had been successfully initialized
45 virtual bool IsOk() const = 0;
47 // get the flag corresponding to our content for PlaySound()
48 virtual DWORD
GetSoundFlag() const = 0;
50 // get the data to be passed to PlaySound()
51 virtual LPCTSTR
GetSoundData() const = 0;
53 virtual ~wxSoundData() { }
56 // class for in-memory sound data
57 class wxSoundDataMemory
: public wxSoundData
61 wxSoundDataMemory(int size
, const wxByte
*buf
);
63 void *GetPtr() const { return m_waveDataPtr
; }
65 virtual bool IsOk() const { return GetPtr() != NULL
; }
66 virtual DWORD
GetSoundFlag() const { return SND_MEMORY
; }
67 virtual LPCTSTR
GetSoundData() const { return (LPCTSTR
)GetPtr(); }
71 GlobalPtrLock m_waveDataPtr
;
73 DECLARE_NO_COPY_CLASS(wxSoundDataMemory
)
76 // class for sound files and resources
77 class wxSoundDataFile
: public wxSoundData
80 wxSoundDataFile(const wxString
& filename
, bool isResource
);
82 virtual bool IsOk() const { return !m_name
.empty(); }
83 virtual DWORD
GetSoundFlag() const
85 return m_isResource
? SND_RESOURCE
: SND_FILENAME
;
87 virtual LPCTSTR
GetSoundData() const { return m_name
.c_str(); }
90 const wxString m_name
;
91 const bool m_isResource
;
93 DECLARE_NO_COPY_CLASS(wxSoundDataFile
)
96 // ============================================================================
98 // ============================================================================
100 // ----------------------------------------------------------------------------
101 // wxSoundData-derived classes
102 // ----------------------------------------------------------------------------
104 wxSoundDataMemory::wxSoundDataMemory(int size
, const wxByte
*buf
)
106 m_waveDataPtr(m_waveData
)
109 ::CopyMemory(m_waveDataPtr
, buf
, size
);
112 wxSoundDataFile::wxSoundDataFile(const wxString
& filename
, bool isResource
)
114 m_isResource(isResource
)
116 // check for file/resource existence?
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
128 wxSound::wxSound(const wxString
& filename
, bool isResource
)
131 Create(filename
, isResource
);
134 wxSound::wxSound(int size
, const wxByte
*data
)
154 bool wxSound::CheckCreatedOk()
156 if ( m_data
&& !m_data
->IsOk() )
159 return m_data
!= NULL
;
162 bool wxSound::Create(const wxString
& filename
, bool isResource
)
166 m_data
= new wxSoundDataFile(filename
, isResource
);
168 return CheckCreatedOk();
171 bool wxSound::Create(int size
, const wxByte
* data
)
175 m_data
= new wxSoundDataMemory(size
, data
);
177 return CheckCreatedOk();
180 bool wxSound::DoPlay(unsigned flags
) const
182 if ( !IsOk() || !m_data
->IsOk() )
185 DWORD flagsMSW
= m_data
->GetSoundFlag();
186 HMODULE hmod
= flagsMSW
== SND_RESOURCE
? wxGetInstance() : NULL
;
188 // we don't want replacement default sound
189 flagsMSW
|= SND_NODEFAULT
;
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
)
195 // looping only works with async flag
196 flagsMSW
|= SND_LOOP
| SND_ASYNC
;
199 return ::PlaySound(m_data
->GetSoundData(), hmod
, flagsMSW
) != FALSE
;
205 ::PlaySound(NULL
, NULL
, 0);
208 #endif // wxUSE_SOUND