1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndaiff.cpp"
12 #include <wx/wxprec.h>
14 #include <wx/stream.h>
15 #include <wx/datstrm.h>
16 #include <wx/filefn.h>
23 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
25 #define FORM_SIGNATURE BUILD_SIGNATURE('F','O','R','M')
26 #define AIFF_SIGNATURE BUILD_SIGNATURE('A','I','F','F')
27 #define AIFC_SIGNATURE BUILD_SIGNATURE('A','I','F','C')
28 #define COMM_SIGNATURE BUILD_SIGNATURE('C','O','M','M')
29 #define SSND_SIGNATURE BUILD_SIGNATURE('S','S','N','D')
31 wxSoundAiff::wxSoundAiff(wxInputStream
& stream
, wxSoundStream
& io_sound
)
32 : wxSoundFileStream(stream
, io_sound
)
36 wxSoundAiff::wxSoundAiff(wxOutputStream
& stream
, wxSoundStream
& io_sound
)
37 : wxSoundFileStream(stream
, io_sound
)
41 wxSoundAiff::~wxSoundAiff()
45 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
47 bool wxSoundAiff::PrepareToPlay()
49 wxDataInputStream
data(*m_input
);
50 wxUint32 signature
, len
, ssnd
;
54 m_snderror
= wxSOUND_INVSTRM
;
58 data
.BigEndianOrdered(TRUE
);
60 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
61 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature
) != FORM_SIGNATURE
, wxSOUND_INVSTRM
);
65 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
68 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
70 wxUINT32_SWAP_ON_BE(signature
) != AIFF_SIGNATURE
&&
71 wxUINT32_SWAP_ON_BE(signature
) != AIFC_SIGNATURE
, wxSOUND_INVSTRM
);
75 while (!end_headers
) {
76 FAIL_WITH(m_input
->Read(&signature
, 4).LastRead() != 4, wxSOUND_INVSTRM
);
79 FAIL_WITH(m_input
->LastRead() != 4, wxSOUND_INVSTRM
);
81 switch (wxUINT32_SWAP_ON_BE(signature
)) {
82 case COMM_SIGNATURE
: { // "COMM"
83 wxUint16 channels
, bps
;
86 wxSoundFormatPcm sndformat
;
88 data
>> channels
>> num_samples
>> bps
>> srate
;
90 sndformat
.SetSampleRate((wxUint32
) srate
);
91 sndformat
.SetBPS(bps
);
92 sndformat
.SetChannels(channels
);
93 sndformat
.Signed(FALSE
);
94 sndformat
.SetOrder(wxBIG_ENDIAN
);
96 if (!SetSoundFormat(sndformat
))
98 m_input
->SeekI(len
-18, wxFromCurrent
);
101 case SSND_SIGNATURE
: { // "SSND"
103 // m_input->SeekI(4, wxFromCurrent); // Pass an INT32
104 // m_input->SeekI(len-4, wxFromCurrent); // Pass the rest
105 m_input
->SeekI(ssnd
+ 4, wxFromCurrent
);
110 m_input
->SeekI(len
, wxFromCurrent
);
117 bool wxSoundAiff::PrepareToRecord(unsigned long time
)
123 bool wxSoundAiff::FinishRecording()
129 wxUint32
wxSoundAiff::GetData(void *buffer
, wxUint32 len
)
131 return m_input
->Read(buffer
, len
).LastRead();
134 wxUint32
wxSoundAiff::PutData(const void *buffer
, wxUint32 len
)
136 return m_output
->Write(buffer
, len
).LastWrite();