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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1276 by stargo, Sun Oct 1 12:16:50 2006 UTC revision 1303 by stargo, Thu Oct 26 22:26:16 2006 UTC
# Line 18  Line 18 
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;
# Line 29  static uint16 softvol_right = MAX_VOLUME Line 37  static uint16 softvol_right = MAX_VOLUME
37  static uint32 resample_to_srate = 44100;  static uint32 resample_to_srate = 44100;
38  static uint16 resample_to_bitspersample = 16;  static uint16 resample_to_bitspersample = 16;
39  static uint16 resample_to_channels = 2;  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)
# Line 119  rdpsnd_dsp_swapbytes(unsigned char *buff Line 130  rdpsnd_dsp_swapbytes(unsigned char *buff
130  BOOL  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          if (device_srate != 44100 && device_srate != 22050)  #ifdef HAVE_LIBSAMPLERATE
134                  return False;          int err;
135    #endif
136    
137          if (device_bitspersample != 16 && device_bitspersample != 8)          if (device_bitspersample != 16 && device_bitspersample != 8)
138                  return False;                  return False;
# Line 132  rdpsnd_dsp_resample_set(uint32 device_sr Line 144  rdpsnd_dsp_resample_set(uint32 device_sr
144          resample_to_bitspersample = device_bitspersample;          resample_to_bitspersample = device_bitspersample;
145          resample_to_channels = device_channels;          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;          return True;
159  }  }
160    
# Line 144  rdpsnd_dsp_resample_supported(WAVEFORMAT Line 167  rdpsnd_dsp_resample_supported(WAVEFORMAT
167                  return False;                  return False;
168          if ((format->wBitsPerSample != 8) && (format->wBitsPerSample != 16))          if ((format->wBitsPerSample != 8) && (format->wBitsPerSample != 16))
169                  return False;                  return False;
         if ((format->nSamplesPerSec != 44100) && (format->nSamplesPerSec != 22050))  
                 return False;  
