]>
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
)
126 wxUint16
*old_linear
;
127 register wxUint16
*linear_buffer
;
128 register wxUint32 real_len
;
129 register wxUint32 countdown
= len
;
131 real_len
= (len
* 8 / m_n_bits
);
133 old_linear
= linear_buffer
= new wxUint16
[real_len
];
135 m_router
->Read(linear_buffer
, real_len
);
137 real_len
= (wxUint32
)(m_router
->GetLastAccess() * ((float)m_n_bits
/ 8));
141 m_io_buffer
= (wxUint8
*)buffer
;
144 while (countdown
!= 0) {
145 PutBits(m_coder(*linear_buffer
++, AUDIO_ENCODING_LINEAR
, m_state
));
148 m_lastcount
= real_len
;
149 m_snderror
= m_router
->GetError();
156 wxSoundStream
& wxSoundStreamG72X::Write(const void *buffer
, wxUint32 len
)
158 wxUint16
*old_linear
;
159 register wxUint16
*linear_buffer
;
160 register wxUint32 countdown
= len
;
161 register wxUint32 real_len
;
163 // Compute the real length (PCM format) to sendt to the sound card
164 real_len
= (len
* m_n_bits
/ 8);
166 // Allocate a temporary buffer
167 old_linear
= linear_buffer
= new wxUint16
[real_len
];
169 // Bad, we override the const
170 m_io_buffer
= (wxUint8
*)buffer
;
174 while (countdown
!= 0) {
175 *linear_buffer
++ = m_decoder(GetBits(), AUDIO_ENCODING_LINEAR
, m_state
);
180 // Send them to the sound card
181 m_router
->Write(old_linear
, real_len
);
183 // Destroy the temporary buffer
189 bool wxSoundStreamG72X::SetSoundFormat(const wxSoundFormatBase
& format
)
191 if (format
.GetType() != wxSOUND_G72X
) {
192 m_snderror
= wxSOUND_INVFRMT
;
196 wxSoundFormatPcm pcm
;
197 wxSoundFormatG72X
*g72x
;
199 wxSoundStreamCodec::SetSoundFormat(format
);
201 g72x
= (wxSoundFormatG72X
*)m_sndformat
;
203 // Set PCM as the output format of the codec
204 pcm
.SetSampleRate(g72x
->GetSampleRate());
208 pcm
.SetOrder(wxBYTE_ORDER
);
210 // Look for the correct codec to use and set its bit width
211 switch (g72x
->GetG72XType()) {
214 m_coder
= g721_encoder
;
215 m_decoder
= g721_decoder
;
217 case wxSOUND_G723_24
:
219 m_coder
= g723_24_encoder
;
220 m_decoder
= g723_24_decoder
;
222 case wxSOUND_G723_40
:
224 m_coder
= g723_40_encoder
;
225 m_decoder
= g723_40_decoder
;
229 // Let the router finish the work
230 m_router
->SetSoundFormat(pcm
);
237 wxUint8
wxSoundStreamG72X::GetBits()
239 register wxUint8 bits
;
241 if (m_current_b_pos
< m_n_bits
) {
242 register wxUint8 b_left
;
244 // TRANSLATE the mask
245 m_current_mask
>>= m_current_b_pos
;
247 // GET the last bits: 0001..1
248 bits
= (m_current_byte
& m_current_mask
) << (m_n_bits
- m_current_b_pos
);
250 // GEN: 1. n times .1000
251 b_left
= BYTE_SIZE
-m_n_bits
;
252 m_current_mask
= ((1 << m_n_bits
) - 1) << b_left
;
255 m_current_byte
= *m_io_buffer
++;
257 register wxUint8 tmp_mask
;
259 // COMPUTE a new temporary mask to get the last bits
260 b_left
= m_n_bits
- b_left
;
261 tmp_mask
= (1 << b_left
) - 1;
262 // TRANSLATE the old mask to get ready for the next time
263 m_current_mask
>>= b_left
;
265 // COMPUTE the new bit position
266 b_left
= BYTE_SIZE
- b_left
;
267 m_current_b_pos
= b_left
;
271 bits
|= (m_current_byte
& tmp_mask
) >> b_left
;
273 m_current_mask
>>= m_n_bits
;
274 m_current_b_pos
-= m_n_bits
;
275 bits
= (m_current_byte
& m_current_mask
) >> m_current_b_pos
;
280 void wxSoundStreamG72X::PutBits(wxUint8 bits
)
282 if (m_current_b_pos
< m_n_bits
) {
283 register wxUint8 tmp_mask
;
284 register wxUint8 diff
;
286 diff
= m_n_bits
- m_current_b_pos
;
287 // Pack bits and put the byte in the buffer
288 m_current_byte
|= bits
>> diff
;
289 *m_io_buffer
++ = m_current_byte
;
292 tmp_mask
= ~((1 << diff
) - 1);
294 m_current_b_pos
= BYTE_SIZE
- (m_n_bits
- m_current_b_pos
);
296 m_current_byte
= (bits
& (tmp_mask
)) << m_current_b_pos
;
298 m_current_b_pos
-= m_n_bits
;
299 bits
<<= m_current_b_pos
;
300 m_current_byte
|= bits
;