1 | /*
|
---|
2 | rdesktop: A Remote Desktop Protocol client.
|
---|
3 | Sound DSP routines
|
---|
4 | Copyright (C) Michael Gernoth 2006-2007
|
---|
5 |
|
---|
6 | 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
|
---|
8 | the Free Software Foundation; either version 2 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
24 | * the General Public License version 2 (GPLv2) at this time for any software where
|
---|
25 | * a choice of GPL license versions is made available with the language indicating
|
---|
26 | * that GPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the GPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include <strings.h>
|
---|
31 |
|
---|
32 | #include "rdesktop.h"
|
---|
33 | #include "rdpsnd.h"
|
---|
34 | #include "rdpsnd_dsp.h"
|
---|
35 |
|
---|
36 | #ifdef HAVE_LIBSAMPLERATE
|
---|
37 | #include <samplerate.h>
|
---|
38 |
|
---|
39 | #define SRC_CONVERTER SRC_SINC_MEDIUM_QUALITY
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #define MAX_VOLUME 65535
|
---|
43 |
|
---|
44 | static uint16 softvol_left = MAX_VOLUME;
|
---|
45 | static uint16 softvol_right = MAX_VOLUME;
|
---|
46 | static uint32 resample_to_srate = 44100;
|
---|
47 | static uint16 resample_to_bitspersample = 16;
|
---|
48 | static uint16 resample_to_channels = 2;
|
---|
49 | #ifdef HAVE_LIBSAMPLERATE
|
---|
50 | static SRC_STATE *src_converter = NULL;
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | void
|
---|
54 | rdpsnd_dsp_softvol_set(uint16 left, uint16 right)
|
---|
55 | {
|
---|
56 | softvol_left = left;
|
---|
57 | softvol_right = right;
|
---|
58 | DEBUG(("rdpsnd_dsp_softvol_set: left: %u, right: %u\n", left, right));
|
---|
59 | }
|
---|
60 |
|
---|
61 | void
|
---|
62 | rdpsnd_dsp_softvol(unsigned char *buffer, unsigned int size, RD_WAVEFORMATEX * format)
|
---|
63 | {
|
---|
64 | unsigned int factor_left, factor_right;
|
---|
65 | unsigned char *posin = buffer;
|
---|
66 | unsigned char *posout = buffer;
|
---|
67 |
|
---|
68 | if ((softvol_left == MAX_VOLUME) && (softvol_right == MAX_VOLUME))
|
---|
69 | return;
|
---|
70 |
|
---|
71 | factor_left = (softvol_left * 256) / MAX_VOLUME;
|
---|
72 | factor_right = (softvol_right * 256) / MAX_VOLUME;
|
---|
73 |
|
---|
74 | if (format->nChannels == 1)
|
---|
75 | {
|
---|
76 | factor_left = factor_right = (factor_left + factor_right) / 2;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (format->wBitsPerSample == 8)
|
---|
80 | {
|
---|
81 | sint8 val;
|
---|
82 |
|
---|
83 | while (posout < buffer + size)
|
---|
84 | {
|
---|
85 | /* Left */
|
---|
86 | val = *posin++;
|
---|
87 | val = (val * factor_left) >> 8;
|
---|
88 | *posout++ = val;
|
---|
89 |
|
---|
90 | /* Right */
|
---|
91 | val = *posin++;
|
---|
92 | val = (val * factor_right) >> 8;
|
---|
93 | *posout++ = val;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | sint16 val;
|
---|
99 |
|
---|
100 | while (posout < buffer + size)
|
---|
101 | {
|
---|
102 | /* Left */
|
---|
103 | val = *posin++;
|
---|
104 | val |= *posin++ << 8;
|
---|
105 | val = (val * factor_left) >> 8;
|
---|
106 | *posout++ = val & 0xff;
|
---|
107 | *posout++ = val >> 8;
|
---|
108 |
|
---|
109 | /* Right */
|
---|
110 | val = *posin++;
|
---|
111 | val |= *posin++ << 8;
|
---|
112 | val = (val * factor_right) >> 8;
|
---|
113 | *posout++ = val & 0xff;
|
---|
114 | *posout++ = val >> 8;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | DEBUG(("using softvol with factors left: %d, right: %d (%d/%d)\n", factor_left,
|
---|
119 | factor_right, format->wBitsPerSample, format->nChannels));
|
---|
120 | }
|
---|
121 |
|
---|
122 | void
|
---|
123 | rdpsnd_dsp_swapbytes(unsigned char *buffer, unsigned int size, RD_WAVEFORMATEX * format)
|
---|
124 | {
|
---|
125 | int i;
|
---|
126 | uint8 swap;
|
---|
127 |
|
---|
128 | if (format->wBitsPerSample == 8)
|
---|
129 | return;
|
---|
130 |
|
---|
131 | if (size & 0x1)
|
---|
132 | warning("badly aligned sound data");
|
---|
133 |
|
---|
134 | for (i = 0; i < (int) size; i += 2)
|
---|
135 | {
|
---|
136 | swap = *(buffer + i);
|
---|
137 | *(buffer + i) = *(buffer + i + 1);
|
---|
138 | *(buffer + i + 1) = swap;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | RD_BOOL
|
---|
143 | rdpsnd_dsp_resample_set(uint32 device_srate, uint16 device_bitspersample, uint16 device_channels)
|
---|
144 | {
|
---|
145 | #ifdef HAVE_LIBSAMPLERATE
|
---|
146 | int err;
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | if (device_bitspersample != 16 && device_bitspersample != 8)
|
---|
150 | return False;
|
---|
151 |
|
---|
152 | if (device_channels != 1 && device_channels != 2)
|
---|
153 | return False;
|
---|
154 |
|
---|
155 | resample_to_srate = device_srate;
|
---|
156 | resample_to_bitspersample = device_bitspersample;
|
---|
157 | resample_to_channels = device_channels;
|
---|
158 |
|
---|
159 | #ifdef HAVE_LIBSAMPLERATE
|
---|
160 | if (src_converter != NULL)
|
---|
161 | src_converter = src_delete(src_converter);
|
---|
162 |
|
---|
163 | if ((src_converter = src_new(SRC_CONVERTER, device_channels, &err)) == NULL)
|
---|
164 | {
|
---|
165 | warning("src_new failed: %d!\n", err);
|
---|
166 | return False;
|
---|
167 | }
|
---|
168 | #endif
|
---|
169 |
|
---|
170 | return True;
|
---|
171 | }
|
---|
172 |
|
---|
173 | RD_BOOL
|
---|
174 | rdpsnd_dsp_resample_supported(RD_WAVEFORMATEX * format)
|
---|
175 | {
|
---|
176 | if (format->wFormatTag != WAVE_FORMAT_PCM)
|
---|
177 | return False;
|
---|
178 | if ((format->nChannels != 1) && (format->nChannels != 2))
|
---|
179 | return False;
|
---|
180 | if ((format->wBitsPerSample != 8) && (format->wBitsPerSample != 16))
|
---|
181 | return False;
|
---|
182 |
|
---|
183 | return True;
|
---|
184 | }
|
---|
185 |
|
---|
186 | uint32
|
---|
187 | rdpsnd_dsp_resample(unsigned char **out, unsigned char *in, unsigned int size,
|
---|
188 | RD_WAVEFORMATEX * format, RD_BOOL stream_be)
|
---|
189 | {
|
---|
190 | #ifdef HAVE_LIBSAMPLERATE
|
---|
191 | SRC_DATA resample_data;
|
---|
192 | float *infloat, *outfloat;
|
---|
193 | int err;
|
---|
194 | #else
|
---|
195 | int ratio1k = (resample_to_srate * 1000) / format->nSamplesPerSec;
|
---|
196 | #endif
|
---|
197 | int innum, outnum;
|
---|
198 | unsigned char *tmpdata = NULL, *tmp = NULL;
|
---|
199 | int samplewidth = format->wBitsPerSample / 8;
|
---|
200 | int outsize = 0;
|
---|
201 | int i;
|
---|
202 |
|
---|
203 | if ((resample_to_bitspersample == format->wBitsPerSample) &&
|
---|
204 | (resample_to_channels == format->nChannels) &&
|
---|
205 | (resample_to_srate == format->nSamplesPerSec))
|
---|
206 | return 0;
|
---|
207 |
|
---|
208 | #ifdef B_ENDIAN
|
---|
209 | if (!stream_be)
|
---|
210 | rdpsnd_dsp_swapbytes(in, size, format);
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | if (resample_to_channels != format->nChannels)
|
---|
214 | {
|
---|
215 | int newsize = (size / format->nChannels) * resample_to_channels;
|
---|
216 | tmpdata = (unsigned char *) xmalloc(newsize);
|
---|
217 |
|
---|
218 | for (i = 0; i < newsize / samplewidth; i++)
|
---|
219 | {
|
---|
220 | if (format->nChannels > resample_to_channels)
|
---|
221 | memcpy(tmpdata + (i * samplewidth),
|
---|
222 | in +
|
---|
223 | (((i * format->nChannels) / resample_to_channels) *
|
---|
224 | samplewidth), samplewidth);
|
---|
225 | else
|
---|
226 | memcpy(tmpdata + (i * samplewidth),
|
---|
227 | in +
|
---|
228 | (((i / resample_to_channels) * format->nChannels +
|
---|
229 | (i % format->nChannels)) * samplewidth), samplewidth);
|
---|
230 |
|
---|
231 | }
|
---|
232 |
|
---|
233 | in = tmpdata;
|
---|
234 | size = newsize;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /* Expand 8bit input-samples to 16bit */
|
---|
239 | #ifndef HAVE_LIBSAMPLERATE /* libsamplerate needs 16bit samples */
|
---|
240 | if (format->wBitsPerSample != resample_to_bitspersample)
|
---|
241 | #endif
|
---|
242 | {
|
---|
243 | /* source: 8 bit, dest: 16bit */
|
---|
244 | if (format->wBitsPerSample == 8)
|
---|
245 | {
|
---|
246 | tmp = tmpdata;
|
---|
247 | tmpdata = (unsigned char *) xmalloc(size * 2);
|
---|
248 | for (i = 0; i < (int) size; i++)
|
---|
249 | {
|
---|
250 | tmpdata[i * 2] = in[i];
|
---|
251 | tmpdata[(i * 2) + 1] = 0x00;
|
---|
252 | }
|
---|
253 | in = tmpdata;
|
---|
254 | samplewidth = 16 / 2;
|
---|
255 | size *= 2;
|
---|
256 |
|
---|
257 | if (tmp != NULL)
|
---|
258 | xfree(tmp);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | innum = size / samplewidth;
|
---|
263 |
|
---|
264 | /* Do the resampling */
|
---|
265 | #ifdef HAVE_LIBSAMPLERATE
|
---|
266 | if (src_converter == NULL)
|
---|
267 | {
|
---|
268 | warning("no samplerate converter available!!\n");
|
---|
269 | return 0;
|
---|
270 | }
|
---|
271 |
|
---|
272 | outnum = ((float) innum * ((float) resample_to_srate / (float) format->nSamplesPerSec)) + 1;
|
---|
273 |
|
---|
274 | infloat = (float *) xmalloc(sizeof(float) * innum);
|
---|
275 | outfloat = (float *) xmalloc(sizeof(float) * outnum);
|
---|
276 |
|
---|
277 | src_short_to_float_array((short *) in, infloat, innum);
|
---|
278 |
|
---|
279 | bzero(&resample_data, sizeof(resample_data));
|
---|
280 | resample_data.data_in = infloat;
|
---|
281 | resample_data.data_out = outfloat;
|
---|
282 | resample_data.input_frames = innum / resample_to_channels;
|
---|
283 | resample_data.output_frames = outnum / resample_to_channels;
|
---|
284 | resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec;
|
---|
285 | resample_data.end_of_input = 0;
|
---|
286 |
|
---|
287 | if ((err = src_process(src_converter, &resample_data)) != 0)
|
---|
288 | error("src_process: %s", src_strerror(err));
|
---|
289 |
|
---|
290 | xfree(infloat);
|
---|
291 |
|
---|
292 | outsize = resample_data.output_frames_gen * resample_to_channels * samplewidth;
|
---|
293 | *out = (unsigned char *) xmalloc(outsize);
|
---|
294 | src_float_to_short_array(outfloat, (short *) *out,
|
---|
295 | resample_data.output_frames_gen * resample_to_channels);
|
---|
296 | xfree(outfloat);
|
---|
297 |
|
---|
298 | #else
|
---|
299 | /* Michaels simple linear resampler */
|
---|
300 | if (resample_to_srate < format->nSamplesPerSec)
|
---|
301 | {
|
---|
302 | warning("downsampling currently not supported!\n");
|
---|
303 | return 0;
|
---|
304 | }
|
---|
305 |
|
---|
306 | outnum = (innum * ratio1k) / 1000;
|
---|
307 |
|
---|
308 | outsize = outnum * samplewidth;
|
---|
309 | *out = (unsigned char *) xmalloc(outsize);
|
---|
310 | bzero(*out, outsize);
|
---|
311 |
|
---|
312 | for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++)
|
---|
313 | {
|
---|
314 | int source = (i * 1000) / ratio1k;
|
---|
315 | #if 0 /* Partial for linear resampler */
|
---|
316 | int part = (i * 100000) / ratio1k - source * 100;
|
---|
317 | #endif
|
---|
318 | int j;
|
---|
319 |
|
---|
320 | if (source * resample_to_channels + samplewidth > (int) size)
|
---|
321 | break;
|
---|
322 |
|
---|
323 | #if 0 /* Linear resampling, TODO: soundquality fixes (LP filter) */
|
---|
324 | if (samplewidth == 1)
|
---|
325 | {
|
---|
326 | sint8 cval1, cval2;
|
---|
327 | for (j = 0; j < resample_to_channels; j++)
|
---|
328 | {
|
---|
329 | memcpy(&cval1,
|
---|
330 | in + (source * resample_to_channels * samplewidth) +
|
---|
331 | (samplewidth * j), samplewidth);
|
---|
332 | memcpy(&cval2,
|
---|
333 | in + ((source + 1) * resample_to_channels * samplewidth) +
|
---|
334 | (samplewidth * j), samplewidth);
|
---|
335 |
|
---|
336 | cval1 += (sint8) (cval2 * part) / 100;
|
---|
337 |
|
---|
338 | memcpy(*out + (i * resample_to_channels * samplewidth) +
|
---|
339 | (samplewidth * j), &cval1, samplewidth);
|
---|
340 | }
|
---|
341 | }
|
---|
342 | else
|
---|
343 | {
|
---|
344 | sint16 sval1, sval2;
|
---|
345 | for (j = 0; j < resample_to_channels; j++)
|
---|
346 | {
|
---|
347 | memcpy(&sval1,
|
---|
348 | in + (source * resample_to_channels * samplewidth) +
|
---|
349 | (samplewidth * j), samplewidth);
|
---|
350 | memcpy(&sval2,
|
---|
351 | in + ((source + 1) * resample_to_channels * samplewidth) +
|
---|
352 | (samplewidth * j), samplewidth);
|
---|
353 |
|
---|
354 | sval1 += (sint16) (sval2 * part) / 100;
|
---|
355 |
|
---|
356 | memcpy(*out + (i * resample_to_channels * samplewidth) +
|
---|
357 | (samplewidth * j), &sval1, samplewidth);
|
---|
358 | }
|
---|
359 | }
|
---|
360 | #else /* Nearest neighbor search */
|
---|
361 | for (j = 0; j < resample_to_channels; j++)
|
---|
362 | {
|
---|
363 | memcpy(*out + (i * resample_to_channels * samplewidth) + (samplewidth * j),
|
---|
364 | in + (source * resample_to_channels * samplewidth) +
|
---|
365 | (samplewidth * j), samplewidth);
|
---|
366 | }
|
---|
367 | #endif
|
---|
368 | }
|
---|
369 | outsize = i * resample_to_channels * samplewidth;
|
---|
370 | #endif
|
---|
371 |
|
---|
372 | if (tmpdata != NULL)
|
---|
373 | xfree(tmpdata);
|
---|
374 |
|
---|
375 | /* Shrink 16bit output-samples to 8bit */
|
---|
376 | #ifndef HAVE_LIBSAMPLERATE /* libsamplerate produces 16bit samples */
|
---|
377 | if (format->wBitsPerSample != resample_to_bitspersample)
|
---|
378 | #endif
|
---|
379 | {
|
---|
380 | /* source: 16 bit, dest: 8 bit */
|
---|
381 | if (resample_to_bitspersample == 8)
|
---|
382 | {
|
---|
383 | for (i = 0; i < outsize; i++)
|
---|
384 | {
|
---|
385 | *out[i] = *out[i * 2];
|
---|
386 | }
|
---|
387 | outsize /= 2;
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | #ifdef B_ENDIAN
|
---|
392 | if (!stream_be)
|
---|
393 | rdpsnd_dsp_swapbytes(*out, outsize, format);
|
---|
394 | #endif
|
---|
395 | return outsize;
|
---|
396 | }
|
---|
397 |
|
---|
398 | STREAM
|
---|
399 | rdpsnd_dsp_process(unsigned char *data, unsigned int size, struct audio_driver * current_driver,
|
---|
400 | RD_WAVEFORMATEX * format)
|
---|
401 | {
|
---|
402 | static struct stream out;
|
---|
403 | RD_BOOL stream_be = False;
|
---|
404 |
|
---|
405 | /* softvol and byteswap do not change the amount of data they
|
---|
406 | return, so they can operate on the input-stream */
|
---|
407 | if (current_driver->wave_out_volume == rdpsnd_dsp_softvol_set)
|
---|
408 | rdpsnd_dsp_softvol(data, size, format);
|
---|
409 |
|
---|
410 | #ifdef B_ENDIAN
|
---|
411 | if (current_driver->need_byteswap_on_be)
|
---|
412 | {
|
---|
413 | rdpsnd_dsp_swapbytes(data, size, format);
|
---|
414 | stream_be = True;
|
---|
415 | }
|
---|
416 | #endif
|
---|
417 |
|
---|
418 | out.data = NULL;
|
---|
419 |
|
---|
420 | if (current_driver->need_resampling)
|
---|
421 | out.size = rdpsnd_dsp_resample(&out.data, data, size, format, stream_be);
|
---|
422 |
|
---|
423 | if (out.data == NULL)
|
---|
424 | {
|
---|
425 | out.data = (unsigned char *) xmalloc(size);
|
---|
426 | memcpy(out.data, data, size);
|
---|
427 | out.size = size;
|
---|
428 | }
|
---|
429 |
|
---|
430 | out.p = out.data;
|
---|
431 | out.end = out.p + out.size;
|
---|
432 |
|
---|
433 | return &out;
|
---|
434 | }
|
---|