]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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 and Markus Holzem | |
9 | // Licence: wxWindows license | |
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 | #ifndef WX_PRECOMP | |
ee4f8c2a | 24 | #include "wx/wx.h" |
2bda0e17 KB |
25 | #endif |
26 | ||
ee4f8c2a JS |
27 | #include "wx/file.h" |
28 | #include "wx/msw/wave.h" | |
29 | #include "wx/msw/private.h" | |
2bda0e17 KB |
30 | |
31 | #include <windows.h> | |
32 | #include <windowsx.h> | |
33 | ||
34 | #ifndef __GNUWIN32__ | |
35 | #include <mmsystem.h> | |
36 | #endif | |
37 | ||
57c208c5 | 38 | #ifndef __TWIN32__ |
2bda0e17 | 39 | #ifdef __GNUWIN32__ |
ee4f8c2a | 40 | #include "wx/msw/gnuwin32/extra.h" |
2bda0e17 | 41 | #endif |
57c208c5 | 42 | #endif |
2bda0e17 | 43 | |
ee4f8c2a | 44 | wxWave::wxWave() |
0f358732 | 45 | : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE) |
2bda0e17 KB |
46 | { |
47 | } | |
48 | ||
49 | wxWave::wxWave(const wxString& sFileName, bool isResource) | |
0f358732 | 50 | : m_waveData(NULL), m_waveLength(0), m_isResource(isResource) |
2bda0e17 KB |
51 | { |
52 | Create(sFileName, isResource); | |
53 | } | |
54 | ||
57c208c5 | 55 | wxWave::wxWave(int size, const wxByte* data) |
0f358732 | 56 | : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE) |
321db4b6 JS |
57 | { |
58 | Create(size, data); | |
59 | } | |
2bda0e17 | 60 | |
ee4f8c2a | 61 | wxWave::~wxWave() |
2bda0e17 KB |
62 | { |
63 | Free(); | |
64 | } | |
65 | ||
66 | bool wxWave::Create(const wxString& fileName, bool isResource) | |
67 | { | |
68 | Free(); | |
69 | ||
70 | if (isResource) | |
71 | { | |
72 | m_isResource = TRUE; | |
73 | ||
74 | HRSRC hresInfo; | |
57c208c5 | 75 | #if defined(__WIN32__) && !defined(__TWIN32__) |
837e5743 OK |
76 | #ifdef _UNICODE |
77 | hresInfo = ::FindResourceW((HMODULE) wxhInstance, fileName, _T("WAVE")); | |
2bda0e17 | 78 | #else |
837e5743 OK |
79 | hresInfo = ::FindResourceA((HMODULE) wxhInstance, fileName, _T("WAVE")); |
80 | #endif | |
81 | #else | |
82 | hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, _T("WAVE")); | |
2bda0e17 KB |
83 | #endif |
84 | if (!hresInfo) | |
85 | return FALSE; | |
86 | ||
87 | HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo); | |
88 | ||
89 | if (waveData) | |
90 | { | |
57c208c5 | 91 | m_waveData= (wxByte*)::LockResource(waveData); |
2bda0e17 KB |
92 | m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo); |
93 | } | |
94 | ||
95 | return (m_waveData ? TRUE : FALSE); | |
96 | } | |
97 | else | |
98 | { | |
99 | m_isResource = FALSE; | |
100 | ||
101 | wxFile fileWave; | |
102 | if (!fileWave.Open(fileName, wxFile::read)) | |
103 | return FALSE; | |
104 | ||
105 | m_waveLength = (int) fileWave.Length(); | |
106 | ||
57c208c5 | 107 | m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength)); |
2bda0e17 KB |
108 | if (!m_waveData) |
109 | return FALSE; | |
110 | ||
111 | fileWave.Read(m_waveData, m_waveLength); | |
112 | ||
113 | return TRUE; | |
114 | } | |
115 | } | |
116 | ||
57c208c5 | 117 | bool wxWave::Create(int size, const wxByte* data) |
321db4b6 JS |
118 | { |
119 | Free(); | |
120 | m_isResource = FALSE; | |
121 | m_waveLength=size; | |
57c208c5 | 122 | m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength)); |
321db4b6 JS |
123 | if (!m_waveData) |
124 | return FALSE; | |
125 | ||
126 | for (int i=0; i<size; i++) m_waveData[i] = data[i]; | |
127 | return TRUE; | |
128 | } | |
129 | ||
2bda0e17 KB |
130 | bool wxWave::Play(bool async, bool looped) const |
131 | { | |
132 | if (!IsOk()) | |
133 | return FALSE; | |
134 | ||
135 | #ifdef __WIN32__ | |
837e5743 | 136 | return ( ::PlaySound((LPCTSTR)m_waveData, NULL, SND_MEMORY | |
2bda0e17 KB |
137 | SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 ); |
138 | #else | |
139 | return ( ::sndPlaySound((LPCSTR)m_waveData, SND_MEMORY | | |
140 | SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 ); | |
141 | #endif | |
142 | } | |
143 | ||
ee4f8c2a | 144 | bool wxWave::Free() |
2bda0e17 KB |
145 | { |
146 | if (m_waveData) | |
147 | { | |
148 | #ifdef __WIN32__ | |
149 | HGLOBAL waveData = ::GlobalHandle(m_waveData); | |
150 | #else | |
151 | HGLOBAL waveData = GlobalPtrHandle(m_waveData); | |
152 | #endif | |
153 | ||
154 | if (waveData) | |
155 | { | |
156 | if (m_isResource) | |
157 | ::FreeResource(waveData); | |
158 | else | |
159 | { | |
160 | ::GlobalUnlock(waveData); | |
161 | ::GlobalFree(waveData); | |
162 | } | |
163 | ||
164 | m_waveData = NULL; | |
165 | m_waveLength = 0; | |
166 | return TRUE; | |
167 | } | |
168 | } | |
169 | return FALSE; | |
170 | } | |
171 | ||
172 |