1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndcpcm.cpp"
12 #include <wx/wxprec.h>
17 wxSoundStreamPcm::wxSoundStreamPcm(wxSoundStream
& sndio
)
18 : wxSoundStreamCodec(sndio
)
21 m_function_out
= NULL
;
24 wxSoundStreamPcm::~wxSoundStreamPcm()
30 #include "converter.def"
34 #include "converter.def"
37 wxSoundStreamPcm::ConverterType s_convert_out_16_to_8
[] = {
39 Convert_16to8_U2S_16_no
,
42 Convert_16to8_U2S_16_yes
,
46 wxSoundStreamPcm::ConverterType s_convert_out_16
[] = {
49 Convert_U2S_SWAP_16_no
,
50 Convert_U2S_SWAP_16_yes
,
55 wxSoundStreamPcm::ConverterType s_convert_out_8
[] = {
64 wxSoundStreamPcm::ConverterType s_convert_in_8_to_16
[] = {
67 Convert_8to16_U2S_SWAP_8
,
73 wxSoundStreamPcm::ConverterType
*s_convert_in_16
= s_convert_out_16
;
75 wxSoundStreamPcm::ConverterType
*s_convert_in_8
= s_convert_out_8
;
78 #define CONVERTER_SIGN 1
79 #define CONVERTER_SIGN_SWAP 2
80 #define CONVERTER_SWAP_SIGN_SWAP 3
81 #define CONVERTER_SWAP_SIGN 4
82 #define CONVERTER_SWAP 5
84 wxSoundStream
& wxSoundStreamPcm::Read(void *buffer
, wxUint32 len
)
90 m_sndio
->Read(buffer
, len
);
91 m_lastcount
= m_sndio
->GetLastAccess();
92 m_snderror
= m_sndio
->GetError();
96 real_len
= (m_16_to_8
) ? len
/ 2 : len
;
98 tmp_buf
= new char[real_len
];
100 m_sndio
->Read(tmp_buf
, real_len
);
101 m_lastcount
= m_sndio
->GetLastAccess();
102 m_snderror
= m_sndio
->GetError();
103 if (m_snderror
!= wxSOUND_NOERR
)
106 m_function_in(tmp_buf
, (char *)buffer
, m_lastcount
);
116 wxSoundStream
& wxSoundStreamPcm::Write(const void *buffer
, wxUint32 len
)
122 return m_sndio
->Write(buffer
, len
);
124 len2
= (m_16_to_8
) ? len
/ 2 : len
;
126 tmp_buf
= new char[len2
];
127 m_function_out((const char *)buffer
, tmp_buf
, len
);
128 m_sndio
->Write(tmp_buf
, len
);
131 m_lastcount
= (m_16_to_8
) ?
132 (m_sndio
->GetLastAccess() * 2) : m_sndio
->GetLastAccess();
137 bool wxSoundStreamPcm::SetSoundFormat(const wxSoundFormatBase
& format
)
139 wxSoundFormatBase
*new_format
;
140 wxSoundFormatPcm
*pcm_format
, *pcm_format2
;
141 ConverterType
*current_table_out
, *current_table_in
;
145 if (m_sndio
->SetSoundFormat(format
)) {
146 m_function_out
= NULL
;
147 m_function_in
= NULL
;
150 if (format
.GetType() != wxSOUND_PCM
) {
151 m_snderror
= wxSOUND_INVFRMT
;
157 new_format
= m_sndio
->GetSoundFormat().Clone();
158 pcm_format
= (wxSoundFormatPcm
*)&format
;
159 pcm_format2
= (wxSoundFormatPcm
*)new_format
;
162 if (pcm_format
->GetBPS() != pcm_format2
->GetBPS()) {
164 current_table_out
= s_convert_out_16_to_8
;
165 current_table_in
= s_convert_in_8_to_16
;
166 } else if (pcm_format
->GetBPS() == 16) {
167 current_table_out
= s_convert_out_16
;
168 current_table_in
= s_convert_in_16
;
170 current_table_out
= s_convert_out_8
;
171 current_table_in
= s_convert_in_8
;
174 change_sign
= (pcm_format2
->Signed() != pcm_format
->Signed());
176 #define MY_ORDER wxBYTE_ORDER
177 #if wxBYTE_ORDER == wxLITTLE_ENDIAN
178 #define OTHER_ORDER wxBIG_ENDIAN
180 #define OTHER_ORDER wxLITTLE_ENDIAN
183 if (pcm_format
->GetOrder() == OTHER_ORDER
&&
184 pcm_format2
->GetOrder() == OTHER_ORDER
&& change_sign
)
185 index
= CONVERTER_SWAP_SIGN_SWAP
;
187 else if (pcm_format
->GetOrder() == OTHER_ORDER
&&
188 pcm_format2
->GetOrder() == MY_ORDER
&& change_sign
)
189 index
= CONVERTER_SWAP_SIGN
;
191 else if (pcm_format
->GetOrder() == MY_ORDER
&&
192 pcm_format
->GetOrder() == OTHER_ORDER
&& change_sign
)
193 index
= CONVERTER_SIGN_SWAP
;
195 else if (!change_sign
&&
196 pcm_format
->GetOrder() != pcm_format2
->GetOrder())
197 index
= CONVERTER_SWAP
;
202 m_function_out
= current_table_out
[index
];
203 m_function_in
= current_table_in
[index
];
205 m_sndio
->SetSoundFormat(*new_format
);
206 m_sndformat
= new_format
;