annotate audio_convert.c @ 543:989a46bb88d2 stable-1.0 release-1.0.2

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