]> git.saurik.com Git - wxWidgets.git/blame - src/mac/sound.cpp
made wxFFile a bit more safe: don't crash when Tell() and Length() are called on...
[wxWidgets.git] / src / mac / sound.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
315ebf68
VS
2// Name: sound.cpp
3// Purpose: wxSound class implementation: optional
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e40298d5 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
315ebf68 13#pragma implementation "sound.h"
e9576ca5
SC
14#endif
15
16#include "wx/object.h"
17#include "wx/string.h"
315ebf68 18#include "wx/sound.h"
e9576ca5 19
315ebf68 20#if wxUSE_SOUND
4a69b060 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
315ebf68 29wxSound::wxSound()
03e11df5 30 : m_sndChan(0), m_hSnd(NULL), m_waveLength(0), m_isResource(true)
e9576ca5
SC
31{
32}
33
315ebf68 34wxSound::wxSound(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
315ebf68 41wxSound::~wxSound()
e9576ca5 42{
4b651a46 43 FreeData();
e9576ca5
SC
44}
45
315ebf68 46wxSound::wxSound(int size, const wxByte* data)
5b925569
SC
47 : m_sndChan(0), m_hSnd(NULL), m_waveLength(0), m_isResource(false)
48{
49 //TODO convert data
50}
5b781a67 51
315ebf68 52bool wxSound::Create(const wxString& fileName, bool isResource)
e9576ca5 53{
e40298d5
JS
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;
e9576ca5
SC
132}
133
5b781a67
SC
134
135//don't know what to do with looped, wth
315ebf68 136bool wxSound::DoPlay(unsigned flags) const
e9576ca5 137{
e40298d5 138 bool ret = false;
4a69b060 139
e40298d5
JS
140 if (m_isResource)
141 {
427ff662
SC
142 Str255 snd ;
143 wxMacStringToPascal( m_sndname , snd ) ;
144 SndListHandle hSnd;
4a69b060 145
427ff662 146 hSnd = (SndListHandle) GetNamedResource('snd ', snd);
e9576ca5 147
315ebf68 148 if ((hSnd != NULL) && (SndPlay((SndChannelPtr)m_sndChan, (SndListHandle) hSnd, (flags & wxSOUND_ASYNC)) == noErr))
427ff662 149 ret = true;
e40298d5 150 }
4a69b060 151
e40298d5 152 return ret;
e9576ca5
SC
153}
154
5b781a67 155
315ebf68 156bool wxSound::FreeData()
e9576ca5 157{
e40298d5
JS
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;
e9576ca5
SC
171}
172
173
5b781a67
SC
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{
e40298d5
JS
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;
5b781a67
SC
215}
216
217
218void TimerCallBack(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
219{
e40298d5
JS
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);
5b781a67
SC
240}*/
241
242
4a69b060 243#endif