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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "sound.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
34 #include "wx/msw/private.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // ABC for different sound data representations
48 // return true if we had been successfully initialized
49 virtual bool IsOk() const = 0;
51 // get the flag corresponding to our content for PlaySound()
52 virtual DWORD
GetSoundFlag() const = 0;
54 // get the data to be passed to PlaySound()
55 virtual LPCTSTR
GetSoundData() const = 0;
57 virtual ~wxSoundData() { }
60 // class for in-memory sound data
61 class wxSoundDataMemory
: public wxSoundData
65 wxSoundDataMemory(int size
, const wxByte
*buf
);
67 void *GetPtr() const { return m_waveDataPtr
; }
69 virtual bool IsOk() const { return GetPtr() != NULL
; }
70 virtual DWORD
GetSoundFlag() const { return SND_MEMORY
; }
71 virtual LPCTSTR
GetSoundData() const { return (LPCTSTR
)GetPtr(); }
75 GlobalPtrLock m_waveDataPtr
;
77 DECLARE_NO_COPY_CLASS(wxSoundDataMemory
)
80 // class for sound files and resources
81 class wxSoundDataFile
: public wxSoundData
84 wxSoundDataFile(const wxString
& filename
, bool isResource
);
86 virtual bool IsOk() const { return !m_name
.empty(); }
87 virtual DWORD
GetSoundFlag() const
89 return m_isResource
? SND_RESOURCE
: SND_FILENAME
;
91 virtual LPCTSTR
GetSoundData() const { return m_name
.c_str(); }
94 const wxString m_name
;
95 const bool m_isResource
;
97 DECLARE_NO_COPY_CLASS(wxSoundDataFile
)
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
105 // wxSoundData-derived classes
106 // ----------------------------------------------------------------------------
108 wxSoundDataMemory::wxSoundDataMemory(int size
, const wxByte
*buf
)
110 m_waveDataPtr(m_waveData
)
113 ::CopyMemory(m_waveDataPtr
, buf
, size
);
116 wxSoundDataFile::wxSoundDataFile(const wxString
& filename
, bool isResource
)
118 m_isResource(isResource
)
120 // check for file/resource existence?
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
132 wxSound::wxSound(const wxString
& filename
, bool isResource
)
135 Create(filename
, isResource
);
138 wxSound::wxSound(int size
, const wxByte
*data
)
158 bool wxSound::CheckCreatedOk()
160 if ( m_data
&& !m_data
->IsOk() )
163 return m_data
!= NULL
;
166 bool wxSound::Create(const wxString
& filename
, bool isResource
)
170 m_data
= new wxSoundDataFile(filename
, isResource
);
172 return CheckCreatedOk();
175 bool wxSound::Create(int size
, const wxByte
* data
)
179 m_data
= new wxSoundDataMemory(size
, data
);
181 return CheckCreatedOk();
184 bool wxSound::DoPlay(unsigned flags
) const
186 if ( !IsOk() || !m_data
->IsOk() )
189 DWORD flagsMSW
= m_data
->GetSoundFlag();
190 HMODULE hmod
= flagsMSW
== SND_RESOURCE
? wxGetInstance() : NULL
;
192 // we don't want replacement default sound
193 flagsMSW
|= SND_NODEFAULT
;
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
)
199 // looping only works with async flag
200 flagsMSW
|= SND_LOOP
| SND_ASYNC
;
203 return ::PlaySound(m_data
->GetSoundData(), hmod
, flagsMSW
) != FALSE
;
209 ::PlaySound(NULL
, NULL
, 0);
212 #endif // wxUSE_SOUND