]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndcpcm.cpp
some changes to make wxHtmlHelpController easier to subclass
[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
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
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
84 wxSoundStream& wxSoundStreamPcm::Read(void *buffer, wxUint32 len)
85 {
86 wxUint32 real_len;
87 char *tmp_buf;
88
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
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);
101 m_lastcount = m_sndio->GetLastAccess();
102 m_snderror = m_sndio->GetError();
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
113 return *this;
114 }
115
116 wxSoundStream& wxSoundStreamPcm::Write(const void *buffer, wxUint32 len)
117 {
118 char *tmp_buf;
119 wxUint32 len2;
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;
141 ConverterType *current_table_out, *current_table_in;
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;
162 if (pcm_format->GetBPS() != pcm_format2->GetBPS()) {
163 m_16_to_8 = TRUE;
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 }
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
202 m_function_out = current_table_out[index];
203 m_function_in = current_table_in[index];
204
205 m_sndio->SetSoundFormat(*new_format);
206 m_sndformat = new_format;
207 return TRUE;
208 }