/[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 1260 by stargo, Sun Sep 17 15:25:10 2006 UTC revision 1276 by stargo, Sun Oct 1 12:16:50 2006 UTC
# Line 26  Line 26 
26    
27  static uint16 softvol_left = MAX_VOLUME;  static uint16 softvol_left = MAX_VOLUME;
28  static uint16 softvol_right = MAX_VOLUME;  static uint16 softvol_right = MAX_VOLUME;
29    static uint32 resample_to_srate = 44100;
30    static uint16 resample_to_bitspersample = 16;
31    static uint16 resample_to_channels = 2;
32    
33  void  void
34  rdpsnd_dsp_softvol_set(uint16 left, uint16 right)  rdpsnd_dsp_softvol_set(uint16 left, uint16 right)
# Line 45  rdpsnd_dsp_softvol(unsigned char *buffer Line 48  rdpsnd_dsp_softvol(unsigned char *buffer
48          if ((softvol_left == MAX_VOLUME) && (softvol_right == MAX_VOLUME))          if ((softvol_left == MAX_VOLUME) && (softvol_right == MAX_VOLUME))
49                  return;                  return;
50    
51          factor_left = (softvol_left * 256) / 65535;          factor_left = (softvol_left * 256) / MAX_VOLUME;
52          factor_right = (softvol_right * 256) / 65535;          factor_right = (softvol_right * 256) / MAX_VOLUME;
53    
54          if (format->nChannels == 1)          if (format->nChannels == 1)
55          {          {
# Line 55  rdpsnd_dsp_softvol(unsigned char *buffer Line 58  rdpsnd_dsp_softvol(unsigned char *buffer
58    
59          if (format->wBitsPerSample == 8)          if (format->wBitsPerSample == 8)
60          {          {
61                  char val;                  sint8 val;
62    
63                  while (posout < buffer + size)                  while (posout < buffer + size)
64                  {                  {
# Line 72  rdpsnd_dsp_softvol(unsigned char *buffer Line 75  rdpsnd_dsp_softvol(unsigned char *buffer
75          }          }
76          else          else
77          {          {
78                  short val;                  sint16 val;
79    
80                  while (posout < buffer + size)                  while (posout < buffer + size)
81                  {                  {
# Line 92  rdpsnd_dsp_softvol(unsigned char *buffer Line 95  rdpsnd_dsp_softvol(unsigned char *buffer
95                  }                  }
96          }          }
97    
98          DEBUG(("using softvol with shifts left: %d, right: %d (%d/%d)\n", factor_left, factor_right,          DEBUG(("using softvol with factors left: %d, right: %d (%d/%d)\n", factor_left,
99                 format->wBitsPerSample, format->nChannels));                 factor_right, format->wBitsPerSample, format->nChannels));
100  }  }
101    
102  void  void
# Line 113  rdpsnd_dsp_swapbytes(unsigned char *buff Line 116  rdpsnd_dsp_swapbytes(unsigned char *buff
116          }          }
117  }  }
118    
119  unsigned char *  BOOL
120  rdpsnd_dsp_process(unsigned char *inbuffer, unsigned int size, struct audio_driver *current_driver,  rdpsnd_dsp_resample_set(uint32 device_srate, uint16 device_bitspersample, uint16 device_channels)
                    WAVEFORMATEX * format)  
