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