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