1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndaiff.cpp"
12 #include "wx/wxprec.h"
22 #include "wx/stream.h"
23 #include "wx/datstrm.h"
24 #include "wx/filefn.h"
26 #include "wx/mmedia/sndbase.h"
27 #include "wx/mmedia/sndcodec.h"
28 #include "wx/mmedia/sndfile.h"
29 #include "wx/mmedia/sndpcm.h"
30 #include "wx/mmedia/sndaiff.h"
32 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
34 #define FORM_SIGNATURE BUILD_SIGNATURE('F','O','R','M')
35 #define AIFF_SIGNATURE BUILD_SIGNATURE('A','I','F','F')
36 #define AIFC_SIGNATURE BUILD_SIGNATURE('A','I','F','C')
37 #define COMM_SIGNATURE BUILD_SIGNATURE('C','O','M','M')
38 #define SSND_SIGNATURE BUILD_SIGNATURE('S','S','N','D')
40 wxSoundAiff::wxSoundAiff(wxInputStream
& stream
, wxSoundStream
& io_sound
)
41 : wxSoundFileStream(stream
, io_sound
)
43 m_base_offset
= wxInvalidOffset
;
46 wxSoundAiff::wxSoundAiff(wxOutputStream
& stream
, wxSoundStream
& io_sound
)
47 : wxSoundFileStream(stream
, io_sound
)
49 m_base_offset
= wxInvalidOffset
;
52 wxSoundAiff::~wxSoundAiff()
56 wxString
wxSoundAiff::GetCodecName() const
58 return "wxSoundAiff codec";
61 bool wxSoundAiff::CanRead()
63 wxUint32 signature1
, signature2
, len
;
65 if (m_input
->Read(&signature1
, 4).LastRead() != 4)
68 if (wxUINT32_SWAP_ON_BE(signature1
) != FORM_SIGNATURE
) {
69 m_input
->Ungetch(&signature1
, 4);
73 m_input
->Read(&len
, 4);
74 if (m_input
->LastRead() != 4) {
75 m_input
->Ungetch(&len
, m_input
->LastRead());
76 m_input
->Ungetch(&signature1
, 4);
80 if (m_input
->Read(&signature2
, 4).LastRead() != 4) {
81 m_input
->Ungetch(&signature2
, m_input
->LastRead());
82 m_input
->Ungetch(&len
, 4);
83 m_input
->Ungetch(&signature1
, 4);
87 m_input
->Ungetch(&signature2
, 4);
88 m_input
->Ungetch(&len
, 4);
89 m_input
->Ungetch(&signature1
, 4);
92 wxUINT32_SWAP_ON_BE(signature2
) != AIFF_SIGNATURE
&&
93 wxUINT32_SWAP_ON_BE(signature2
) != AIFC_SIGNATURE
)
99 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
101 bool wxSoundAiff::PrepareToPlay()
103 wxDataInputStream
data(*m_input
);
104 wxUint32 signature
, len
, ssnd
;
108 m_snderror
= wxSOUND_INVSTRM
;
111 m_snderror
= wxSOUND_NOERROR
;
113 data
.BigEndianOrdered(TRUE
);
115 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
116 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != FORM_SIGNATURE
, wxSOUND_INVSTRM
);
120 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
123 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
125 wxUINT32_SWAP_ON_BE(signature
) != AIFF_SIGNATURE
&&
126 wxUINT32_SWAP_ON_BE(signature
) != AIFC_SIGNATURE
, wxSOUND_INVSTRM
);
130 while (!end_headers
) {
131 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
134 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
136 switch (wxUINT32_SWAP_ON_BE(signature
)) {
137 case COMM_SIGNATURE
: { // "COMM"
138 wxUint16 channels
, bps
;
139 wxUint32 num_samples
;
141 wxSoundFormatPcm sndformat
;
143 // Get sound data informations
144 data
>> channels
>> num_samples
>> bps
>> srate
;
146 // Convert them in a wxSoundFormat object
147 sndformat
.SetSampleRate((wxUint32
) srate
);
148 sndformat
.SetBPS(bps
);
149 sndformat
.SetChannels(channels
);
150 sndformat
.Signed(FALSE
);
151 sndformat
.SetOrder(wxBIG_ENDIAN
);
153 if (!SetSoundFormat(sndformat
))
155 // We pass all data left
156 m_input
->SeekI(len
-18, wxFromCurrent
);
159 case SSND_SIGNATURE
: { // "SSND"
161 // m_input->SeekI(4, wxFromCurrent); // Pass an INT32
162 // m_input->SeekI(len-4, wxFromCurrent); // Pass the rest
163 m_input
->SeekI(ssnd
+ 4, wxFromCurrent
);
164 m_base_offset
= m_input
->TellI();
165 // len-8 bytes of samples
166 FinishPreparation(len
- 8);
171 m_input
->SeekI(len
, wxFromCurrent
);
178 bool wxSoundAiff::PrepareToRecord(wxUint32 time
)
184 bool wxSoundAiff::FinishRecording()
190 bool wxSoundAiff::RepositionStream(wxUint32 position
)
192 // If the stream is not seekable "TellI() returns wxInvalidOffset" we cannot reposition stream
193 if (m_base_offset
== wxInvalidOffset
)
195 m_input
->SeekI(m_base_offset
, wxFromStart
);
199 wxUint32
wxSoundAiff::GetData(void *buffer
, wxUint32 len
)
201 return m_input
->Read(buffer
, len
).LastRead();
204 wxUint32
wxSoundAiff::PutData(const void *buffer
, wxUint32 len
)
206 return m_output
->Write(buffer
, len
).LastWrite();