]>
Commit | Line | Data |
---|---|---|
526ddb13 GL |
1 | // -------------------------------------------------------------------------- |
2 | // Name: sndcpcm.cpp | |
3 | // Purpose: | |
4 | // Date: 08/11/1999 | |
5 | // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999 | |
6 | // CVSID: $Id$ | |
7 | // -------------------------------------------------------------------------- | |
8 | #ifdef __GNUG__ | |
9 | #pragma implementation "sndcpcm.cpp" | |
10 | #endif | |
11 | ||
6c5e6376 | 12 | #include <wx/wxprec.h> |
526ddb13 GL |
13 | #include "sndbase.h" |
14 | #include "sndpcm.h" | |
15 | #include "sndcpcm.h" | |
16 | ||
17 | wxSoundStreamPcm::wxSoundStreamPcm(wxSoundStream& sndio) | |
18 | : wxSoundStreamCodec(sndio) | |
19 | { | |
20 | m_function_in = NULL; | |
21 | m_function_out = NULL; | |
22 | } | |
23 | ||
24 | wxSoundStreamPcm::~wxSoundStreamPcm() | |
25 | { | |
26 | } | |
27 | ||
28 | ||
29 | #define SWAP_BYTES 0 | |
30 | #include "converter.def" | |
31 | #undef SWAP_BYTES | |
32 | ||
33 | #define SWAP_BYTES 1 | |
34 | #include "converter.def" | |
35 | #undef SWAP_BYTES | |
36 | ||
0662cd32 | 37 | wxSoundStreamPcm::ConverterType s_convert_out_16_to_8[] = { |
526ddb13 GL |
38 | Convert_16to8_16_no, |
39 | Convert_16to8_U2S_16_no, | |
40 | NULL, | |
41 | NULL, | |
42 | Convert_16to8_U2S_16_yes, | |
0662cd32 | 43 | Convert_16to8_16_yes |
526ddb13 GL |
44 | }; |
45 | ||
0662cd32 | 46 | wxSoundStreamPcm::ConverterType s_convert_out_16[] = { |
526ddb13 GL |
47 | NULL, |
48 | Convert_U2S_16_no, | |
49 | Convert_U2S_SWAP_16_no, | |
50 | Convert_U2S_SWAP_16_yes, | |
51 | Convert_U2S_16_yes, | |
52 | Convert_SWAP_16_no | |
53 | }; | |
54 | ||
0662cd32 | 55 | wxSoundStreamPcm::ConverterType s_convert_out_8[] = { |
526ddb13 GL |
56 | NULL, |
57 | Convert_U2S_8, | |
58 | Convert_U2S_8, | |
59 | Convert_U2S_8, | |
60 | Convert_U2S_8, | |
61 | NULL | |
62 | }; | |
63 | ||
0662cd32 GL |
64 | wxSoundStreamPcm::ConverterType s_convert_in_8_to_16[] = { |
65 | Convert_8to16_8, | |
66 | Convert_8to16_U2S_8, | |
67 | Convert_8to16_U2S_SWAP_8, | |
68 | NULL, | |
69 | NULL, | |
70 | Convert_8to16_SWAP_8 | |
71 | }; | |
72 | ||
73 | wxSoundStreamPcm::ConverterType *s_convert_in_16 = s_convert_out_16; | |
74 | ||
75 | wxSoundStreamPcm::ConverterType *s_convert_in_8 = s_convert_out_8; | |
76 | ||
526ddb13 GL |
77 | #define CONVERTER 0 |
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 | |
83 | ||
0662cd32 | 84 | wxSoundStream& wxSoundStreamPcm::Read(void *buffer, wxUint32 len) |
526ddb13 | 85 | { |
0662cd32 GL |
86 | wxUint32 real_len; |
87 | char *tmp_buf; | |
88 | ||
526ddb13 GL |
89 | if (!m_function_in) { |
90 | m_sndio->Read(buffer, len); | |
91 | m_lastcount = m_sndio->GetLastAccess(); | |
92 | m_snderror = m_sndio->GetError(); | |
93 | return *this; | |
94 | } | |
95 | ||
0662cd32 GL |
96 | real_len = (m_16_to_8) ? len / 2 : len; |
97 | ||
98 | tmp_buf = new char[real_len]; | |
99 | ||
100 | m_sndio->Read(tmp_buf, real_len); | |
526ddb13 GL |
101 | m_lastcount = m_sndio->GetLastAccess(); |
102 | m_snderror = m_sndio->GetError(); | |
0662cd32 GL |
103 | if (m_snderror != wxSOUND_NOERR) |
104 | return *this; | |
105 | ||
106 | m_function_in(tmp_buf, (char *)buffer, m_lastcount); | |
107 | ||
108 | delete[] tmp_buf; | |
109 | ||
110 | if (m_16_to_8) | |
111 | m_lastcount *= 2; | |
112 | ||
526ddb13 GL |
113 | return *this; |
114 | } | |
115 | ||
0662cd32 | 116 | wxSoundStream& wxSoundStreamPcm::Write(const void *buffer, wxUint32 len) |
526ddb13 GL |
117 | { |
118 | char *tmp_buf; | |
0662cd32 | 119 | wxUint32 len2; |
526ddb13 GL |
120 | |
121 | if (!m_function_out) | |
122 | return m_sndio->Write(buffer, len); | |
123 | ||
124 | len2 = (m_16_to_8) ? len / 2 : len; | |
125 | ||
126 | tmp_buf = new char[len2]; | |
127 | m_function_out((const char *)buffer, tmp_buf, len); | |
128 | m_sndio->Write(tmp_buf, len); | |
129 | delete[] tmp_buf; | |
130 | ||
131 | m_lastcount = (m_16_to_8) ? | |
132 | (m_sndio->GetLastAccess() * 2) : m_sndio->GetLastAccess(); | |
133 | ||
134 | return *this; | |
135 | } | |
136 | ||
137 | bool wxSoundStreamPcm::SetSoundFormat(const wxSoundFormatBase& format) | |
138 | { | |
139 | wxSoundFormatBase *new_format; | |
140 | wxSoundFormatPcm *pcm_format, *pcm_format2; | |
0662cd32 | 141 | ConverterType *current_table_out, *current_table_in; |
526ddb13 GL |
142 | int index; |
143 | bool change_sign; | |
144 | ||
145 | if (m_sndio->SetSoundFormat(format)) { | |
146 | m_function_out = NULL; | |
147 | m_function_in = NULL; | |
148 | return TRUE; | |
149 | } | |
150 | if (format.GetType() != wxSOUND_PCM) { | |
151 | m_snderror = wxSOUND_INVFRMT; | |
152 | return FALSE; | |
153 | } | |
154 | if (m_sndformat) | |
155 | delete m_sndformat; | |
156 | ||
157 | new_format = m_sndio->GetSoundFormat().Clone(); | |
158 | pcm_format = (wxSoundFormatPcm *)&format; | |
159 | pcm_format2 = (wxSoundFormatPcm *)new_format; | |
160 | ||
161 | m_16_to_8 = FALSE; | |
aa95f52e | 162 | if (pcm_format->GetBPS() != pcm_format2->GetBPS()) { |
526ddb13 | 163 | m_16_to_8 = TRUE; |
0662cd32 GL |
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; | |
169 | } else { | |
170 | current_table_out = s_convert_out_8; | |
171 | current_table_in = s_convert_in_8; | |
172 | } | |
526ddb13 GL |
173 | |
174 | change_sign = (pcm_format2->Signed() != pcm_format->Signed()); | |
175 | ||
176 | #define MY_ORDER wxBYTE_ORDER | |
177 | #if wxBYTE_ORDER == wxLITTLE_ENDIAN | |
178 | #define OTHER_ORDER wxBIG_ENDIAN | |
179 | #else | |
180 | #define OTHER_ORDER wxLITTLE_ENDIAN | |
181 | #endif | |
182 | ||
183 | if (pcm_format->GetOrder() == OTHER_ORDER && | |
184 | pcm_format2->GetOrder() == OTHER_ORDER && change_sign) | |
185 | index = CONVERTER_SWAP_SIGN_SWAP; | |
186 | ||
187 | else if (pcm_format->GetOrder() == OTHER_ORDER && | |
188 | pcm_format2->GetOrder() == MY_ORDER && change_sign) | |
189 | index = CONVERTER_SWAP_SIGN; | |
190 | ||
191 | else if (pcm_format->GetOrder() == MY_ORDER && | |
192 | pcm_format->GetOrder() == OTHER_ORDER && change_sign) | |
193 | index = CONVERTER_SIGN_SWAP; | |
194 | ||
195 | else if (!change_sign && | |
196 | pcm_format->GetOrder() != pcm_format2->GetOrder()) | |
197 | index = CONVERTER_SWAP; | |
198 | ||
199 | else | |
200 | index = CONVERTER; | |
201 | ||
0662cd32 GL |
202 | m_function_out = current_table_out[index]; |
203 | m_function_in = current_table_in[index]; | |
526ddb13 GL |
204 | |
205 | m_sndio->SetSoundFormat(*new_format); | |
206 | m_sndformat = new_format; | |
207 | return TRUE; | |
208 | } |