]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia/sndfrmt.cpp
750f17c83b34effe4bee52b089c4a07d070184e2
[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 if (m_mode != WAITING)
169 ExitMode();
170 }
171
172 void wxSoundCodec::InitIO(const wxSoundDataFormat& format)
173 {
174 m_io_format = format;
175 }
176
177 void wxSoundCodec::InitMode(ModeType mode)
178 {
179 wxStreamBuffer *buf_snd;
180
181 m_mode = mode;
182 if (!m_chain_codec) {
183 if (m_mode == ENCODING) {
184 m_out_sound = new wxStreamBuffer(*this, wxStreamBuffer::write);
185 m_out_sound->SetBufferIO(1024);
186 } else {
187 m_in_sound = new wxStreamBuffer(*this, wxStreamBuffer::read);
188 m_in_sound->SetBufferIO(1024);
189 }
190 }
191 if (m_chain_codec) {
192 if (m_chain_before) {
193 m_chain_codec->SetInStream(m_in_sound);
194 buf_snd = new wxStreamBuffer(wxStreamBuffer::read_write);
195 buf_snd->Fixed(FALSE);
196 m_chain_codec->SetOutStream(buf_snd);
197 m_chain_codec->Decode();
198 buf_snd->Seek(0, wxFromStart);
199 m_in_sound = buf_snd;
200 } else {
201 buf_snd = new wxStreamBuffer(wxStreamBuffer::read_write);
202 buf_snd->Fixed(FALSE);
203
204 m_chain_codec->SetInStream(buf_snd);
205 m_chain_codec->SetOutStream(m_out_sound);
206 m_out_sound = buf_snd;
207
208 buf_snd->Seek(0, wxFromStart);
209 }
210 }
211 }
212
213 void wxSoundCodec::ExitMode()
214 {
215 if (m_chain_codec) {
216 if (m_chain_before) {
217 delete m_in_sound;
218 m_in_sound = m_chain_codec->GetInStream();
219 } else {
220 delete m_out_sound;
221 m_out_sound = m_chain_codec->GetOutStream();
222 }
223 }
224 m_mode = WAITING;
225 }
226
227 bool wxSoundCodec::ChainCodecBefore(wxSoundDataFormat& format)
228 {
229 m_chain_codec = format.GetCodec();
230
231 if (!m_chain_codec)
232 return FALSE;
233
234 m_chain_before = TRUE;
235 return TRUE;
236 }
237
238 bool wxSoundCodec::ChainCodecAfter(wxSoundDataFormat& format)
239 {
240 m_chain_codec = format.GetCodec();
241
242 if (!m_chain_codec)
243 return FALSE;
244
245 m_chain_before = FALSE;
246 return TRUE;
247 }
248
249 void wxSoundCodec::CopyToOutput()
250 {
251 m_out_sound->Write(m_in_sound);
252 }
253
254 size_t wxSoundCodec::Available()
255 {
256 return m_io_sndbuf->Available();
257 }
258
259 size_t wxSoundCodec::OnSysRead(void *buffer, size_t bsize)
260 {
261 wxUint32 s = bsize;
262 m_io_sndbuf->OnNeedOutputData((char *)buffer, s);
263 return bsize;
264 }
265
266 size_t wxSoundCodec::OnSysWrite(const void *buffer, size_t bsize)
267 {
268 wxUint32 s = bsize;
269 m_io_sndbuf->OnBufferInFinished((char *)buffer, s);
270 return bsize;
271 }
272
273 wxSoundCodec *wxSoundCodec::Get(int no)
274 {
275 if (no < 0 || no >= l_nb_formats)
276 return NULL;
277
278 if (!l_sound_formats[no])
279 return NULL;
280
281 return (wxSoundCodec *)l_sound_formats[no]->CreateObject();
282 }