1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndaiff.cpp"
13 #include <wx/stream.h>
14 #include <wx/datstrm.h>
15 #include <wx/filefn.h>
22 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
24 #define FORM_SIGNATURE BUILD_SIGNATURE('F','O','R','M')
25 #define AIFF_SIGNATURE BUILD_SIGNATURE('A','I','F','F')
26 #define AIFC_SIGNATURE BUILD_SIGNATURE('A','I','F','C')
27 #define COMM_SIGNATURE BUILD_SIGNATURE('C','O','M','M')
28 #define SSND_SIGNATURE BUILD_SIGNATURE('S','S','N','D')
30 wxSoundAiff::wxSoundAiff(wxInputStream
& stream
, wxSoundStream
& io_sound
)
31 : wxSoundFileStream(stream
, io_sound
)
35 wxSoundAiff::wxSoundAiff(wxOutputStream
& stream
, wxSoundStream
& io_sound
)
36 : wxSoundFileStream(stream
, io_sound
)
40 wxSoundAiff::~wxSoundAiff()
44 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
46 bool wxSoundAiff::PrepareToPlay()
48 wxDataInputStream
data(*m_input
);
49 wxUint32 signature
, len
, ssnd
;
53 m_snderror
= wxSOUND_INVSTRM
;
57 data
.BigEndianOrdered(TRUE
);
59 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
60 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != FORM_SIGNATURE
, wxSOUND_INVSTRM
);
64 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
67 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
69 wxUINT32_SWAP_ON_BE(signature
) != AIFF_SIGNATURE
&&
70 wxUINT32_SWAP_ON_BE(signature
) != AIFC_SIGNATURE
, wxSOUND_INVSTRM
);
74 while (!end_headers
) {
75 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
78 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
80 switch (wxUINT32_SWAP_ON_BE(signature
)) {
81 case COMM_SIGNATURE
: { // "COMM"
82 wxUint16 channels
, bps
;
85 wxSoundFormatPcm sndformat
;
87 data
>> channels
>> num_samples
>> bps
>> srate
;
89 sndformat
.SetSampleRate((wxUint32
) srate
);
90 sndformat
.SetBPS(bps
);
91 sndformat
.SetChannels(channels
);
92 sndformat
.Signed(TRUE
);
93 sndformat
.SetOrder(wxBIG_ENDIAN
);
95 if (!SetSoundFormat(sndformat
))
97 m_input
->SeekI(len
-18, wxFromCurrent
);
100 case SSND_SIGNATURE
: { // "SSND"
102 // m_input->SeekI(4, wxFromCurrent); // Pass an INT32
103 // m_input->SeekI(len-4, wxFromCurrent); // Pass the rest
104 m_input
->SeekI(ssnd
+ 4, wxFromCurrent
);
109 m_input
->SeekI(len
, wxFromCurrent
);
116 bool wxSoundAiff::PrepareToRecord(unsigned long time
)
121 bool wxSoundAiff::FinishRecording()
126 size_t wxSoundAiff::GetData(void *buffer
, size_t len
)
128 return m_input
->Read(buffer
, len
).LastRead();
131 size_t wxSoundAiff::PutData(const void *buffer
, size_t len
)
133 return m_output
->Write(buffer
, len
).LastWrite();