]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/mmedia/sndg72x.cpp
f9ce3ca8109448298897fbe1354b4fcddfc5f01f
1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndg72x.cpp"
12 #include "wx/wxprec.h"
18 #include "wx/mmedia/sndbase.h"
19 #include "wx/mmedia/sndfile.h"
20 #include "wx/mmedia/sndpcm.h"
21 #include "wx/mmedia/sndg72x.h"
22 #include "wx/mmedia/internal/g72x.h"
24 // --------------------------------------------------------------------------
26 // --------------------------------------------------------------------------
28 wxSoundFormatG72X::wxSoundFormatG72X()
33 wxSoundFormatG72X::~wxSoundFormatG72X()
37 void wxSoundFormatG72X::SetSampleRate(wxUint32 srate
)
42 wxUint32
wxSoundFormatG72X::GetSampleRate() const
47 void wxSoundFormatG72X::SetG72XType(wxSoundG72XType type
)
52 wxSoundFormatBase
*wxSoundFormatG72X::Clone() const
54 wxSoundFormatG72X
*g72x
= new wxSoundFormatG72X();
56 g72x
->m_srate
= m_srate
;
57 g72x
->m_g72x_type
= m_g72x_type
;
61 wxUint32
wxSoundFormatG72X::GetTimeFromBytes(wxUint32 bytes
) const
65 switch (m_g72x_type
) {
79 return (wxUint32
)((bytes
/ m_srate
) * n_bits
) / 8;
82 wxUint32
wxSoundFormatG72X::GetBytesFromTime(wxUint32 time
) const
86 switch (m_g72x_type
) {
99 return (wxUint32
)((time
* m_srate
* n_bits
) / 8);
102 bool wxSoundFormatG72X::operator !=(const wxSoundFormatBase
& frmt2
) const
104 wxSoundFormatG72X
*g72x
= (wxSoundFormatG72X
*)&frmt2
;
106 if (frmt2
.GetType() != wxSOUND_G72X
)
109 return (g72x
->m_srate
!= m_srate
|| g72x
->m_g72x_type
!= m_g72x_type
);
112 // --------------------------------------------------------------------------
114 // --------------------------------------------------------------------------
116 wxSoundStreamG72X::wxSoundStreamG72X(wxSoundStream
& sndio
)
117 : wxSoundStreamCodec(sndio
)
120 m_router
= new wxSoundRouterStream(sndio
);
121 m_state
= new g72state
;
122 g72x_init_state(m_state
);
125 wxSoundStreamG72X::~wxSoundStreamG72X()
130 wxSoundStream
& wxSoundStreamG72X::Read(void *buffer
, wxUint32 len
)
132 wxUint16
*old_linear
;
133 register wxUint16
*linear_buffer
;
134 register wxUint32 real_len
;
135 register wxUint32 countdown
= len
;
137 real_len
= (len
* 8 / m_n_bits
);
139 old_linear
= linear_buffer
= new wxUint16
[real_len
];
141 m_router
->Read(linear_buffer
, real_len
);
143 real_len
= (wxUint32
)(m_router
->GetLastAccess() * ((float)m_n_bits
/ 8));
147 m_io_buffer
= (wxUint8
*)buffer
;
150 while (countdown
!= 0) {
151 PutBits(m_coder(*linear_buffer
++, AUDIO_ENCODING_LINEAR
, m_state
));
154 m_lastcount
= real_len
;
155 m_snderror
= m_router
->GetError();
162 wxSoundStream
& wxSoundStreamG72X::Write(const void *buffer
, wxUint32 len
)
164 wxUint16
*old_linear
;
165 register wxUint16
*linear_buffer
;
166 register wxUint32 countdown
= len
;
167 register wxUint32 real_len
;
169 // Compute the real length (PCM format) to sendt to the sound card
170 real_len
= (len
* m_n_bits
/ 8);
172 // Allocate a temporary buffer
173 old_linear
= linear_buffer
= new wxUint16
[real_len
];
175 // Bad, we override the const
176 m_io_buffer
= (wxUint8
*)buffer
;
180 while (countdown
!= 0) {
181 *linear_buffer
++ = m_decoder(GetBits(), AUDIO_ENCODING_LINEAR
, m_state
);
186 // Send them to the sound card
187 m_router
->Write(old_linear
, real_len
);
189 // Destroy the temporary buffer
195 bool wxSoundStreamG72X::SetSoundFormat(const wxSoundFormatBase
& format
)
197 if (format
.GetType() != wxSOUND_G72X
) {
198 m_snderror
= wxSOUND_INVFRMT
;
202 wxSoundFormatPcm pcm
;
203 wxSoundFormatG72X
*g72x
;
205 wxSoundStreamCodec::SetSoundFormat(format
);
207 g72x
= (wxSoundFormatG72X
*)m_sndformat
;
209 // Set PCM as the output format of the codec
210 pcm
.SetSampleRate(g72x
->GetSampleRate());
212 pcm
.SetChannels(1); // Only mono supported
214 pcm
.SetOrder(wxBYTE_ORDER
);
216 // Look for the correct codec to use and set its bit width
217 switch (g72x
->GetG72XType()) {
220 m_coder
= g721_encoder
;
221 m_decoder
= g721_decoder
;
223 case wxSOUND_G723_24
:
225 m_coder
= g723_24_encoder
;
226 m_decoder
= g723_24_decoder
;
228 case wxSOUND_G723_40
:
230 m_coder
= g723_40_encoder
;
231 m_decoder
= g723_40_decoder
;
235 // Let the router finish the work
236 m_router
->SetSoundFormat(pcm
);
243 wxUint8
wxSoundStreamG72X::GetBits()
245 register wxUint8 bits
;
247 // We have two bytes to compute
248 if (m_current_b_pos
< m_n_bits
) {
249 register wxUint8 b_left
;
251 // TRANSLATE the mask
252 m_current_mask
>>= m_current_b_pos
;
254 // GET the last bits: 0001..1
255 bits
= (m_current_byte
& m_current_mask
) << (m_n_bits
- m_current_b_pos
);
257 // GEN: 1. n times .1000
258 b_left
= BYTE_SIZE
-m_n_bits
;
259 m_current_mask
= ((1 << m_n_bits
) - 1) << b_left
;
262 m_current_byte
= *m_io_buffer
++;
264 register wxUint8 tmp_mask
;
266 // COMPUTE a new temporary mask to get the last bits
267 b_left
= m_n_bits
- b_left
;
268 tmp_mask
= (1 << b_left
) - 1;
269 // TRANSLATE the old mask to get ready for the next time
270 m_current_mask
>>= b_left
;
272 // COMPUTE the new bit position
273 b_left
= BYTE_SIZE
- b_left
;
274 m_current_b_pos
= b_left
;
278 bits
|= (m_current_byte
& tmp_mask
) >> b_left
;
280 m_current_mask
>>= m_n_bits
;
281 m_current_b_pos
-= m_n_bits
;
282 bits
= (m_current_byte
& m_current_mask
) >> m_current_b_pos
;
287 void wxSoundStreamG72X::PutBits(wxUint8 bits
)
289 if (m_current_b_pos
< m_n_bits
) {
290 register wxUint8 tmp_mask
;
291 register wxUint8 diff
;
293 diff
= m_n_bits
- m_current_b_pos
;
294 // Pack bits and put the byte in the buffer
295 m_current_byte
|= bits
>> diff
;
296 *m_io_buffer
++ = m_current_byte
;
299 tmp_mask
= ~((1 << diff
) - 1);
301 m_current_b_pos
= BYTE_SIZE
- (m_n_bits
- m_current_b_pos
);
303 m_current_byte
= (bits
& (tmp_mask
)) << m_current_b_pos
;
305 m_current_b_pos
-= m_n_bits
;
306 bits
<<= m_current_b_pos
;
307 m_current_byte
|= bits
;