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