]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndcpcm.cpp
Added a lot of comments
[wxWidgets.git] / utils / wxMMedia2 / lib / sndcpcm.cpp
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
12 #include <wx/wxprec.h>
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
37 wxSoundStreamPcm::ConverterType s_convert_out_16_to_8[] = {
38 Convert_16to8_16_no,
39 Convert_16to8_U2S_16_no,
40 NULL,
41 NULL,
42 Convert_16to8_U2S_16_yes,
43 Convert_16to8_16_yes
44 };
45
46 wxSoundStreamPcm::ConverterType s_convert_out_16[] = {
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
55 wxSoundStreamPcm::ConverterType s_convert_out_8[] = {
56 NULL,
57 Convert_U2S_8,
58 Convert_U2S_8,
59 Convert_U2S_8,
60 Convert_U2S_8,
61 NULL
62 /*,
63 Convert_U2S_S2M_8,
64 Convert_U2S_S2M_8,
65 Convert_U2S_S2M_8,
66 Convert_U2S_S2M_8,
67 Convert_S2M_8 */
68 };
69
70 wxSoundStreamPcm::ConverterType s_convert_in_8_to_16[] = {
71 Convert_8to16_8,
72 Convert_8to16_U2S_8,
73 Convert_8to16_U2S_SWAP_8,
74 NULL,
75 NULL,
76 Convert_8to16_SWAP_8
77 };
78
79 wxSoundStreamPcm::ConverterType *s_convert_in_16 = s_convert_out_16;
80
81 wxSoundStreamPcm::ConverterType *s_convert_in_8 = s_convert_out_8;
82
83 #define CONVERTER 0
84 #define CONVERTER_SIGN 1
85 #define CONVERTER_SIGN_SWAP 2
86 #define CONVERTER_SWAP_SIGN_SWAP 3
87 #define CONVERTER_SWAP_SIGN 4
88 #define CONVERTER_SWAP 5
89 #define CONVERTER_SIGN_STEREO_MONO 6
90 #define CONVERTER_SIGN_SWAP_STEREO_MONO 7
91 #define CONVERTER_SWAP_SIGN_SWAP_STEREO_MONO 8
92 #define CONVERTER_SWAP_SIGN_STEREO_MONO 9
93 #define CONVERTER_SWAP_STEREO_MONO 10
94 #define CONVERTER_STEREO_MONO 11
95
96 //
97 // TODO: Read() and Write() aren't really safe. If you give it a buffer which
98 // is not aligned on 8, you may crash (See converter.def).
99 //
100
101 wxSoundStream& wxSoundStreamPcm::Read(void *buffer, wxUint32 len)
102 {
103 wxUint32 real_len;
104 char *tmp_buf;
105
106 if (!m_function_in) {
107 m_sndio->Read(buffer, len);
108 m_lastcount = m_sndio->GetLastAccess();
109 m_snderror = m_sndio->GetError();
110 return *this;
111 }
112
113 real_len = (m_16_to_8) ? len / 2 : len;
114
115 tmp_buf = new char[real_len];
116
117 m_sndio->Read(tmp_buf, real_len);
118 m_lastcount = m_sndio->GetLastAccess();
119 m_snderror = m_sndio->GetError();
120 if (m_snderror != wxSOUND_NOERR)
121 return *this;
122
123 m_function_in(tmp_buf, (char *)buffer, m_lastcount);
124
125 delete[] tmp_buf;
126
127 if (m_16_to_8)
128 m_lastcount *= 2;
129
130 return *this;
131 }
132
133 wxSoundStream& wxSoundStreamPcm::Write(const void *buffer, wxUint32 len)
134 {
135 char *tmp_buf;
136 wxUint32 len2;
137
138 if (!m_function_out)
139 return m_sndio->Write(buffer, len);
140
141 len2 = (m_16_to_8) ? len / 2 : len;
142
143 tmp_buf = new char[len2];
144 m_function_out((const char *)buffer, tmp_buf, len);
145 m_sndio->Write(tmp_buf, len);
146 delete[] tmp_buf;
147
148 m_lastcount = (m_16_to_8) ?
149 (m_sndio->GetLastAccess() * 2) : m_sndio->GetLastAccess();
150
151 return *this;
152 }
153
154 bool wxSoundStreamPcm::SetSoundFormat(const wxSoundFormatBase& format)
155 {
156 wxSoundFormatBase *new_format;
157 wxSoundFormatPcm *pcm_format, *pcm_format2;
158 ConverterType *current_table_out, *current_table_in;
159 int index;
160 bool change_sign;
161
162 if (m_sndio->SetSoundFormat(format)) {
163 m_function_out = NULL;
164 m_function_in = NULL;
165 return TRUE;
166 }
167 if (format.GetType() != wxSOUND_PCM) {
168 m_snderror = wxSOUND_INVFRMT;
169 return FALSE;
170 }
171 if (m_sndformat)
172 delete m_sndformat;
173
174 new_format = m_sndio->GetSoundFormat().Clone();
175 pcm_format = (wxSoundFormatPcm *)&format;
176 pcm_format2 = (wxSoundFormatPcm *)new_format;
177
178 m_16_to_8 = FALSE;
179 if (pcm_format->GetBPS() != pcm_format2->GetBPS()) {
180 m_16_to_8 = TRUE;
181 current_table_out = s_convert_out_16_to_8;
182 current_table_in = s_convert_in_8_to_16;
183 } else if (pcm_format->GetBPS() == 16) {
184 current_table_out = s_convert_out_16;
185 current_table_in = s_convert_in_16;
186 } else {
187 current_table_out = s_convert_out_8;
188 current_table_in = s_convert_in_8;
189 }
190
191 change_sign = (pcm_format2->Signed() != pcm_format->Signed());
192
193 #define MY_ORDER wxBYTE_ORDER
194 #if wxBYTE_ORDER == wxLITTLE_ENDIAN
195 #define OTHER_ORDER wxBIG_ENDIAN
196 #else
197 #define OTHER_ORDER wxLITTLE_ENDIAN
198 #endif
199
200 if (pcm_format->GetOrder() == OTHER_ORDER &&
201 pcm_format2->GetOrder() == OTHER_ORDER && change_sign)
202 index = CONVERTER_SWAP_SIGN_SWAP;
203
204 else if (pcm_format->GetOrder() == OTHER_ORDER &&
205 pcm_format2->GetOrder() == MY_ORDER && change_sign)
206 index = CONVERTER_SWAP_SIGN;
207
208 else if (pcm_format->GetOrder() == MY_ORDER &&
209 pcm_format->GetOrder() == OTHER_ORDER && change_sign)
210 index = CONVERTER_SIGN_SWAP;
211
212 else if (!change_sign &&
213 pcm_format->GetOrder() != pcm_format2->GetOrder())
214 index = CONVERTER_SWAP;
215
216 else
217 index = CONVERTER;
218
219 m_function_out = current_table_out[index];
220 m_function_in = current_table_in[index];
221
222 m_sndio->SetSoundFormat(*new_format);
223 m_sndformat = new_format;
224 return TRUE;
225 }