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