1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndwav.cpp"
12 #include "wx/wxprec.h"
22 #include "wx/stream.h"
23 #include "wx/datstrm.h"
24 #include "wx/filefn.h"
25 #include "wx/mstream.h"
27 #include "wx/mmedia/sndbase.h"
28 #include "wx/mmedia/sndcodec.h"
29 #include "wx/mmedia/sndfile.h"
30 #include "wx/mmedia/sndpcm.h"
31 #include "wx/mmedia/sndg72x.h"
32 #include "wx/mmedia/sndmsad.h"
33 #include "wx/mmedia/sndwav.h"
35 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
37 #define RIFF_SIGNATURE BUILD_SIGNATURE('R','I','F','F')
38 #define WAVE_SIGNATURE BUILD_SIGNATURE('W','A','V','E')
39 #define FMT_SIGNATURE BUILD_SIGNATURE('f','m','t',' ')
40 #define DATA_SIGNATURE BUILD_SIGNATURE('d','a','t','a')
42 #define HEADER_SIZE 4+4 + 4+4+16 + 4+4
46 wxSoundWave::wxSoundWave(wxInputStream
& stream
, wxSoundStream
& io_sound
)
47 : wxSoundFileStream(stream
, io_sound
)
49 m_base_offset
= wxInvalidOffset
;
52 wxSoundWave::wxSoundWave(wxOutputStream
& stream
, wxSoundStream
& io_sound
)
53 : wxSoundFileStream(stream
, io_sound
)
55 m_base_offset
= wxInvalidOffset
;
58 wxSoundWave::~wxSoundWave()
62 wxString
wxSoundWave::GetCodecName() const
64 return wxString(wxT("wxSoundWave codec"));
67 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return false; }
69 bool wxSoundWave::CanRead()
71 wxUint32 len
, signature1
, signature2
;
72 m_snderror
= wxSOUND_NOERROR
;
74 // Test the main signatures:
76 FAIL_WITH(m_input
->Read(&signature1
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
78 if (wxUINT32_SWAP_ON_BE(signature1
) != RIFF_SIGNATURE
) {
79 m_input
->Ungetch(&signature1
, 4);
83 // Pass the global length
84 m_input
->Read(&len
, 4);
85 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
87 // Get the second signature
88 FAIL_WITH(m_input
->Read(&signature2
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
90 m_input
->Ungetch(&signature2
, 4);
91 m_input
->Ungetch(&len
, 4);
92 m_input
->Ungetch(&signature1
, 4);
94 // Test the second signature
95 if (wxUINT32_SWAP_ON_BE(signature2
) != WAVE_SIGNATURE
)
101 bool wxSoundWave::HandleOutputPCM(wxDataInputStream
& WXUNUSED(data
), wxUint32 len
,
103 wxUint32 sample_fq
, wxUint32
WXUNUSED(byte_p_sec
),
104 wxUint16
WXUNUSED(byte_p_spl
), wxUint16 bits_p_spl
)
106 wxSoundFormatPcm sndformat
;
108 sndformat
.SetSampleRate(sample_fq
);
109 sndformat
.SetBPS(bits_p_spl
);
110 sndformat
.SetChannels(channels
);
111 sndformat
.Signed(true);
112 sndformat
.SetOrder(wxLITTLE_ENDIAN
);
114 if (!SetSoundFormat(sndformat
))
117 m_input
->SeekI(len
, wxFromCurrent
);
122 bool wxSoundWave::HandleOutputMSADPCM(wxDataInputStream
& data
, wxUint32 len
,
124 wxUint32 sample_fq
, wxUint32
WXUNUSED(byte_p_sec
),
125 wxUint16
WXUNUSED(byte_p_spl
), wxUint16
WXUNUSED(bits_p_spl
))
127 wxSoundFormatMSAdpcm sndformat
;
129 wxUint16 coefs_len
, i
;
132 sndformat
.SetSampleRate(sample_fq
);
133 sndformat
.SetChannels(channels
);
135 block_size
= data
.Read16();
136 coefs_len
= data
.Read16();
138 coefs
[0] = new wxInt16
[coefs_len
];
139 coefs
[1] = new wxInt16
[coefs_len
];
141 for (i
=0;i
<coefs_len
;i
++) {
142 coefs
[0][i
] = data
.Read16();
143 coefs
[1][i
] = data
.Read16();
146 sndformat
.SetCoefs(coefs
, 2, coefs_len
);
147 sndformat
.SetBlockSize(block_size
);
152 if (!SetSoundFormat(sndformat
))
155 len
-= coefs_len
*4 + 4;
157 m_input
->SeekI(len
, wxFromCurrent
);
162 bool wxSoundWave::HandleOutputG721(wxDataInputStream
& WXUNUSED(data
), wxUint32 len
,
163 wxUint16
WXUNUSED(channels
),
164 wxUint32 sample_fq
, wxUint32
WXUNUSED(byte_p_sec
),
165 wxUint16
WXUNUSED(byte_p_spl
), wxUint16
WXUNUSED(bits_p_spl
))
167 wxSoundFormatG72X sndformat
;
169 sndformat
.SetSampleRate(sample_fq
);
170 sndformat
.SetG72XType(wxSOUND_G721
);
172 if (!SetSoundFormat(sndformat
))
175 m_input
->SeekI(len
, wxFromCurrent
);
180 bool wxSoundWave::PrepareToPlay()
182 wxUint32 signature
, len
;
186 m_snderror
= wxSOUND_INVSTRM
;
190 wxDataInputStream
data(*m_input
);
191 data
.BigEndianOrdered(false);
193 // Get the first signature
194 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
195 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != RIFF_SIGNATURE
, wxSOUND_INVSTRM
);
200 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
203 // Get the second signature
204 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
205 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != WAVE_SIGNATURE
, wxSOUND_INVSTRM
);
210 while (!end_headers
) {
211 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
214 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
216 switch (wxUINT32_SWAP_ON_BE(signature
)) {
217 case FMT_SIGNATURE
: { // "fmt "
218 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
219 wxUint32 sample_fq
, byte_p_sec
;
221 // Get the common parameters
222 data
>> format
>> channels
>> sample_fq
223 >> byte_p_sec
>> byte_p_spl
>> bits_p_spl
;
228 if (!HandleOutputPCM(data
, len
, channels
, sample_fq
,
229 byte_p_sec
, byte_p_spl
,
233 case 0x02: // MS ADPCM
234 if (!HandleOutputMSADPCM(data
, len
,
236 byte_p_sec
, byte_p_spl
,
241 if (!HandleOutputG721(data
, len
,
243 byte_p_sec
, byte_p_spl
,
248 m_snderror
= wxSOUND_NOCODEC
;
253 case DATA_SIGNATURE
: // "data"
254 m_base_offset
= m_input
->TellI();
256 FinishPreparation(len
);
260 m_input
->SeekI(len
, wxFromCurrent
);
267 wxSoundFormatBase
*wxSoundWave::HandleInputPCM(wxDataOutputStream
& data
)
269 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
270 wxUint32 sample_fq
, byte_p_sec
;
271 wxSoundFormatPcm
*pcm
;
273 pcm
= (wxSoundFormatPcm
*)(m_sndformat
->Clone());
275 // Write block length
278 sample_fq
= pcm
->GetSampleRate();
279 bits_p_spl
= pcm
->GetBPS();
280 channels
= pcm
->GetChannels();
281 byte_p_spl
= pcm
->GetBPS() / 8;
282 byte_p_sec
= pcm
->GetBytesFromTime(1);
286 pcm
->SetOrder(wxLITTLE_ENDIAN
);
288 data
<< format
<< channels
<< sample_fq
289 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
294 wxSoundFormatBase
*wxSoundWave::HandleInputG72X(wxDataOutputStream
& data
)
296 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
297 wxUint32 sample_fq
, byte_p_sec
;
298 wxSoundFormatG72X
*g72x
;
300 // Write block length
303 g72x
= (wxSoundFormatG72X
*)(m_sndformat
->Clone());
304 if (g72x
->GetG72XType() != wxSOUND_G721
) {
309 sample_fq
= g72x
->GetSampleRate();
313 byte_p_sec
= g72x
->GetBytesFromTime(1);
315 data
<< format
<< channels
<< sample_fq
316 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
321 bool wxSoundWave::PrepareToRecord(wxUint32 time
)
323 #define WRITE_SIGNATURE(s,sig) \
325 signature = wxUINT32_SWAP_ON_BE(signature); \
326 FAIL_WITH(s->Write(&signature, 4).LastWrite() != 4, wxSOUND_INVSTRM);
329 wxMemoryOutputStream fmt_data
;
332 m_snderror
= wxSOUND_INVSTRM
;
336 wxDataOutputStream
data(*m_output
);
337 wxDataOutputStream
fmt_d_data(fmt_data
);
339 data
.BigEndianOrdered(false);
340 fmt_d_data
.BigEndianOrdered(false);
342 WRITE_SIGNATURE(m_output
, RIFF_SIGNATURE
);
344 FAIL_WITH(m_output
->LastWrite() != 4, wxSOUND_INVSTRM
);
346 WRITE_SIGNATURE((&fmt_data
), WAVE_SIGNATURE
);
349 wxSoundFormatBase
*frmt
;
351 WRITE_SIGNATURE((&fmt_data
), FMT_SIGNATURE
);
353 switch (m_sndformat
->GetType()) {
355 frmt
= HandleInputPCM(fmt_d_data
);
358 frmt
= HandleInputG72X(fmt_d_data
);
361 m_snderror
= wxSOUND_NOCODEC
;
365 FAIL_WITH(!frmt
, wxSOUND_NOCODEC
);
367 if (!SetSoundFormat(*frmt
)) {
375 data
<< (fmt_data
.GetSize() + m_sndformat
->GetBytesFromTime(time
));
377 // We, finally, copy the header block to the output stream
380 out_buf
= new char[fmt_data
.GetSize()];
382 fmt_data
.CopyTo(out_buf
, fmt_data
.GetSize());
383 m_output
->Write(out_buf
, fmt_data
.GetSize());
388 WRITE_SIGNATURE(m_output
, DATA_SIGNATURE
);
389 data
.Write32(m_sndformat
->GetBytesFromTime(time
));
393 bool wxSoundWave::FinishRecording()
395 if (m_output
->SeekO(0, wxFromStart
) == wxInvalidOffset
)
396 // We can't but there is no error.
399 if (m_bytes_left
== 0)
402 // TODO: Update headers when we stop before the specified time (if possible)
406 bool wxSoundWave::RepositionStream(wxUint32
WXUNUSED(position
))
408 if (m_base_offset
== wxInvalidOffset
)
410 m_input
->SeekI(m_base_offset
, wxFromStart
);
414 wxUint32
wxSoundWave::GetData(void *buffer
, wxUint32 len
)
416 return m_input
->Read(buffer
, len
).LastRead();
419 wxUint32
wxSoundWave::PutData(const void *buffer
, wxUint32 len
)
421 return m_output
->Write(buffer
, len
).LastWrite();