]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndfile.cpp
Make wxMMedia2 compile on VC++ 5
[wxWidgets.git] / utils / wxMMedia2 / lib / sndfile.cpp
1 // --------------------------------------------------------------------------
2 // Name: sndfile.cpp
3 // Purpose:
4 // Date: 08/11/1999
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
6 // CVSID: $Id$
7 // --------------------------------------------------------------------------
8 #include <wx/wxprec.h>
9
10 #ifndef WX_PRECOMP
11 #include <wx/stream.h>
12 #endif
13
14 #include "sndbase.h"
15 #include "sndcodec.h"
16 #include "sndfile.h"
17 #include "sndcpcm.h"
18 #include "sndulaw.h"
19
20 // --------------------------------------------------------------------------
21 // Sound codec router
22 // --------------------------------------------------------------------------
23
24 wxSoundRouterStream::wxSoundRouterStream(wxSoundStream& sndio)
25 : wxSoundStreamCodec(sndio)
26 {
27 m_router = NULL;
28 }
29
30 wxSoundRouterStream::~wxSoundRouterStream()
31 {
32 if (m_router)
33 delete m_router;
34 }
35
36 wxSoundStream& wxSoundRouterStream::Read(void *buffer, size_t len)
37 {
38 if (m_router) {
39 m_router->Read(buffer, len);
40 m_snderror = m_router->GetError();
41 m_lastcount = m_router->GetLastAccess();
42 } else {
43 m_sndio->Read(buffer, len);
44 m_snderror = m_sndio->GetError();
45 m_lastcount = m_sndio->GetLastAccess();
46 }
47 return *this;
48 }
49
50 wxSoundStream& wxSoundRouterStream::Write(const void *buffer, size_t len)
51 {
52 if (m_router) {
53 m_router->Write(buffer, len);
54 m_snderror = m_router->GetError();
55 m_lastcount = m_router->GetLastAccess();
56 } else {
57 m_sndio->Write(buffer, len);
58 m_snderror = m_sndio->GetError();
59 m_lastcount = m_sndio->GetLastAccess();
60 }
61 return *this;
62 }
63
64 bool wxSoundRouterStream::SetSoundFormat(const wxSoundFormatBase& format)
65 {
66 if (m_router)
67 delete m_router;
68
69 if (m_sndio->SetSoundFormat(format)) {
70 wxSoundStream::SetSoundFormat(m_sndio->GetSoundFormat());
71 return TRUE;
72 }
73
74 switch(format.GetType()) {
75 case wxSOUND_NOFORMAT:
76 return FALSE;
77 case wxSOUND_PCM:
78 m_router = new wxSoundStreamPcm(*m_sndio);
79 m_router->SetSoundFormat(format);
80 break;
81 case wxSOUND_ULAW:
82 m_router = new wxSoundStreamUlaw(*m_sndio);
83 m_router->SetSoundFormat(format);
84 break;
85 }
86 wxSoundStream::SetSoundFormat(m_router->GetSoundFormat());
87 return TRUE;
88 }
89
90 bool wxSoundRouterStream::StartProduction(int evt)
91 {
92 if (!m_router) {
93 if (m_sndio->StartProduction(evt))
94 return TRUE;
95
96 m_snderror = m_sndio->GetError();
97 m_lastcount = m_sndio->GetLastAccess();
98 return FALSE;
99 }
100
101 if (m_router->StartProduction(evt))
102 return TRUE;
103
104 m_snderror = m_router->GetError();
105 m_lastcount = m_router->GetLastAccess();
106 return FALSE;
107 }
108
109 bool wxSoundRouterStream::StopProduction()
110 {
111 if (!m_router) {
112 if (m_sndio->StopProduction())
113 return TRUE;
114
115 m_snderror = m_sndio->GetError();
116 m_lastcount = m_sndio->GetLastAccess();
117 return FALSE;
118 }
119
120 if (m_router->StopProduction())
121 return TRUE;
122
123 m_snderror = m_router->GetError();
124 m_lastcount = m_router->GetLastAccess();
125 return FALSE;
126 }
127
128
129 // --------------------------------------------------------------------------
130 // wxSoundFileStream: generic reader
131 // --------------------------------------------------------------------------
132
133 wxSoundFileStream::wxSoundFileStream(wxInputStream& stream,
134 wxSoundStream& io_sound)
135 : m_codec(io_sound), m_sndio(&io_sound),
136 m_input(&stream), m_output(NULL), m_state(wxSOUND_FILE_STOPPED)
137 {
138 }
139
140 wxSoundFileStream::wxSoundFileStream(wxOutputStream& stream,
141 wxSoundStream& io_sound)
142 : m_codec(io_sound), m_sndio(&io_sound),
143 m_input(NULL), m_output(&stream), m_state(wxSOUND_FILE_STOPPED)
144 {
145 }
146
147 wxSoundFileStream::~wxSoundFileStream()
148 {
149 if (m_state != wxSOUND_FILE_STOPPED)
150 Stop();
151 }
152
153 bool wxSoundFileStream::Play()
154 {
155 if (m_state != wxSOUND_FILE_STOPPED)
156 return FALSE;
157
158 if (!PrepareToPlay())
159 return FALSE;
160
161 if (!StartProduction(wxSOUND_OUTPUT))
162 return FALSE;
163
164 m_state = wxSOUND_FILE_PLAYING;
165 return TRUE;
166 }
167
168 bool wxSoundFileStream::Record(unsigned long time)
169 {
170 if (m_state != wxSOUND_FILE_STOPPED)
171 return FALSE;
172
173 if (!PrepareToRecord(time))
174 return FALSE;
175
176 m_len = m_sndformat->GetBytesFromTime(time);
177
178 if (!StartProduction(wxSOUND_INPUT))
179 return FALSE;
180
181 m_state = wxSOUND_FILE_RECORDING;
182 return TRUE;
183 }
184
185 bool wxSoundFileStream::Stop()
186 {
187 if (m_state == wxSOUND_FILE_STOPPED)
188 return FALSE;
189
190 if (!StopProduction())
191 return FALSE;
192
193 if (m_state == wxSOUND_FILE_RECORDING)
194 if (!FinishRecording()) {
195 m_state = wxSOUND_FILE_STOPPED;
196 return FALSE;
197 }
198
199 // TODO reset counter
200 m_state = wxSOUND_FILE_STOPPED;
201 return TRUE;
202 }
203
204 bool wxSoundFileStream::Pause()
205 {
206 if (m_state == wxSOUND_FILE_PAUSED || m_state == wxSOUND_FILE_STOPPED)
207 return FALSE;
208
209 if (!StopProduction())
210 return FALSE;
211
212 m_oldstate = m_state;
213 m_state = wxSOUND_FILE_PAUSED;
214 return TRUE;
215 }
216
217 bool wxSoundFileStream::Resume()
218 {
219 if (m_state == wxSOUND_FILE_PLAYING || m_state == wxSOUND_FILE_RECORDING ||
220 m_state == wxSOUND_FILE_STOPPED)
221 return FALSE;
222
223 if (!StartProduction( (m_oldstate == wxSOUND_FILE_PLAYING) ?
224 wxSOUND_OUTPUT : wxSOUND_INPUT))
225 return FALSE;
226
227 m_state = m_oldstate;
228
229 return TRUE;
230 }
231
232 wxSoundStream& wxSoundFileStream::Read(void *buffer, size_t len)
233 {
234 m_lastcount = GetData(buffer, len);
235 return *this;
236 }
237
238 wxSoundStream& wxSoundFileStream::Write(const void *buffer, size_t len)
239 {
240 m_lastcount = PutData(buffer, len);
241 return *this;
242 }
243
244 void wxSoundFileStream::SetDuplexMode(bool duplex)
245 {
246 }
247
248 bool wxSoundFileStream::StartProduction(int evt)
249 {
250 m_sndio->SetEventHandler(this);
251
252 if (!m_codec.StartProduction(evt))
253 return FALSE;
254
255 return TRUE;
256 }
257
258 bool wxSoundFileStream::StopProduction()
259 {
260 return m_codec.StopProduction();
261 }
262
263 void wxSoundFileStream::OnSoundEvent(int evt)
264 {
265 size_t len = m_sndio->GetBestSize();
266 char *buffer;
267
268 buffer = new char[m_sndio->GetBestSize()];
269 wxSoundStream::OnSoundEvent(evt);
270
271 switch(evt) {
272 case wxSOUND_INPUT:
273 if (len > m_len)
274 len = m_len;
275
276 len = m_codec.Read(buffer, len).GetLastAccess();
277 PutData(buffer, len);
278 m_len -= len;
279 if (m_len == 0) {
280 Stop();
281 return;
282 }
283 break;
284 case wxSOUND_OUTPUT:
285 len = GetData(buffer, len);
286 if (len == 0) {
287 Stop();
288 return;
289 }
290 m_codec.Write(buffer, len);
291 break;
292 }
293 delete[] buffer;
294 }
295
296 bool wxSoundFileStream::SetSoundFormat(const wxSoundFormatBase& format)
297 {
298 wxSoundStream::SetSoundFormat(format);
299 return m_codec.SetSoundFormat(format);
300 }