]>
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 // --------------------------------------------------------------------------
110 wxSoundStreamG72X::wxSoundStreamG72X(wxSoundStream
& sndio
)
111 : wxSoundStreamCodec(sndio
)
114 m_router
= new wxSoundRouterStream(sndio
);
115 m_state
= new struct g72x_state
;
116 g72x_init_state(m_state
);
119 wxSoundStreamG72X::~wxSoundStreamG72X()
124 wxSoundStream
& wxSoundStreamG72X::Read(void *buffer
, wxUint32 len
)
129 wxSoundStream
& wxSoundStreamG72X::Write(const void *buffer
, wxUint32 len
)
131 wxUint16
*old_linear
;
132 register wxUint16
*linear_buffer
;
133 register wxUint32 countdown
= len
;
134 register wxUint32 real_len
;
136 real_len
= (wxUint32
)(len
* ((float)m_n_bits
/ 8));
138 old_linear
= linear_buffer
= new wxUint16
[real_len
];
140 // Bad, we override the const
141 m_io_buffer
= (wxUint8
*)buffer
;
144 while (countdown
!= 0) {
145 *linear_buffer
++ = m_decoder(GetBits(), AUDIO_ENCODING_LINEAR
, m_state
);
150 m_router
->Write(old_linear
, real_len
);
157 bool wxSoundStreamG72X::SetSoundFormat(const wxSoundFormatBase
& format
)
159 if (format
.GetType() != wxSOUND_G72X
) {
160 m_snderror
= wxSOUND_INVFRMT
;
164 wxSoundFormatPcm pcm
;
165 wxSoundFormatG72X
*g72x
;
167 wxSoundStreamCodec::SetSoundFormat(format
);
169 g72x
= (wxSoundFormatG72X
*)m_sndformat
;
171 pcm
.SetSampleRate(g72x
->GetSampleRate());
175 pcm
.SetOrder(wxBYTE_ORDER
);
177 switch (g72x
->GetG72XType()) {
180 m_coder
= g721_encoder
;
181 m_decoder
= g721_decoder
;
183 case wxSOUND_G723_24
:
185 m_coder
= g723_24_encoder
;
186 m_decoder
= g723_24_decoder
;
188 case wxSOUND_G723_40
:
190 m_coder
= g723_40_encoder
;
191 m_decoder
= g723_40_decoder
;
195 m_router
->SetSoundFormat(pcm
);
202 wxUint8
wxSoundStreamG72X::GetBits()
204 register wxUint8 bits
;
206 if (m_current_b_pos
< m_n_bits
) {
207 register wxUint8 b_left
;
209 // TRANSLATE the mask
210 m_current_mask
>>= m_current_b_pos
;
212 // GET the last bits: 0001..1
213 bits
= (m_current_byte
& m_current_mask
) << (m_n_bits
- m_current_b_pos
);
215 // GEN: 1. n times .1000
216 b_left
= BYTE_SIZE
-m_n_bits
;
217 m_current_mask
= ((1 << m_n_bits
) - 1) << b_left
;
220 m_current_byte
= *m_io_buffer
++;
222 register wxUint8 tmp_mask
;
224 // COMPUTE a new temporary mask to get the last bits
225 b_left
= m_n_bits
- b_left
;
226 tmp_mask
= (1 << b_left
) - 1;
227 // TRANSLATE the old mask to get ready for the next time
228 m_current_mask
>>= b_left
;
230 // COMPUTE the new bit position
231 b_left
= BYTE_SIZE
- b_left
;
232 m_current_b_pos
= b_left
;
236 bits
|= (m_current_byte
& tmp_mask
) >> b_left
;
238 m_current_mask
>>= m_n_bits
;
239 m_current_b_pos
-= m_n_bits
;
240 bits
= (m_current_byte
& m_current_mask
) >> m_current_b_pos
;
245 void wxSoundStreamG72X::PutBits(wxUint8 bits
)
247 if (m_current_b_pos
< m_n_bits
) {
248 register wxUint8 tmp_mask
;
249 register wxUint8 diff
;
251 diff
= m_n_bits
- m_current_b_pos
;
252 // Pack bits and put the byte in the buffer
253 m_current_byte
|= bits
>> diff
;
254 *m_io_buffer
++ = m_current_byte
;
257 tmp_mask
= ~((1 << diff
) - 1);
259 m_current_b_pos
= BYTE_SIZE
- (m_n_bits
- m_current_b_pos
);
261 m_current_byte
= (bits
& (tmp_mask
)) << m_current_b_pos
;
263 m_current_b_pos
-= m_n_bits
;
264 bits
<<= m_current_b_pos
;
265 m_current_byte
|= bits
;