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