18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
*/ |
20 |
|
|
21 |
|
#include <strings.h> |
22 |
|
|
23 |
#include "rdesktop.h" |
#include "rdesktop.h" |
24 |
#include "rdpsnd.h" |
#include "rdpsnd.h" |
25 |
#include "rdpsnd_dsp.h" |
#include "rdpsnd_dsp.h" |
26 |
|
|
27 |
|
#ifdef HAVE_LIBSAMPLERATE |
28 |
|
#include <samplerate.h> |
29 |
|
|
30 |
|
#define SRC_CONVERTER SRC_SINC_MEDIUM_QUALITY |
31 |
|
#endif |
32 |
|
|
33 |
#define MAX_VOLUME 65535 |
#define MAX_VOLUME 65535 |
34 |
|
|
35 |
static uint16 softvol_left = MAX_VOLUME; |
static uint16 softvol_left = MAX_VOLUME; |
36 |
static uint16 softvol_right = MAX_VOLUME; |
static uint16 softvol_right = MAX_VOLUME; |
37 |
|
static uint32 resample_to_srate = 44100; |
38 |
|
static uint16 resample_to_bitspersample = 16; |
39 |
|
static uint16 resample_to_channels = 2; |
40 |
|
#ifdef HAVE_LIBSAMPLERATE |
41 |
|
static SRC_STATE *src_converter = NULL; |
42 |
|
#endif |
43 |
|
|
44 |
void |
void |
45 |
rdpsnd_dsp_softvol_set(uint16 left, uint16 right) |
rdpsnd_dsp_softvol_set(uint16 left, uint16 right) |
59 |
if ((softvol_left == MAX_VOLUME) && (softvol_right == MAX_VOLUME)) |
if ((softvol_left == MAX_VOLUME) && (softvol_right == MAX_VOLUME)) |
60 |
return; |
return; |
61 |
|
|
62 |
factor_left = (softvol_left * 256) / 65535; |
factor_left = (softvol_left * 256) / MAX_VOLUME; |
63 |
factor_right = (softvol_right * 256) / 65535; |
factor_right = (softvol_right * 256) / MAX_VOLUME; |
64 |
|
|
65 |
if (format->nChannels == 1) |
if (format->nChannels == 1) |
66 |
{ |
{ |
127 |
} |
} |
128 |
} |
} |
129 |
|
|
130 |
|
BOOL |
131 |
|
rdpsnd_dsp_resample_set(uint32 device_srate, uint16 device_bitspersample, uint16 device_channels) |
132 |
|
{ |
133 |
|
#ifdef HAVE_LIBSAMPLERATE |
134 |
|
int err; |
135 |
|
#endif |
136 |
|
|
137 |
|
if (device_bitspersample != 16 && device_bitspersample != 8) |
138 |
|
return False; |
139 |
|
|
140 |
|
if (device_channels != 1 && device_channels != 2) |
141 |
|
return False; |
142 |
|
|
143 |
|
resample_to_srate = device_srate; |
144 |
|
resample_to_bitspersample = device_bitspersample; |
145 |
|
resample_to_channels = device_channels; |
146 |
|
|
147 |
|
#ifdef HAVE_LIBSAMPLERATE |
148 |
|
if (src_converter != NULL) |
149 |
|
src_converter = src_delete(src_converter); |
150 |
|
|
151 |
|
if ((src_converter = src_new(SRC_CONVERTER, device_channels, &err)) == NULL) |
152 |
|
{ |
153 |
|
warning("src_new failed: %d!\n", err); |
154 |
|
return False; |
155 |
|
} |
156 |
|
#endif |
157 |
|
|
158 |
|
return True; |
159 |
|
} |
160 |
|
|
161 |
|
BOOL |
162 |
|
rdpsnd_dsp_resample_supported(WAVEFORMATEX * format) |
163 |
|
{ |
164 |
|
if (format->wFormatTag != WAVE_FORMAT_PCM) |
165 |
|
return False; |
166 |
|
if ((format->nChannels != 1) && (format->nChannels != 2)) |
167 |
|
return False; |
168 |
|
if ((format->wBitsPerSample != 8) && (format->wBitsPerSample != 16)) |
169 |
|
return False; |
170 |
|
|
171 |
|
return True; |
172 |
|
} |
173 |
|
|
174 |
|
uint32 |
175 |
|
rdpsnd_dsp_resample(unsigned char **out, unsigned char *in, unsigned int size, |
176 |
|
WAVEFORMATEX * format, BOOL stream_be) |
177 |
|
{ |
178 |
|
#ifdef HAVE_LIBSAMPLERATE |
179 |
|
SRC_DATA resample_data; |
180 |
|
float *infloat, *outfloat; |
181 |
|
int err; |
182 |
|
#else |
183 |
|
int ratio1k = (resample_to_srate * 1000) / format->nSamplesPerSec; |
184 |
|
#endif |
185 |
|
int innum, outnum; |
186 |
|
static BOOL warned = False; |
187 |
|
unsigned char *tmpdata = NULL; |
188 |
|
int samplewidth = format->wBitsPerSample / 8; |
189 |
|
int outsize = 0; |
190 |
|
int i; |
191 |
|
|
192 |
|
if ((resample_to_bitspersample == format->wBitsPerSample) && |
193 |
|
(resample_to_channels == format->nChannels) && |
194 |
|
(resample_to_srate == format->nSamplesPerSec)) |
195 |
|
return 0; |
196 |
|
|
197 |
|
#ifdef B_ENDIAN |
198 |
|
if (!stream_be) |
199 |
|
rdpsnd_dsp_swapbytes(in, size, format); |
200 |
|
#endif |
201 |
|
|
202 |
|
/* Expand 8bit input-samples to 16bit */ |
203 |
|
#ifndef HAVE_LIBSAMPLERATE /* libsamplerate needs 16bit samples */ |
204 |
|
if (format->wBitsPerSample != resample_to_bitspersample) |
205 |
|
#endif |
206 |
|
{ |
207 |
|
/* source: 8 bit, dest: 16bit */ |
208 |
|
if (format->wBitsPerSample == 8) |
209 |
|
{ |
210 |
|
tmpdata = xmalloc(size * 2); |
211 |
|
for (i = 0; i < size; i++) |
212 |
|
{ |
213 |
|
tmpdata[i * 2] = in[i]; |
214 |
|
tmpdata[(i * 2) + 1] = 0x00; |
215 |
|
} |
216 |
|
in = tmpdata; |
217 |
|
samplewidth = 16 / 2; |
218 |
|
size *= 2; |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
|
if (resample_to_channels != format->nChannels) |
223 |
|
{ |
224 |
|
warning("unsupported resample-settings (%u -> %u/%u -> %u/%u -> %u), not resampling!\n", format->nSamplesPerSec, resample_to_srate, format->wBitsPerSample, resample_to_bitspersample, format->nChannels, resample_to_channels); |
225 |
|
warned = True; |
226 |
|
} |
227 |
|
|
228 |
|
innum = size / samplewidth; |
229 |
|
|
230 |
|
/* Do the resampling */ |
231 |
|
#ifdef HAVE_LIBSAMPLERATE |
232 |
|
if (src_converter == NULL) |
233 |
|
{ |
234 |
|
warning("no samplerate converter available!!\n"); |
235 |
|
return 0; |
236 |
|
} |
237 |
|
|
238 |
|
outnum = ((float) innum * ((float) resample_to_srate / (float) format->nSamplesPerSec)) + 1; |
239 |
|
|
240 |
|
infloat = xmalloc(sizeof(float) * innum); |
241 |
|
outfloat = xmalloc(sizeof(float) * outnum); |
242 |
|
|
243 |
|
src_short_to_float_array((short *) in, infloat, innum); |
244 |
|
|
245 |
|
bzero(&resample_data, sizeof(resample_data)); |
246 |
|
resample_data.data_in = infloat; |
247 |
|
resample_data.data_out = outfloat; |
248 |
|
resample_data.input_frames = innum / resample_to_channels; |
249 |
|
resample_data.output_frames = outnum / resample_to_channels; |
250 |
|
resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec; |
251 |
|
resample_data.end_of_input = 0; |
252 |
|
|
253 |
|
if ((err = src_process(src_converter, &resample_data)) != 0) |
254 |
|
error("src_process: %s", src_strerror(err)); |
255 |
|
|
256 |
|
xfree(infloat); |
257 |
|
|
258 |
|
outsize = resample_data.output_frames_gen * resample_to_channels * samplewidth; |
259 |
|
*out = xmalloc(outsize); |
260 |
|
src_float_to_short_array(outfloat, (short *) *out, |
261 |
|
resample_data.output_frames_gen * resample_to_channels); |
262 |
|
xfree(outfloat); |
263 |
|
|
264 |
|
#else |
265 |
|
/* Michaels simple linear resampler */ |
266 |
|
if (resample_to_srate < format->nSamplesPerSec) |
267 |
|
{ |
268 |
|
warning("downsampling currently not supported!\n"); |
269 |
|
return 0; |
270 |
|
} |
271 |
|
|
272 |
|
outnum = (innum * ratio1k) / 1000; |
273 |
|
|
274 |
|
outsize = outnum * samplewidth; |
275 |
|
*out = xmalloc(outsize); |
276 |
|
bzero(*out, outsize); |
277 |
|
|
278 |
|
for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++) |
279 |
|
{ |
280 |
|
int source = ((i * 1000) + ratio1k - 1000) / (ratio1k + 1); |
281 |
|
int j; |
282 |
|
|
283 |
|
if (source * resample_to_channels + samplewidth > size) |
284 |
|
break; |
285 |
|
|
286 |
|
for (j = 0; j < resample_to_channels; j++) |
287 |
|
{ |
288 |
|
memcpy(*out + (i * resample_to_channels * samplewidth) + (samplewidth * j), |
289 |
|
in + (source * resample_to_channels * samplewidth) + (samplewidth * j), |
290 |
|
samplewidth); |
291 |
|
} |
292 |
|
} |
293 |
|
outsize = i * resample_to_channels * samplewidth; |
294 |
|
#endif |
295 |
|
|
296 |
|
if (tmpdata != NULL) |
297 |
|
xfree(tmpdata); |
298 |
|
|
299 |
|
/* Shrink 16bit output-samples to 8bit */ |
300 |
|
#ifndef HAVE_LIBSAMPLERATE /* libsamplerate produces 16bit samples */ |
301 |
|
if (format->wBitsPerSample != resample_to_bitspersample) |
302 |
|
#endif |
303 |
|
{ |
304 |
|
/* source: 16 bit, dest: 8 bit */ |
305 |
|
if (resample_to_bitspersample == 8) |
306 |
|
{ |
307 |
|
for (i = 0; i < outsize; i++) |
308 |
|
{ |
309 |
|
*out[i] = *out[i * 2]; |
310 |
|
} |
311 |
|
outsize /= 2; |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
#ifdef B_ENDIAN |
316 |
|
if (!stream_be) |
317 |
|
rdpsnd_dsp_swapbytes(*out, outsize, format); |
318 |
|
#endif |
319 |
|
return outsize; |
320 |
|
} |
321 |
|
|
322 |
STREAM |
STREAM |
323 |
rdpsnd_dsp_process(STREAM s, struct audio_driver *current_driver, WAVEFORMATEX * format) |
rdpsnd_dsp_process(STREAM s, struct audio_driver * current_driver, WAVEFORMATEX * format) |
324 |
{ |
{ |
325 |
static struct stream out; |
static struct stream out; |
326 |
|
BOOL stream_be = False; |
327 |
|
|
328 |
/* softvol and byteswap do not change the amount of data they |
/* softvol and byteswap do not change the amount of data they |
329 |
return, so they can operate on the input-stream */ |
return, so they can operate on the input-stream */ |
332 |
|
|
333 |
#ifdef B_ENDIAN |
#ifdef B_ENDIAN |
334 |
if (current_driver->need_byteswap_on_be) |
if (current_driver->need_byteswap_on_be) |
335 |
|
{ |
336 |
rdpsnd_dsp_swapbytes(s->data, s->size, format); |
rdpsnd_dsp_swapbytes(s->data, s->size, format); |
337 |
|
stream_be = True; |
338 |
|
} |
339 |
#endif |
#endif |
340 |
|
|
341 |
/* FIXME: where do we lose the 4 bytes referenced here? */ |
out.data = NULL; |
342 |
out.data = xmalloc(s->size - 4); |
|
343 |
|
if (current_driver->need_resampling) |
344 |
|
out.size = rdpsnd_dsp_resample(&out.data, s->data, s->size, format, stream_be); |
345 |
|
|
346 |
memcpy(out.data, s->data + 4, s->size - 4); |
if (out.data == NULL) |
347 |
|
{ |
348 |
|
out.data = xmalloc(s->size); |
349 |
|
memcpy(out.data, s->data, s->size); |
350 |
|
out.size = s->size; |
351 |
|
} |
352 |
|
|
|
out.size = s->size - 4; |
|
353 |
out.p = out.data; |
out.p = out.data; |
354 |
out.end = out.p + out.size; |
out.end = out.p + out.size; |
355 |
|
|