]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndaiff.cpp
2242cc263d3cae09e32ae16c409c06af58df100e
[wxWidgets.git] / utils / wxMMedia2 / lib / 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 #include <wx/stream.h>
15 #include <wx/datstrm.h>
16 #include <wx/filefn.h>
17 #include "sndbase.h"
18 #include "sndcodec.h"
19 #include "sndfile.h"
20 #include "sndpcm.h"
21 #include "sndaiff.h"
22
23 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
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')
30
31 wxSoundAiff::wxSoundAiff(wxInputStream& stream, wxSoundStream& io_sound)
32 : wxSoundFileStream(stream, io_sound)
33 {
34 }
35
36 wxSoundAiff::wxSoundAiff(wxOutputStream& stream, wxSoundStream& io_sound)
37 : wxSoundFileStream(stream, io_sound)
38 {
39 }
40
41 wxSoundAiff::~wxSoundAiff()
42 {
43 }
44
45 wxString wxSoundAiff::GetCodecName() const
46 {
47 return "wxSoundAiff codec";
48 }
49
50 bool wxSoundAiff::CanRead()
51 {
52 wxUint32 signature1, signature2, len;
53
54 if (m_input->Read(&signature1, 4).LastRead() != 4)
55 return FALSE;
56
57 if (wxUINT32_SWAP_ON_BE(signature1) != FORM_SIGNATURE) {
58 m_input->Ungetch(&signature1, 4);
59 return FALSE;
60 }
61
62 m_input->Read(&len, 4);
63 if (m_input->LastRead() != 4) {
64 m_input->Ungetch(&len, m_input->LastRead());
65 m_input->Ungetch(&signature1, 4);
66 return FALSE;
67 }
68
69 if (m_input->Read(&signature2, 4).LastRead() != 4) {
70 m_input->Ungetch(&signature2, m_input->LastRead());
71 m_input->Ungetch(&len, 4);
72 m_input->Ungetch(&signature1, 4);
73 return FALSE;
74 }
75
76 m_input->Ungetch(&signature2, 4);
77 m_input->Ungetch(&len, 4);
78 m_input->Ungetch(&signature1, 4);
79
80 if (
81 wxUINT32_SWAP_ON_BE(signature2) != AIFF_SIGNATURE &&
82 wxUINT32_SWAP_ON_BE(signature2) != AIFC_SIGNATURE)
83 return FALSE;
84
85 return TRUE;
86 }
87
88 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
89
90 bool wxSoundAiff::PrepareToPlay()
91 {
92 wxDataInputStream data(*m_input);
93 wxUint32 signature, len, ssnd;
94 bool end_headers;
95
96 if (!m_input) {
97 m_snderror = wxSOUND_INVSTRM;
98 return FALSE;
99 }
100
101 data.BigEndianOrdered(TRUE);
102
103 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
104 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature) != FORM_SIGNATURE, wxSOUND_INVSTRM);
105 // "FORM"
106
107 len = data.Read32();
108 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
109 // dummy len
110
111 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
112 FAIL_WITH(
113 wxUINT32_SWAP_ON_BE(signature) != AIFF_SIGNATURE &&
114 wxUINT32_SWAP_ON_BE(signature) != AIFC_SIGNATURE, wxSOUND_INVSTRM);
115 // "AIFF" / "AIFC"
116
117 end_headers = FALSE;
118 while (!end_headers) {
119 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
120
121 len = data.Read32();
122 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
123
124 switch (wxUINT32_SWAP_ON_BE(signature)) {
125 case COMM_SIGNATURE: { // "COMM"
126 wxUint16 channels, bps;
127 wxUint32 num_samples;
128 double srate;
129 wxSoundFormatPcm sndformat;
130
131 data >> channels >> num_samples >> bps >> srate;
132
133 sndformat.SetSampleRate((wxUint32) srate);
134 sndformat.SetBPS(bps);
135 sndformat.SetChannels(channels);
136 sndformat.Signed(FALSE);
137 sndformat.SetOrder(wxBIG_ENDIAN);
138
139 if (!SetSoundFormat(sndformat))
140 return FALSE;
141 m_input->SeekI(len-18, wxFromCurrent);
142 break;
143 }
144 case SSND_SIGNATURE: { // "SSND"
145 data >> ssnd;
146 // m_input->SeekI(4, wxFromCurrent); // Pass an INT32
147 // m_input->SeekI(len-4, wxFromCurrent); // Pass the rest
148 m_input->SeekI(ssnd + 4, wxFromCurrent);
149 FinishPreparation(len - 8);
150 end_headers = TRUE;
151 break;
152 }
153 default:
154 m_input->SeekI(len, wxFromCurrent);
155 break;
156 }
157 }
158 return TRUE;
159 }
160
161 bool wxSoundAiff::PrepareToRecord(unsigned long time)
162 {
163 // TODO
164 return FALSE;
165 }
166
167 bool wxSoundAiff::FinishRecording()
168 {
169 // TODO
170 return FALSE;
171 }
172
173 wxUint32 wxSoundAiff::GetData(void *buffer, wxUint32 len)
174 {
175 return m_input->Read(buffer, len).LastRead();
176 }
177
178 wxUint32 wxSoundAiff::PutData(const void *buffer, wxUint32 len)
179 {
180 return m_output->Write(buffer, len).LastWrite();
181 }