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