]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
315ebf68 VS |
2 | // Name: sound.cpp |
3 | // Purpose: wxSound | |
2bda0e17 KB |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
315ebf68 | 13 | #pragma implementation "sound.h" |
2bda0e17 KB |
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 | ||
315ebf68 | 23 | #if wxUSE_SOUND |
1e6feb95 | 24 | |
2bda0e17 | 25 | #ifndef WX_PRECOMP |
ee4f8c2a | 26 | #include "wx/wx.h" |
2bda0e17 KB |
27 | #endif |
28 | ||
ee4f8c2a | 29 | #include "wx/file.h" |
315ebf68 | 30 | #include "wx/sound.h" |
ee4f8c2a | 31 | #include "wx/msw/private.h" |
2bda0e17 | 32 | |
2bda0e17 KB |
33 | #include <windowsx.h> |
34 | ||
2cef71bc | 35 | #include <mmsystem.h> |
2bda0e17 | 36 | |
315ebf68 VS |
37 | wxSound::wxSound() |
38 | : m_waveData(NULL), m_waveLength(0), m_isResource(false) | |
2bda0e17 KB |
39 | { |
40 | } | |
41 | ||
315ebf68 | 42 | wxSound::wxSound(const wxString& sFileName, bool isResource) |
0f358732 | 43 | : m_waveData(NULL), m_waveLength(0), m_isResource(isResource) |
2bda0e17 KB |
44 | { |
45 | Create(sFileName, isResource); | |
46 | } | |
47 | ||
315ebf68 VS |
48 | wxSound::wxSound(int size, const wxByte* data) |
49 | : m_waveData(NULL), m_waveLength(0), m_isResource(false) | |
321db4b6 JS |
50 | { |
51 | Create(size, data); | |
52 | } | |
2bda0e17 | 53 | |
315ebf68 | 54 | wxSound::~wxSound() |
2bda0e17 KB |
55 | { |
56 | Free(); | |
57 | } | |
58 | ||
315ebf68 | 59 | bool wxSound::Create(const wxString& fileName, bool isResource) |
2bda0e17 KB |
60 | { |
61 | Free(); | |
62 | ||
63 | if (isResource) | |
64 | { | |
57f4f925 | 65 | m_isResource = true; |
2bda0e17 KB |
66 | |
67 | HRSRC hresInfo; | |
223d09f6 | 68 | hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, wxT("WAVE")); |
2bda0e17 | 69 | if (!hresInfo) |
315ebf68 | 70 | return false; |
2bda0e17 KB |
71 | |
72 | HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo); | |
73 | ||
74 | if (waveData) | |
75 | { | |
57c208c5 | 76 | m_waveData= (wxByte*)::LockResource(waveData); |
2bda0e17 KB |
77 | m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo); |
78 | } | |
79 | ||
315ebf68 | 80 | return (m_waveData ? true : false); |
2bda0e17 KB |
81 | } |
82 | else | |
83 | { | |
315ebf68 | 84 | m_isResource = false; |
2bda0e17 KB |
85 | |
86 | wxFile fileWave; | |
87 | if (!fileWave.Open(fileName, wxFile::read)) | |
315ebf68 | 88 | return false; |
2bda0e17 KB |
89 | |
90 | m_waveLength = (int) fileWave.Length(); | |
91 | ||
4676948b | 92 | m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength)); |
2bda0e17 | 93 | if (!m_waveData) |
315ebf68 | 94 | return false; |
2bda0e17 KB |
95 | |
96 | fileWave.Read(m_waveData, m_waveLength); | |
97 | ||
315ebf68 | 98 | return true; |
2bda0e17 KB |
99 | } |
100 | } | |
101 | ||
315ebf68 | 102 | bool wxSound::Create(int size, const wxByte* data) |
321db4b6 JS |
103 | { |
104 | Free(); | |
315ebf68 | 105 | m_isResource = true; |
321db4b6 | 106 | m_waveLength=size; |
4676948b | 107 | m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength)); |
321db4b6 | 108 | if (!m_waveData) |
315ebf68 | 109 | return false; |
321db4b6 JS |
110 | |
111 | for (int i=0; i<size; i++) m_waveData[i] = data[i]; | |
315ebf68 | 112 | return true; |
321db4b6 JS |
113 | } |
114 | ||
315ebf68 | 115 | bool wxSound::DoPlay(unsigned flags) const |
2bda0e17 KB |
116 | { |
117 | if (!IsOk()) | |
315ebf68 | 118 | return false; |
2bda0e17 | 119 | |
315ebf68 VS |
120 | return (::PlaySound((LPCTSTR)m_waveData, NULL, |
121 | SND_MEMORY | SND_NODEFAULT | | |
122 | ((flags & wxSOUND_ASYNC) ? SND_ASYNC : SND_SYNC) | | |
123 | ((flags & wxSOUND_LOOP) ? (SND_LOOP | SND_ASYNC) : 0)) | |
124 | != 0); | |
2bda0e17 KB |
125 | } |
126 | ||
315ebf68 | 127 | bool wxSound::Free() |
2bda0e17 KB |
128 | { |
129 | if (m_waveData) | |
130 | { | |
4676948b JS |
131 | #ifdef __WXWINCE__ |
132 | HGLOBAL waveData = (HGLOBAL) m_waveData; | |
2bda0e17 | 133 | #else |
315ebf68 | 134 | HGLOBAL waveData = GlobalHandle(m_waveData); |
2bda0e17 KB |
135 | #endif |
136 | ||
137 | if (waveData) | |
138 | { | |
4676948b JS |
139 | #ifndef __WXWINCE__ |
140 | if (m_isResource) | |
2bda0e17 KB |
141 | ::FreeResource(waveData); |
142 | else | |
4676948b | 143 | #endif |
2bda0e17 | 144 | { |
4676948b JS |
145 | GlobalUnlock(waveData); |
146 | GlobalFree(waveData); | |
2bda0e17 KB |
147 | } |
148 | ||
149 | m_waveData = NULL; | |
150 | m_waveLength = 0; | |
315ebf68 | 151 | return true; |
2bda0e17 KB |
152 | } |
153 | } | |
315ebf68 VS |
154 | return false; |
155 | } | |
156 | ||
157 | /*static*/ void wxSound::Stop() | |
158 | { | |
159 | ::PlaySound(NULL, NULL, 0); | |
2bda0e17 KB |
160 | } |
161 | ||
315ebf68 | 162 | #endif // wxUSE_SOUND |