1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/sound.cpp
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 wxDECLARE_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 wxDECLARE_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
)
150 bool wxSound::CheckCreatedOk()
152 if ( m_data
&& !m_data
->IsOk() )
155 return m_data
!= NULL
;
158 bool wxSound::Create(const wxString
& filename
, bool isResource
)
162 m_data
= new wxSoundDataFile(filename
, isResource
);
164 return CheckCreatedOk();
167 bool wxSound::Create(int size
, const wxByte
* data
)
171 m_data
= new wxSoundDataMemory(size
, data
);
173 return CheckCreatedOk();
176 bool wxSound::DoPlay(unsigned flags
) const
178 if ( !IsOk() || !m_data
->IsOk() )
181 DWORD flagsMSW
= m_data
->GetSoundFlag();
182 HMODULE hmod
= flagsMSW
== SND_RESOURCE
? wxGetInstance() : NULL
;
184 // we don't want replacement default sound
185 flagsMSW
|= SND_NODEFAULT
;
187 // NB: wxSOUND_SYNC is 0, don't test for it
188 flagsMSW
|= (flags
& wxSOUND_ASYNC
) ? SND_ASYNC
: SND_SYNC
;
189 if ( flags
& wxSOUND_LOOP
)
191 // looping only works with async flag
192 flagsMSW
|= SND_LOOP
| SND_ASYNC
;
195 return ::PlaySound(m_data
->GetSoundData(), hmod
, flagsMSW
) != FALSE
;
201 ::PlaySound(NULL
, NULL
, 0);
204 #endif // wxUSE_SOUND