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