]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndg72x.cpp
1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndg72x.cpp"
12 #include <wx/wxprec.h>
19 // --------------------------------------------------------------------------
21 // --------------------------------------------------------------------------
23 wxSoundFormatG72X::wxSoundFormatG72X()
28 wxSoundFormatG72X::~wxSoundFormatG72X()
32 void wxSoundFormatG72X::SetSampleRate(wxUint32 srate
)
37 wxUint32
wxSoundFormatG72X::GetSampleRate() const
42 void wxSoundFormatG72X::SetG72XType(wxSoundG72XType type
)
47 wxSoundFormatBase
*wxSoundFormatG72X::Clone() const
49 wxSoundFormatG72X
*g72x
= new wxSoundFormatG72X();
51 g72x
->m_srate
= m_srate
;
52 g72x
->m_g72x_type
= m_g72x_type
;
56 wxUint32
wxSoundFormatG72X::GetTimeFromBytes(wxUint32 bytes
) const
60 switch (m_g72x_type
) {
74 return (wxUint32
)((bytes
/ m_srate
) * ((float)n_bits
/ 8));
77 wxUint32
wxSoundFormatG72X::GetBytesFromTime(wxUint32 time
) const
81 switch (m_g72x_type
) {
94 return (wxUint32
)(time
* (m_srate
* ((float)n_bits
/ 8)));
97 bool wxSoundFormatG72X::operator !=(const wxSoundFormatBase
& frmt2
) const
99 wxSoundFormatG72X
*g72x
= (wxSoundFormatG72X
*)&frmt2
;
101 if (frmt2
.GetType() != wxSOUND_G72X
)
104 return (g72x
->m_srate
!= m_srate
|| g72x
->m_g72x_type
!= m_g72x_type
);
107 // --------------------------------------------------------------------------
109 // --------------------------------------------------------------------------
111 wxSoundStreamG72X::wxSoundStreamG72X(wxSoundStream
& sndio
)
112 : wxSoundStreamCodec(sndio
)
115 m_router
= new wxSoundRouterStream(sndio
);
116 m_state
= new g72state
;
117 g72x_init_state(m_state
);
120 wxSoundStreamG72X::~wxSoundStreamG72X()
125 wxSoundStream
& wxSoundStreamG72X::Read(void *buffer
, wxUint32 len
)
127 wxUint16
*old_linear
;
128 register wxUint16
*linear_buffer
;
129 register wxUint32 real_len
;
130 register wxUint32 countdown
= len
;
132 real_len
= (len
* 8 / m_n_bits
);
134 old_linear
= linear_buffer
= new wxUint16
[real_len
];
136 m_router
->Read(linear_buffer
, real_len
);
138 real_len
= (wxUint32
)(m_router
->GetLastAccess() * ((float)m_n_bits
/ 8));
142 m_io_buffer
= (wxUint8
*)buffer
;
145 while (countdown
!= 0) {
146 PutBits(m_coder(*linear_buffer
++, AUDIO_ENCODING_LINEAR
, m_state
));
149 m_lastcount
= real_len
;
150 m_snderror
= m_router
->GetError();
157 wxSoundStream
& wxSoundStreamG72X::Write(const void *buffer
, wxUint32 len
)
159 wxUint16
*old_linear
;
160 register wxUint16
*linear_buffer
;
161 register wxUint32 countdown
= len
;
162 register wxUint32 real_len
;
164 // Compute the real length (PCM format) to sendt to the sound card
165 real_len
= (len
* m_n_bits
/ 8);
167 // Allocate a temporary buffer
168 old_linear
= linear_buffer
= new wxUint16
[real_len
];
170 // Bad, we override the const
171 m_io_buffer
= (wxUint8
*)buffer
;
175 while (countdown
!= 0) {
176 *linear_buffer
++ = m_decoder(GetBits(), AUDIO_ENCODING_LINEAR
, m_state
);
181 // Send them to the sound card
182 m_router
->Write(old_linear
, real_len
);
184 // Destroy the temporary buffer
190 bool wxSoundStreamG72X::SetSoundFormat(const wxSoundFormatBase
& format
)
192 if (format
.GetType() != wxSOUND_G72X
) {
193 m_snderror
= wxSOUND_INVFRMT
;
197 wxSoundFormatPcm pcm
;
198 wxSoundFormatG72X
*g72x
;
200 wxSoundStreamCodec::SetSoundFormat(format
);
202 g72x
= (wxSoundFormatG72X
*)m_sndformat
;
204 // Set PCM as the output format of the codec
205 pcm
.SetSampleRate(g72x
->GetSampleRate());
209 pcm
.SetOrder(wxBYTE_ORDER
);
211 // Look for the correct codec to use and set its bit width
212 switch (g72x
->GetG72XType()) {
215 m_coder
= g721_encoder
;
216 m_decoder
= g721_decoder
;
218 case wxSOUND_G723_24
:
220 m_coder
= g723_24_encoder
;
221 m_decoder
= g723_24_decoder
;
223 case wxSOUND_G723_40
:
225 m_coder
= g723_40_encoder
;
226 m_decoder
= g723_40_decoder
;
230 // Let the router finish the work
231 m_router
->SetSoundFormat(pcm
);
238 wxUint8
wxSoundStreamG72X::GetBits()
240 register wxUint8 bits
;
242 if (m_current_b_pos
< m_n_bits
) {
243 register wxUint8 b_left
;
245 // TRANSLATE the mask
246 m_current_mask
>>= m_current_b_pos
;
248 // GET the last bits: 0001..1
249 bits
= (m_current_byte
& m_current_mask
) << (m_n_bits
- m_current_b_pos
);
251 // GEN: 1. n times .1000
252 b_left
= BYTE_SIZE
-m_n_bits
;
253 m_current_mask
= ((1 << m_n_bits
) - 1) << b_left
;
256 m_current_byte
= *m_io_buffer
++;
258 register wxUint8 tmp_mask
;
260 // COMPUTE a new temporary mask to get the last bits
261 b_left
= m_n_bits
- b_left
;
262 tmp_mask
= (1 << b_left
) - 1;
263 // TRANSLATE the old mask to get ready for the next time
264 m_current_mask
>>= b_left
;
266 // COMPUTE the new bit position
267 b_left
= BYTE_SIZE
- b_left
;
268 m_current_b_pos
= b_left
;
272 bits
|= (m_current_byte
& tmp_mask
) >> b_left
;
274 m_current_mask
>>= m_n_bits
;
275 m_current_b_pos
-= m_n_bits
;
276 bits
= (m_current_byte
& m_current_mask
) >> m_current_b_pos
;
281 void wxSoundStreamG72X::PutBits(wxUint8 bits
)
283 if (m_current_b_pos
< m_n_bits
) {
284 register wxUint8 tmp_mask
;
285 register wxUint8 diff
;
287 diff
= m_n_bits
- m_current_b_pos
;
288 // Pack bits and put the byte in the buffer
289 m_current_byte
|= bits
>> diff
;
290 *m_io_buffer
++ = m_current_byte
;
293 tmp_mask
= ~((1 << diff
) - 1);
295 m_current_b_pos
= BYTE_SIZE
- (m_n_bits
- m_current_b_pos
);
297 m_current_byte
= (bits
& (tmp_mask
)) << m_current_b_pos
;
299 m_current_b_pos
-= m_n_bits
;
300 bits
<<= m_current_b_pos
;
301 m_current_byte
|= bits
;