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"
24 #include "wx/mmedia/sndbase.h"
25 #include "wx/mmedia/sndcodec.h"
26 #include "wx/mmedia/sndfile.h"
27 #include "wx/mmedia/sndpcm.h"
28 #include "wx/mmedia/sndaiff.h"
30 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
32 #define FORM_SIGNATURE BUILD_SIGNATURE('F','O','R','M')
33 #define AIFF_SIGNATURE BUILD_SIGNATURE('A','I','F','F')
34 #define AIFC_SIGNATURE BUILD_SIGNATURE('A','I','F','C')
35 #define COMM_SIGNATURE BUILD_SIGNATURE('C','O','M','M')
36 #define SSND_SIGNATURE BUILD_SIGNATURE('S','S','N','D')
38 wxSoundAiff::wxSoundAiff(wxInputStream
& stream
, wxSoundStream
& io_sound
)
39 : wxSoundFileStream(stream
, io_sound
)
41 m_base_offset
= wxInvalidOffset
;
44 wxSoundAiff::wxSoundAiff(wxOutputStream
& stream
, wxSoundStream
& io_sound
)
45 : wxSoundFileStream(stream
, io_sound
)
47 m_base_offset
= wxInvalidOffset
;
50 wxSoundAiff::~wxSoundAiff()
54 wxString
wxSoundAiff::GetCodecName() const
56 return wxT("wxSoundAiff codec");
59 bool wxSoundAiff::CanRead()
61 wxUint32 signature1
, signature2
, len
;
63 if (m_input
->Read(&signature1
, 4).LastRead() != 4)
66 if (wxUINT32_SWAP_ON_BE(signature1
) != FORM_SIGNATURE
) {
67 m_input
->Ungetch(&signature1
, 4);
71 m_input
->Read(&len
, 4);
72 if (m_input
->LastRead() != 4) {
73 m_input
->Ungetch(&len
, m_input
->LastRead());
74 m_input
->Ungetch(&signature1
, 4);
78 if (m_input
->Read(&signature2
, 4).LastRead() != 4) {
79 m_input
->Ungetch(&signature2
, m_input
->LastRead());
80 m_input
->Ungetch(&len
, 4);
81 m_input
->Ungetch(&signature1
, 4);
85 m_input
->Ungetch(&signature2
, 4);
86 m_input
->Ungetch(&len
, 4);
87 m_input
->Ungetch(&signature1
, 4);
90 wxUINT32_SWAP_ON_BE(signature2
) != AIFF_SIGNATURE
&&
91 wxUINT32_SWAP_ON_BE(signature2
) != AIFC_SIGNATURE
)
97 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return false; }
99 bool wxSoundAiff::PrepareToPlay()
101 wxDataInputStream
data(*m_input
);
102 wxUint32 signature
, len
, ssnd
;
106 m_snderror
= wxSOUND_INVSTRM
;
109 m_snderror
= wxSOUND_NOERROR
;
111 data
.BigEndianOrdered(true);
113 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
114 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != FORM_SIGNATURE
, wxSOUND_INVSTRM
);
119 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
122 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
124 wxUINT32_SWAP_ON_BE(signature
) != AIFF_SIGNATURE
&&
125 wxUINT32_SWAP_ON_BE(signature
) != AIFC_SIGNATURE
, wxSOUND_INVSTRM
);
129 while (!end_headers
) {
130 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
133 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
135 switch (wxUINT32_SWAP_ON_BE(signature
)) {
136 case COMM_SIGNATURE
: { // "COMM"
137 wxUint16 channels
, bps
;
138 wxUint32 num_samples
;
140 wxSoundFormatPcm sndformat
;
142 // Get sound data informations
143 data
>> channels
>> num_samples
>> bps
>> srate
;
145 // Convert them in a wxSoundFormat object
146 sndformat
.SetSampleRate((wxUint32
) srate
);
147 sndformat
.SetBPS(bps
);
148 sndformat
.SetChannels(channels
);
149 sndformat
.Signed(false);
150 sndformat
.SetOrder(wxBIG_ENDIAN
);
152 if (!SetSoundFormat(sndformat
))
154 // We pass all data left
155 m_input
->SeekI(len
-18, wxFromCurrent
);
158 case SSND_SIGNATURE
: { // "SSND"
160 // m_input->SeekI(4, wxFromCurrent); // Pass an INT32
161 // m_input->SeekI(len-4, wxFromCurrent); // Pass the rest
162 m_input
->SeekI(ssnd
+ 4, wxFromCurrent
);
163 m_base_offset
= m_input
->TellI();
164 // len-8 bytes of samples
165 FinishPreparation(len
- 8);
170 m_input
->SeekI(len
, wxFromCurrent
);
177 bool wxSoundAiff::PrepareToRecord(wxUint32
WXUNUSED(time
))
183 bool wxSoundAiff::FinishRecording()
189 bool wxSoundAiff::RepositionStream(wxUint32
WXUNUSED(position
))
191 // If the stream is not seekable "TellI() returns wxInvalidOffset" we cannot reposition stream
192 if (m_base_offset
== wxInvalidOffset
)
194 m_input
->SeekI(m_base_offset
, wxFromStart
);
198 wxUint32
wxSoundAiff::GetData(void *buffer
, wxUint32 len
)
200 return m_input
->Read(buffer
, len
).LastRead();
203 wxUint32
wxSoundAiff::PutData(const void *buffer
, wxUint32 len
)
205 return m_output
->Write(buffer
, len
).LastWrite();