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