1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
8 // --------------------------------------------------------------------------
10 #include "wx/wxprec.h"
20 #include "wx/stream.h"
21 #include "wx/datstrm.h"
22 #include "wx/filefn.h"
23 #include "wx/mstream.h"
25 #include "wx/mmedia/sndbase.h"
26 #include "wx/mmedia/sndcodec.h"
27 #include "wx/mmedia/sndfile.h"
28 #include "wx/mmedia/sndpcm.h"
29 #include "wx/mmedia/sndg72x.h"
30 #include "wx/mmedia/sndmsad.h"
31 #include "wx/mmedia/sndwav.h"
33 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
35 #define RIFF_SIGNATURE BUILD_SIGNATURE('R','I','F','F')
36 #define WAVE_SIGNATURE BUILD_SIGNATURE('W','A','V','E')
37 #define FMT_SIGNATURE BUILD_SIGNATURE('f','m','t',' ')
38 #define DATA_SIGNATURE BUILD_SIGNATURE('d','a','t','a')
40 #define HEADER_SIZE 4+4 + 4+4+16 + 4+4
44 wxSoundWave::wxSoundWave(wxInputStream
& stream
, wxSoundStream
& io_sound
)
45 : wxSoundFileStream(stream
, io_sound
)
47 m_base_offset
= wxInvalidOffset
;
50 wxSoundWave::wxSoundWave(wxOutputStream
& stream
, wxSoundStream
& io_sound
)
51 : wxSoundFileStream(stream
, io_sound
)
53 m_base_offset
= wxInvalidOffset
;
56 wxSoundWave::~wxSoundWave()
60 wxString
wxSoundWave::GetCodecName() const
62 return wxString(wxT("wxSoundWave codec"));
65 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return false; }
67 bool wxSoundWave::CanRead()
69 wxUint32 len
, signature1
, signature2
;
70 m_snderror
= wxSOUND_NOERROR
;
72 // Test the main signatures:
74 FAIL_WITH(m_input
->Read(&signature1
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
76 if (wxUINT32_SWAP_ON_BE(signature1
) != RIFF_SIGNATURE
) {
77 m_input
->Ungetch(&signature1
, 4);
81 // Pass the global length
82 m_input
->Read(&len
, 4);
83 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
85 // Get the second signature
86 FAIL_WITH(m_input
->Read(&signature2
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
88 m_input
->Ungetch(&signature2
, 4);
89 m_input
->Ungetch(&len
, 4);
90 m_input
->Ungetch(&signature1
, 4);
92 // Test the second signature
93 if (wxUINT32_SWAP_ON_BE(signature2
) != WAVE_SIGNATURE
)
99 bool wxSoundWave::HandleOutputPCM(wxDataInputStream
& WXUNUSED(data
), wxUint32 len
,
101 wxUint32 sample_fq
, wxUint32
WXUNUSED(byte_p_sec
),
102 wxUint16
WXUNUSED(byte_p_spl
), wxUint16 bits_p_spl
)
104 wxSoundFormatPcm sndformat
;
106 sndformat
.SetSampleRate(sample_fq
);
107 sndformat
.SetBPS(bits_p_spl
);
108 sndformat
.SetChannels(channels
);
109 sndformat
.Signed(true);
110 sndformat
.SetOrder(wxLITTLE_ENDIAN
);
112 if (!SetSoundFormat(sndformat
))
115 m_input
->SeekI(len
, wxFromCurrent
);
120 bool wxSoundWave::HandleOutputMSADPCM(wxDataInputStream
& data
, wxUint32 len
,
122 wxUint32 sample_fq
, wxUint32
WXUNUSED(byte_p_sec
),
123 wxUint16
WXUNUSED(byte_p_spl
), wxUint16
WXUNUSED(bits_p_spl
))
125 wxSoundFormatMSAdpcm sndformat
;
127 wxUint16 coefs_len
, i
;
130 sndformat
.SetSampleRate(sample_fq
);
131 sndformat
.SetChannels(channels
);
133 block_size
= data
.Read16();
134 coefs_len
= data
.Read16();
136 coefs
[0] = new wxInt16
[coefs_len
];
137 coefs
[1] = new wxInt16
[coefs_len
];
139 for (i
=0;i
<coefs_len
;i
++) {
140 coefs
[0][i
] = data
.Read16();
141 coefs
[1][i
] = data
.Read16();
144 sndformat
.SetCoefs(coefs
, 2, coefs_len
);
145 sndformat
.SetBlockSize(block_size
);
150 if (!SetSoundFormat(sndformat
))
153 len
-= coefs_len
*4 + 4;
155 m_input
->SeekI(len
, wxFromCurrent
);
160 bool wxSoundWave::HandleOutputG721(wxDataInputStream
& WXUNUSED(data
), wxUint32 len
,
161 wxUint16
WXUNUSED(channels
),
162 wxUint32 sample_fq
, wxUint32
WXUNUSED(byte_p_sec
),
163 wxUint16
WXUNUSED(byte_p_spl
), wxUint16
WXUNUSED(bits_p_spl
))
165 wxSoundFormatG72X sndformat
;
167 sndformat
.SetSampleRate(sample_fq
);
168 sndformat
.SetG72XType(wxSOUND_G721
);
170 if (!SetSoundFormat(sndformat
))
173 m_input
->SeekI(len
, wxFromCurrent
);
178 bool wxSoundWave::PrepareToPlay()
180 wxUint32 signature
, len
;
184 m_snderror
= wxSOUND_INVSTRM
;
188 wxDataInputStream
data(*m_input
);
189 data
.BigEndianOrdered(false);
191 // Get the first signature
192 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
193 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != RIFF_SIGNATURE
, wxSOUND_INVSTRM
);
198 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
201 // Get the second signature
202 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
203 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != WAVE_SIGNATURE
, wxSOUND_INVSTRM
);
208 while (!end_headers
) {
209 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
212 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
214 switch (wxUINT32_SWAP_ON_BE(signature
)) {
215 case FMT_SIGNATURE
: { // "fmt "
216 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
217 wxUint32 sample_fq
, byte_p_sec
;
219 // Get the common parameters
220 data
>> format
>> channels
>> sample_fq
221 >> byte_p_sec
>> byte_p_spl
>> bits_p_spl
;
226 if (!HandleOutputPCM(data
, len
, channels
, sample_fq
,
227 byte_p_sec
, byte_p_spl
,
231 case 0x02: // MS ADPCM
232 if (!HandleOutputMSADPCM(data
, len
,
234 byte_p_sec
, byte_p_spl
,
239 if (!HandleOutputG721(data
, len
,
241 byte_p_sec
, byte_p_spl
,
246 m_snderror
= wxSOUND_NOCODEC
;
251 case DATA_SIGNATURE
: // "data"
252 m_base_offset
= m_input
->TellI();
254 FinishPreparation(len
);
258 m_input
->SeekI(len
, wxFromCurrent
);
265 wxSoundFormatBase
*wxSoundWave::HandleInputPCM(wxDataOutputStream
& data
)
267 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
268 wxUint32 sample_fq
, byte_p_sec
;
269 wxSoundFormatPcm
*pcm
;
271 pcm
= (wxSoundFormatPcm
*)(m_sndformat
->Clone());
273 // Write block length
276 sample_fq
= pcm
->GetSampleRate();
277 bits_p_spl
= pcm
->GetBPS();
278 channels
= pcm
->GetChannels();
279 byte_p_spl
= pcm
->GetBPS() / 8;
280 byte_p_sec
= pcm
->GetBytesFromTime(1);
284 pcm
->SetOrder(wxLITTLE_ENDIAN
);
286 data
<< format
<< channels
<< sample_fq
287 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
292 wxSoundFormatBase
*wxSoundWave::HandleInputG72X(wxDataOutputStream
& data
)
294 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
295 wxUint32 sample_fq
, byte_p_sec
;
296 wxSoundFormatG72X
*g72x
;
298 // Write block length
301 g72x
= (wxSoundFormatG72X
*)(m_sndformat
->Clone());
302 if (g72x
->GetG72XType() != wxSOUND_G721
) {
307 sample_fq
= g72x
->GetSampleRate();
311 byte_p_sec
= g72x
->GetBytesFromTime(1);
313 data
<< format
<< channels
<< sample_fq
314 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
319 bool wxSoundWave::PrepareToRecord(wxUint32 time
)
321 #define WRITE_SIGNATURE(s,sig) \
323 signature = wxUINT32_SWAP_ON_BE(signature); \
324 FAIL_WITH(s->Write(&signature, 4).LastWrite() != 4, wxSOUND_INVSTRM);
327 wxMemoryOutputStream fmt_data
;
330 m_snderror
= wxSOUND_INVSTRM
;
334 wxDataOutputStream
data(*m_output
);
335 wxDataOutputStream
fmt_d_data(fmt_data
);
337 data
.BigEndianOrdered(false);
338 fmt_d_data
.BigEndianOrdered(false);
340 WRITE_SIGNATURE(m_output
, RIFF_SIGNATURE
);
342 FAIL_WITH(m_output
->LastWrite() != 4, wxSOUND_INVSTRM
);
344 WRITE_SIGNATURE((&fmt_data
), WAVE_SIGNATURE
);
347 wxSoundFormatBase
*frmt
;
349 WRITE_SIGNATURE((&fmt_data
), FMT_SIGNATURE
);
351 switch (m_sndformat
->GetType()) {
353 frmt
= HandleInputPCM(fmt_d_data
);
356 frmt
= HandleInputG72X(fmt_d_data
);
359 m_snderror
= wxSOUND_NOCODEC
;
363 FAIL_WITH(!frmt
, wxSOUND_NOCODEC
);
365 if (!SetSoundFormat(*frmt
)) {
373 data
<< (wxUint32
)(fmt_data
.GetSize() + m_sndformat
->GetBytesFromTime(time
));
375 // We, finally, copy the header block to the output stream
378 out_buf
= new char[fmt_data
.GetSize()];
380 fmt_data
.CopyTo(out_buf
, fmt_data
.GetSize());
381 m_output
->Write(out_buf
, fmt_data
.GetSize());
386 WRITE_SIGNATURE(m_output
, DATA_SIGNATURE
);
387 data
.Write32(m_sndformat
->GetBytesFromTime(time
));
391 bool wxSoundWave::FinishRecording()
393 if (m_output
->SeekO(0, wxFromStart
) == wxInvalidOffset
)
394 // We can't but there is no error.
397 if (m_bytes_left
== 0)
400 // TODO: Update headers when we stop before the specified time (if possible)
404 bool wxSoundWave::RepositionStream(wxUint32
WXUNUSED(position
))
406 if (m_base_offset
== wxInvalidOffset
)
408 m_input
->SeekI(m_base_offset
, wxFromStart
);
412 wxUint32
wxSoundWave::GetData(void *buffer
, wxUint32 len
)
414 return m_input
->Read(buffer
, len
).LastRead();
417 wxUint32
wxSoundWave::PutData(const void *buffer
, wxUint32 len
)
419 return m_output
->Write(buffer
, len
).LastWrite();