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 wxT("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
);
121 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
124 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
126 wxUINT32_SWAP_ON_BE(signature
) != AIFF_SIGNATURE
&&
127 wxUINT32_SWAP_ON_BE(signature
) != AIFC_SIGNATURE
, wxSOUND_INVSTRM
);
131 while (!end_headers
) {
132 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
135 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
137 switch (wxUINT32_SWAP_ON_BE(signature
)) {
138 case COMM_SIGNATURE
: { // "COMM"
139 wxUint16 channels
, bps
;
140 wxUint32 num_samples
;
142 wxSoundFormatPcm sndformat
;
144 // Get sound data informations
145 data
>> channels
>> num_samples
>> bps
>> srate
;
147 // Convert them in a wxSoundFormat object
148 sndformat
.SetSampleRate((wxUint32
) srate
);
149 sndformat
.SetBPS(bps
);
150 sndformat
.SetChannels(channels
);
151 sndformat
.Signed(FALSE
);
152 sndformat
.SetOrder(wxBIG_ENDIAN
);
154 if (!SetSoundFormat(sndformat
))
156 // We pass all data left
157 m_input
->SeekI(len
-18, wxFromCurrent
);
160 case SSND_SIGNATURE
: { // "SSND"
162 // m_input->SeekI(4, wxFromCurrent); // Pass an INT32
163 // m_input->SeekI(len-4, wxFromCurrent); // Pass the rest
164 m_input
->SeekI(ssnd
+ 4, wxFromCurrent
);
165 m_base_offset
= m_input
->TellI();
166 // len-8 bytes of samples
167 FinishPreparation(len
- 8);
172 m_input
->SeekI(len
, wxFromCurrent
);
179 bool wxSoundAiff::PrepareToRecord(wxUint32
WXUNUSED(time
))
185 bool wxSoundAiff::FinishRecording()
191 bool wxSoundAiff::RepositionStream(wxUint32
WXUNUSED(position
))
193 // If the stream is not seekable "TellI() returns wxInvalidOffset" we cannot reposition stream
194 if (m_base_offset
== wxInvalidOffset
)
196 m_input
->SeekI(m_base_offset
, wxFromStart
);
200 wxUint32
wxSoundAiff::GetData(void *buffer
, wxUint32 len
)
202 return m_input
->Read(buffer
, len
).LastRead();
205 wxUint32
wxSoundAiff::PutData(const void *buffer
, wxUint32 len
)
207 return m_output
->Write(buffer
, len
).LastWrite();