Mercurial > SDL_sound_CoreAudio
annotate audio_convert.c @ 541:1f0a57a2483f stable-1.0
Updated.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 17 Apr 2008 18:16:59 +0000 |
parents | 636796aed4e2 |
children | 9e761a594df1 |
rev | line source |
---|---|
141 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@devolution.com | |
21 */ | |
22 | |
23 /* | |
24 * This file was derived from SDL's SDL_audiocvt.c and is an attempt to | |
25 * address the shortcomings of it. | |
26 * | |
27 * Perhaps we can adapt some good filters from SoX? | |
28 */ | |
29 | |
30 #if HAVE_CONFIG_H | |
31 # include <config.h> | |
32 #endif | |
33 | |
465
636796aed4e2
Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
34 #if !SOUND_USE_ALTCVT |
636796aed4e2
Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
35 |
141 | 36 #include "SDL.h" |
37 #include "SDL_sound.h" | |
38 | |
39 #define __SDL_SOUND_INTERNAL__ | |
40 #include "SDL_sound_internal.h" | |
41 | |
42 /* Functions for audio drivers to perform runtime conversion of audio format */ | |
43 | |
44 | |
45 /* | |
46 * Toggle endianness. This filter is, of course, only applied to 16-bit | |
47 * audio data. | |
48 */ | |
49 | |
342 | 50 static void Sound_ConvertEndian(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 51 { |
52 int i; | |
53 Uint8 *data, tmp; | |
54 | |
55 /* SNDDBG(("Converting audio endianness\n")); */ | |
56 | |
57 data = cvt->buf; | |
58 | |
59 for (i = cvt->len_cvt / 2; i; --i) | |
60 { | |
61 tmp = data[0]; | |
62 data[0] = data[1]; | |
63 data[1] = tmp; | |
64 data += 2; | |
65 } /* for */ | |
66 | |
67 *format = (*format ^ 0x1000); | |
68 } /* Sound_ConvertEndian */ | |
69 | |
70 | |
71 /* | |
72 * Toggle signed/unsigned. Apparently this is done by toggling the most | |
73 * significant bit of each sample. | |
74 */ | |
75 | |
342 | 76 static void Sound_ConvertSign(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 77 { |
78 int i; | |
79 Uint8 *data; | |
80 | |
81 /* SNDDBG(("Converting audio signedness\n")); */ | |
82 | |
83 data = cvt->buf; | |
84 | |
85 /* 16-bit sound? */ | |
86 if ((*format & 0xFF) == 16) | |
87 { | |
88 /* Little-endian? */ | |
89 if ((*format & 0x1000) != 0x1000) | |
90 ++data; | |
91 | |
92 for (i = cvt->len_cvt / 2; i; --i) | |
93 { | |
94 *data ^= 0x80; | |
95 data += 2; | |
96 } /* for */ | |
97 } /* if */ | |
98 else | |
99 { | |
100 for (i = cvt->len_cvt; i; --i) | |
101 *data++ ^= 0x80; | |
102 } /* else */ | |
103 | |
104 *format = (*format ^ 0x8000); | |
105 } /* Sound_ConvertSign */ | |
106 | |
107 | |
108 /* | |
109 * Convert 16-bit to 8-bit. This is done by taking the most significant byte | |
110 * of each 16-bit sample. | |
111 */ | |
112 | |
342 | 113 static void Sound_Convert8(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 114 { |
115 int i; | |
116 Uint8 *src, *dst; | |
117 | |
118 /* SNDDBG(("Converting to 8-bit\n")); */ | |
119 | |
120 src = cvt->buf; | |
121 dst = cvt->buf; | |
122 | |
123 /* Little-endian? */ | |
124 if ((*format & 0x1000) != 0x1000) | |
125 ++src; | |
126 | |
127 for (i = cvt->len_cvt / 2; i; --i) | |
128 { | |
129 *dst = *src; | |
130 src += 2; | |
131 dst += 1; | |
132 } /* for */ | |
133 | |
134 *format = ((*format & ~0x9010) | AUDIO_U8); | |
135 cvt->len_cvt /= 2; | |
136 } /* Sound_Convert8 */ | |
137 | |
138 | |
139 /* Convert 8-bit to 16-bit - LSB */ | |
140 | |
342 | 141 static void Sound_Convert16LSB(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 142 { |
143 int i; | |
144 Uint8 *src, *dst; | |
145 | |
146 /* SNDDBG(("Converting to 16-bit LSB\n")); */ | |
147 | |
148 src = cvt->buf + cvt->len_cvt; | |
149 dst = cvt->buf + cvt->len_cvt * 2; | |
150 | |
151 for (i = cvt->len_cvt; i; --i) | |
152 { | |
153 src -= 1; | |
154 dst -= 2; | |
155 dst[1] = *src; | |
156 dst[0] = 0; | |
157 } /* for */ | |
158 | |
159 *format = ((*format & ~0x0008) | AUDIO_U16LSB); | |
160 cvt->len_cvt *= 2; | |
161 } /* Sound_Convert16LSB */ | |
162 | |
163 | |
164 /* Convert 8-bit to 16-bit - MSB */ | |
165 | |
342 | 166 static void Sound_Convert16MSB(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 167 { |
168 int i; | |
169 Uint8 *src, *dst; | |
170 | |
171 /* SNDDBG(("Converting to 16-bit MSB\n")); */ | |
172 | |
173 src = cvt->buf + cvt->len_cvt; | |
174 dst = cvt->buf + cvt->len_cvt * 2; | |
175 | |
176 for (i = cvt->len_cvt; i; --i) | |
177 { | |
178 src -= 1; | |
179 dst -= 2; | |
180 dst[0] = *src; | |
181 dst[1] = 0; | |
182 } /* for */ | |
183 | |
184 *format = ((*format & ~0x0008) | AUDIO_U16MSB); | |
185 cvt->len_cvt *= 2; | |
186 } /* Sound_Convert16MSB */ | |
187 | |
188 | |
189 /* Duplicate a mono channel to both stereo channels */ | |
190 | |
342 | 191 static void Sound_ConvertStereo(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 192 { |
193 int i; | |
194 | |
195 /* SNDDBG(("Converting to stereo\n")); */ | |
196 | |
197 /* 16-bit sound? */ | |
198 if ((*format & 0xFF) == 16) | |
199 { | |
200 Uint16 *src, *dst; | |
201 | |
202 src = (Uint16 *) (cvt->buf + cvt->len_cvt); | |
203 dst = (Uint16 *) (cvt->buf + cvt->len_cvt * 2); | |
204 | |
205 for (i = cvt->len_cvt/2; i; --i) | |
206 { | |
207 dst -= 2; | |
208 src -= 1; | |
209 dst[0] = src[0]; | |
210 dst[1] = src[0]; | |
211 } /* for */ | |
212 } /* if */ | |
213 else | |
214 { | |
215 Uint8 *src, *dst; | |
216 | |
217 src = cvt->buf + cvt->len_cvt; | |
218 dst = cvt->buf + cvt->len_cvt * 2; | |
219 | |
220 for (i = cvt->len_cvt; i; --i) | |
221 { | |
222 dst -= 2; | |
223 src -= 1; | |
224 dst[0] = src[0]; | |
225 dst[1] = src[0]; | |
226 } /* for */ | |
227 } /* else */ | |
228 | |
229 cvt->len_cvt *= 2; | |
230 } /* Sound_ConvertStereo */ | |
231 | |
232 | |
233 /* Effectively mix right and left channels into a single channel */ | |
234 | |
342 | 235 static void Sound_ConvertMono(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 236 { |
237 int i; | |
238 Sint32 sample; | |
239 Uint8 *u_src, *u_dst; | |
240 Sint8 *s_src, *s_dst; | |
241 | |
242 /* SNDDBG(("Converting to mono\n")); */ | |
243 | |
244 switch (*format) | |
245 { | |
246 case AUDIO_U8: | |
247 u_src = cvt->buf; | |
248 u_dst = cvt->buf; | |
249 | |
250 for (i = cvt->len_cvt / 2; i; --i) | |
251 { | |
252 sample = u_src[0] + u_src[1]; | |
253 *u_dst = (sample > 255) ? 255 : sample; | |
254 u_src += 2; | |
255 u_dst += 1; | |
256 } /* for */ | |
257 break; | |
258 | |
259 case AUDIO_S8: | |
260 s_src = (Sint8 *) cvt->buf; | |
261 s_dst = (Sint8 *) cvt->buf; | |
262 | |
263 for (i = cvt->len_cvt / 2; i; --i) | |
264 { | |
265 sample = s_src[0] + s_src[1]; | |
266 if (sample > 127) | |
267 *s_dst = 127; | |
268 else if (sample < -128) | |
269 *s_dst = -128; | |
270 else | |
271 *s_dst = sample; | |
272 | |
273 s_src += 2; | |
274 s_dst += 1; | |
275 } /* for */ | |
276 break; | |
277 | |
278 case AUDIO_U16MSB: | |
279 u_src = cvt->buf; | |
280 u_dst = cvt->buf; | |
281 | |
282 for (i = cvt->len_cvt / 4; i; --i) | |
283 { | |
284 sample = (Uint16) ((u_src[0] << 8) | u_src[1]) | |
285 + (Uint16) ((u_src[2] << 8) | u_src[3]); | |
286 if (sample > 65535) | |
287 { | |
288 u_dst[0] = 0xFF; | |
289 u_dst[1] = 0xFF; | |
290 } /* if */ | |
291 else | |
292 { | |
293 u_dst[1] = (sample & 0xFF); | |
294 sample >>= 8; | |
295 u_dst[0] = (sample & 0xFF); | |
296 } /* else */ | |
297 u_src += 4; | |
298 u_dst += 2; | |
299 } /* for */ | |
300 break; | |
301 | |
302 case AUDIO_U16LSB: | |
303 u_src = cvt->buf; | |
304 u_dst = cvt->buf; | |
305 | |
306 for (i = cvt->len_cvt / 4; i; --i) | |
307 { | |
308 sample = (Uint16) ((u_src[1] << 8) | u_src[0]) | |
309 + (Uint16) ((u_src[3] << 8) | u_src[2]); | |
310 if (sample > 65535) | |
311 { | |
312 u_dst[0] = 0xFF; | |
313 u_dst[1] = 0xFF; | |
314 } /* if */ | |
315 else | |
316 { | |
317 u_dst[0] = (sample & 0xFF); | |
318 sample >>= 8; | |
319 u_dst[1] = (sample & 0xFF); | |
320 } /* else */ | |
321 u_src += 4; | |
322 u_dst += 2; | |
323 } /* for */ | |
324 break; | |
325 | |
326 case AUDIO_S16MSB: | |
327 u_src = cvt->buf; | |
328 u_dst = cvt->buf; | |
329 | |
330 for (i = cvt->len_cvt / 4; i; --i) | |
331 { | |
332 sample = (Sint16) ((u_src[0] << 8) | u_src[1]) | |
333 + (Sint16) ((u_src[2] << 8) | u_src[3]); | |
334 if (sample > 32767) | |
335 { | |
336 u_dst[0] = 0x7F; | |
337 u_dst[1] = 0xFF; | |
338 } /* if */ | |
339 else if (sample < -32768) | |
340 { | |
341 u_dst[0] = 0x80; | |
342 u_dst[1] = 0x00; | |
343 } /* else if */ | |
344 else | |
345 { | |
346 u_dst[1] = (sample & 0xFF); | |
347 sample >>= 8; | |
348 u_dst[0] = (sample & 0xFF); | |
349 } /* else */ | |
350 u_src += 4; | |
351 u_dst += 2; | |
352 } /* for */ | |
353 break; | |
354 | |
355 case AUDIO_S16LSB: | |
356 u_src = cvt->buf; | |
357 u_dst = cvt->buf; | |
358 | |
359 for (i = cvt->len_cvt / 4; i; --i) | |
360 { | |
361 sample = (Sint16) ((u_src[1] << 8) | u_src[0]) | |
362 + (Sint16) ((u_src[3] << 8) | u_src[2]); | |
363 if (sample > 32767) | |
364 { | |
365 u_dst[1] = 0x7F; | |
366 u_dst[0] = 0xFF; | |
367 } /* if */ | |
368 else if (sample < -32768) | |
369 { | |
370 u_dst[1] = 0x80; | |
371 u_dst[0] = 0x00; | |
372 } /* else if */ | |
373 else | |
374 { | |
375 u_dst[0] = (sample & 0xFF); | |
376 sample >>= 8; | |
377 u_dst[1] = (sample & 0xFF); | |
378 } /* else */ | |
379 u_src += 4; | |
380 u_dst += 2; | |
381 } /* for */ | |
382 break; | |
383 } /* switch */ | |
384 | |
385 cvt->len_cvt /= 2; | |
386 } /* Sound_ConvertMono */ | |
387 | |
388 | |
389 /* Convert rate up by multiple of 2 */ | |
390 | |
342 | 391 static void Sound_RateMUL2(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 392 { |
393 int i; | |
394 Uint8 *src, *dst; | |
395 | |
396 /* SNDDBG(("Converting audio rate * 2\n")); */ | |
397 | |
398 src = cvt->buf + cvt->len_cvt; | |
399 dst = cvt->buf + cvt->len_cvt*2; | |
400 | |
401 /* 8- or 16-bit sound? */ | |
402 switch (*format & 0xFF) | |
403 { | |
404 case 8: | |
405 for (i = cvt->len_cvt; i; --i) | |
406 { | |
407 src -= 1; | |
408 dst -= 2; | |
409 dst[0] = src[0]; | |
410 dst[1] = src[0]; | |
411 } /* for */ | |
412 break; | |
413 | |
414 case 16: | |
415 for (i = cvt->len_cvt / 2; i; --i) | |
416 { | |
417 src -= 2; | |
418 dst -= 4; | |
419 dst[0] = src[0]; | |
420 dst[1] = src[1]; | |
421 dst[2] = src[0]; | |
422 dst[3] = src[1]; | |
423 } /* for */ | |
424 break; | |
425 } /* switch */ | |
426 | |
427 cvt->len_cvt *= 2; | |
428 } /* Sound_RateMUL2 */ | |
429 | |
430 | |
431 /* Convert rate down by multiple of 2 */ | |
432 | |
342 | 433 static void Sound_RateDIV2(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 434 { |
435 int i; | |
436 Uint8 *src, *dst; | |
437 | |
438 /* SNDDBG(("Converting audio rate / 2\n")); */ | |
439 | |
440 src = cvt->buf; | |
441 dst = cvt->buf; | |
442 | |
443 /* 8- or 16-bit sound? */ | |
444 switch (*format & 0xFF) | |
445 { | |
446 case 8: | |
447 for (i = cvt->len_cvt / 2; i; --i) | |
448 { | |
449 dst[0] = src[0]; | |
450 src += 2; | |
451 dst += 1; | |
452 } /* for */ | |
453 break; | |
454 | |
455 case 16: | |
456 for (i = cvt->len_cvt / 4; i; --i) | |
457 { | |
458 dst[0] = src[0]; | |
459 dst[1] = src[1]; | |
460 src += 4; | |
461 dst += 2; | |
462 } | |
463 break; | |
464 } /* switch */ | |
465 | |
466 cvt->len_cvt /= 2; | |
467 } /* Sound_RateDIV2 */ | |
468 | |
469 | |
470 /* Very slow rate conversion routine */ | |
471 | |
342 | 472 static void Sound_RateSLOW(Sound_AudioCVT *cvt, Uint16 *format) |
141 | 473 { |
474 double ipos; | |
475 int i, clen; | |
476 Uint8 *output8; | |
477 Uint16 *output16; | |
478 | |
479 /* SNDDBG(("Converting audio rate * %4.4f\n", 1.0/cvt->rate_incr)); */ | |
480 | |
481 clen = (int) ((double) cvt->len_cvt / cvt->rate_incr); | |
482 | |
483 if (cvt->rate_incr > 1.0) | |
484 { | |
485 /* 8- or 16-bit sound? */ | |
486 switch (*format & 0xFF) | |
487 { | |
488 case 8: | |
489 output8 = cvt->buf; | |
490 | |
491 ipos = 0.0; | |
492 for (i = clen; i; --i) | |
493 { | |
494 *output8 = cvt->buf[(int) ipos]; | |
495 ipos += cvt->rate_incr; | |
496 output8 += 1; | |
497 } /* for */ | |
498 break; | |
499 | |
500 case 16: | |
501 output16 = (Uint16 *) cvt->buf; | |
502 | |
503 clen &= ~1; | |
504 ipos = 0.0; | |
505 for (i = clen / 2; i; --i) | |
506 { | |
507 *output16 = ((Uint16 *) cvt->buf)[(int) ipos]; | |
508 ipos += cvt->rate_incr; | |
509 output16 += 1; | |
510 } /* for */ | |
511 break; | |
512 } /* switch */ | |
513 } /* if */ | |
514 else | |
515 { | |
516 /* 8- or 16-bit sound */ | |
517 switch (*format & 0xFF) | |
518 { | |
519 case 8: | |
520 output8 = cvt->buf + clen; | |
521 | |
522 ipos = (double) cvt->len_cvt; | |
523 for (i = clen; i; --i) | |
524 { | |
525 ipos -= cvt->rate_incr; | |
526 output8 -= 1; | |
527 *output8 = cvt->buf[(int) ipos]; | |
528 } /* for */ | |
529 break; | |
530 | |
531 case 16: | |
532 clen &= ~1; | |
533 output16 = (Uint16 *) (cvt->buf + clen); | |
534 ipos = (double) cvt->len_cvt / 2; | |
535 for (i = clen / 2; i; --i) | |
536 { | |
537 ipos -= cvt->rate_incr; | |
538 output16 -= 1; | |
539 *output16 = ((Uint16 *) cvt->buf)[(int) ipos]; | |
540 } /* for */ | |
541 break; | |
542 } /* switch */ | |
543 } /* else */ | |
544 | |
545 cvt->len_cvt = clen; | |
546 } /* Sound_RateSLOW */ | |
547 | |
548 | |
549 int Sound_ConvertAudio(Sound_AudioCVT *cvt) | |
550 { | |
551 Uint16 format; | |
552 | |
553 /* Make sure there's data to convert */ | |
554 if (cvt->buf == NULL) | |
555 { | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
382
diff
changeset
|
556 __Sound_SetError("No buffer allocated for conversion"); |
141 | 557 return(-1); |
558 } /* if */ | |
559 | |
560 /* Return okay if no conversion is necessary */ | |
561 cvt->len_cvt = cvt->len; | |
562 if (cvt->filters[0] == NULL) | |
563 return(0); | |
564 | |
565 /* Set up the conversion and go! */ | |
566 format = cvt->src_format; | |
567 for (cvt->filter_index = 0; cvt->filters[cvt->filter_index]; | |
568 cvt->filter_index++) | |
569 { | |
570 cvt->filters[cvt->filter_index](cvt, &format); | |
571 } | |
572 return(0); | |
573 } /* Sound_ConvertAudio */ | |
574 | |
575 | |
576 /* | |
577 * Creates a set of audio filters to convert from one format to another. | |
578 * Returns -1 if the format conversion is not supported, or 1 if the | |
579 * audio filter is set up. | |
580 */ | |
581 | |
582 int Sound_BuildAudioCVT(Sound_AudioCVT *cvt, | |
583 Uint16 src_format, Uint8 src_channels, Uint32 src_rate, | |
382
ce998ee6194f
Sync'd with latest altcvt.
Ryan C. Gordon <icculus@icculus.org>
parents:
342
diff
changeset
|
584 Uint16 dst_format, Uint8 dst_channels, Uint32 dst_rate, |
ce998ee6194f
Sync'd with latest altcvt.
Ryan C. Gordon <icculus@icculus.org>
parents:
342
diff
changeset
|
585 Uint32 dst_size) |
141 | 586 { |
587 /* Start off with no conversion necessary */ | |
588 cvt->needed = 0; | |
589 cvt->filter_index = 0; | |
590 cvt->filters[0] = NULL; | |
591 cvt->len_mult = 1; | |
592 cvt->len_ratio = 1.0; | |
593 | |
594 /* First filter: Endian conversion from src to dst */ | |
595 if ((src_format & 0x1000) != (dst_format & 0x1000) && | |
596 ((src_format & 0xff) != 8)) | |
597 { | |
598 SNDDBG(("Adding filter: Sound_ConvertEndian\n")); | |
599 cvt->filters[cvt->filter_index++] = Sound_ConvertEndian; | |
600 } /* if */ | |
601 | |
602 /* Second filter: Sign conversion -- signed/unsigned */ | |
603 if ((src_format & 0x8000) != (dst_format & 0x8000)) | |
604 { | |
605 SNDDBG(("Adding filter: Sound_ConvertSign\n")); | |
606 cvt->filters[cvt->filter_index++] = Sound_ConvertSign; | |
607 } /* if */ | |
608 | |
609 /* Next filter: Convert 16 bit <--> 8 bit PCM. */ | |
610 if ((src_format & 0xFF) != (dst_format & 0xFF)) | |
611 { | |
612 switch (dst_format & 0x10FF) | |
613 { | |
614 case AUDIO_U8: | |
615 SNDDBG(("Adding filter: Sound_Convert8\n")); | |
616 cvt->filters[cvt->filter_index++] = Sound_Convert8; | |
617 cvt->len_ratio /= 2; | |
618 break; | |
619 | |
620 case AUDIO_U16LSB: | |
621 SNDDBG(("Adding filter: Sound_Convert16LSB\n")); | |
622 cvt->filters[cvt->filter_index++] = Sound_Convert16LSB; | |
623 cvt->len_mult *= 2; | |
624 cvt->len_ratio *= 2; | |
625 break; | |
626 | |
627 case AUDIO_U16MSB: | |
628 SNDDBG(("Adding filter: Sound_Convert16MSB\n")); | |
629 cvt->filters[cvt->filter_index++] = Sound_Convert16MSB; | |
630 cvt->len_mult *= 2; | |
631 cvt->len_ratio *= 2; | |
632 break; | |
633 } /* switch */ | |
634 } /* if */ | |
635 | |
636 /* Next filter: Mono/Stereo conversion */ | |
637 if (src_channels != dst_channels) | |
638 { | |
639 while ((src_channels * 2) <= dst_channels) | |
640 { | |
641 SNDDBG(("Adding filter: Sound_ConvertStereo\n")); | |
642 cvt->filters[cvt->filter_index++] = Sound_ConvertStereo; | |
643 cvt->len_mult *= 2; | |
644 src_channels *= 2; | |
645 cvt->len_ratio *= 2; | |
646 } /* while */ | |
647 | |
648 /* This assumes that 4 channel audio is in the format: | |
649 * Left {front/back} + Right {front/back} | |
650 * so converting to L/R stereo works properly. | |
651 */ | |
652 while (((src_channels % 2) == 0) && | |
653 ((src_channels / 2) >= dst_channels)) | |
654 { | |
655 SNDDBG(("Adding filter: Sound_ConvertMono\n")); | |
656 cvt->filters[cvt->filter_index++] = Sound_ConvertMono; | |
657 src_channels /= 2; | |
658 cvt->len_ratio /= 2; | |
659 } /* while */ | |
660 | |
661 if ( src_channels != dst_channels ) { | |
662 /* Uh oh.. */; | |
663 } /* if */ | |
664 } /* if */ | |
665 | |
666 /* Do rate conversion */ | |
667 cvt->rate_incr = 0.0; | |
668 if ((src_rate / 100) != (dst_rate / 100)) | |
669 { | |
670 Uint32 hi_rate, lo_rate; | |
671 int len_mult; | |
672 double len_ratio; | |
673 void (*rate_cvt)(Sound_AudioCVT *cvt, Uint16 *format); | |
674 | |
675 if (src_rate > dst_rate) | |
676 { | |
677 hi_rate = src_rate; | |
678 lo_rate = dst_rate; | |
679 SNDDBG(("Adding filter: Sound_RateDIV2\n")); | |
680 rate_cvt = Sound_RateDIV2; | |
681 len_mult = 1; | |
682 len_ratio = 0.5; | |
683 } /* if */ | |
684 else | |
685 { | |
686 hi_rate = dst_rate; | |
687 lo_rate = src_rate; | |
688 SNDDBG(("Adding filter: Sound_RateMUL2\n")); | |
689 rate_cvt = Sound_RateMUL2; | |
690 len_mult = 2; | |
691 len_ratio = 2.0; | |
692 } /* else */ | |
693 | |
694 /* If hi_rate = lo_rate*2^x then conversion is easy */ | |
695 while (((lo_rate * 2) / 100) <= (hi_rate / 100)) | |
696 { | |
697 cvt->filters[cvt->filter_index++] = rate_cvt; | |
698 cvt->len_mult *= len_mult; | |
699 lo_rate *= 2; | |
700 cvt->len_ratio *= len_ratio; | |
701 } /* while */ | |
702 | |
703 /* We may need a slow conversion here to finish up */ | |
704 if ((lo_rate / 100) != (hi_rate / 100)) | |
705 { | |
706 if (src_rate < dst_rate) | |
707 { | |
708 cvt->rate_incr = (double) lo_rate / hi_rate; | |
709 cvt->len_mult *= 2; | |
710 cvt->len_ratio /= cvt->rate_incr; | |
711 } /* if */ | |
712 else | |
713 { | |
714 cvt->rate_incr = (double) hi_rate / lo_rate; | |
715 cvt->len_ratio *= cvt->rate_incr; | |
716 } /* else */ | |
717 SNDDBG(("Adding filter: Sound_RateSLOW\n")); | |
718 cvt->filters[cvt->filter_index++] = Sound_RateSLOW; | |
719 } /* if */ | |
720 } /* if */ | |
721 | |
722 /* Set up the filter information */ | |
723 if (cvt->filter_index != 0) | |
724 { | |
725 cvt->needed = 1; | |
726 cvt->src_format = src_format; | |
727 cvt->dst_format = dst_format; | |
728 cvt->len = 0; | |
729 cvt->buf = NULL; | |
730 cvt->filters[cvt->filter_index] = NULL; | |
731 } /* if */ | |
732 | |
733 return(cvt->needed); | |
734 } /* Sound_BuildAudioCVT */ | |
465
636796aed4e2
Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
735 |
636796aed4e2
Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
736 #endif /* !SOUND_USE_ALTCVT */ |
636796aed4e2
Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
737 |
636796aed4e2
Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
738 /* end of audio_convert.c ... */ |
636796aed4e2
Build/packaging fixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
739 |