/[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 1281 by stargo, Sun Oct 1 14:31:09 2006 UTC revision 1372 by jsorg71, Mon Jan 8 04:47:06 2007 UTC
# Line 1  Line 1 
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
# 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"
# Line 48  rdpsnd_dsp_softvol_set(uint16 left, uint Line 50  rdpsnd_dsp_softvol_set(uint16 left, uint
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;
# Line 109  rdpsnd_dsp_softvol(unsigned char *buffer Line 111  rdpsnd_dsp_softvol(unsigned char *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;
# Line 125  rdpsnd_dsp_swapbytes(unsigned char *buff Line 127  rdpsnd_dsp_swapbytes(unsigned char *buff
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
134          int err;          int err;
135  #endif  #endif
136    
 #ifndef HAVE_LIBSAMPLERATE  
         if (device_srate != 44100 && device_srate != 22050)  
                 return False;  
 #endif  
   
137          if (device_bitspersample != 16 && device_bitspersample != 8)          if (device_bitspersample != 16 && device_bitspersample != 8)
138                  return False;                  return False;
139    
# Line 161  rdpsnd_dsp_resample_set(uint32 device_sr Line 158  rdpsnd_dsp_resample_set(uint32 device_sr
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;
# Line 170  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;
 #ifndef HAVE_LIBSAMPLERATE  
         if ((format->nSamplesPerSec != 44100) && (format->nSamplesPerSec != 22050))  
                 return False;  
 #endif  
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, 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;
180          float *infloat, *outfloat;          float *infloat, *outfloat;
181          int innum, outnum;          int err;
182  #else  #else
183          int offset;          int ratio1k = (resample_to_srate * 1000) / format->nSamplesPerSec;
184  #endif  #endif
185          static BOOL warned = False;          int innum, outnum;
186          unsigned char *tmpdata = NULL;          unsigned char *tmpdata = NULL, *tmp = NULL;
187          int samplewidth = format->wBitsPerSample / 8;          int samplewidth = format->wBitsPerSample / 8;
188          int outsize = 0;          int outsize = 0;
189          int i;          int i;
# Line 205  rdpsnd_dsp_resample(unsigned char **out, Line 198  rdpsnd_dsp_resample(unsigned char **out,
198                  rdpsnd_dsp_swapbytes(in, size, format);                  rdpsnd_dsp_swapbytes(in, size, format);
199  #endif  #endif
200    
201            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          /* Expand 8bit input-samples to 16bit */          /* Expand 8bit input-samples to 16bit */
227  #ifndef HAVE_LIBSAMPLERATE      /* libsamplerate needs 16bit samples */  #ifndef HAVE_LIBSAMPLERATE      /* libsamplerate needs 16bit samples */
228          if (format->wBitsPerSample != resample_to_bitspersample)          if (format->wBitsPerSample != resample_to_bitspersample)
# Line 213  rdpsnd_dsp_resample(unsigned char **out, Line 231  rdpsnd_dsp_resample(unsigned char **out,
231                  /* source: 8 bit, dest: 16bit */                  /* source: 8 bit, dest: 16bit */
232                  if (format->wBitsPerSample == 8)                  if (format->wBitsPerSample == 8)
233                  {                  {
234                            tmp = tmpdata;
235                          tmpdata = xmalloc(size * 2);                          tmpdata = xmalloc(size * 2);
236                          for (i = 0; i < size; i++)                          for (i = 0; i < size; i++)
237                          {                          {
# Line 222  rdpsnd_dsp_resample(unsigned char **out, Line 241  rdpsnd_dsp_resample(unsigned char **out,
241                          in = tmpdata;                          in = tmpdata;
242                          samplewidth = 16 / 2;                          samplewidth = 16 / 2;
243                          size *= 2;                          size *= 2;
244    
245                            if (tmp != NULL)
246                                    xfree(tmp);
247                  }                  }
248          }          }
249    
250          if (resample_to_channels != format->nChannels)          innum = size / samplewidth;
         {  
                 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);  
                 warned = True;  
         }  
