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" |
134 |
int err; |
int err; |
135 |
#endif |
#endif |
136 |
|
|
|
#ifdef HAVE_LIBSAMPLERATE |
|
|
if (device_bitspersample != 16) |
|
|
return False; |
|
|
#else |
|
|
if (device_srate != 44100 && device_srate != 22050) |
|
|
return False; |
|
|
|
|
137 |
if (device_bitspersample != 16 && device_bitspersample != 8) |
if (device_bitspersample != 16 && device_bitspersample != 8) |
138 |
return False; |
return False; |
|
#endif |
|
139 |
|
|
140 |
if (device_channels != 1 && device_channels != 2) |
if (device_channels != 1 && device_channels != 2) |
141 |
return False; |
return False; |
165 |
return False; |
return False; |
166 |
if ((format->nChannels != 1) && (format->nChannels != 2)) |
if ((format->nChannels != 1) && (format->nChannels != 2)) |
167 |
return False; |
return False; |
|
#ifdef HAVE_LIBSAMPLERATE |
|
|
if (format->wBitsPerSample != 16) |
|
|
return False; |
|
|
#else |
|
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; |
|
|
#endif |
|
170 |
|
|
171 |
return True; |
return True; |
172 |
} |
} |
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; |
|
int i; |
|
184 |
#endif |
#endif |
185 |
|
int innum, outnum; |
186 |
static BOOL warned = False; |
static BOOL warned = False; |
187 |
|
unsigned char *tmpdata = NULL; |
188 |
int samplewidth = format->wBitsPerSample / 8; |
int samplewidth = format->wBitsPerSample / 8; |
189 |
int outsize = 0; |
int outsize = 0; |
190 |
|
int i; |
191 |
|
|
192 |
if ((resample_to_bitspersample == format->wBitsPerSample) && |
if ((resample_to_bitspersample == format->wBitsPerSample) && |
193 |
(resample_to_channels == format->nChannels) && |
(resample_to_channels == format->nChannels) && |
196 |
|
|
197 |
#ifdef B_ENDIAN |
#ifdef B_ENDIAN |
198 |
if (!stream_be) |
if (!stream_be) |
199 |
rdpsnd_dsp_swapbytes(in, size, format) |
rdpsnd_dsp_swapbytes(in, size, format); |
200 |
#endif |
#endif |
201 |
if ((resample_to_channels != format->nChannels) || |
|
202 |
(resample_to_bitspersample != format->wBitsPerSample)) |
/* Expand 8bit input-samples to 16bit */ |
203 |
|
#ifndef HAVE_LIBSAMPLERATE /* libsamplerate needs 16bit samples */ |
204 |
|
if (format->wBitsPerSample != resample_to_bitspersample) |
205 |
|
#endif |
206 |
|
{ |
207 |
|
/* source: 8 bit, dest: 16bit */ |
208 |
|
if (format->wBitsPerSample == 8) |
209 |
{ |
{ |
210 |
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); |
tmpdata = xmalloc(size * 2); |
211 |
warned = True; |
for (i = 0; i < size; i++) |
212 |
|
{ |
213 |
|
tmpdata[i * 2] = in[i]; |
214 |
|
tmpdata[(i * 2) + 1] = 0x00; |
215 |
|
} |
216 |
|
in = tmpdata; |
217 |
|
samplewidth = 16 / 2; |
218 |
|
size *= 2; |
219 |
} |
} |
220 |
|
} |
221 |
|
|
222 |
|
if (resample_to_channels != format->nChannels) |
223 |
|
{ |
224 |
|
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); |
225 |
|
warned = True; |
226 |
|
} |
227 |
|
|
228 |
|
innum = size / samplewidth; |
229 |
|
|
230 |
|
/* Do the resampling */ |
231 |
#ifdef HAVE_LIBSAMPLERATE |
#ifdef HAVE_LIBSAMPLERATE |
232 |
if (src_converter == NULL) |
if (src_converter == NULL) |
233 |
{ |
{ |
235 |
return 0; |
return 0; |
236 |
} |
} |
237 |
|
|
238 |
innum = size / samplewidth; |
outnum = ((float) innum * ((float) resample_to_srate / (float) format->nSamplesPerSec)) + 1; |
|
outnum = innum * (resample_to_srate / format->nSamplesPerSec); |
|
239 |
|
|
240 |
infloat = xmalloc(sizeof(float) * innum); |
infloat = xmalloc(sizeof(float) * innum); |
241 |
outfloat = xmalloc(sizeof(float) * outnum); |
outfloat = xmalloc(sizeof(float) * outnum); |
250 |
resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec; |
resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec; |
251 |
resample_data.end_of_input = 0; |
resample_data.end_of_input = 0; |
252 |
|
|
253 |
src_process(src_converter, &resample_data); |
if ((err = src_process(src_converter, &resample_data)) != 0) |
254 |
|
error("src_process: %s", src_strerror(err)); |
255 |
|
|
256 |
xfree(infloat); |
xfree(infloat); |
257 |
|
|
258 |
outsize = outnum * samplewidth; |
outsize = resample_data.output_frames_gen * resample_to_channels * samplewidth; |
259 |
*out = xmalloc(outsize); |
*out = xmalloc(outsize); |
260 |
src_float_to_short_array(outfloat, (short *) *out, outnum); |
src_float_to_short_array(outfloat, (short *) *out, |
261 |
|
resample_data.output_frames_gen * resample_to_channels); |
262 |
xfree(outfloat); |
xfree(outfloat); |
263 |
|
|
264 |
#else |
#else |
265 |
if (format->nSamplesPerSec != 22050) |
/* Michaels simple linear resampler */ |
266 |
|
if (resample_to_srate < format->nSamplesPerSec) |
267 |
{ |
{ |
268 |
if (!warned) |
warning("downsampling currently not supported!\n"); |
|
{ |
|
|
warning("unsupported source samplerate (%u), not resampling!\n", |
|
|
format->nSamplesPerSec); |
|
|
warned = True; |
|
|
} |
|
269 |
return 0; |
return 0; |
270 |
} |
} |
271 |
|
|
272 |
outsize = size * 2; |
outnum = (innum * ratio1k) / 1000; |
273 |
|
|
274 |
|
outsize = outnum * samplewidth; |
275 |
*out = xmalloc(outsize); |
*out = xmalloc(outsize); |
276 |
|
bzero(*out, outsize); |
277 |
|
|
278 |
/* Resample from 22050 to 44100 */ |
for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++) |
|
for (i = 0; i < (size / samplewidth); i++) |
|
279 |
{ |
{ |
280 |
/* On a stereo-channel we must make sure that left and right |
int source = ((i * 1000) + ratio1k - 1000) / (ratio1k + 1); |
281 |
does not get mixed up, so we need to expand the sample- |
|
282 |
data with channels in mind: 1234 -> 12123434 |
if (source * resample_to_channels + samplewidth > size) |
283 |
If we have a mono-channel, we can expand the data by simply |
break; |
284 |
doubling the sample-data: 1234 -> 11223344 */ |
|
285 |
if (resample_to_channels == 2) |
if (resample_to_channels == 2) |
286 |
offset = ((i * 2) - (i & 1)) * samplewidth; |
{ |
287 |
|
memcpy(*out + (i * resample_to_channels * samplewidth), |
288 |
|
in + (source * resample_to_channels * samplewidth), samplewidth); |
289 |
|
memcpy(*out + (i * resample_to_channels * samplewidth) + samplewidth, |
290 |
|
in + (source * resample_to_channels * samplewidth) + samplewidth, |
291 |
|
samplewidth); |
292 |
|
} |
293 |
else |
else |
294 |
offset = (i * 2) * samplewidth; |
{ |
295 |
|
memcpy(*out + (i * samplewidth), in + (source * samplewidth), samplewidth); |
296 |
|
} |
297 |
|
} |
298 |
|
outsize = i * resample_to_channels * samplewidth; |
299 |
|
#endif |
300 |
|
|
301 |
memcpy(*out + offset, in + (i * samplewidth), samplewidth); |
if (tmpdata != NULL) |
302 |
memcpy(*out + (resample_to_channels * samplewidth + offset), |
xfree(tmpdata); |
|
in + (i * samplewidth), samplewidth); |
|
303 |
|
|
304 |
} |
/* Shrink 16bit output-samples to 8bit */ |
305 |
|
#ifndef HAVE_LIBSAMPLERATE /* libsamplerate produces 16bit samples */ |
306 |
|
if (format->wBitsPerSample != resample_to_bitspersample) |
307 |
#endif |
#endif |
308 |
|
{ |
309 |
|
/* source: 16 bit, dest: 8 bit */ |
310 |
|
if (resample_to_bitspersample == 8) |
311 |
|
{ |
312 |
|
for (i = 0; i < outsize; i++) |
313 |
|
{ |
314 |
|
*out[i] = *out[i * 2]; |
315 |
|
} |
316 |
|
outsize /= 2; |
317 |
|
} |
318 |
|
} |
319 |
|
|
320 |
#ifdef B_ENDIAN |
#ifdef B_ENDIAN |
321 |
if (!stream_be) |
if (!stream_be) |
322 |
rdpsnd_dsp_swapbytes(*out, outsize, format) |
rdpsnd_dsp_swapbytes(*out, outsize, format); |
323 |
#endif |
#endif |
324 |
return outsize; |
return outsize; |
325 |
} |
} |
326 |
|
|
327 |
STREAM |
STREAM |
345 |
|
|
346 |
out.data = NULL; |
out.data = NULL; |
347 |
|
|
348 |
if (current_driver->wave_out_format_supported == rdpsnd_dsp_resample_supported) |
if (current_driver->need_resampling) |
349 |
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, stream_be); |
350 |
|
|
351 |
if (out.data == NULL) |
if (out.data == NULL) |