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