]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: sound.cpp | |
3 | // Purpose: wxSound | |
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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
13 | #pragma implementation "sound.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_SOUND | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/wx.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/file.h" | |
30 | #include "wx/sound.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 | wxSound::wxSound() | |
42 | : m_waveData(NULL), m_waveLength(0), m_isResource(false) | |
43 | { | |
44 | } | |
45 | ||
46 | wxSound::wxSound(const wxString& sFileName, bool isResource) | |
47 | : m_waveData(NULL), m_waveLength(0), m_isResource(isResource) | |
48 | { | |
49 | Create(sFileName, isResource); | |
50 | } | |
51 | ||
52 | wxSound::wxSound(int size, const wxByte* data) | |
53 | : m_waveData(NULL), m_waveLength(0), m_isResource(false) | |
54 | { | |
55 | Create(size, data); | |
56 | } | |
57 | ||
58 | wxSound::~wxSound() | |
59 | { | |
60 | Free(); | |
61 | } | |
62 | ||
63 | bool wxSound::Create(const wxString& fileName, bool isResource) | |
64 | { | |
65 | Free(); | |
66 | ||
67 | if (isResource) | |
68 | { | |
69 | m_isResource = TRUE; | |
70 | ||
71 | HRSRC hresInfo; | |
72 | hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, wxT("WAVE")); | |
73 | if (!hresInfo) | |
74 | return false; | |
75 | ||
76 | HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo); | |
77 | ||
78 | if (waveData) | |
79 | { | |
80 | m_waveData= (wxByte*)::LockResource(waveData); | |
81 | m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo); | |
82 | } | |
83 | ||
84 | return (m_waveData ? true : false); | |
85 | } | |
86 | else | |
87 | { | |
88 | m_isResource = false; | |
89 | ||
90 | wxFile fileWave; | |
91 | if (!fileWave.Open(fileName, wxFile::read)) | |
92 | return false; | |
93 | ||
94 | m_waveLength = (int) fileWave.Length(); | |
95 | ||
96 | m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength)); | |
97 | if (!m_waveData) | |
98 | return false; | |
99 | ||
100 | fileWave.Read(m_waveData, m_waveLength); | |
101 | ||
102 | return true; | |
103 | } | |
104 | } | |
105 | ||
106 | bool wxSound::Create(int size, const wxByte* data) | |
107 | { | |
108 | Free(); | |
109 | m_isResource = true; | |
110 | m_waveLength=size; | |
111 | m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength)); | |
112 | if (!m_waveData) | |
113 | return false; | |
114 | ||
115 | for (int i=0; i<size; i++) m_waveData[i] = data[i]; | |
116 | return true; | |
117 | } | |
118 | ||
119 | bool wxSound::DoPlay(unsigned flags) const | |
120 | { | |
121 | if (!IsOk()) | |
122 | return false; | |
123 | ||
124 | return (::PlaySound((LPCTSTR)m_waveData, NULL, | |
125 | SND_MEMORY | SND_NODEFAULT | | |
126 | ((flags & wxSOUND_ASYNC) ? SND_ASYNC : SND_SYNC) | | |
127 | ((flags & wxSOUND_LOOP) ? (SND_LOOP | SND_ASYNC) : 0)) | |
128 | != 0); | |
129 | } | |
130 | ||
131 | bool wxSound::Free() | |
132 | { | |
133 | if (m_waveData) | |
134 | { | |
135 | #ifdef __WXWINCE__ | |
136 | HGLOBAL waveData = (HGLOBAL) m_waveData; | |
137 | #else | |
138 | HGLOBAL waveData = GlobalHandle(m_waveData); | |
139 | #endif | |
140 | ||
141 | if (waveData) | |
142 | { | |
143 | #ifndef __WXWINCE__ | |
144 | if (m_isResource) | |
145 | ::FreeResource(waveData); | |
146 | else | |
147 | #endif | |
148 | { | |
149 | GlobalUnlock(waveData); | |
150 | GlobalFree(waveData); | |
151 | } | |
152 | ||
153 | m_waveData = NULL; | |
154 | m_waveLength = 0; | |
155 | return true; | |
156 | } | |
157 | } | |
158 | return false; | |
159 | } | |
160 | ||
161 | /*static*/ void wxSound::Stop() | |
162 | { | |
163 | ::PlaySound(NULL, NULL, 0); | |
164 | } | |
165 | ||
166 | #endif // wxUSE_SOUND |