1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndwav.cpp"
12 #include <wx/wxprec.h>
14 #include <wx/stream.h>
15 #include <wx/datstrm.h>
16 #include <wx/filefn.h>
17 #include <wx/mstream.h>
26 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
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')
33 #define HEADER_SIZE 4+4 + 4+4+16 + 4+4
37 wxSoundWave::wxSoundWave(wxInputStream
& stream
, wxSoundStream
& io_sound
)
38 : wxSoundFileStream(stream
, io_sound
)
40 m_base_offset
= wxInvalidOffset
;
43 wxSoundWave::wxSoundWave(wxOutputStream
& stream
, wxSoundStream
& io_sound
)
44 : wxSoundFileStream(stream
, io_sound
)
46 m_base_offset
= wxInvalidOffset
;
49 wxSoundWave::~wxSoundWave()
53 wxString
wxSoundWave::GetCodecName() const
55 return wxString(wxT("wxSoundWave codec"));
58 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
60 bool wxSoundWave::CanRead()
62 wxUint32 len
, signature1
, signature2
;
63 m_snderror
= wxSOUND_NOERR
;
65 // Test the main signatures:
67 FAIL_WITH(m_input
->Read(&signature1
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
69 if (wxUINT32_SWAP_ON_BE(signature1
) != RIFF_SIGNATURE
) {
70 m_input
->Ungetch(&signature1
, 4);
74 // Pass the global length
75 m_input
->Read(&len
, 4);
76 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
78 // Get the second signature
79 FAIL_WITH(m_input
->Read(&signature2
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
81 m_input
->Ungetch(&signature2
, 4);
82 m_input
->Ungetch(&len
, 4);
83 m_input
->Ungetch(&signature1
, 4);
85 // Test the second signature
86 if (wxUINT32_SWAP_ON_BE(signature2
) != WAVE_SIGNATURE
)
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
)
96 wxSoundFormatPcm sndformat
;
98 sndformat
.SetSampleRate(sample_fq
);
99 sndformat
.SetBPS(bits_p_spl
);
100 sndformat
.SetChannels(channels
);
101 sndformat
.Signed(TRUE
);
102 sndformat
.SetOrder(wxLITTLE_ENDIAN
);
104 if (!SetSoundFormat(sndformat
))
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
)
114 wxSoundFormatG72X sndformat
;
116 sndformat
.SetSampleRate(sample_fq
);
117 sndformat
.SetG72XType(wxSOUND_G721
);
119 if (!SetSoundFormat(sndformat
))
125 bool wxSoundWave::PrepareToPlay()
127 wxUint32 signature
, len
;
131 m_snderror
= wxSOUND_INVSTRM
;
135 wxDataInputStream
data(*m_input
);
136 data
.BigEndianOrdered(FALSE
);
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
);
144 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
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
);
154 while (!end_headers
) {
155 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
158 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
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
;
165 // Get the common parameters
166 data
>> format
>> channels
>> sample_fq
167 >> byte_p_sec
>> byte_p_spl
>> bits_p_spl
;
171 if (!HandleOutputPCM(data
, channels
, sample_fq
,
172 byte_p_sec
, byte_p_spl
, bits_p_spl
))
176 if (!HandleOutputG721(data
, channels
, sample_fq
,
177 byte_p_sec
, byte_p_spl
, bits_p_spl
))
181 m_snderror
= wxSOUND_NOCODEC
;
186 case DATA_SIGNATURE
: // "data"
187 m_base_offset
= m_input
->TellI();
189 FinishPreparation(len
);
193 m_input
->SeekI(len
, wxFromCurrent
);
200 wxSoundFormatBase
*wxSoundWave::HandleInputPCM(wxDataOutputStream
& data
)
202 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
203 wxUint32 sample_fq
, byte_p_sec
;
204 wxSoundFormatPcm
*pcm
;
206 pcm
= (wxSoundFormatPcm
*)(m_sndformat
->Clone());
208 // Write block length
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);
219 pcm
->SetOrder(wxLITTLE_ENDIAN
);
221 data
<< format
<< channels
<< sample_fq
222 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
227 wxSoundFormatBase
*wxSoundWave::HandleInputG72X(wxDataOutputStream
& data
)
229 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
230 wxUint32 sample_fq
, byte_p_sec
;
231 wxSoundFormatG72X
*g72x
;
233 // Write block length
236 g72x
= (wxSoundFormatG72X
*)(m_sndformat
->Clone());
237 if (g72x
->GetG72XType() != wxSOUND_G721
) {
242 sample_fq
= g72x
->GetSampleRate();
246 byte_p_sec
= g72x
->GetBytesFromTime(1);
248 data
<< format
<< channels
<< sample_fq
249 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
254 bool wxSoundWave::PrepareToRecord(wxUint32 time
)
256 #define WRITE_SIGNATURE(s,sig) \
258 signature = wxUINT32_SWAP_ON_BE(signature); \
259 FAIL_WITH(s->Write(&signature, 4).LastWrite() != 4, wxSOUND_INVSTRM);
262 wxMemoryOutputStream fmt_data
;
265 m_snderror
= wxSOUND_INVSTRM
;
269 wxDataOutputStream
data(*m_output
);
270 wxDataOutputStream
fmt_d_data(fmt_data
);
272 data
.BigEndianOrdered(FALSE
);
273 fmt_d_data
.BigEndianOrdered(FALSE
);
275 WRITE_SIGNATURE(m_output
, RIFF_SIGNATURE
);
277 FAIL_WITH(m_output
->LastWrite() != 4, wxSOUND_INVSTRM
);
279 WRITE_SIGNATURE((&fmt_data
), WAVE_SIGNATURE
);
282 wxSoundFormatBase
*frmt
;
284 WRITE_SIGNATURE((&fmt_data
), FMT_SIGNATURE
);
286 switch (m_sndformat
->GetType()) {
288 frmt
= HandleInputPCM(fmt_d_data
);
291 frmt
= HandleInputG72X(fmt_d_data
);
294 m_snderror
= wxSOUND_NOCODEC
;
298 FAIL_WITH(!frmt
, wxSOUND_NOCODEC
);
300 if (!SetSoundFormat(*frmt
)) {
308 data
<< (fmt_data
.GetSize() + m_sndformat
->GetBytesFromTime(time
));
310 // We, finally, copy the header block to the output stream
313 out_buf
= new char[fmt_data
.GetSize()];
315 fmt_data
.CopyTo(out_buf
, fmt_data
.GetSize());
316 m_output
->Write(out_buf
, fmt_data
.GetSize());
321 WRITE_SIGNATURE(m_output
, DATA_SIGNATURE
);
322 data
.Write32(m_sndformat
->GetBytesFromTime(time
));
326 bool wxSoundWave::FinishRecording()
328 if (m_output
->SeekO(0, wxFromStart
) == wxInvalidOffset
)
329 // We can't but there is no error.
332 if (m_bytes_left
== 0)
335 // TODO: Update headers when we stop before the specified time (if possible)
339 bool wxSoundWave::RepositionStream(wxUint32 position
)
341 if (m_base_offset
== wxInvalidOffset
)
343 m_input
->SeekI(m_base_offset
, wxFromStart
);
347 wxUint32
wxSoundWave::GetData(void *buffer
, wxUint32 len
)
349 return m_input
->Read(buffer
, len
).LastRead();
352 wxUint32
wxSoundWave::PutData(const void *buffer
, wxUint32 len
)
354 return m_output
->Write(buffer
, len
).LastWrite();