1 |
/* |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Sound DSP routines |
Sound DSP routines |
4 |
Copyright (C) Michael Gernoth 2006 |
Copyright (C) Michael Gernoth 2006-2007 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
50 |
} |
} |
51 |
|
|
52 |
void |
void |
53 |
rdpsnd_dsp_softvol(unsigned char *buffer, unsigned int size, WAVEFORMATEX * format) |
rdpsnd_dsp_softvol(unsigned char *buffer, unsigned int size, RD_WAVEFORMATEX * format) |
54 |
{ |
{ |
55 |
unsigned int factor_left, factor_right; |
unsigned int factor_left, factor_right; |
56 |
unsigned char *posin = buffer; |
unsigned char *posin = buffer; |
111 |
} |
} |
112 |
|
|
113 |
void |
void |
114 |
rdpsnd_dsp_swapbytes(unsigned char *buffer, unsigned int size, WAVEFORMATEX * format) |
rdpsnd_dsp_swapbytes(unsigned char *buffer, unsigned int size, RD_WAVEFORMATEX * format) |
115 |
{ |
{ |
116 |
int i; |
int i; |
117 |
uint8 swap; |
uint8 swap; |
127 |
} |
} |
128 |
} |
} |
129 |
|
|
130 |
BOOL |
RD_BOOL |
131 |
rdpsnd_dsp_resample_set(uint32 device_srate, uint16 device_bitspersample, uint16 device_channels) |
rdpsnd_dsp_resample_set(uint32 device_srate, uint16 device_bitspersample, uint16 device_channels) |
132 |
{ |
{ |
133 |
#ifdef HAVE_LIBSAMPLERATE |
#ifdef HAVE_LIBSAMPLERATE |
158 |
return True; |
return True; |
159 |
} |
} |
160 |
|
|
161 |
BOOL |
RD_BOOL |
162 |
rdpsnd_dsp_resample_supported(WAVEFORMATEX * format) |
rdpsnd_dsp_resample_supported(RD_WAVEFORMATEX * format) |
163 |
{ |
{ |
164 |
if (format->wFormatTag != WAVE_FORMAT_PCM) |
if (format->wFormatTag != WAVE_FORMAT_PCM) |
165 |
return False; |
return False; |
173 |
|
|
174 |
uint32 |
uint32 |
175 |
rdpsnd_dsp_resample(unsigned char **out, unsigned char *in, unsigned int size, |
rdpsnd_dsp_resample(unsigned char **out, unsigned char *in, unsigned int size, |
176 |
WAVEFORMATEX * format, BOOL stream_be) |
RD_WAVEFORMATEX * format, RD_BOOL stream_be) |
177 |
{ |
{ |
178 |
#ifdef HAVE_LIBSAMPLERATE |
#ifdef HAVE_LIBSAMPLERATE |
179 |
SRC_DATA resample_data; |
SRC_DATA resample_data; |
299 |
|
|
300 |
for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++) |
for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++) |
301 |
{ |
{ |
302 |
int source = ((i * 1000) + ratio1k - 1000) / (ratio1k + 1); |
int source = (i * 1000) / ratio1k; |
303 |
|
#if 0 /* Partial for linear resampler */ |
304 |
|
int part = (i * 100000) / ratio1k - source * 100; |
305 |
|
#endif |
306 |
int j; |
int j; |
307 |
|
|
308 |
if (source * resample_to_channels + samplewidth > size) |
if (source * resample_to_channels + samplewidth > size) |
309 |
break; |
break; |
310 |
|
|
311 |
|
#if 0 /* Linear resampling, TODO: soundquality fixes (LP filter) */ |
312 |
|
if (samplewidth == 1) |
313 |
|
{ |
314 |
|
sint8 cval1, cval2; |
315 |
|
for (j = 0; j < resample_to_channels; j++) |
316 |
|
{ |
317 |
|
memcpy(&cval1, |
318 |
|
in + (source * resample_to_channels * samplewidth) + |
319 |
|
(samplewidth * j), samplewidth); |
320 |
|
memcpy(&cval2, |
321 |
|
in + ((source + 1) * resample_to_channels * samplewidth) + |
322 |
|
(samplewidth * j), samplewidth); |
323 |
|
|
324 |
|
cval1 += (sint8) (cval2 * part) / 100; |
325 |
|
|
326 |
|
memcpy(*out + (i * resample_to_channels * samplewidth) + |
327 |
|
(samplewidth * j), &cval1, samplewidth); |
328 |
|
} |
329 |
|
} |
330 |
|
else |
331 |
|
{ |
332 |
|
sint16 sval1, sval2; |
333 |
|
for (j = 0; j < resample_to_channels; j++) |
334 |
|
{ |
335 |
|
memcpy(&sval1, |
336 |
|
in + (source * resample_to_channels * samplewidth) + |
337 |
|
(samplewidth * j), samplewidth); |
338 |
|
memcpy(&sval2, |
339 |
|
in + ((source + 1) * resample_to_channels * samplewidth) + |
340 |
|
(samplewidth * j), samplewidth); |
341 |
|
|
342 |
|
sval1 += (sint16) (sval2 * part) / 100; |
343 |
|
|
344 |
|
memcpy(*out + (i * resample_to_channels * samplewidth) + |
345 |
|
(samplewidth * j), &sval1, samplewidth); |
346 |
|
} |
347 |
|
} |
348 |
|
#else /* Nearest neighbor search */ |
349 |
for (j = 0; j < resample_to_channels; j++) |
for (j = 0; j < resample_to_channels; j++) |
350 |
{ |
{ |
351 |
memcpy(*out + (i * resample_to_channels * samplewidth) + (samplewidth * j), |
memcpy(*out + (i * resample_to_channels * samplewidth) + (samplewidth * j), |
352 |
in + (source * resample_to_channels * samplewidth) + |
in + (source * resample_to_channels * samplewidth) + |
353 |
(samplewidth * j), samplewidth); |
(samplewidth * j), samplewidth); |
354 |
} |
} |
355 |
|
#endif |
356 |
} |
} |
357 |
outsize = i * resample_to_channels * samplewidth; |
outsize = i * resample_to_channels * samplewidth; |
358 |
#endif |
#endif |
384 |
} |
} |
385 |
|
|
386 |
STREAM |
STREAM |
387 |
rdpsnd_dsp_process(STREAM s, struct audio_driver * current_driver, WAVEFORMATEX * format) |
rdpsnd_dsp_process(unsigned char *data, unsigned int size, struct audio_driver * current_driver, |
388 |
|
RD_WAVEFORMATEX * format) |
389 |
{ |
{ |
390 |
static struct stream out; |
static struct stream out; |
391 |
BOOL stream_be = False; |
RD_BOOL stream_be = False; |
392 |
|
|
393 |
/* softvol and byteswap do not change the amount of data they |
/* softvol and byteswap do not change the amount of data they |
394 |
return, so they can operate on the input-stream */ |
return, so they can operate on the input-stream */ |
395 |
if (current_driver->wave_out_volume == rdpsnd_dsp_softvol_set) |
if (current_driver->wave_out_volume == rdpsnd_dsp_softvol_set) |
396 |
rdpsnd_dsp_softvol(s->data, s->size, format); |
rdpsnd_dsp_softvol(data, size, format); |
397 |
|
|
398 |
#ifdef B_ENDIAN |
#ifdef B_ENDIAN |
399 |
if (current_driver->need_byteswap_on_be) |
if (current_driver->need_byteswap_on_be) |
400 |
{ |
{ |
401 |
rdpsnd_dsp_swapbytes(s->data, s->size, format); |
rdpsnd_dsp_swapbytes(data, size, format); |
402 |
stream_be = True; |
stream_be = True; |
403 |
} |
} |
404 |
#endif |
#endif |
406 |
out.data = NULL; |
out.data = NULL; |
407 |
|
|
408 |
if (current_driver->need_resampling) |
if (current_driver->need_resampling) |
409 |
out.size = rdpsnd_dsp_resample(&out.data, s->data, s->size, format, stream_be); |
out.size = rdpsnd_dsp_resample(&out.data, data, size, format, stream_be); |
410 |
|
|
411 |
if (out.data == NULL) |
if (out.data == NULL) |
412 |
{ |
{ |
413 |
out.data = xmalloc(s->size); |
out.data = xmalloc(size); |
414 |
memcpy(out.data, s->data, s->size); |
memcpy(out.data, data, size); |
415 |
out.size = s->size; |
out.size = size; |
416 |
} |
} |
417 |
|
|
418 |
out.p = out.data; |
out.p = out.data; |