]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/mmedia/sndg72x.cpp
1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
8 // --------------------------------------------------------------------------
10 #include "wx/wxprec.h"
16 #include "wx/mmedia/sndbase.h"
17 #include "wx/mmedia/sndfile.h"
18 #include "wx/mmedia/sndpcm.h"
19 #include "wx/mmedia/sndg72x.h"
20 #include "wx/mmedia/internal/g72x.h"
22 // --------------------------------------------------------------------------
24 // --------------------------------------------------------------------------
26 wxSoundFormatG72X::wxSoundFormatG72X()
31 wxSoundFormatG72X::~wxSoundFormatG72X()
35 void wxSoundFormatG72X::SetSampleRate(wxUint32 srate
)
40 wxUint32
wxSoundFormatG72X::GetSampleRate() const
45 void wxSoundFormatG72X::SetG72XType(wxSoundG72XType type
)
50 wxSoundFormatBase
*wxSoundFormatG72X::Clone() const
52 wxSoundFormatG72X
*g72x
= new wxSoundFormatG72X();
54 g72x
->m_srate
= m_srate
;
55 g72x
->m_g72x_type
= m_g72x_type
;
59 wxUint32
wxSoundFormatG72X::GetTimeFromBytes(wxUint32 bytes
) const
63 switch (m_g72x_type
) {
77 return (wxUint32
)((bytes
/ m_srate
) * n_bits
) / 8;
80 wxUint32
wxSoundFormatG72X::GetBytesFromTime(wxUint32 time
) const
84 switch (m_g72x_type
) {
97 return (wxUint32
)((time
* m_srate
* n_bits
) / 8);
100 bool wxSoundFormatG72X::operator !=(const wxSoundFormatBase
& frmt2
) const
102 wxSoundFormatG72X
*g72x
= (wxSoundFormatG72X
*)&frmt2
;
104 if (frmt2
.GetType() != wxSOUND_G72X
)
107 return (g72x
->m_srate
!= m_srate
|| g72x
->m_g72x_type
!= m_g72x_type
);
110 // --------------------------------------------------------------------------
112 // --------------------------------------------------------------------------
114 wxSoundStreamG72X::wxSoundStreamG72X(wxSoundStream
& sndio
)
115 : wxSoundStreamCodec(sndio
)
118 m_router
= new wxSoundRouterStream(sndio
);
119 m_state
= new g72state
;
120 g72x_init_state(m_state
);
123 wxSoundStreamG72X::~wxSoundStreamG72X()
128 wxSoundStream
& wxSoundStreamG72X::Read(void *buffer
, wxUint32 len
)
130 wxUint16
*old_linear
;
131 register wxUint16
*linear_buffer
;
132 register wxUint32 real_len
;
133 register wxUint32 countdown
= len
;
135 real_len
= (len
* 8 / m_n_bits
);
137 old_linear
= linear_buffer
= new wxUint16
[real_len
];
139 m_router
->Read(linear_buffer
, real_len
);
141 real_len
= (wxUint32
)(m_router
->GetLastAccess() * ((float)m_n_bits
/ 8));
145 m_io_buffer
= (wxUint8
*)buffer
;
148 while (countdown
!= 0) {
149 PutBits(m_coder(*linear_buffer
++, AUDIO_ENCODING_LINEAR
, m_state
));
152 m_lastcount
= real_len
;
153 m_snderror
= m_router
->GetError();
160 wxSoundStream
& wxSoundStreamG72X::Write(const void *buffer
, wxUint32 len
)
162 wxUint16
*old_linear
;
163 register wxUint16
*linear_buffer
;
164 register wxUint32 countdown
= len
;
165 register wxUint32 real_len
;
167 // Compute the real length (PCM format) to sendt to the sound card
168 real_len
= (len
* m_n_bits
/ 8);
170 // Allocate a temporary buffer
171 old_linear
= linear_buffer
= new wxUint16
[real_len
];
173 // Bad, we override the const
174 m_io_buffer
= (wxUint8
*)buffer
;
178 while (countdown
!= 0) {
179 *linear_buffer
++ = m_decoder(GetBits(), AUDIO_ENCODING_LINEAR
, m_state
);
184 // Send them to the sound card
185 m_router
->Write(old_linear
, real_len
);
187 // Destroy the temporary buffer
193 bool wxSoundStreamG72X::SetSoundFormat(const wxSoundFormatBase
& format
)
195 if (format
.GetType() != wxSOUND_G72X
) {
196 m_snderror
= wxSOUND_INVFRMT
;
200 wxSoundFormatPcm pcm
;
201 wxSoundFormatG72X
*g72x
;
203 wxSoundStreamCodec::SetSoundFormat(format
);
205 g72x
= (wxSoundFormatG72X
*)m_sndformat
;
207 // Set PCM as the output format of the codec
208 pcm
.SetSampleRate(g72x
->GetSampleRate());
210 pcm
.SetChannels(1); // Only mono supported
212 pcm
.SetOrder(wxBYTE_ORDER
);
214 // Look for the correct codec to use and set its bit width
215 switch (g72x
->GetG72XType()) {
218 m_coder
= g721_encoder
;
219 m_decoder
= g721_decoder
;
221 case wxSOUND_G723_24
:
223 m_coder
= g723_24_encoder
;
224 m_decoder
= g723_24_decoder
;
226 case wxSOUND_G723_40
:
228 m_coder
= g723_40_encoder
;
229 m_decoder
= g723_40_decoder
;
233 // Let the router finish the work
234 m_router
->SetSoundFormat(pcm
);
241 wxUint8
wxSoundStreamG72X::GetBits()
243 register wxUint8 bits
;
245 // We have two bytes to compute
246 if (m_current_b_pos
< m_n_bits
) {
247 register wxUint8 b_left
;
249 // TRANSLATE the mask
250 m_current_mask
>>= m_current_b_pos
;
252 // GET the last bits: 0001..1
253 bits
= (m_current_byte
& m_current_mask
) << (m_n_bits
- m_current_b_pos
);
255 // GEN: 1. n times .1000
256 b_left
= BYTE_SIZE
-m_n_bits
;
257 m_current_mask
= ((1 << m_n_bits
) - 1) << b_left
;
260 m_current_byte
= *m_io_buffer
++;
262 register wxUint8 tmp_mask
;
264 // COMPUTE a new temporary mask to get the last bits
265 b_left
= m_n_bits
- b_left
;
266 tmp_mask
= (1 << b_left
) - 1;
267 // TRANSLATE the old mask to get ready for the next time
268 m_current_mask
>>= b_left
;
270 // COMPUTE the new bit position
271 b_left
= BYTE_SIZE
- b_left
;
272 m_current_b_pos
= b_left
;
276 bits
|= (m_current_byte
& tmp_mask
) >> b_left
;
278 m_current_mask
>>= m_n_bits
;
279 m_current_b_pos
-= m_n_bits
;
280 bits
= (m_current_byte
& m_current_mask
) >> m_current_b_pos
;
285 void wxSoundStreamG72X::PutBits(wxUint8 bits
)
287 if (m_current_b_pos
< m_n_bits
) {
288 register wxUint8 tmp_mask
;
289 register wxUint8 diff
;
291 diff
= m_n_bits
- m_current_b_pos
;
292 // Pack bits and put the byte in the buffer
293 m_current_byte
|= bits
>> diff
;
294 *m_io_buffer
++ = m_current_byte
;
297 tmp_mask
= ~((1 << diff
) - 1);
299 m_current_b_pos
= BYTE_SIZE
- (m_n_bits
- m_current_b_pos
);
301 m_current_byte
= (bits
& (tmp_mask
)) << m_current_b_pos
;
303 m_current_b_pos
-= m_n_bits
;
304 bits
<<= m_current_b_pos
;
305 m_current_byte
|= bits
;