/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1364 - (hide annotations)
Thu Jan 4 04:55:56 2007 UTC (17 years, 5 months ago) by jsorg71
File MIME type: text/plain
File size: 10437 byte(s)
RD_ prefix and win32 compatibility for core files

1 stargo 1258 /*
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 stargo 1285 #include <strings.h>
22    
23 stargo 1258 #include "rdesktop.h"
24     #include "rdpsnd.h"
25     #include "rdpsnd_dsp.h"
26    
27 stargo 1278 #ifdef HAVE_LIBSAMPLERATE
28     #include <samplerate.h>
29    
30     #define SRC_CONVERTER SRC_SINC_MEDIUM_QUALITY
31     #endif
32    
33 stargo 1258 #define MAX_VOLUME 65535
34    
35     static uint16 softvol_left = MAX_VOLUME;
36     static uint16 softvol_right = MAX_VOLUME;
37 stargo 1276 static uint32 resample_to_srate = 44100;
38     static uint16 resample_to_bitspersample = 16;
39     static uint16 resample_to_channels = 2;
40 stargo 1278 #ifdef HAVE_LIBSAMPLERATE
41     static SRC_STATE *src_converter = NULL;
42     #endif
43 stargo 1258
44     void
45     rdpsnd_dsp_softvol_set(uint16 left, uint16 right)
46     {
47     softvol_left = left;
48     softvol_right = right;
49     DEBUG(("rdpsnd_dsp_softvol_set: left: %u, right: %u\n", left, right));
50     }
51    
52 stargo 1260 void
53 jsorg71 1364 rdpsnd_dsp_softvol(unsigned char *buffer, unsigned int size, RD_WAVEFORMATEX * format)
54 stargo 1258 {
55     unsigned int factor_left, factor_right;
56 stargo 1259 unsigned char *posin = buffer;
57     unsigned char *posout = buffer;
58 stargo 1258
59 stargo 1259 if ((softvol_left == MAX_VOLUME) && (softvol_right == MAX_VOLUME))
60     return;
61    
62 stargo 1270 factor_left = (softvol_left * 256) / MAX_VOLUME;
63     factor_right = (softvol_right * 256) / MAX_VOLUME;
64 stargo 1258
65     if (format->nChannels == 1)
66     {
67     factor_left = factor_right = (factor_left + factor_right) / 2;
68     }
69    
70     if (format->wBitsPerSample == 8)
71     {
72 stargo 1262 sint8 val;
73 stargo 1258
74 stargo 1259 while (posout < buffer + size)
75 stargo 1258 {
76     /* Left */
77     val = *posin++;
78     val = (val * factor_left) >> 8;
79     *posout++ = val;
80    
81     /* Right */
82     val = *posin++;
83     val = (val * factor_right) >> 8;
84     *posout++ = val;
85     }
86     }
87     else
88     {
89 stargo 1262 sint16 val;
90 stargo 1258
91 stargo 1259 while (posout < buffer + size)
92 stargo 1258 {
93     /* Left */
94     val = *posin++;
95     val |= *posin++ << 8;
96     val = (val * factor_left) >> 8;
97     *posout++ = val & 0xff;
98     *posout++ = val >> 8;
99    
100     /* Right */
101     val = *posin++;
102     val |= *posin++ << 8;
103     val = (val * factor_right) >> 8;
104     *posout++ = val & 0xff;
105     *posout++ = val >> 8;
106     }
107     }
108    
109 stargo 1263 DEBUG(("using softvol with factors left: %d, right: %d (%d/%d)\n", factor_left,
110     factor_right, format->wBitsPerSample, format->nChannels));
111 stargo 1258 }
112    
113 stargo 1260 void
114 jsorg71 1364 rdpsnd_dsp_swapbytes(unsigned char *buffer, unsigned int size, RD_WAVEFORMATEX * format)
115 stargo 1260 {
116     int i;
117     uint8 swap;
118    
119     if (format->wBitsPerSample == 8)
120     return;
121    
122     for (i = 0; i < size; i += 2)
123     {
124     swap = *(buffer + i);
125     *(buffer + i) = *(buffer + i + 1);
126     *(buffer + i + 1) = swap;
127     }
128     }
129    
130 stargo 1276 BOOL
131     rdpsnd_dsp_resample_set(uint32 device_srate, uint16 device_bitspersample, uint16 device_channels)
132     {
133 stargo 1278 #ifdef HAVE_LIBSAMPLERATE
134     int err;
135     #endif
136    
137 stargo 1276 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 stargo 1278 #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 stargo 1276 return True;
159     }
160    
161     BOOL
162 jsorg71 1364 rdpsnd_dsp_resample_supported(RD_WAVEFORMATEX * format)
163 stargo 1276 {
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 jsorg71 1364 RD_WAVEFORMATEX * format, BOOL stream_be)
177 stargo 1276 {
178 stargo 1278 #ifdef HAVE_LIBSAMPLERATE
179     SRC_DATA resample_data;
180     float *infloat, *outfloat;
181 stargo 1282 int err;
182 stargo 1278 #else
183 stargo 1283 int ratio1k = (resample_to_srate * 1000) / format->nSamplesPerSec;
184 stargo 1278 #endif
185 stargo 1283 int innum, outnum;
186 stargo 1288 unsigned char *tmpdata = NULL, *tmp = NULL;
187 stargo 1276 int samplewidth = format->wBitsPerSample / 8;
188 stargo 1278 int outsize = 0;
189 stargo 1281 int i;
190 stargo 1276
191 stargo 1277 if ((resample_to_bitspersample == format->wBitsPerSample) &&
192     (resample_to_channels == format->nChannels) &&
193     (resample_to_srate == format->nSamplesPerSec))
194     return 0;
195    
196 stargo 1278 #ifdef B_ENDIAN
197     if (!stream_be)
198 stargo 1281 rdpsnd_dsp_swapbytes(in, size, format);
199 stargo 1278 #endif
200 stargo 1281
201 stargo 1288 if (resample_to_channels != format->nChannels)
202     {
203     int newsize = (size / format->nChannels) * resample_to_channels;
204     tmpdata = xmalloc(newsize);
205    
206     for (i = 0; i < newsize / samplewidth; i++)
207     {
208     if (format->nChannels > resample_to_channels)
209     memcpy(tmpdata + (i * samplewidth),
210     in +
211     (((i * format->nChannels) / resample_to_channels) *
212     samplewidth), samplewidth);
213     else
214     memcpy(tmpdata + (i * samplewidth),
215     in +
216     (((i / resample_to_channels) * format->nChannels +
217     (i % format->nChannels)) * samplewidth), samplewidth);
218    
219     }
220    
221     in = tmpdata;
222     size = newsize;
223     }
224    
225    
226 stargo 1281 /* Expand 8bit input-samples to 16bit */
227     #ifndef HAVE_LIBSAMPLERATE /* libsamplerate needs 16bit samples */
228     if (format->wBitsPerSample != resample_to_bitspersample)
229     #endif
230     {
231     /* source: 8 bit, dest: 16bit */
232     if (format->wBitsPerSample == 8)
233 stargo 1278 {
234 stargo 1288 tmp = tmpdata;
235 stargo 1281 tmpdata = xmalloc(size * 2);
236     for (i = 0; i < size; i++)
237     {
238     tmpdata[i * 2] = in[i];
239     tmpdata[(i * 2) + 1] = 0x00;
240     }
241     in = tmpdata;
242     samplewidth = 16 / 2;
243     size *= 2;
244 stargo 1288
245     if (tmp != NULL)
246     xfree(tmp);
247 stargo 1278 }
248 stargo 1281 }
249 stargo 1278
250 stargo 1283 innum = size / samplewidth;
251    
252 stargo 1281 /* Do the resampling */
253 stargo 1278 #ifdef HAVE_LIBSAMPLERATE
254     if (src_converter == NULL)
255 stargo 1276 {
256 stargo 1278 warning("no samplerate converter available!!\n");
257     return 0;
258     }
259    
260 stargo 1283 outnum = ((float) innum * ((float) resample_to_srate / (float) format->nSamplesPerSec)) + 1;
261 stargo 1278
262     infloat = xmalloc(sizeof(float) * innum);
263     outfloat = xmalloc(sizeof(float) * outnum);
264    
265     src_short_to_float_array((short *) in, infloat, innum);
266    
267     bzero(&resample_data, sizeof(resample_data));
268     resample_data.data_in = infloat;
269     resample_data.data_out = outfloat;
270     resample_data.input_frames = innum / resample_to_channels;
271     resample_data.output_frames = outnum / resample_to_channels;
272     resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec;
273     resample_data.end_of_input = 0;
274    
275 stargo 1282 if ((err = src_process(src_converter, &resample_data)) != 0)
276     error("src_process: %s", src_strerror(err));
277    
278 stargo 1278 xfree(infloat);
279    
280 stargo 1282 outsize = resample_data.output_frames_gen * resample_to_channels * samplewidth;
281 stargo 1278 *out = xmalloc(outsize);
282 stargo 1283 src_float_to_short_array(outfloat, (short *) *out,
283     resample_data.output_frames_gen * resample_to_channels);
284 stargo 1278 xfree(outfloat);
285    
286     #else
287 stargo 1283 /* Michaels simple linear resampler */
288     if (resample_to_srate < format->nSamplesPerSec)
289 stargo 1278 {
290 stargo 1283 warning("downsampling currently not supported!\n");
291 stargo 1276 return 0;
292     }
293    
294 stargo 1283 outnum = (innum * ratio1k) / 1000;
295    
296     outsize = outnum * samplewidth;
297 stargo 1277 *out = xmalloc(outsize);
298 stargo 1283 bzero(*out, outsize);
299 stargo 1277
300 stargo 1283 for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++)
301 stargo 1276 {
302 stargo 1291 int source = (i * 1000) / ratio1k;
303 stargo 1292 #if 0 /* Partial for linear resampler */
304     int part = (i * 100000) / ratio1k - source * 100;
305     #endif
306 stargo 1287 int j;
307 stargo 1283
308     if (source * resample_to_channels + samplewidth > size)
309     break;
310    
311 stargo 1303 #if 0 /* Linear resampling, TODO: soundquality fixes (LP filter) */
312 stargo 1292 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 astrand 1305 cval1 += (sint8) (cval2 * part) / 100;
325 stargo 1292
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 astrand 1305 sval1 += (sint16) (sval2 * part) / 100;
343 stargo 1292
344     memcpy(*out + (i * resample_to_channels * samplewidth) +
345     (samplewidth * j), &sval1, samplewidth);
346     }
347     }
348     #else /* Nearest neighbor search */
349 stargo 1287 for (j = 0; j < resample_to_channels; j++)
350 stargo 1283 {
351 stargo 1287 memcpy(*out + (i * resample_to_channels * samplewidth) + (samplewidth * j),
352 stargo 1288 in + (source * resample_to_channels * samplewidth) +
353     (samplewidth * j), samplewidth);
354 stargo 1283 }
355 stargo 1292 #endif
356 stargo 1276 }
357 stargo 1283 outsize = i * resample_to_channels * samplewidth;
358 stargo 1278 #endif
359 stargo 1276
360 stargo 1281 if (tmpdata != NULL)
361     xfree(tmpdata);
362    
363     /* Shrink 16bit output-samples to 8bit */
364     #ifndef HAVE_LIBSAMPLERATE /* libsamplerate produces 16bit samples */
365     if (format->wBitsPerSample != resample_to_bitspersample)
366     #endif
367     {
368     /* source: 16 bit, dest: 8 bit */
369     if (resample_to_bitspersample == 8)
370     {
371     for (i = 0; i < outsize; i++)
372     {
373     *out[i] = *out[i * 2];
374     }
375     outsize /= 2;
376     }
377     }
378    
379 stargo 1278 #ifdef B_ENDIAN
380     if (!stream_be)
381 stargo 1281 rdpsnd_dsp_swapbytes(*out, outsize, format);
382 stargo 1278 #endif
383 stargo 1281 return outsize;
384 stargo 1276 }
385    
386 stargo 1263 STREAM
387 ossman_ 1342 rdpsnd_dsp_process(unsigned char *data, unsigned int size, struct audio_driver * current_driver,
388 jsorg71 1364 RD_WAVEFORMATEX * format)
389 stargo 1258 {
390 stargo 1263 static struct stream out;
391 stargo 1278 BOOL stream_be = False;
392 stargo 1258
393 stargo 1263 /* softvol and byteswap do not change the amount of data they
394     return, so they can operate on the input-stream */
395 stargo 1258 if (current_driver->wave_out_volume == rdpsnd_dsp_softvol_set)
396 ossman_ 1342 rdpsnd_dsp_softvol(data, size, format);
397 stargo 1258
398 stargo 1260 #ifdef B_ENDIAN
399     if (current_driver->need_byteswap_on_be)
400 stargo 1278 {
401 ossman_ 1342 rdpsnd_dsp_swapbytes(data, size, format);
402 stargo 1278 stream_be = True;
403     }
404 stargo 1260 #endif
405    
406 stargo 1276 out.data = NULL;
407 stargo 1263
408 stargo 1279 if (current_driver->need_resampling)
409 ossman_ 1342 out.size = rdpsnd_dsp_resample(&out.data, data, size, format, stream_be);
410 stargo 1263
411 stargo 1276 if (out.data == NULL)
412     {
413 ossman_ 1342 out.data = xmalloc(size);
414     memcpy(out.data, data, size);
415     out.size = size;
416 stargo 1276 }
417    
418 stargo 1263 out.p = out.data;
419     out.end = out.p + out.size;
420    
421     return &out;
422 stargo 1258 }

  ViewVC Help
Powered by ViewVC 1.1.26