]> git.saurik.com Git - wxWidgets.git/blob - src/msw/wave.cpp
preliminary EXTENDED_RTTI
[wxWidgets.git] / src / msw / wave.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wave.cpp
3 // Purpose: wxWave
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wave.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if defined(__BORLANDC__)
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_WAVE
24
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #include "wx/file.h"
30 #include "wx/msw/wave.h"
31 #include "wx/msw/private.h"
32
33 #include <windowsx.h>
34
35 #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
36 #include "wx/msw/gnuwin32/extra.h"
37 #else
38 #include <mmsystem.h>
39 #endif
40
41 wxWave::wxWave()
42 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
43 {
44 }
45
46 wxWave::wxWave(const wxString& sFileName, bool isResource)
47 : m_waveData(NULL), m_waveLength(0), m_isResource(isResource)
48 {
49 Create(sFileName, isResource);
50 }
51
52 wxWave::wxWave(int size, const wxByte* data)
53 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
54 {
55 Create(size, data);
56 }
57
58 wxWave::~wxWave()
59 {
60 Free();
61 }
62
63 bool wxWave::Create(const wxString& fileName, bool isResource)
64 {
65 Free();
66
67 if (isResource)
68 {
69 m_isResource = TRUE;
70
71 HRSRC hresInfo;
72 #if defined(__WIN32__)
73 #ifdef _UNICODE
74 hresInfo = ::FindResourceW((HMODULE) wxhInstance, fileName, wxT("WAVE"));
75 #else
76 hresInfo = ::FindResourceA((HMODULE) wxhInstance, fileName, wxT("WAVE"));
77 #endif
78 #else
79 hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, wxT("WAVE"));
80 #endif
81 if (!hresInfo)
82 return FALSE;
83
84 HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo);
85
86 if (waveData)
87 {
88 m_waveData= (wxByte*)::LockResource(waveData);
89 m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo);
90 }
91
92 return (m_waveData ? TRUE : FALSE);
93 }
94 else
95 {
96 m_isResource = FALSE;
97
98 wxFile fileWave;
99 if (!fileWave.Open(fileName, wxFile::read))
100 return FALSE;
101
102 m_waveLength = (int) fileWave.Length();
103
104 m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
105 if (!m_waveData)
106 return FALSE;
107
108 fileWave.Read(m_waveData, m_waveLength);
109
110 return TRUE;
111 }
112 }
113
114 bool wxWave::Create(int size, const wxByte* data)
115 {
116 Free();
117 m_isResource = FALSE;
118 m_waveLength=size;
119 m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
120 if (!m_waveData)
121 return FALSE;
122
123 for (int i=0; i<size; i++) m_waveData[i] = data[i];
124 return TRUE;
125 }
126
127 bool wxWave::Play(bool async, bool looped) const
128 {
129 if (!IsOk())
130 return FALSE;
131
132 #ifdef __WIN32__
133 return ( ::PlaySound((LPCTSTR)m_waveData, NULL, SND_MEMORY |
134 SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 );
135 #else
136 return ( ::sndPlaySound((LPCSTR)m_waveData, SND_MEMORY |
137 SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 );
138 #endif
139 }
140
141 bool wxWave::Free()
142 {
143 if (m_waveData)
144 {
145 #ifdef __WXWINCE__
146 HGLOBAL waveData = (HGLOBAL) m_waveData;
147 #elif defined(__WIN32__)
148 HGLOBAL waveData = GlobalHandle(m_waveData);
149 #else
150 HGLOBAL waveData = GlobalPtrHandle(m_waveData);
151 #endif
152
153 if (waveData)
154 {
155 #ifndef __WXWINCE__
156 if (m_isResource)
157 ::FreeResource(waveData);
158 else
159 #endif
160 {
161 GlobalUnlock(waveData);
162 GlobalFree(waveData);
163 }
164
165 m_waveData = NULL;
166 m_waveLength = 0;
167 return TRUE;
168 }
169 }
170 return FALSE;
171 }
172
173 #endif // wxUSE_WAVE