]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/wave.cpp
switching off tight borders to have it look properly under 10.3
[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 bool ret = false;
139
140 if (m_isResource)
141 {
142 Str255 snd ;
143 wxMacStringToPascal( m_sndname , snd ) ;
144 SndListHandle hSnd;
145
146 hSnd = (SndListHandle) GetNamedResource('snd ', snd);
147
148 if ((hSnd != NULL) && (SndPlay((SndChannelPtr)m_sndChan, (SndListHandle) hSnd, async) == noErr))
149 ret = true;
150 }
151
152 return ret;
153 }
154
155
156 bool wxWave::FreeData()
157 {
158 bool ret = false;
159
160 if (m_isResource)
161 {
162 m_sndname.Empty();
163 ret = true;
164 }
165 else
166 {
167 //TODO,
168 }
169
170 return ret;
171 }
172
173
174 //code below is from an old implementation used for telinfo with MSVC crossplatform support
175 //technology proceeds, so it would be the wisest to drop this code, but it's left here just
176 //for the sake of a reference. BTW: Wave files can now be played with QT, starting from V3
177
178 /*static short MacOpenSndFile (char * path)
179 {
180 VolumeParam vp;
181 FSSpec fspec;
182 Str255 name;
183 char *c;
184
185 // first, get the volume reference number for the file. Start by
186 // making a Pstring with just the volume name
187 strcpy ((char *) name, path);
188 if (c = strchr ((char *) name, ':'))
189 {
190 c++;
191 *c = '\0';
192 }
193
194 c2pstr ((char *) name);
195 vp.ioCompletion = 0;
196 vp.ioVolIndex = -1;
197 vp.ioNamePtr = name;
198 vp.ioVRefNum = 0;
199
200 if (PBGetVInfo((ParamBlockRec *)&vp, 0) != noErr)
201 return 0;
202
203 // next, buld an FSSpec for the file
204 strcpy ((char *) name, path);
205 c2pstr ((char *) name);
206 if (FSMakeFSSpec (vp.ioVRefNum, 0, name, &fspec) != noErr)
207 return 0;
208
209 short frefnum;
210 // now open the file, and return it's reference number
211 if (FSpOpenDF(&fspec, fsRdPerm, &frefnum) != noErr)
212 return 0;
213
214 return frefnum;
215 }
216
217
218 void TimerCallBack(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
219 {
220 if(!sndChan)
221 {
222 KillTimer(0,timerID);
223 return;
224 }
225
226 SCStatus scstat;
227
228 if (noErr == SndChannelStatus (sndChan, sizeof (SCStatus), &scstat)) {
229 if (scstat.scChannelPaused || scstat.scChannelBusy)
230 return; // not done yet
231 }
232
233 // either error or done.
234 FSClose (SndRefNum);
235 SndRefNum = 0;
236 SndDisposeChannel (sndChan, TRUE);
237 free (sndChan);
238 sndChan = 0;
239 KillTimer(0,timerID);
240 }*/
241
242
243 #endif