]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/mmedia/sndaiff.cpp
fixed wrong srcdir in contrib w/ autoconf
[wxWidgets.git] / contrib / src / mmedia / sndaiff.cpp
1 // --------------------------------------------------------------------------
2 // Name: sndaiff.cpp
3 // Purpose:
4 // Date: 08/11/1999
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
6 // CVSID: $Id$
7 // --------------------------------------------------------------------------
8 #ifdef __GNUG__
9 #pragma implementation "sndaiff.cpp"
10 #endif
11
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/defs.h"
16 #endif
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #include "wx/stream.h"
23 #include "wx/datstrm.h"
24 #include "wx/filefn.h"
25
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"
31
32 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
33
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')
39
40 wxSoundAiff::wxSoundAiff(wxInputStream& stream, wxSoundStream& io_sound)
41 : wxSoundFileStream(stream, io_sound)
42 {
43 m_base_offset = wxInvalidOffset;
44 }
45
46 wxSoundAiff::wxSoundAiff(wxOutputStream& stream, wxSoundStream& io_sound)
47 : wxSoundFileStream(stream, io_sound)
48 {
49 m_base_offset = wxInvalidOffset;
50 }
51
52 wxSoundAiff::~wxSoundAiff()
53 {
54 }
55
56 wxString wxSoundAiff::GetCodecName() const
57 {
58 return "wxSoundAiff codec";
59 }
60
61 bool wxSoundAiff::CanRead()
62 {
63 wxUint32 signature1, signature2, len;
64
65 if (m_input->Read(&signature1, 4).LastRead() != 4)
66 return FALSE;
67
68 if (wxUINT32_SWAP_ON_BE(signature1) != FORM_SIGNATURE) {
69 m_input->Ungetch(&signature1, 4);
70 return FALSE;
71 }
72
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);
77 return FALSE;
78 }
79
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);
84 return FALSE;
85 }
86
87 m_input->Ungetch(&signature2, 4);
88 m_input->Ungetch(&len, 4);
89 m_input->Ungetch(&signature1, 4);
90
91 if (
92 wxUINT32_SWAP_ON_BE(signature2) != AIFF_SIGNATURE &&
93 wxUINT32_SWAP_ON_BE(signature2) != AIFC_SIGNATURE)
94 return FALSE;
95
96 return TRUE;
97 }
98
99 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
100
101 bool wxSoundAiff::PrepareToPlay()
102 {
103 wxDataInputStream data(*m_input);
104 wxUint32 signature, len, ssnd;
105 bool end_headers;
106
107 if (!m_input) {
108 m_snderror = wxSOUND_INVSTRM;
109 return FALSE;
110 }
111 m_snderror = wxSOUND_NOERROR;
112
113 data.BigEndianOrdered(TRUE);
114
115 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
116 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature) != FORM_SIGNATURE, wxSOUND_INVSTRM);
117 // "FORM"
118
119 len = data.Read32();
120 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
121 // dummy len
122
123 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
124 FAIL_WITH(
125 wxUINT32_SWAP_ON_BE(signature) != AIFF_SIGNATURE &&
126 wxUINT32_SWAP_ON_BE(signature) != AIFC_SIGNATURE, wxSOUND_INVSTRM);
127 // "AIFF" / "AIFC"
128
129 end_headers = FALSE;
130 while (!end_headers) {
131 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
132
133 len = data.Read32();
134 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
135
136 switch (wxUINT32_SWAP_ON_BE(signature)) {
137 case COMM_SIGNATURE: { // "COMM"
138 wxUint16 channels, bps;
139 wxUint32 num_samples;
140 double srate;
141 wxSoundFormatPcm sndformat;
142
143 // Get sound data informations
144 data >> channels >> num_samples >> bps >> srate;
145
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);
152
153 if (!SetSoundFormat(sndformat))
154 return FALSE;
155 // We pass all data left
156 m_input->SeekI(len-18, wxFromCurrent);
157 break;
158 }
159 case SSND_SIGNATURE: { // "SSND"
160 data >> 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);
167 end_headers = TRUE;
168 break;
169 }
170 default:
171 m_input->SeekI(len, wxFromCurrent);
172 break;
173 }
174 }
175 return TRUE;
176 }
177
178 bool wxSoundAiff::PrepareToRecord(wxUint32 time)
179 {
180 // TODO
181 return FALSE;
182 }
183
184 bool wxSoundAiff::FinishRecording()
185 {
186 // TODO
187 return FALSE;
188 }
189
190 bool wxSoundAiff::RepositionStream(wxUint32 position)
191 {
192 // If the stream is not seekable "TellI() returns wxInvalidOffset" we cannot reposition stream
193 if (m_base_offset == wxInvalidOffset)
194 return FALSE;
195 m_input->SeekI(m_base_offset, wxFromStart);
196 return TRUE;
197 }
198
199 wxUint32 wxSoundAiff::GetData(void *buffer, wxUint32 len)
200 {
201 return m_input->Read(buffer, len).LastRead();
202 }
203
204 wxUint32 wxSoundAiff::PutData(const void *buffer, wxUint32 len)
205 {
206 return m_output->Write(buffer, len).LastWrite();
207 }