170    
171          return True;          return True;
172  }  }
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)                      WAVEFORMATEX * format, BOOL stream_be)
177  {  {
178          static BOOL warned = False;  #ifdef HAVE_LIBSAMPLERATE
179          int outsize, offset;          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            unsigned char *tmpdata = NULL, *tmp = NULL;
187          int samplewidth = format->wBitsPerSample / 8;          int samplewidth = format->wBitsPerSample / 8;
188            int outsize = 0;
189          int i;          int i;
190    
191          if ((resample_to_bitspersample != format->wBitsPerSample) ||          if ((resample_to_bitspersample == format->wBitsPerSample) &&
192              (resample_to_channels != format->nChannels) ||              (resample_to_channels == format->nChannels) &&
193              ((format->nSamplesPerSec != 44100) && (format->nSamplesPerSec != 22050)))              (resample_to_srate == format->nSamplesPerSec))
         {  
                 if (!warned)  
                 {  
                         warning("unsupported resample-settings (%u/%u/%u), not resampling!\n",  
                                 format->nSamplesPerSec, format->wBitsPerSample, format->nChannels);  
                         warned = True;  
                 }  
194                  return 0;                  return 0;
         }  
195    
196          if (format->nSamplesPerSec == 22050)  #ifdef B_ENDIAN
197            if (!stream_be)
198                    rdpsnd_dsp_swapbytes(in, size, format);
199    #endif
200    
201            if (resample_to_channels != format->nChannels)
202          {          {
203                  outsize = size * 2;                  int newsize = (size / format->nChannels) * resample_to_channels;
204                  *out = xmalloc(outsize);                  tmpdata = xmalloc(newsize);
205    
206                  /* Resample to 44100 */                  for (i = 0; i < newsize / samplewidth; i++)
                 for (i = 0; i < (size / samplewidth); i++)  
207                  {                  {
208                          /* On a stereo-channel we must make sure that left and right                          if (format->nChannels > resample_to_channels)
209                             does not get mixed up, so we need to expand the sample-                                  memcpy(tmpdata + (i * samplewidth),
210                             data with channels in mind: 1234 -> 12123434                                         in +
211                             If we have a mono-channel, we can expand the data by simply                                         (((i * format->nChannels) / resample_to_channels) *
212                             doubling the sample-data: 1234 -> 11223344 */                                          samplewidth), samplewidth);
                         if (resample_to_channels == 2)  
                                 offset = ((i * 2) - (i & 1)) * samplewidth;  
213                          else                          else
214                                  offset = (i * 2) * samplewidth;                                  memcpy(tmpdata + (i * samplewidth),
215                                           in +
216                                           (((i / resample_to_channels) * format->nChannels +
217                                             (i % format->nChannels)) * samplewidth), samplewidth);
218    
219                          memcpy(*out + offset, in + (i * samplewidth), samplewidth);                  }
                         memcpy(*out + (resample_to_channels * samplewidth + offset),  
                                in + (i * samplewidth), samplewidth);  
220    
221                    in = tmpdata;
222                    size = newsize;
223            }
224    
225    
226            /* 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                    {
234                            tmp = tmpdata;
235                            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    
245                            if (tmp != NULL)
246                                    xfree(tmp);
247                  }                  }
248          }          }
249          else  
250            innum = size / samplewidth;
251    
252            /* Do the resampling */
253    #ifdef HAVE_LIBSAMPLERATE
254            if (src_converter == NULL)
255            {
256                    warning("no samplerate converter available!!\n");
257                    return 0;
258            }
259    
260            outnum = ((float) innum * ((float) resample_to_srate / (float) format->nSamplesPerSec)) + 1;
261    
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            if ((err = src_process(src_converter, &resample_data)) != 0)
276                    error("src_process: %s", src_strerror(err));
277    
278            xfree(infloat);
279    
280            outsize = resample_data.output_frames_gen * resample_to_channels * samplewidth;
281            *out = xmalloc(outsize);
282            src_float_to_short_array(outfloat, (short *) *out,
283                                     resample_data.output_frames_gen * resample_to_channels);
284            xfree(outfloat);
285    
286    #else
287            /* Michaels simple linear resampler */
288            if (resample_to_srate < format->nSamplesPerSec)
289            {
290                    warning("downsampling currently not supported!\n");
291                    return 0;
292            }
293    
294            outnum = (innum * ratio1k) / 1000;
295    
296            outsize = outnum * samplewidth;
297            *out = xmalloc(outsize);
298            bzero(*out, outsize);
299    
300            for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++)
301            {
302                    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;
307    
308                    if (source * resample_to_channels + samplewidth > size)
309                            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++)
350                    {
351                            memcpy(*out + (i * resample_to_channels * samplewidth) + (samplewidth * j),
352                                   in + (source * resample_to_channels * samplewidth) +
353                                   (samplewidth * j), samplewidth);
354                    }
355    #endif
356            }
357            outsize = i * resample_to_channels * samplewidth;
358    #endif
359    
360            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                  outsize = 0;                  /* 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    #ifdef B_ENDIAN
380            if (!stream_be)
381                    rdpsnd_dsp_swapbytes(*out, outsize, format);
382    #endif
383          return outsize;          return outsize;
384  }  }
385    
# Line 208  STREAM Line 387  STREAM
387  rdpsnd_dsp_process(STREAM s, struct audio_driver * current_driver, WAVEFORMATEX * format)  rdpsnd_dsp_process(STREAM s, struct audio_driver * current_driver, WAVEFORMATEX * format)
388  {  {
389          static struct stream out;          static struct stream out;
390            BOOL stream_be = False;
391    
392          /* softvol and byteswap do not change the amount of data they          /* softvol and byteswap do not change the amount of data they
393             return, so they can operate on the input-stream */             return, so they can operate on the input-stream */
# Line 216  rdpsnd_dsp_process(STREAM s, struct audi Line 396  rdpsnd_dsp_process(STREAM s, struct audi
396    
397  #ifdef B_ENDIAN  #ifdef B_ENDIAN
398          if (current_driver->need_byteswap_on_be)          if (current_driver->need_byteswap_on_be)
399            {
400                  rdpsnd_dsp_swapbytes(s->data, s->size, format);                  rdpsnd_dsp_swapbytes(s->data, s->size, format);
401                    stream_be = True;
402            }
403  #endif  #endif
404    
405          out.data = NULL;          out.data = NULL;
406    
407          if (current_driver->wave_out_format_supported == rdpsnd_dsp_resample_supported)          if (current_driver->need_resampling)
408          {                  out.size = rdpsnd_dsp_resample(&out.data, s->data, s->size, format, stream_be);
                 out.size = rdpsnd_dsp_resample(&out.data, s->data, s->size, format);  
         }  
409    
410          if (out.data == NULL)          if (out.data == NULL)
411          {          {

Legend:
Removed from v.1276  
changed lines
  Added in v.1303

  ViewVC Help
Powered by ViewVC 1.1.26