]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndwav.cpp
Removed unnecessary code from utilsunx.cpp
[wxWidgets.git] / utils / wxMMedia2 / lib / sndwav.cpp
1 // --------------------------------------------------------------------------
2 // Name: sndwav.cpp
3 // Purpose:
4 // Date: 08/11/1999
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
6 // CVSID: $Id$
7 // --------------------------------------------------------------------------
8 #ifdef __GNUG__
9 #pragma implementation "sndwav.cpp"
10 #endif
11
12 #include <wx/wxprec.h>
13
14 #include <wx/stream.h>
15 #include <wx/datstrm.h>
16 #include <wx/filefn.h>
17 #include <wx/mstream.h>
18
19 #include "sndbase.h"
20 #include "sndcodec.h"
21 #include "sndfile.h"
22 #include "sndpcm.h"
23 #include "sndg72x.h"
24 #include "sndwav.h"
25
26 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
27
28 #define RIFF_SIGNATURE BUILD_SIGNATURE('R','I','F','F')
29 #define WAVE_SIGNATURE BUILD_SIGNATURE('W','A','V','E')
30 #define FMT_SIGNATURE BUILD_SIGNATURE('f','m','t',' ')
31 #define DATA_SIGNATURE BUILD_SIGNATURE('d','a','t','a')
32
33 #define HEADER_SIZE 4+4 + 4+4+16 + 4+4
34 // 4+4 => NAME + LEN
35 // 16 => fmt size
36
37 wxSoundWave::wxSoundWave(wxInputStream& stream, wxSoundStream& io_sound)
38 : wxSoundFileStream(stream, io_sound)
39 {
40 m_base_offset = wxInvalidOffset;
41 }
42
43 wxSoundWave::wxSoundWave(wxOutputStream& stream, wxSoundStream& io_sound)
44 : wxSoundFileStream(stream, io_sound)
45 {
46 m_base_offset = wxInvalidOffset;
47 }
48
49 wxSoundWave::~wxSoundWave()
50 {
51 }
52
53 wxString wxSoundWave::GetCodecName() const
54 {
55 return wxString(wxT("wxSoundWave codec"));
56 }
57
58 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
59
60 bool wxSoundWave::CanRead()
61 {
62 wxUint32 len, signature1, signature2;
63 m_snderror = wxSOUND_NOERR;
64
65 // Test the main signatures:
66 // "RIFF"
67 FAIL_WITH(m_input->Read(&signature1, 4).LastRead() != 4, wxSOUND_INVSTRM);
68
69 if (wxUINT32_SWAP_ON_BE(signature1) != RIFF_SIGNATURE) {
70 m_input->Ungetch(&signature1, 4);
71 return FALSE;
72 }
73
74 // Pass the global length
75 m_input->Read(&len, 4);
76 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
77
78 // Get the second signature
79 FAIL_WITH(m_input->Read(&signature2, 4).LastRead() != 4, wxSOUND_INVSTRM);
80 // Ungetch all
81 m_input->Ungetch(&signature2, 4);
82 m_input->Ungetch(&len, 4);
83 m_input->Ungetch(&signature1, 4);
84
85 // Test the second signature
86 if (wxUINT32_SWAP_ON_BE(signature2) != WAVE_SIGNATURE)
87 return FALSE;
88
89 return TRUE;
90 }
91
92 bool wxSoundWave::HandleOutputPCM(wxDataInputStream& data, wxUint16 channels,
93 wxUint32 sample_fq, wxUint32 byte_p_sec,
94 wxUint16 byte_p_spl, wxUint16 bits_p_spl)
95 {
96 wxSoundFormatPcm sndformat;
97
98 sndformat.SetSampleRate(sample_fq);
99 sndformat.SetBPS(bits_p_spl);
100 sndformat.SetChannels(channels);
101 sndformat.Signed(TRUE);
102 sndformat.SetOrder(wxLITTLE_ENDIAN);
103
104 if (!SetSoundFormat(sndformat))
105 return FALSE;
106
107 return TRUE;
108 }
109
110 bool wxSoundWave::HandleOutputG721(wxDataInputStream& data, wxUint16 channels,
111 wxUint32 sample_fq, wxUint32 byte_p_sec,
112 wxUint16 byte_p_spl, wxUint16 bits_p_spl)
113 {
114 wxSoundFormatG72X sndformat;
115
116 sndformat.SetSampleRate(sample_fq);
117 sndformat.SetG72XType(wxSOUND_G721);
118
119 if (!SetSoundFormat(sndformat))
120 return FALSE;
121
122 return TRUE;
123 }
124
125 bool wxSoundWave::PrepareToPlay()
126 {
127 wxUint32 signature, len;
128 bool end_headers;
129
130 if (!m_input) {
131 m_snderror = wxSOUND_INVSTRM;
132 return FALSE;
133 }
134
135 wxDataInputStream data(*m_input);
136 data.BigEndianOrdered(FALSE);
137
138 // Get the first signature
139 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
140 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature) != RIFF_SIGNATURE, wxSOUND_INVSTRM);
141 // "RIFF"
142
143 len = data.Read32();
144 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
145 // dummy len
146
147 // Get the second signature
148 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
149 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature) != WAVE_SIGNATURE, wxSOUND_INVSTRM);
150 // "WAVE"
151
152 end_headers = FALSE;
153 // Chunk loop
154 while (!end_headers) {
155 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
156
157 len = data.Read32();
158 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
159
160 switch (wxUINT32_SWAP_ON_BE(signature)) {
161 case FMT_SIGNATURE: { // "fmt "
162 wxUint16 format, channels, byte_p_spl, bits_p_spl;
163 wxUint32 sample_fq, byte_p_sec;
164
165 // Get the common parameters
166 data >> format >> channels >> sample_fq
167 >> byte_p_sec >> byte_p_spl >> bits_p_spl;
168
169 switch (format) {
170 case 0x01: // PCM
171 if (!HandleOutputPCM(data, channels, sample_fq,
172 byte_p_sec, byte_p_spl, bits_p_spl))
173 return FALSE;
174 break;
175 case 0x40: // G721
176 if (!HandleOutputG721(data, channels, sample_fq,
177 byte_p_sec, byte_p_spl, bits_p_spl))
178 return FALSE;
179 break;
180 default:
181 m_snderror = wxSOUND_NOCODEC;
182 return FALSE;
183 }
184 break;
185 }
186 case DATA_SIGNATURE: // "data"
187 m_base_offset = m_input->TellI();
188 end_headers = TRUE;
189 FinishPreparation(len);
190 break;
191 default:
192 // We pass the chunk
193 m_input->SeekI(len, wxFromCurrent);
194 break;
195 }
196 }
197 return TRUE;
198 }
199
200 wxSoundFormatBase *wxSoundWave::HandleInputPCM(wxDataOutputStream& data)
201 {
202 wxUint16 format, channels, byte_p_spl, bits_p_spl;
203 wxUint32 sample_fq, byte_p_sec;
204 wxSoundFormatPcm *pcm;
205
206 pcm = (wxSoundFormatPcm *)(m_sndformat->Clone());
207
208 // Write block length
209 data.Write32(16);
210
211 sample_fq = pcm->GetSampleRate();
212 bits_p_spl = pcm->GetBPS();
213 channels = pcm->GetChannels();
214 byte_p_spl = pcm->GetBPS() / 8;
215 byte_p_sec = pcm->GetBytesFromTime(1);
216 format = 0x01;
217
218 pcm->Signed(TRUE);
219 pcm->SetOrder(wxLITTLE_ENDIAN);
220
221 data << format << channels << sample_fq
222 << byte_p_sec << byte_p_spl << bits_p_spl;
223
224 return pcm;
225 }
226
227 wxSoundFormatBase *wxSoundWave::HandleInputG72X(wxDataOutputStream& data)
228 {
229 wxUint16 format, channels, byte_p_spl, bits_p_spl;
230 wxUint32 sample_fq, byte_p_sec;
231 wxSoundFormatG72X *g72x;
232
233 // Write block length
234 data.Write32(16);
235
236 g72x = (wxSoundFormatG72X *)(m_sndformat->Clone());
237 if (g72x->GetG72XType() != wxSOUND_G721) {
238 delete g72x;
239 return NULL;
240 }
241
242 sample_fq = g72x->GetSampleRate();
243 bits_p_spl = 4;
244 channels = 1;
245 byte_p_spl = 0;
246 byte_p_sec = g72x->GetBytesFromTime(1);
247 format = 0x40;
248 data << format << channels << sample_fq
249 << byte_p_sec << byte_p_spl << bits_p_spl;
250
251 return g72x;
252 }
253
254 bool wxSoundWave::PrepareToRecord(wxUint32 time)
255 {
256 #define WRITE_SIGNATURE(s,sig) \
257 signature = sig; \
258 signature = wxUINT32_SWAP_ON_BE(signature); \
259 FAIL_WITH(s->Write(&signature, 4).LastWrite() != 4, wxSOUND_INVSTRM);
260
261 wxUint32 signature;
262 wxMemoryOutputStream fmt_data;
263
264 if (!m_output) {
265 m_snderror = wxSOUND_INVSTRM;
266 return FALSE;
267 }
268
269 wxDataOutputStream data(*m_output);
270 wxDataOutputStream fmt_d_data(fmt_data);
271
272 data.BigEndianOrdered(FALSE);
273 fmt_d_data.BigEndianOrdered(FALSE);
274
275 WRITE_SIGNATURE(m_output, RIFF_SIGNATURE);
276
277 FAIL_WITH(m_output->LastWrite() != 4, wxSOUND_INVSTRM);
278
279 WRITE_SIGNATURE((&fmt_data), WAVE_SIGNATURE);
280
281 {
282 wxSoundFormatBase *frmt;
283
284 WRITE_SIGNATURE((&fmt_data), FMT_SIGNATURE);
285
286 switch (m_sndformat->GetType()) {
287 case wxSOUND_PCM:
288 frmt = HandleInputPCM(fmt_d_data);
289 break;
290 case wxSOUND_G72X:
291 frmt = HandleInputG72X(fmt_d_data);
292 break;
293 default:
294 m_snderror = wxSOUND_NOCODEC;
295 return FALSE;
296 }
297
298 FAIL_WITH(!frmt, wxSOUND_NOCODEC);
299
300 if (!SetSoundFormat(*frmt)) {
301 delete frmt;
302 return FALSE;
303 }
304
305 delete frmt;
306 }
307
308 data << (fmt_data.GetSize() + m_sndformat->GetBytesFromTime(time));
309
310 // We, finally, copy the header block to the output stream
311 {
312 char *out_buf;
313 out_buf = new char[fmt_data.GetSize()];
314
315 fmt_data.CopyTo(out_buf, fmt_data.GetSize());
316 m_output->Write(out_buf, fmt_data.GetSize());
317
318 delete[] out_buf;
319 }
320
321 WRITE_SIGNATURE(m_output, DATA_SIGNATURE);
322 data.Write32(m_sndformat->GetBytesFromTime(time));
323 return TRUE;
324 }
325
326 bool wxSoundWave::FinishRecording()
327 {
328 if (m_output->SeekO(0, wxFromStart) == wxInvalidOffset)
329 // We can't but there is no error.
330 return TRUE;
331
332 if (m_bytes_left == 0)
333 return TRUE;
334
335 // TODO: Update headers when we stop before the specified time (if possible)
336 return TRUE;
337 }
338
339 bool wxSoundWave::RepositionStream(wxUint32 position)
340 {
341 if (m_base_offset == wxInvalidOffset)
342 return FALSE;
343 m_input->SeekI(m_base_offset, wxFromStart);
344 return TRUE;
345 }
346
347 wxUint32 wxSoundWave::GetData(void *buffer, wxUint32 len)
348 {
349 return m_input->Read(buffer, len).LastRead();
350 }
351
352 wxUint32 wxSoundWave::PutData(const void *buffer, wxUint32 len)
353 {
354 return m_output->Write(buffer, len).LastWrite();
355 }