251    
252          /* Do the resampling */          /* Do the resampling */
253  #ifdef HAVE_LIBSAMPLERATE  #ifdef HAVE_LIBSAMPLERATE
# Line 239  rdpsnd_dsp_resample(unsigned char **out, Line 257  rdpsnd_dsp_resample(unsigned char **out,
257                  return 0;                  return 0;
258          }          }
259    
260          innum = size / samplewidth;          outnum = ((float) innum * ((float) resample_to_srate / (float) format->nSamplesPerSec)) + 1;
         outnum = innum * (resample_to_srate / format->nSamplesPerSec);  
261    
262          infloat = xmalloc(sizeof(float) * innum);          infloat = xmalloc(sizeof(float) * innum);
263          outfloat = xmalloc(sizeof(float) * outnum);          outfloat = xmalloc(sizeof(float) * outnum);
# Line 255  rdpsnd_dsp_resample(unsigned char **out, Line 272  rdpsnd_dsp_resample(unsigned char **out,
272          resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec;          resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec;
273          resample_data.end_of_input = 0;          resample_data.end_of_input = 0;
274    
275          src_process(src_converter, &resample_data);          if ((err = src_process(src_converter, &resample_data)) != 0)
276                    error("src_process: %s", src_strerror(err));
277    
278          xfree(infloat);          xfree(infloat);
279    
280          outsize = outnum * samplewidth;          outsize = resample_data.output_frames_gen * resample_to_channels * samplewidth;
281          *out = xmalloc(outsize);          *out = xmalloc(outsize);
282          src_float_to_short_array(outfloat, (short *) *out, outnum);          src_float_to_short_array(outfloat, (short *) *out,
283                                     resample_data.output_frames_gen * resample_to_channels);
284          xfree(outfloat);          xfree(outfloat);
285    
286  #else  #else
287          if (format->nSamplesPerSec != 22050)          /* Michaels simple linear resampler */
288            if (resample_to_srate < format->nSamplesPerSec)
289          {          {
290                  if (!warned)                  warning("downsampling currently not supported!\n");
                 {  
                         warning("unsupported source samplerate (%u), not resampling!\n",  
                                 format->nSamplesPerSec);  
                         warned = True;  
                 }  
291                  return 0;                  return 0;
292          }          }
293    
294          outsize = size * 2;          outnum = (innum * ratio1k) / 1000;
295    
296            outsize = outnum * samplewidth;
297          *out = xmalloc(outsize);          *out = xmalloc(outsize);
298            bzero(*out, outsize);
299    
300          /* Resample from 22050 to 44100 */          for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++)
         for (i = 0; i < (size / samplewidth); i++)  
301          {          {
302                  /* On a stereo-channel we must make sure that left and right                  int source = (i * 1000) / ratio1k;
303                     does not get mixed up, so we need to expand the sample-  #if 0                           /* Partial for linear resampler */
304                     data with channels in mind: 1234 -> 12123434                  int part = (i * 100000) / ratio1k - source * 100;
305                     If we have a mono-channel, we can expand the data by simply  #endif
306                     doubling the sample-data: 1234 -> 11223344 */                  int j;
307                  if (resample_to_channels == 2)  
308                          offset = ((i * 2) - (i & 1)) * samplewidth;                  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                  else
331                          offset = (i * 2) * samplewidth;                  {
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                  memcpy(*out + offset, in + (i * samplewidth), samplewidth);                                  sval1 += (sint16) (sval2 * part) / 100;
                 memcpy(*out + (resample_to_channels * samplewidth + offset),  
                        in + (i * samplewidth), samplewidth);  
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  #endif
359    
360          if (tmpdata != NULL)          if (tmpdata != NULL)
# Line 325  rdpsnd_dsp_resample(unsigned char **out, Line 384  rdpsnd_dsp_resample(unsigned char **out,
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
# Line 346  rdpsnd_dsp_process(STREAM s, struct audi Line 406  rdpsnd_dsp_process(STREAM s, struct audi
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;

Legend:
Removed from v.1281  
changed lines
  Added in v.1372

  ViewVC Help
Powered by ViewVC 1.1.26