comparison mixer/converters.c @ 486:859dd2ef3197

Added some seriously INCOMPLETE mixer code.
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 27 Feb 2005 19:55:29 +0000
parents
children 2df1f5c62d38
comparison
equal deleted inserted replaced
485:137c0b00ea4c 486:859dd2ef3197
1 /*
2 * SDL_sound -- An sound processing toolkit.
3 * Copyright (C) 2001 Ryan C. Gordon.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 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 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /**
21 * This file implements the mixer itself. Largely, this is handled in the
22 * SDL audio callback.
23 *
24 * Documentation is in SDL_sound.h ... It's verbose, honest. :)
25 *
26 * Please see the file COPYING in the source's root directory.
27 *
28 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
29 */
30
31 #if HAVE_CONFIG_H
32 # include <config.h>
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "SDL.h"
40 #include "SDL_thread.h"
41 #include "SDL_sound.h"
42
43 #define __SDL_SOUND_INTERNAL__
44 #include "SDL_sound_internal.h"
45
46
47 static void conv_mix_buf_u8_mono(void *userdata, Uint8 *_stream, int len)
48 {
49 register Uint8 *stream = _stream;
50 register Uint32 i;
51 register Uint32 max = len * 2;
52 for (i = 0; i < max; i += 2)
53 {
54 *stream = (Uint8) ((((mixbuf[i]+mixbuf[i+1])*0.5f) * 127.0f) + 128.0f);
55 stream++;
56 } /* for */
57 } /* conv_mix_buf_s8s */
58
59 static void conv_mix_buf_s8_mono(void *userdata, Uint8 *_stream, int len)
60 {
61 register Uint32 i;
62 register Sint8 *stream = (Sint8 *) _stream;
63 register Uint32 max = len * 2;
64 for (i = 0; i < max; i += 2)
65 {
66 *stream = (Sint8) (((mixbuf[i] + mixbuf[i+1]) * 0.5f) * 127.0f);
67 stream++;
68 } /* for */
69 } /* conv_mix_buf_s8s */
70
71 static void conv_mix_buf_s16_lsb_mono(void *userdata, Uint8 *_stream, int len)
72 {
73 register Uint32 i;
74 register Sint16 *stream = (Sint16 *) _stream;
75 register Sint16 val;
76 register Uint32 max = len / 2;
77 for (i = 0; i < max; i += 2)
78 {
79 val = (Sint16) (((mixbuf[i] + mixbuf[i+1]) * 0.5f) * 32767.0f);
80 *stream = SDL_SwapLE16(val);
81 stream++;
82 } /* for */
83 } /* conv_mix_buf_s16_lsb_mono */
84
85 static void conv_mix_buf_s16_msb_mono(void *userdata, Uint8 *_stream, int len)
86 {
87 register Uint32 i;
88 register Sint16 *stream = (Sint16 *) _stream;
89 register Sint16 val;
90 register Uint32 max = len / 2;
91 for (i = 0; i < max; i += 2)
92 {
93 val = (Sint16) (((mixbuf[i] + mixbuf[i+1]) * 0.5f) * 32767.0f);
94 *stream = SDL_SwapBE16(val);
95 stream++;
96 } /* for */
97 } /* conv_mix_buf_s16_msb_mono */
98
99 static void conv_mix_buf_u16_lsb_mono(void *userdata, Uint8 *_stream, int len)
100 {
101 register Uint32 i;
102 register Uint16 *stream = (Uint16 *) _stream;
103 register Uint16 val;
104 register Uint32 max = len / 2;
105 for (i = 0; i < max; i += 2)
106 {
107 val = (Uint16)((((mixbuf[i]+mixbuf[i+1])*0.5f) * 32767.0f) + 32768.0f);
108 *stream = SDL_SwapLE16(val);
109 stream++;
110 } /* for */
111 } /* conv_mix_buf_s16_lsb_mono */
112
113 static void conv_mix_buf_u16_msb_mono(void *userdata, Uint8 *_stream, int len)
114 {
115 register Uint32 i;
116 register Uint16 *stream = (Uint16 *) _stream;
117 register Uint16 val;
118 register Uint32 max = len / 2;
119 for (i = 0; i < max; i += 2)
120 {
121 val = (Uint16)((((mixbuf[i]+mixbuf[i+1])*0.5f) * 32767.0f) + 32768.0f);
122 *stream = SDL_SwapBE16(val);
123 stream++;
124 } /* for */
125 } /* conv_mix_buf_s16_lsb_mono */
126
127 static void conv_mix_buf_u8_stereo(void *userdata, Uint8 *_stream, int len)
128 {
129 register Uint32 i;
130 register Uint8 *stream = _stream;
131 register Uint32 max = len;
132 for (i = 0; i < max; i++)
133 {
134 *stream = (Uint8) ((mixbuf[i] * 127.0f) + 128.0f);
135 stream++;
136 } /* for */
137 } /* conv_mix_buf_s8s */
138
139 static void conv_mix_buf_s8_stereo(void *userdata, Uint8 *_stream, int len)
140 {
141 register Uint32 i;
142 register Sint8 *stream = (Sint8 *) _stream;
143 register Uint32 max = len;
144 for (i = 0; i < max; i++)
145 {
146 *stream = (Sint8) (mixbuf[i] * 127.0f);
147 stream++;
148 } /* for */
149 } /* conv_mix_buf_s8s */
150
151 static void conv_mix_buf_s16lsb_stereo(void *userdata, Uint8 *_stream, int len)
152 {
153 register Uint32 i;
154 register Sint16 *stream = (Sint16 *) _stream;
155 register Sint16 val;
156 register Uint32 max = len / 2;
157 for (i = 0; i < max; i++)
158 {
159 val = (Sint16) (mixbuf[i] * 32767.0f);
160 *stream = SDL_SwapLE16(val);
161 stream++;
162 } /* for */
163 } /* conv_mix_buf_s16_lsb_stereo */
164
165 static void conv_mix_buf_s16msb_stereo(void *userdata, Uint8 *_stream, int len)
166 {
167 register Uint32 i;
168 register Sint16 *stream = (Sint16 *) _stream;
169 register Sint16 val;
170 register Uint32 max = len / 2;
171 for (i = 0; i < max; i++)
172 {
173 val = (Sint16) (mixbuf[i] * 32767.0f);
174 *stream = SDL_SwapBE16(val);
175 stream++;
176 } /* for */
177 } /* conv_mix_buf_s16_msb_stereo */
178
179 static void conv_mix_buf_u16lsb_stereo(void *userdata, Uint8 *_stream, int len)
180 {
181 register Uint32 i;
182 register Uint16 *stream = (Uint16 *) _stream;
183 register Uint16 val;
184 register Uint32 max = len / 2;
185 for (i = 0; i < max; i++)
186 {
187 val = (Uint16) ((mixbuf[i] * 32767.0f) + 32768.0f);
188 *stream = SDL_SwapLE16(val);
189 stream++;
190 } /* for */
191 } /* conv_mix_buf_s16_lsb_stereo */
192
193 static void conv_mix_buf_u16msb_stereo(void *userdata, Uint8 *_stream, int len)
194 {
195 register Uint32 i;
196 register Uint16 *stream = (Uint16 *) _stream;
197 register Uint16 val;
198 register Uint32 max = len / 2;
199 for (i = 0; i < max; i++)
200 {
201 val = (Uint16) ((mixbuf[i] * 32767.0f) + 32768.0f);
202 *stream = SDL_SwapBE16(val);
203 stream++;
204 } /* for */
205 } /* conv_mix_buf_s16_msb_stereo */
206
207 /* end of converters.c ... */
208