]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia/sndfrmt.cpp
a489109d1e2388db27ef61798341980b2e54f4e9
[wxWidgets.git] / utils / wxMMedia / sndfrmt.cpp
1 #ifdef __GNUG__
2 #pragma implementation "sndfrmt.h"
3 #endif
4 #include "sndsnd.h"
5 #include "sndfrmt.h"
6 #include "sndpcm.h"
7
8 // ----------------------------------------------------------------------------
9 // wxSoundDataFormat
10 // ----------------------------------------------------------------------------
11
12 wxSoundDataFormat::wxSoundDataFormat()
13 {
14 m_srate = 22050;
15 m_bps = 8;
16 m_channels = 1;
17 m_codno = 1;
18 m_codec = NULL;
19 m_codchange = FALSE;
20 m_codcreate = TRUE;
21 }
22
23 wxSoundDataFormat::wxSoundDataFormat(const wxSoundDataFormat& format)
24 {
25 m_srate = format.m_srate;
26 m_bps = format.m_bps;
27 m_channels = format.m_channels;
28 m_codno = format.m_codno;
29 m_sign = format.m_sign;
30 m_byteorder = format.m_byteorder;
31 m_codchange = FALSE;
32 m_codcreate = TRUE;
33 m_codec = NULL;
34 }
35
36 wxSoundDataFormat::~wxSoundDataFormat()
37 {
38 wxDELETE(m_codec);
39 }
40
41 void wxSoundDataFormat::SetChannels(int channels)
42 {
43 m_channels = channels;
44 }
45
46 void wxSoundDataFormat::SetBps(int bps)
47 {
48 m_bps = bps;
49 CodecChange();
50 }
51
52 void wxSoundDataFormat::SetSign(int sign)
53 {
54 m_sign = sign;
55 CodecChange();
56 }
57
58 void wxSoundDataFormat::SetByteOrder(int byteorder)
59 {
60 m_byteorder = byteorder;
61 CodecChange();
62 }
63
64 void wxSoundDataFormat::SetCodecNo(int codno)
65 {
66 m_codno = codno;
67 m_codchange = TRUE;
68 CodecChange();
69 }
70
71 wxSoundCodec *wxSoundDataFormat::GetCodec()
72 {
73 if (!m_codcreate)
74 return NULL;
75
76 if (m_codchange)
77 wxDELETE(m_codec);
78
79 if (m_codec)
80 return m_codec;
81
82 m_codchange = FALSE;
83 m_codec = wxSoundCodec::Get(m_codno);
84
85 return m_codec;
86 }
87
88 void wxSoundDataFormat::CodecChange()
89 {
90 wxSoundCodec *codec = GetCodec();
91
92 if (!codec)
93 return;
94
95 switch (m_codno) {
96 case WXSOUND_PCM: {
97 wxSoundPcmCodec *pcm_codec = (wxSoundPcmCodec *)codec;
98
99 pcm_codec->SetBits(m_bps);
100 pcm_codec->SetByteOrder(m_byteorder);
101 pcm_codec->SetSign(m_sign);
102 break;
103 }
104 default:
105 break;
106 }
107 }
108
109 wxSoundDataFormat& wxSoundDataFormat::operator =(const wxSoundDataFormat& format)
110 {
111 wxDELETE(m_codec);
112
113 m_srate = format.m_srate;
114 m_bps = format.m_bps;
115 m_channels = format.m_channels;
116 m_codno = format.m_codno;
117 m_sign = format.m_sign;
118 m_byteorder = format.m_byteorder;
119
120 return *this;
121 }
122
123 bool wxSoundDataFormat::operator ==(const wxSoundDataFormat& format) const
124 {
125 if (m_codno != format.m_codno || m_srate != format.m_srate ||
126 m_bps != format.m_bps || m_channels != format.m_channels)
127 return FALSE;
128
129 if (m_codno == WXSOUND_PCM &&
130 (m_sign != format.m_sign || m_byteorder != format.m_byteorder))
131 return FALSE;
132
133 return TRUE;
134 }
135
136 // ----------------------------------------------------------------------------
137 // wxSoundCodec
138 // ----------------------------------------------------------------------------
139
140 #include "sndpcm.h"
141 //#include "sndadpcm.h"
142 //#include "sndalaw.h"
143 #include "sndmulaw.h"
144
145 static wxClassInfo *l_sound_formats[] = {
146 NULL,
147 CLASSINFO(wxSoundPcmCodec),
148 NULL, // CLASSINFO(wxSoundAdpcmCodec),
149 NULL,
150 NULL,
151 NULL,
152 NULL, // CLASSINFO(wxSoundAlawCodec),
153 NULL // CLASSINFO(wxSoundMulawCodec)
154 };
155
156 static int l_nb_formats = WXSIZEOF(l_sound_formats);
157
158 wxSoundCodec::wxSoundCodec()
159 {
160 m_in_sound = NULL;
161 m_out_sound = NULL;
162 m_init = TRUE;
163 m_chain_codec = NULL;
164 }
165
166 wxSoundCodec::~wxSoundCodec()
167 {
168 }
169
170 void wxSoundCodec::InitIO(const wxSoundDataFormat& format)
171 {
172 m_io_format = format;
173 }
174
175 void wxSoundCodec::InitMode(int mode)
176 {
177 wxStreamBuffer *buf_snd;
178
179 m_mode = (mode == 0) ? ENCODING : DECODING;
180 if (!m_chain_codec) {
181 if (mode == ENCODING) {
182 m_out_sound = new wxStreamBuffer(*this, wxStreamBuffer::write);
183 m_out_sound->SetBufferIO(1024);
184 } else {
185 m_in_sound = new wxStreamBuffer(*this, wxStreamBuffer::read);
186 m_in_sound->SetBufferIO(1024);
187 }
188 }
189 if (m_chain_codec) {
190 if (m_chain_before) {
191 m_chain_codec->SetInStream(m_in_sound);
192 buf_snd = new wxStreamBuffer(wxStreamBuffer::read_write);
193 buf_snd->Fixed(FALSE);
194 m_chain_codec->SetOutStream(buf_snd);
195 m_chain_codec->Decode();
196 buf_snd->Seek(0, wxFromStart);
197 m_in_sound = buf_snd;
198 } else {
199 buf_snd = new wxStreamBuffer(wxStreamBuffer::read_write);
200 buf_snd->Fixed(FALSE);
201
202 m_chain_codec->SetInStream(buf_snd);
203 m_chain_codec->SetOutStream(m_out_sound);
204 m_out_sound = buf_snd;
205
206 buf_snd->Seek(0, wxFromStart);
207 }
208 }
209 }
210
211 void wxSoundCodec::ExitMode()
212 {
213 if (m_chain_codec) {
214 if (m_chain_before) {
215 delete m_in_sound;
216 m_in_sound = m_chain_codec->GetInStream();
217 } else {
218 delete m_out_sound;
219 m_out_sound = m_chain_codec->GetOutStream();
220 }
221 }
222 }
223
224 bool wxSoundCodec::ChainCodecBefore(wxSoundDataFormat& format)
225 {
226 m_chain_codec = format.GetCodec();
227
228 if (!m_chain_codec)
229 return FALSE;
230
231 m_chain_before = TRUE;
232 return TRUE;
233 }
234
235 bool wxSoundCodec::ChainCodecAfter(wxSoundDataFormat& format)
236 {
237 m_chain_codec = format.GetCodec();
238
239 if (!m_chain_codec)
240 return FALSE;
241
242 m_chain_before = FALSE;
243 return TRUE;
244 }
245
246 void wxSoundCodec::CopyToOutput()
247 {
248 m_out_sound->Write(m_in_sound);
249 }
250
251 size_t wxSoundCodec::Available()
252 {
253 return m_io_sndbuf->Available();
254 }
255
256 size_t wxSoundCodec::OnSysRead(void *buffer, size_t bsize)
257 {
258 wxUint32 s = bsize;
259 m_io_sndbuf->OnNeedOutputData((char *)buffer, s);
260 return bsize;
261 }
262
263 size_t wxSoundCodec::OnSysWrite(const void *buffer, size_t bsize)
264 {
265 wxUint32 s = bsize;
266 m_io_sndbuf->OnBufferInFinished((char *)buffer, s);
267 return bsize;
268 }
269
270 wxSoundCodec *wxSoundCodec::Get(int no)
271 {
272 if (no < 0 || no >= l_nb_formats)
273 return NULL;
274
275 if (!l_sound_formats[no])
276 return NULL;
277
278 return (wxSoundCodec *)l_sound_formats[no]->CreateObject();
279 }