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"
173 m_input
->SeekI(len
, wxFromCurrent
);
180 wxSoundFormatBase
*wxSoundWave::HandleInputPCM(wxDataOutputStream
& data
)
182 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
183 wxUint32 sample_fq
, byte_p_sec
;
184 wxSoundFormatPcm
*pcm
;
186 pcm
= (wxSoundFormatPcm
*)(m_sndformat
->Clone());
188 // Write block length
191 sample_fq
= pcm
->GetSampleRate();
192 bits_p_spl
= pcm
->GetBPS();
193 channels
= pcm
->GetChannels();
194 byte_p_spl
= pcm
->GetBPS() / 8;
195 byte_p_sec
= pcm
->GetBytesFromTime(1);
199 pcm
->SetOrder(wxLITTLE_ENDIAN
);
201 data
<< format
<< channels
<< sample_fq
202 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
207 wxSoundFormatBase
*wxSoundWave::HandleInputG72X(wxDataOutputStream
& data
)
209 wxUint16 format
, channels
, byte_p_spl
, bits_p_spl
;
210 wxUint32 sample_fq
, byte_p_sec
;
211 wxSoundFormatG72X
*g72x
;
213 // Write block length
216 g72x
= (wxSoundFormatG72X
*)(m_sndformat
->Clone());
217 if (g72x
->GetG72XType() != wxSOUND_G721
) {
222 sample_fq
= g72x
->GetSampleRate();
226 byte_p_sec
= g72x
->GetBytesFromTime(1);
228 data
<< format
<< channels
<< sample_fq
229 << byte_p_sec
<< byte_p_spl
<< bits_p_spl
;
234 bool wxSoundWave::PrepareToRecord(unsigned long time
)
236 #define WRITE_SIGNATURE(s,sig) \
238 signature = wxUINT32_SWAP_ON_BE(signature); \
239 FAIL_WITH(s->Write(&signature, 4).LastWrite() != 4, wxSOUND_INVSTRM);
242 wxMemoryOutputStream fmt_data
;
245 m_snderror
= wxSOUND_INVSTRM
;
249 wxDataOutputStream
data(*m_output
);
250 wxDataOutputStream
fmt_d_data(fmt_data
);
252 data
.BigEndianOrdered(FALSE
);
253 fmt_d_data
.BigEndianOrdered(FALSE
);
255 WRITE_SIGNATURE(m_output
, RIFF_SIGNATURE
);
257 FAIL_WITH(m_output
->LastWrite() != 4, wxSOUND_INVSTRM
);
259 WRITE_SIGNATURE((&fmt_data
), WAVE_SIGNATURE
);
262 wxSoundFormatBase
*frmt
;
264 WRITE_SIGNATURE((&fmt_data
), FMT_SIGNATURE
);
266 switch (m_sndformat
->GetType()) {
268 frmt
= HandleInputPCM(fmt_d_data
);
271 frmt
= HandleInputG72X(fmt_d_data
);
274 m_snderror
= wxSOUND_NOCODEC
;
278 FAIL_WITH(!frmt
, wxSOUND_NOCODEC
);
280 if (!SetSoundFormat(*frmt
)) {
288 data
<< (fmt_data
.GetSize() + 8 + m_sndformat
->GetBytesFromTime(time
));
292 out_buf
= new char[fmt_data
.GetSize()];
294 fmt_data
.CopyTo(out_buf
, fmt_data
.GetSize());
295 m_output
->Write(out_buf
, fmt_data
.GetSize());
300 WRITE_SIGNATURE(m_output
, DATA_SIGNATURE
);
301 data
.Write32(m_sndformat
->GetBytesFromTime(time
));
305 bool wxSoundWave::FinishRecording()
307 // TODO: Update headers when we stop before the specified time (if possible)
311 wxUint32
wxSoundWave::GetData(void *buffer
, wxUint32 len
)
313 return m_input
->Read(buffer
, len
).LastRead();
316 wxUint32
wxSoundWave::PutData(const void *buffer
, wxUint32 len
)
318 return m_output
->Write(buffer
, len
).LastWrite();