/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpsnd_dsp.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/rdpsnd_dsp.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1283 - (show annotations)
Sun Oct 1 17:23:30 2006 UTC (17 years, 8 months ago) by stargo
File MIME type: text/plain
File size: 8982 byte(s)
add simple linear resampling implementation to be used when libsamplerate
is not available

1 /*
2 rdesktop: A Remote Desktop Protocol client.
3 Sound DSP routines
4 Copyright (C) Michael Gernoth 2006
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 #include "rdesktop.h"
22 #include "rdpsnd.h"
23 #include "rdpsnd_dsp.h"
24
25 #ifdef HAVE_LIBSAMPLERATE
26 #include <samplerate.h>
27
28 #define SRC_CONVERTER SRC_SINC_MEDIUM_QUALITY
29 #endif
30
31 #define MAX_VOLUME 65535
32
33 static uint16 softvol_left = MAX_VOLUME;
34 static uint16 softvol_right = MAX_VOLUME;
35 static uint32 resample_to_srate = 44100;
36 static uint16 resample_to_bitspersample = 16;
37 static uint16 resample_to_channels = 2;
38 #ifdef HAVE_LIBSAMPLERATE
39 static SRC_STATE *src_converter = NULL;
40 #endif
41
42 void
43 rdpsnd_dsp_softvol_set(uint16 left, uint16 right)
44 {
45 softvol_left = left;
46 softvol_right = right;
47 DEBUG(("rdpsnd_dsp_softvol_set: left: %u, right: %u\n", left, right));
48 }
49
50 void
51 rdpsnd_dsp_softvol(unsigned char *buffer, unsigned int size, WAVEFORMATEX * format)
52 {
53 unsigned int factor_left, factor_right;
54 unsigned char *posin = buffer;
55 unsigned char *posout = buffer;
56
57 if ((softvol_left == MAX_VOLUME) && (softvol_right == MAX_VOLUME))
58 return;
59
60 factor_left = (softvol_left * 256) / MAX_VOLUME;
61 factor_right = (softvol_right * 256) / MAX_VOLUME;
62
63 if (format->nChannels == 1)
64 {
65 factor_left = factor_right = (factor_left + factor_right) / 2;
66 }
67
68 if (format->wBitsPerSample == 8)
69 {
70 sint8 val;
71
72 while (posout < buffer + size)
73 {
74 /* Left */
75 val = *posin++;
76 val = (val * factor_left) >> 8;
77 *posout++ = val;
78
79 /* Right */
80 val = *posin++;
81 val = (val * factor_right) >> 8;
82 *posout++ = val;
83 }
84 }
85 else
86 {
87 sint16 val;
88
89 while (posout < buffer + size)
90 {
91 /* Left */
92 val = *posin++;
93 val |= *posin++ << 8;
94 val = (val * factor_left) >> 8;
95 *posout++ = val & 0xff;
96 *posout++ = val >> 8;
97
98 /* Right */
99 val = *posin++;
100 val |= *posin++ << 8;
101 val = (val * factor_right) >> 8;
102 *posout++ = val & 0xff;
103 *posout++ = val >> 8;
104 }
105 }
106
107 DEBUG(("using softvol with factors left: %d, right: %d (%d/%d)\n", factor_left,
108 factor_right, format->wBitsPerSample, format->nChannels));
109 }
110
111 void
112 rdpsnd_dsp_swapbytes(unsigned char *buffer, unsigned int size, WAVEFORMATEX * format)
113 {
114 int i;
115 uint8 swap;
116
117 if (format->wBitsPerSample == 8)
118 return;
119
120 for (i = 0; i < size; i += 2)
121 {
122 swap = *(buffer + i);
123 *(buffer + i) = *(buffer + i + 1);
124 *(buffer + i + 1) = swap;
125 }
126 }
127
128 BOOL
129 rdpsnd_dsp_resample_set(uint32 device_srate, uint16 device_bitspersample, uint16 device_channels)
130 {
131 #ifdef HAVE_LIBSAMPLERATE
132 int err;
133 #endif
134
135 if (device_bitspersample != 16 && device_bitspersample != 8)
136 return False;
137
138 if (device_channels != 1 && device_channels != 2)
139 return False;
140
141 resample_to_srate = device_srate;
142 resample_to_bitspersample = device_bitspersample;
143 resample_to_channels = device_channels;
144
145 #ifdef HAVE_LIBSAMPLERATE
146 if (src_converter != NULL)
147 src_converter = src_delete(src_converter);
148
149 if ((src_converter = src_new(SRC_CONVERTER, device_channels, &err)) == NULL)
150 {
151 warning("src_new failed: %d!\n", err);
152 return False;
153 }
154 #endif
155
156 return True;
157 }
158
159 BOOL
160 rdpsnd_dsp_resample_supported(WAVEFORMATEX * format)
161 {
162 if (format->wFormatTag != WAVE_FORMAT_PCM)
163 return False;
164 if ((format->nChannels != 1) && (format->nChannels != 2))
165 return False;
166 if ((format->wBitsPerSample != 8) && (format->wBitsPerSample != 16))
167 return False;
168
169 return True;
170 }
171
172 uint32
173 rdpsnd_dsp_resample(unsigned char **out, unsigned char *in, unsigned int size,
174 WAVEFORMATEX * format, BOOL stream_be)
175 {
176 #ifdef HAVE_LIBSAMPLERATE
177 SRC_DATA resample_data;
178 float *infloat, *outfloat;
179 int err;
180 #else
181 int ratio1k = (resample_to_srate * 1000) / format->nSamplesPerSec;
182 #endif
183 int innum, outnum;
184 static BOOL warned = False;
185 unsigned char *tmpdata = NULL;
186 int samplewidth = format->wBitsPerSample / 8;
187 int outsize = 0;
188 int i;
189
190 if ((resample_to_bitspersample == format->wBitsPerSample) &&
191 (resample_to_channels == format->nChannels) &&
192 (resample_to_srate == format->nSamplesPerSec))
193 return 0;
194
195 #ifdef B_ENDIAN
196 if (!stream_be)
197 rdpsnd_dsp_swapbytes(in, size, format);
198 #endif
199
200 /* Expand 8bit input-samples to 16bit */
201 #ifndef HAVE_LIBSAMPLERATE /* libsamplerate needs 16bit samples */
202 if (format->wBitsPerSample != resample_to_bitspersample)
203 #endif
204 {
205 /* source: 8 bit, dest: 16bit */
206 if (format->wBitsPerSample == 8)
207 {
208 tmpdata = xmalloc(size * 2);
209 for (i = 0; i < size; i++)
210 {
211 tmpdata[i * 2] = in[i];
212 tmpdata[(i * 2) + 1] = 0x00;
213 }
214 in = tmpdata;
215 samplewidth = 16 / 2;
216 size *= 2;
217 }
218 }
219
220 if (resample_to_channels != format->nChannels)
221 {
222 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);
223 warned = True;
224 }
225
226 innum = size / samplewidth;
227
228 /* Do the resampling */
229 #ifdef HAVE_LIBSAMPLERATE
230 if (src_converter == NULL)
231 {
232 warning("no samplerate converter available!!\n");
233 return 0;
234 }
235
236 outnum = ((float) innum * ((float) resample_to_srate / (float) format->nSamplesPerSec)) + 1;
237
238 infloat = xmalloc(sizeof(float) * innum);
239 outfloat = xmalloc(sizeof(float) * outnum);
240
241 src_short_to_float_array((short *) in, infloat, innum);
242
243 bzero(&resample_data, sizeof(resample_data));
244 resample_data.data_in = infloat;
245 resample_data.data_out = outfloat;
246 resample_data.input_frames = innum / resample_to_channels;
247 resample_data.output_frames = outnum / resample_to_channels;
248 resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec;
249 resample_data.end_of_input = 0;
250
251 if ((err = src_process(src_converter, &resample_data)) != 0)
252 error("src_process: %s", src_strerror(err));
253
254 xfree(infloat);
255
256 outsize = resample_data.output_frames_gen * resample_to_channels * samplewidth;
257 *out = xmalloc(outsize);
258 src_float_to_short_array(outfloat, (short *) *out,
259 resample_data.output_frames_gen * resample_to_channels);
260 xfree(outfloat);
261
262 #else
263 /* Michaels simple linear resampler */
264 if (resample_to_srate < format->nSamplesPerSec)
265 {
266 warning("downsampling currently not supported!\n");
267 return 0;
268 }
269
270 outnum = (innum * ratio1k) / 1000;
271
272 outsize = outnum * samplewidth;
273 *out = xmalloc(outsize);
274 bzero(*out, outsize);
275
276 for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++)
277 {
278 int source = ((i + ratio1k / 1000 - 1) * 1000) / ratio1k;
279
280 if (source * resample_to_channels + samplewidth > size)
281 break;
282
283 if (resample_to_channels == 2)
284 {
285 memcpy(*out + (i * resample_to_channels * samplewidth),
286 in + (source * resample_to_channels * samplewidth), samplewidth);
287 memcpy(*out + (i * resample_to_channels * samplewidth) + samplewidth,
288 in + (source * resample_to_channels * samplewidth) + samplewidth,
289 samplewidth);
290 }
291 else
292 {
293 memcpy(*out + (i * samplewidth), in + (source * samplewidth), samplewidth);
294 }
295 }
296 outsize = i * resample_to_channels * samplewidth;
297 #endif
298
299 if (tmpdata != NULL)
300 xfree(tmpdata);
301
302 /* Shrink 16bit output-samples to 8bit */
303 #ifndef HAVE_LIBSAMPLERATE /* libsamplerate produces 16bit samples */
304 if (format->wBitsPerSample != resample_to_bitspersample)
305 #endif
306 {
307 /* source: 16 bit, dest: 8 bit */
308 if (resample_to_bitspersample == 8)
309 {
310 for (i = 0; i < outsize; i++)
311 {
312 *out[i] = *out[i * 2];
313 }
314 outsize /= 2;
315 }
316 }
317
318 #ifdef B_ENDIAN
319 if (!stream_be)
320 rdpsnd_dsp_swapbytes(*out, outsize, format);
321 #endif
322 return outsize;
323 }
324
325 STREAM
326 rdpsnd_dsp_process(STREAM s, struct audio_driver * current_driver, WAVEFORMATEX * format)
327 {
328 static struct stream out;
329 BOOL stream_be = False;
330
331 /* softvol and byteswap do not change the amount of data they
332 return, so they can operate on the input-stream */
333 if (current_driver->wave_out_volume == rdpsnd_dsp_softvol_set)
334 rdpsnd_dsp_softvol(s->data, s->size, format);
335
336 #ifdef B_ENDIAN
337 if (current_driver->need_byteswap_on_be)
338 {
339 rdpsnd_dsp_swapbytes(s->data, s->size, format);
340 stream_be = True;
341 }
342 #endif
343
344 out.data = NULL;
345
346 if (current_driver->need_resampling)
347 out.size = rdpsnd_dsp_resample(&out.data, s->data, s->size, format, stream_be);
348
349 if (out.data == NULL)
350 {
351 out.data = xmalloc(s->size);
352 memcpy(out.data, s->data, s->size);
353 out.size = s->size;
354 }
355
356 out.p = out.data;
357 out.end = out.p + out.size;
358
359 return &out;
360 }

  ViewVC Help
Powered by ViewVC 1.1.26