121  {  {
122          unsigned char *outbuffer;          if (device_srate != 44100 && device_srate != 22050)
123                    return False;
124    
125          outbuffer = xmalloc(size);          if (device_bitspersample != 16 && device_bitspersample != 8)
126                    return False;
127    
128          memcpy(outbuffer, inbuffer, size);          if (device_channels != 1 && device_channels != 2)
129                    return False;
130    
131          /* Software volume control */          resample_to_srate = device_srate;
132            resample_to_bitspersample = device_bitspersample;
133            resample_to_channels = device_channels;
134    
135            return True;
136    }
137    
138    BOOL
139    rdpsnd_dsp_resample_supported(WAVEFORMATEX * format)
140    {
141            if (format->wFormatTag != WAVE_FORMAT_PCM)
142                    return False;
143            if ((format->nChannels != 1) && (format->nChannels != 2))
144                    return False;
145            if ((format->wBitsPerSample != 8) && (format->wBitsPerSample != 16))
146                    return False;
147            if ((format->nSamplesPerSec != 44100) && (format->nSamplesPerSec != 22050))
148                    return False;
149    
150            return True;
151    }
152    
153    uint32
154    rdpsnd_dsp_resample(unsigned char **out, unsigned char *in, unsigned int size,
155                        WAVEFORMATEX * format)
156    {
157            static BOOL warned = False;
158            int outsize, offset;
159            int samplewidth = format->wBitsPerSample / 8;
160            int i;
161    
162            if ((resample_to_bitspersample != format->wBitsPerSample) ||
163                (resample_to_channels != format->nChannels) ||
164                ((format->nSamplesPerSec != 44100) && (format->nSamplesPerSec != 22050)))
165            {
166                    if (!warned)
167                    {
168                            warning("unsupported resample-settings (%u/%u/%u), not resampling!\n",
169                                    format->nSamplesPerSec, format->wBitsPerSample, format->nChannels);
170                            warned = True;
171                    }
172                    return 0;
173            }
174    
175            if (format->nSamplesPerSec == 22050)
176            {
177                    outsize = size * 2;
178                    *out = xmalloc(outsize);
179    
180                    /* Resample to 44100 */
181                    for (i = 0; i < (size / samplewidth); i++)
182                    {
183                            /* On a stereo-channel we must make sure that left and right
184                               does not get mixed up, so we need to expand the sample-
185                               data with channels in mind: 1234 -> 12123434
186                               If we have a mono-channel, we can expand the data by simply
187                               doubling the sample-data: 1234 -> 11223344 */
188                            if (resample_to_channels == 2)
189                                    offset = ((i * 2) - (i & 1)) * samplewidth;
190                            else
191                                    offset = (i * 2) * samplewidth;
192    
193                            memcpy(*out + offset, in + (i * samplewidth), samplewidth);
194                            memcpy(*out + (resample_to_channels * samplewidth + offset),
195                                   in + (i * samplewidth), samplewidth);
196    
197                    }
198            }
199            else
200            {
201                    outsize = 0;
202            }
203    
204            return outsize;
205    }
206    
207    STREAM
208    rdpsnd_dsp_process(STREAM s, struct audio_driver * current_driver, WAVEFORMATEX * format)
209    {
210            static struct stream out;
211    
212            /* softvol and byteswap do not change the amount of data they
213               return, so they can operate on the input-stream */
214          if (current_driver->wave_out_volume == rdpsnd_dsp_softvol_set)          if (current_driver->wave_out_volume == rdpsnd_dsp_softvol_set)
215                  rdpsnd_dsp_softvol(outbuffer, size, format);                  rdpsnd_dsp_softvol(s->data, s->size, format);
216    
217  #ifdef B_ENDIAN  #ifdef B_ENDIAN
218          if (current_driver->need_byteswap_on_be)          if (current_driver->need_byteswap_on_be)
219                  rdpsnd_dsp_swapbytes(outbuffer, size, format);                  rdpsnd_dsp_swapbytes(s->data, s->size, format);
220  #endif  #endif
221    
222          return outbuffer;          out.data = NULL;
223    
224            if (current_driver->wave_out_format_supported == rdpsnd_dsp_resample_supported)
225            {
226                    out.size = rdpsnd_dsp_resample(&out.data, s->data, s->size, format);
227            }
228    
229            if (out.data == NULL)
230            {
231                    out.data = xmalloc(s->size);
232                    memcpy(out.data, s->data, s->size);
233                    out.size = s->size;
234            }
235    
236            out.p = out.data;
237            out.end = out.p + out.size;
238    
239            return &out;
240  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26