]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/wave.cpp
Tidied space and tabs in wxMac files
[wxWidgets.git] / src / mac / carbon / wave.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wave.cpp
3 // Purpose: wxWave class implementation: optional
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
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 #if wxUSE_WAVE
21
22 #ifdef __WXMAC__
23 #include "wx/mac/private.h"
24 #ifndef __DARWIN__
25 #include <Sound.h>
26 #endif
27 #endif
28
29 wxWave::wxWave()
30 : m_sndChan(0), m_hSnd(NULL), m_waveLength(0), m_isResource(true)
31 {
32 }
33
34 wxWave::wxWave(const wxString& sFileName, bool isResource)
35 : m_sndChan(0), m_hSnd(NULL), m_waveLength(0), m_isResource(true)
36 {
37 Create(sFileName, isResource);
38 }
39
40
41 wxWave::~wxWave()
42 {
43 FreeData();
44 }
45
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 }
51
52 bool wxWave::Create(const wxString& fileName, bool isResource)
53 {
54 bool ret = false;
55 m_sndname = fileName;
56 m_isResource = isResource;
57
58 if (m_isResource)
59 ret = true;
60 else
61 { /*
62 if (sndChan)
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 }
71
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
86 if (noErr != SndNewChannel (&sndChan, sampledSynth, initMono | initNoInterp, 0))
87 {
88 free(sndChan);
89 sndChan = 0;
90 return false;
91 }
92
93 if (!(SndRefNum = MacOpenSndFile ((char *)lpSnd)))
94 {
95 SndDisposeChannel(sndChan, TRUE);
96 free(sndChan);
97 sndChan = 0;
98
99 return false;
100 }
101
102 bool async = false;
103
104 if (fdwSound & SND_ASYNC)
105 async = true;
106
107 if (SndStartFilePlay(sndChan, SndRefNum, 0, 81920, 0, 0, 0, async) != noErr)
108 {
109 FSClose (SndRefNum);
110 SndRefNum = 0;
111 SndDisposeChannel (sndChan, TRUE);
112 free (sndChan);
113 sndChan = 0;
114 return false;
115 }
116
117 if (async)
118 { // haven't finish yet
119 timerID = SetTimer(0, 0, 250, TimerCallBack);
120 }
121 else
122 {
123 FSClose (SndRefNum);
124 SndRefNum = 0;
125 SndDisposeChannel (sndChan, TRUE);
126 free (sndChan);
127 sndChan = 0;
128 }*/
129 }
130
131 return ret;
132 }
133
134
135 //don't know what to do with looped, wth
136 bool wxWave::Play(bool async, bool looped) const
137 {
138 char lpSnd[32];
139 bool ret = false;
140
141 if (m_isResource)
142 {
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;
150
151 hSnd = (SndListHandle) GetNamedResource('snd ',(const unsigned char *) lpSnd);
152
153 if ((hSnd != NULL) && (SndPlay((SndChannelPtr)m_sndChan, (SndListHandle) hSnd, async) == noErr))
154 ret = true;
155 }
156
157 return ret;
158 }
159
160
161 bool wxWave::FreeData()
162 {
163 bool ret = false;
164
165 if (m_isResource)
166 {
167 m_sndname.Empty();
168 ret = true;
169 }
170 else
171 {
172 //TODO,
173 }
174
175 return ret;
176 }
177
178
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);
193 if (c = strchr ((char *) name, ':'))
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 {
225 if(!sndChan)
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
248 #endif