]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wave.cpp | |
3 | // Purpose: wxWave class implementation: optional | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "wave.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/object.h" | |
17 | #include "wx/string.h" | |
03e11df5 | 18 | #include "wx/wave.h" |
e9576ca5 | 19 | |
4a69b060 RD |
20 | #if wxUSE_WAVE |
21 | ||
76a5e5d2 SC |
22 | #ifdef __WXMAC__ |
23 | #include "wx/mac/private.h" | |
66a09d47 SC |
24 | #ifndef __DARWIN__ |
25 | #include <Sound.h> | |
26 | #endif | |
76a5e5d2 SC |
27 | #endif |
28 | ||
e9576ca5 | 29 | wxWave::wxWave() |
03e11df5 | 30 | : m_sndChan(0), m_hSnd(NULL), m_waveLength(0), m_isResource(true) |
e9576ca5 SC |
31 | { |
32 | } | |
33 | ||
34 | wxWave::wxWave(const wxString& sFileName, bool isResource) | |
03e11df5 | 35 | : m_sndChan(0), m_hSnd(NULL), m_waveLength(0), m_isResource(true) |
e9576ca5 SC |
36 | { |
37 | Create(sFileName, isResource); | |
38 | } | |
39 | ||
40 | ||
41 | wxWave::~wxWave() | |
42 | { | |
4b651a46 | 43 | FreeData(); |
e9576ca5 SC |
44 | } |
45 | ||
5b925569 SC |
46 | wxWave::wxWave(int size, const wxByte* data) |
47 | : m_sndChan(0), m_hSnd(NULL), m_waveLength(0), m_isResource(false) | |
48 | { | |
49 | //TODO convert data | |
50 | } | |
5b781a67 | 51 | |
e9576ca5 SC |
52 | bool wxWave::Create(const wxString& fileName, bool isResource) |
53 | { | |
5b781a67 SC |
54 | bool ret = false; |
55 | m_sndname = fileName; | |
56 | m_isResource = isResource; | |
57 | ||
58 | if (m_isResource) | |
59 | ret = true; | |
60 | else | |
61 | { /* | |
4a69b060 | 62 | if (sndChan) |
5b781a67 SC |
63 | { // we're playing |
64 | FSClose(SndRefNum); | |
65 | SndRefNum = 0; | |
66 | SndDisposeChannel(sndChan, TRUE); | |
67 | free(sndChan); | |
68 | sndChan = 0; | |
69 | KillTimer(0,timerID); | |
70 | } | |
4a69b060 | 71 | |
5b781a67 SC |
72 | if (!lpSnd) |
73 | return true; | |
74 | ||
75 | if (_access(lpSnd,0)) // no file, no service | |
76 | return false; | |
77 | ||
78 | // Allocate SndChannel | |
79 | sndChan = (SndChannelPtr) malloc (sizeof(SndChannel)); | |
80 | ||
81 | if (!sndChan) | |
82 | return false; | |
83 | ||
84 | sndChan->qLength = 128; | |
85 | ||
4a69b060 | 86 | if (noErr != SndNewChannel (&sndChan, sampledSynth, initMono | initNoInterp, 0)) |
5b781a67 SC |
87 | { |
88 | free(sndChan); | |
89 | sndChan = 0; | |
90 | return false; | |
91 | } | |
e9576ca5 | 92 | |
4a69b060 | 93 | if (!(SndRefNum = MacOpenSndFile ((char *)lpSnd))) |
5b781a67 SC |
94 | { |
95 | SndDisposeChannel(sndChan, TRUE); | |
96 | free(sndChan); | |
97 | sndChan = 0; | |
e9576ca5 | 98 | |
5b781a67 SC |
99 | return false; |
100 | } | |
101 | ||
102 | bool async = false; | |
103 | ||
104 | if (fdwSound & SND_ASYNC) | |
105 | async = true; | |
106 | ||
4a69b060 | 107 | if (SndStartFilePlay(sndChan, SndRefNum, 0, 81920, 0, 0, 0, async) != noErr) |
5b781a67 SC |
108 | { |
109 | FSClose (SndRefNum); | |
110 | SndRefNum = 0; | |
111 | SndDisposeChannel (sndChan, TRUE); | |
112 | free (sndChan); | |
113 | sndChan = 0; | |
114 | return false; | |
115 | } | |
116 | ||
4a69b060 | 117 | if (async) |
5b781a67 SC |
118 | { // haven't finish yet |
119 | timerID = SetTimer(0, 0, 250, TimerCallBack); | |
4a69b060 RD |
120 | } |
121 | else | |
5b781a67 SC |
122 | { |
123 | FSClose (SndRefNum); | |
124 | SndRefNum = 0; | |
125 | SndDisposeChannel (sndChan, TRUE); | |
126 | free (sndChan); | |
127 | sndChan = 0; | |
128 | }*/ | |
129 | } | |
4a69b060 | 130 | |
5b781a67 | 131 | return ret; |
e9576ca5 SC |
132 | } |
133 | ||
5b781a67 SC |
134 | |
135 | //don't know what to do with looped, wth | |
e9576ca5 SC |
136 | bool wxWave::Play(bool async, bool looped) const |
137 | { | |
5b781a67 SC |
138 | char lpSnd[32]; |
139 | bool ret = false; | |
4a69b060 RD |
140 | |
141 | if (m_isResource) | |
5b781a67 | 142 | { |
5fde6fcc GD |
143 | #if TARGET_CARBON |
144 | c2pstrcpy((unsigned char *)lpSnd, m_sndname); | |
145 | #else | |
146 | strcpy(lpSnd, m_sndname); | |
147 | c2pstr((char *) lpSnd); | |
148 | #endif | |
149 | SndListHandle hSnd; | |
4a69b060 | 150 | |
5fde6fcc | 151 | hSnd = (SndListHandle) GetNamedResource('snd ',(const unsigned char *) lpSnd); |
e9576ca5 | 152 | |
76a5e5d2 | 153 | if ((hSnd != NULL) && (SndPlay((SndChannelPtr)m_sndChan, (SndListHandle) hSnd, async) == noErr)) |
5fde6fcc | 154 | ret = true; |
4a69b060 RD |
155 | } |
156 | ||
5b781a67 | 157 | return ret; |
e9576ca5 SC |
158 | } |
159 | ||
5b781a67 | 160 | |
4b651a46 | 161 | bool wxWave::FreeData() |
e9576ca5 | 162 | { |
5b781a67 | 163 | bool ret = false; |
4a69b060 | 164 | |
5b781a67 SC |
165 | if (m_isResource) |
166 | { | |
167 | m_sndname.Empty(); | |
168 | ret = true; | |
169 | } | |
170 | else | |
171 | { | |
4a69b060 | 172 | //TODO, |
5b781a67 | 173 | } |
4a69b060 | 174 | |
5b781a67 | 175 | return ret; |
e9576ca5 SC |
176 | } |
177 | ||
178 | ||
5b781a67 SC |
179 | //code below is from an old implementation used for telinfo with MSVC crossplatform support |
180 | //technology proceeds, so it would be the wisest to drop this code, but it's left here just | |
181 | //for the sake of a reference. BTW: Wave files can now be played with QT, starting from V3 | |
182 | ||
183 | /*static short MacOpenSndFile (char * path) | |
184 | { | |
185 | VolumeParam vp; | |
186 | FSSpec fspec; | |
187 | Str255 name; | |
188 | char *c; | |
189 | ||
190 | // first, get the volume reference number for the file. Start by | |
191 | // making a Pstring with just the volume name | |
192 | strcpy ((char *) name, path); | |
4a69b060 | 193 | if (c = strchr ((char *) name, ':')) |
5b781a67 SC |
194 | { |
195 | c++; | |
196 | *c = '\0'; | |
197 | } | |
198 | ||
199 | c2pstr ((char *) name); | |
200 | vp.ioCompletion = 0; | |
201 | vp.ioVolIndex = -1; | |
202 | vp.ioNamePtr = name; | |
203 | vp.ioVRefNum = 0; | |
204 | ||
205 | if (PBGetVInfo((ParamBlockRec *)&vp, 0) != noErr) | |
206 | return 0; | |
207 | ||
208 | // next, buld an FSSpec for the file | |
209 | strcpy ((char *) name, path); | |
210 | c2pstr ((char *) name); | |
211 | if (FSMakeFSSpec (vp.ioVRefNum, 0, name, &fspec) != noErr) | |
212 | return 0; | |
213 | ||
214 | short frefnum; | |
215 | // now open the file, and return it's reference number | |
216 | if (FSpOpenDF(&fspec, fsRdPerm, &frefnum) != noErr) | |
217 | return 0; | |
218 | ||
219 | return frefnum; | |
220 | } | |
221 | ||
222 | ||
223 | void TimerCallBack(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime) | |
224 | { | |
4a69b060 | 225 | if(!sndChan) |
5b781a67 SC |
226 | { |
227 | KillTimer(0,timerID); | |
228 | return; | |
229 | } | |
230 | ||
231 | SCStatus scstat; | |
232 | ||
233 | if (noErr == SndChannelStatus (sndChan, sizeof (SCStatus), &scstat)) { | |
234 | if (scstat.scChannelPaused || scstat.scChannelBusy) | |
235 | return; // not done yet | |
236 | } | |
237 | ||
238 | // either error or done. | |
239 | FSClose (SndRefNum); | |
240 | SndRefNum = 0; | |
241 | SndDisposeChannel (sndChan, TRUE); | |
242 | free (sndChan); | |
243 | sndChan = 0; | |
244 | KillTimer(0,timerID); | |
245 | }*/ | |
246 | ||
247 | ||
4a69b060 | 248 | #endif |