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
)
42 wxSoundWave::wxSoundWave(wxOutputStream
& stream
, wxSoundStream
& io_sound
)
43 : wxSoundFileStream(stream
, io_sound
)
47 wxSoundWave::~wxSoundWave()
51 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
53 bool wxSoundWave::CanRead()
55 wxUint32 len
, signature1
, signature2
;
56 m_snderror
= wxSOUND_NOERR
;
58 FAIL_WITH(m_input
->Read(&signature1
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
60 if (wxUINT32_SWAP_ON_BE(signature1
) != RIFF_SIGNATURE
) {
61 m_input
->Ungetch(&signature1
, 4);
65 m_input
->Read(&len
, 4);
66 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
68 FAIL_WITH(m_input
->Read(&signature2
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
69 m_input
->Ungetch(&signature2
, 4);
70 m_input
->Ungetch(&len
, 4);
71 m_input
->Ungetch(&signature1
, 4);
73 if (wxUINT32_SWAP_ON_BE(signature2
) != WAVE_SIGNATURE
)
79 bool wxSoundWave::HandleOutputPCM(wxDataInputStream
& data
, wxUint16 channels
,
80 wxUint32 sample_fq
, wxUint32 byte_p_sec
,
81 wxUint16 byte_p_spl
, wxUint16 bits_p_spl
)
83 wxSoundFormatPcm sndformat
;
85 sndformat
.SetSampleRate(sample_fq
);
86 sndformat
.SetBPS(bits_p_spl
);
87 sndformat
.SetChannels(channels
);
88 sndformat
.Signed(TRUE
);
89 sndformat
.SetOrder(wxLITTLE_ENDIAN
);
91 if (!SetSoundFormat(sndformat
))
97 bool wxSoundWave::HandleOutputG721(wxDataInputStream
& data
, wxUint16 channels
,
98 wxUint32 sample_fq
, wxUint32 byte_p_sec
,
99 wxUint16 byte_p_spl
, wxUint16 bits_p_spl
)
101 wxSoundFormatG72X sndformat
;
103 sndformat
.SetSampleRate(sample_fq
);
104 sndformat
.SetG72XType(wxSOUND_G721
);
106 if (!SetSoundFormat(sndformat
))
112 bool wxSoundWave::PrepareToPlay()
114 wxUint32 signature
, len
;
118 m_snderror
= wxSOUND_INVSTRM
;
122 wxDataInputStream
data(*m_input
);
123 data
.BigEndianOrdered(FALSE
);
125 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
126 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != RIFF_SIGNATURE
, wxSOUND_INVSTRM
);
130 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
133 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
134 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != WAVE_SIGNATURE
, wxSOUND_INVSTRM
);
138 while (!end_headers
) {
139 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
142 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
144 switch (wxUINT32_SWAP_ON_BE(signature
)) {
145 case FMT_SIGNATURE
: { // "fmt "
146 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
147 wxUint32 sample_fq
, byte_p_sec
;
149 data
>> format
>> channels
>> sample_fq
150 >> byte_p_sec
>> byte_p_spl
>> bits_p_spl
;
154 if (!HandleOutputPCM(data
, channels
, sample_fq
,
155 byte_p_sec
, byte_p_spl
, bits_p_spl
))
159 if (!HandleOutputG721(data
, channels
, sample_fq
,
160 byte_p_sec
, byte_p_spl
, bits_p_spl
))
164 m_snderror
= wxSOUND_NOCODEC
;
169 case DATA_SIGNATURE
: // "data"
171 FinishPreparation(len
);
174 m_input
->SeekI(len
, wxFromCurrent
);
181 wxSoundFormatBase
*wxSoundWave::HandleInputPCM(wxDataOutputStream
& data
)
183 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
184 wxUint32 sample_fq
, byte_p_sec
;
185 wxSoundFormatPcm
*pcm
;
187 pcm
= (wxSoundFormatPcm
*)(m_sndformat
->Clone());
189 // Write block length
192 sample_fq
= pcm
->GetSampleRate();
193 bits_p_spl
= pcm
->GetBPS();
194 channels
= pcm
->GetChannels();
195 byte_p_spl
= pcm
->GetBPS() / 8;
196 byte_p_sec
= pcm
->GetBytesFromTime(1);
200 pcm
->SetOrder(wxLITTLE_ENDIAN
);
202 data
<< format
<< channels
<< sample_fq
203 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
208 wxSoundFormatBase
*wxSoundWave::HandleInputG72X(wxDataOutputStream
& data
)
210 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
211 wxUint32 sample_fq
, byte_p_sec
;
212 wxSoundFormatG72X
*g72x
;
214 // Write block length
217 g72x
= (wxSoundFormatG72X
*)(m_sndformat
->Clone());
218 if (g72x
->GetG72XType() != wxSOUND_G721
) {
223 sample_fq
= g72x
->GetSampleRate();
227 byte_p_sec
= g72x
->GetBytesFromTime(1);
229 data
<< format
<< channels
<< sample_fq
230 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
235 bool wxSoundWave::PrepareToRecord(unsigned long time
)
237 #define WRITE_SIGNATURE(s,sig) \
239 signature = wxUINT32_SWAP_ON_BE(signature); \
240 FAIL_WITH(s->Write(&signature, 4).LastWrite() != 4, wxSOUND_INVSTRM);
243 wxMemoryOutputStream fmt_data
;
246 m_snderror
= wxSOUND_INVSTRM
;
250 wxDataOutputStream
data(*m_output
);
251 wxDataOutputStream
fmt_d_data(fmt_data
);
253 data
.BigEndianOrdered(FALSE
);
254 fmt_d_data
.BigEndianOrdered(FALSE
);
256 WRITE_SIGNATURE(m_output
, RIFF_SIGNATURE
);
258 FAIL_WITH(m_output
->LastWrite() != 4, wxSOUND_INVSTRM
);
260 WRITE_SIGNATURE((&fmt_data
), WAVE_SIGNATURE
);
263 wxSoundFormatBase
*frmt
;
265 WRITE_SIGNATURE((&fmt_data
), FMT_SIGNATURE
);
267 switch (m_sndformat
->GetType()) {
269 frmt
= HandleInputPCM(fmt_d_data
);
272 frmt
= HandleInputG72X(fmt_d_data
);
275 m_snderror
= wxSOUND_NOCODEC
;
279 FAIL_WITH(!frmt
, wxSOUND_NOCODEC
);
281 if (!SetSoundFormat(*frmt
)) {
289 data
<< (fmt_data
.GetSize() + m_sndformat
->GetBytesFromTime(time
));
293 out_buf
= new char[fmt_data
.GetSize()];
295 fmt_data
.CopyTo(out_buf
, fmt_data
.GetSize());
296 m_output
->Write(out_buf
, fmt_data
.GetSize());
301 WRITE_SIGNATURE(m_output
, DATA_SIGNATURE
);
302 data
.Write32(m_sndformat
->GetBytesFromTime(time
));
306 bool wxSoundWave::FinishRecording()
308 if (m_output
->SeekO(0, wxFromStart
) == wxInvalidOffset
)
309 // We can't but there is no error.
312 if (m_bytes_left
== 0)
315 // TODO: Update headers when we stop before the specified time (if possible)
319 wxUint32
wxSoundWave::GetData(void *buffer
, wxUint32 len
)
321 return m_input
->Read(buffer
, len
).LastRead();
324 wxUint32
wxSoundWave::PutData(const void *buffer
, wxUint32 len
)
326 return m_output
->Write(buffer
, len
).LastWrite();