]>
Commit | Line | Data |
---|---|---|
2a040d3f GL |
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 | //////////////////////////////////////////////////////////////////////////////// | |
4d6306eb GL |
10 | #ifdef __GNUG__ |
11 | #pragma implementation "sndfrmt.h" | |
12 | #endif | |
13 | #include "sndsnd.h" | |
14 | #include "sndfrmt.h" | |
15 | #include "sndpcm.h" | |
4d6306eb GL |
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 | ||
926c550d GL |
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 | ||
4d6306eb GL |
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) | |
926c550d | 86 | wxDELETE(m_codec); |
4d6306eb GL |
87 | |
88 | if (m_codec) | |
89 | return m_codec; | |
90 | ||
91 | m_codchange = FALSE; | |
92 | m_codec = wxSoundCodec::Get(m_codno); | |
2a040d3f | 93 | CodecChange(); |
4d6306eb GL |
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 | ||
c06a465c GL |
109 | pcm_codec->m_orig_format.SetSampleRate(m_srate); |
110 | pcm_codec->m_orig_format.SetBps(m_bps); | |
111 | pcm_codec->m_orig_format.SetChannels(m_channels); | |
112 | pcm_codec->m_orig_format.SetByteOrder(m_byteorder); | |
113 | pcm_codec->m_orig_format.SetSign(m_sign); | |
4d6306eb GL |
114 | break; |
115 | } | |
116 | default: | |
117 | break; | |
118 | } | |
119 | } | |
120 | ||
121 | wxSoundDataFormat& wxSoundDataFormat::operator =(const wxSoundDataFormat& format) | |
122 | { | |
123 | wxDELETE(m_codec); | |
124 | ||
125 | m_srate = format.m_srate; | |
126 | m_bps = format.m_bps; | |
127 | m_channels = format.m_channels; | |
128 | m_codno = format.m_codno; | |
129 | m_sign = format.m_sign; | |
130 | m_byteorder = format.m_byteorder; | |
131 | ||
132 | return *this; | |
133 | } | |
134 | ||
135 | bool wxSoundDataFormat::operator ==(const wxSoundDataFormat& format) const | |
136 | { | |
137 | if (m_codno != format.m_codno || m_srate != format.m_srate || | |
138 | m_bps != format.m_bps || m_channels != format.m_channels) | |
139 | return FALSE; | |
140 | ||
141 | if (m_codno == WXSOUND_PCM && | |
142 | (m_sign != format.m_sign || m_byteorder != format.m_byteorder)) | |
143 | return FALSE; | |
144 | ||
145 | return TRUE; | |
146 | } | |
147 | ||
148 | // ---------------------------------------------------------------------------- | |
149 | // wxSoundCodec | |
150 | // ---------------------------------------------------------------------------- | |
151 | ||
152 | #include "sndpcm.h" | |
153 | //#include "sndadpcm.h" | |
154 | //#include "sndalaw.h" | |
155 | #include "sndmulaw.h" | |
156 | ||
157 | static wxClassInfo *l_sound_formats[] = { | |
158 | NULL, | |
159 | CLASSINFO(wxSoundPcmCodec), | |
160 | NULL, // CLASSINFO(wxSoundAdpcmCodec), | |
161 | NULL, | |
162 | NULL, | |
163 | NULL, | |
164 | NULL, // CLASSINFO(wxSoundAlawCodec), | |
165 | NULL // CLASSINFO(wxSoundMulawCodec) | |
166 | }; | |
167 | ||
168 | static int l_nb_formats = WXSIZEOF(l_sound_formats); | |
169 | ||
170 | wxSoundCodec::wxSoundCodec() | |
171 | { | |
172 | m_in_sound = NULL; | |
173 | m_out_sound = NULL; | |
174 | m_init = TRUE; | |
926c550d | 175 | m_chain_codec = NULL; |
4d6306eb GL |
176 | } |
177 | ||
178 | wxSoundCodec::~wxSoundCodec() | |
179 | { | |
eb4e516d GL |
180 | if (m_mode != WAITING) |
181 | ExitMode(); | |
4d6306eb GL |
182 | } |
183 | ||
184 | void wxSoundCodec::InitIO(const wxSoundDataFormat& format) | |
185 | { | |
186 | m_io_format = format; | |
187 | } | |
188 | ||
eb4e516d | 189 | void wxSoundCodec::InitMode(ModeType mode) |
4d6306eb GL |
190 | { |
191 | wxStreamBuffer *buf_snd; | |
192 | ||
eb4e516d | 193 | m_mode = mode; |
4d6306eb | 194 | if (!m_chain_codec) { |
eb4e516d | 195 | if (m_mode == ENCODING) { |
4d6306eb GL |
196 | m_out_sound = new wxStreamBuffer(*this, wxStreamBuffer::write); |
197 | m_out_sound->SetBufferIO(1024); | |
198 | } else { | |
199 | m_in_sound = new wxStreamBuffer(*this, wxStreamBuffer::read); | |
200 | m_in_sound->SetBufferIO(1024); | |
201 | } | |
202 | } | |
203 | if (m_chain_codec) { | |
204 | if (m_chain_before) { | |
205 | m_chain_codec->SetInStream(m_in_sound); | |
206 | buf_snd = new wxStreamBuffer(wxStreamBuffer::read_write); | |
207 | buf_snd->Fixed(FALSE); | |
208 | m_chain_codec->SetOutStream(buf_snd); | |
209 | m_chain_codec->Decode(); | |
210 | buf_snd->Seek(0, wxFromStart); | |
211 | m_in_sound = buf_snd; | |
212 | } else { | |
213 | buf_snd = new wxStreamBuffer(wxStreamBuffer::read_write); | |
214 | buf_snd->Fixed(FALSE); | |
215 | ||
216 | m_chain_codec->SetInStream(buf_snd); | |
217 | m_chain_codec->SetOutStream(m_out_sound); | |
218 | m_out_sound = buf_snd; | |
219 | ||
220 | buf_snd->Seek(0, wxFromStart); | |
221 | } | |
222 | } | |
223 | } | |
224 | ||
225 | void wxSoundCodec::ExitMode() | |
226 | { | |
227 | if (m_chain_codec) { | |
228 | if (m_chain_before) { | |
229 | delete m_in_sound; | |
230 | m_in_sound = m_chain_codec->GetInStream(); | |
231 | } else { | |
232 | delete m_out_sound; | |
233 | m_out_sound = m_chain_codec->GetOutStream(); | |
234 | } | |
235 | } | |
eb4e516d | 236 | m_mode = WAITING; |
4d6306eb GL |
237 | } |
238 | ||
239 | bool wxSoundCodec::ChainCodecBefore(wxSoundDataFormat& format) | |
240 | { | |
241 | m_chain_codec = format.GetCodec(); | |
242 | ||
243 | if (!m_chain_codec) | |
244 | return FALSE; | |
245 | ||
246 | m_chain_before = TRUE; | |
247 | return TRUE; | |
248 | } | |
249 | ||
250 | bool wxSoundCodec::ChainCodecAfter(wxSoundDataFormat& format) | |
251 | { | |
252 | m_chain_codec = format.GetCodec(); | |
253 | ||
254 | if (!m_chain_codec) | |
255 | return FALSE; | |
256 | ||
257 | m_chain_before = FALSE; | |
258 | return TRUE; | |
259 | } | |
260 | ||
261 | void wxSoundCodec::CopyToOutput() | |
262 | { | |
263 | m_out_sound->Write(m_in_sound); | |
264 | } | |
265 | ||
266 | size_t wxSoundCodec::Available() | |
267 | { | |
268 | return m_io_sndbuf->Available(); | |
269 | } | |
270 | ||
271 | size_t wxSoundCodec::OnSysRead(void *buffer, size_t bsize) | |
272 | { | |
273 | wxUint32 s = bsize; | |
274 | m_io_sndbuf->OnNeedOutputData((char *)buffer, s); | |
275 | return bsize; | |
276 | } | |
277 | ||
278 | size_t wxSoundCodec::OnSysWrite(const void *buffer, size_t bsize) | |
279 | { | |
280 | wxUint32 s = bsize; | |
281 | m_io_sndbuf->OnBufferInFinished((char *)buffer, s); | |
282 | return bsize; | |
283 | } | |
284 | ||
285 | wxSoundCodec *wxSoundCodec::Get(int no) | |
286 | { | |
287 | if (no < 0 || no >= l_nb_formats) | |
288 | return NULL; | |
289 | ||
290 | if (!l_sound_formats[no]) | |
291 | return NULL; | |
292 | ||
293 | return (wxSoundCodec *)l_sound_formats[no]->CreateObject(); | |
294 | } |