Mercurial > sdl-ios-xcode
annotate src/audio/dmedia/SDL_irixaudio.c @ 942:41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
surround sound on Linux using the Alsa driver. To use them, naturally
you need a sound card that will do 4 or 6 channels and probably also a
recent version of the Alsa drivers and library. Since the only SDL
output driver that knows about surround sound is the Alsa driver,
you���ll want to choose it, using:
export SDL_AUDIODRIVER=alsa
There are no syntactic changes to the programming API. No new
library calls, no differences in arguments.
There are two semantic changes:
(1) For library calls with number of channels as an argument, formerly
you could use only 1 or 2 for the number of channels. Now you
can also use 4 or 6.
(2) The two "left" and "right" arguments to Mix_SetPanning, for the
case of 4 or 6 channels, no longer simply control the volumes of
the left and right channels. Now the "left" argument is converted
to an angle and Mix_SetPosition is called, and the "right" argu-
ment is ignored.
With two exceptions, so far as I know, the modified SDL12 and
SDL_mixer work the same way as the original versions, when opened for
1 or 2 channel output. The two exceptions are bugs which I fixed.
Well, the first, anyway, is a bug for sure. When rate conversions up
or down by a factor of two are applied (in src/audio/SDL_audiocvt.c),
streams with different numbers of channels (that is, mono and stereo)
are treated the same way: either each sample is copied or every other
sample is omitted. This is ok for mono, but for stereo, it is frames
that should be copied or omitted, where by "frame" I mean a portion of
the stream containing one sample for each channel. (In the SDL source,
confusingly, sometimes frames are called "samples".) So for these
rate conversions, stereo streams have to be treated differently, and
they are, in my modified version.
The other problem that might be characterized as a bug arises
when SDL_mixer is passed a multichannel chunk which does not have an
integral number of frames. Due to the way the effect_position code
loops over frames, when the chunk ends with a partial frame, memory
outside the chunk buffer will be accessed. In the case of stereo,
it���s possible that because malloc may give more memory than requested,
this potential problem never actually causes a segment fault. I don���t
know. For 6 channel chunks, I do know, and it does cause segment
faults.
If SDL_mixer is passed defective chunks and this causes a segment
fault, arguably, that���s not a bug in SDL_mixer. Still, whether or not
it counts as a bug, it���s easy to protect against, so why not? I added
code in mixer.c to discard any partial frame at the end of a chunk.
Then what about when SDL or SDL_mixer is opened for 4 or 6 chan-
nel output? What happens with the parts of the current library
designed for stereo? I don���t know whether I���ve covered all the bases,
but I���ve tried:
(1) For playing 2 channel waves, or other cases where SDL knows it has
to match up a 2 channel source with a 4 or 6 channel output, I���ve
added code in SDL_audiocvt.c to make the necessary conversions.
(2) For playing midis using timidity, I���ve converted timidity to do 4
or 6 channel output, upon request.
(3) For playing mods using mikmod, I put ad hoc code in music.c to
convert the stereo output that mikmod produces to 4 or 6 chan-
nels. Obviously it would be better to change the mikmod code to
mix down into 4 or 6 channels, but I have a hard time following
the code in mikmod, so I didn���t do that.
(4) For playing mp3s, I put ad hoc code in smpeg to copy channels in
the case when 4 or 6 channel output is needed.
(5) There seems to be no problem with .ogg files - stereo .oggs can be
up converted as .wavs are.
(6) The effect_position code in SDL_mixer is now generalized to in-
clude the cases of 4 and 6 channel streams.
I���ve done a very limited amount of compatibility testing for some
of the games using SDL I happen to have. For details, see the file
TESTS.
I���ve put into a separate archive, Surround-SDL-testfiles.tgz, a
couple of 6 channel wave files for testing and a 6 channel ogg file.
If you have the right hardware and version of Alsa, you should be able
to play the wave files with the Alsa utility aplay (and hear all
channels, except maybe lfe, for chan-id.wav, since it���s rather faint).
Don���t expect aplay to give good sound, though. There���s something
wrong with the current version of aplay.
The canyon.ogg file is to test loading of 6 channel oggs. After
patching and compiling, you can play it with playmus. (My version of
ogg123 will not play it, and I had to patch mplayer to get it to play
6 channel oggs.)
Greg Lee <greg@ling.lll.hawaii.edu>
Thus, July 1, 2004
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 21 Aug 2004 12:27:02 +0000 |
parents | 6e6248801043 |
children | c9b51268668f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998 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 5635-34 Springhouse Dr. | |
21 Pleasanton, CA 94588 (USA) | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
148
diff
changeset
|
22 slouken@libsdl.org |
0 | 23 */ |
24 | |
25 #ifdef SAVE_RCSID | |
26 static char rcsid = | |
27 "@(#) $Id$"; | |
28 #endif | |
29 | |
30 /* Allow access to a raw mixing buffer (For IRIX 6.5 and higher) */ | |
903
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
31 /* patch for IRIX 5 by Georg Schwarz 18/07/2004 */ |
0 | 32 |
33 #include <stdlib.h> | |
34 | |
35 #include "SDL_endian.h" | |
36 #include "SDL_timer.h" | |
37 #include "SDL_audio.h" | |
38 #include "SDL_audiomem.h" | |
39 #include "SDL_audio_c.h" | |
40 #include "SDL_irixaudio.h" | |
41 | |
42 | |
903
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
43 #ifndef AL_RESOURCE /* as a test whether we use the old IRIX audio libraries */ |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
44 #define OLD_IRIX_AUDIO |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
45 #define alClosePort(x) ALcloseport(x) |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
46 #define alFreeConfig(x) ALfreeconfig(x) |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
47 #define alGetFillable(x) ALgetfillable(x) |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
48 #define alNewConfig() ALnewconfig() |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
49 #define alOpenPort(x,y,z) ALopenport(x,y,z) |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
50 #define alSetChannels(x,y) ALsetchannels(x,y) |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
51 #define alSetQueueSize(x,y) ALsetqueuesize(x,y) |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
52 #define alSetSampFmt(x,y) ALsetsampfmt(x,y) |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
53 #define alSetWidth(x,y) ALsetwidth(x,y) |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
54 #endif |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
55 |
0 | 56 /* Audio driver functions */ |
57 static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
58 static void AL_WaitAudio(_THIS); | |
59 static void AL_PlayAudio(_THIS); | |
60 static Uint8 *AL_GetAudioBuf(_THIS); | |
61 static void AL_CloseAudio(_THIS); | |
62 | |
63 /* Audio driver bootstrap functions */ | |
64 | |
65 static int Audio_Available(void) | |
66 { | |
67 return 1; | |
68 } | |
69 | |
70 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
71 { | |
72 free(device->hidden); | |
73 free(device); | |
74 } | |
75 | |
76 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
77 { | |
78 SDL_AudioDevice *this; | |
79 | |
80 /* Initialize all variables that we clean on shutdown */ | |
81 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
82 if ( this ) { | |
83 memset(this, 0, (sizeof *this)); | |
84 this->hidden = (struct SDL_PrivateAudioData *) | |
85 malloc((sizeof *this->hidden)); | |
86 } | |
87 if ( (this == NULL) || (this->hidden == NULL) ) { | |
88 SDL_OutOfMemory(); | |
89 if ( this ) { | |
90 free(this); | |
91 } | |
92 return(0); | |
93 } | |
94 memset(this->hidden, 0, (sizeof *this->hidden)); | |
95 | |
96 /* Set the function pointers */ | |
97 this->OpenAudio = AL_OpenAudio; | |
98 this->WaitAudio = AL_WaitAudio; | |
99 this->PlayAudio = AL_PlayAudio; | |
100 this->GetAudioBuf = AL_GetAudioBuf; | |
101 this->CloseAudio = AL_CloseAudio; | |
102 | |
103 this->free = Audio_DeleteDevice; | |
104 | |
105 return this; | |
106 } | |
107 | |
148
8758b8d42cd9
Audio subsystem no longer assumes sun audio API on UNIX systems
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
108 AudioBootStrap DMEDIA_bootstrap = { |
0 | 109 "AL", "IRIX DMedia audio", |
110 Audio_Available, Audio_CreateDevice | |
111 }; | |
112 | |
113 | |
114 void static AL_WaitAudio(_THIS) | |
115 { | |
116 Sint32 timeleft; | |
117 | |
118 timeleft = this->spec.samples - alGetFillable(audio_port); | |
119 if ( timeleft > 0 ) { | |
120 timeleft /= (this->spec.freq/1000); | |
121 SDL_Delay((Uint32)timeleft); | |
122 } | |
123 } | |
124 | |
125 static void AL_PlayAudio(_THIS) | |
126 { | |
127 /* Write the audio data out */ | |
128 if ( alWriteFrames(audio_port, mixbuf, this->spec.samples) < 0 ) { | |
129 /* Assume fatal error, for now */ | |
130 this->enabled = 0; | |
131 } | |
132 } | |
133 | |
134 static Uint8 *AL_GetAudioBuf(_THIS) | |
135 { | |
136 return(mixbuf); | |
137 } | |
138 | |
139 static void AL_CloseAudio(_THIS) | |
140 { | |
141 if ( mixbuf != NULL ) { | |
142 SDL_FreeAudioMem(mixbuf); | |
143 mixbuf = NULL; | |
144 } | |
145 if ( audio_port != NULL ) { | |
605
6399f4e90211
IRIX patches from Andrea Suatoni
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
146 alClosePort(audio_port); |
0 | 147 audio_port = NULL; |
148 } | |
149 } | |
150 | |
151 static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
152 { | |
153 ALconfig audio_config; | |
903
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
154 #ifdef OLD_IRIX_AUDIO |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
155 long audio_param[2]; |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
156 #else |
0 | 157 ALpv audio_param; |
903
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
158 #endif |
0 | 159 int width; |
160 | |
161 /* Determine the audio parameters from the AudioSpec */ | |
162 switch ( spec->format & 0xFF ) { | |
163 | |
164 case 8: { /* Signed 8 bit audio data */ | |
165 spec->format = AUDIO_S8; | |
166 width = AL_SAMPLE_8; | |
167 } | |
168 break; | |
169 | |
170 case 16: { /* Signed 16 bit audio data */ | |
171 spec->format = AUDIO_S16MSB; | |
172 width = AL_SAMPLE_16; | |
173 } | |
174 break; | |
175 | |
176 default: { | |
177 SDL_SetError("Unsupported audio format"); | |
178 return(-1); | |
179 } | |
180 } | |
181 | |
182 /* Update the fragment size as size in bytes */ | |
183 SDL_CalculateAudioSpec(spec); | |
184 | |
185 /* Set output frequency */ | |
903
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
186 #ifdef OLD_IRIX_AUDIO |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
187 audio_param[0] = AL_OUTPUT_RATE; |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
188 audio_param[1] = spec->freq; |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
189 if( ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0 ) { |
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
190 #else |
0 | 191 audio_param.param = AL_RATE; |
192 audio_param.value.i = spec->freq; | |
193 if( alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0 ) { | |
903
6e6248801043
Date: Sun, 18 Jul 2004 16:46:44 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
605
diff
changeset
|
194 #endif |
0 | 195 SDL_SetError("alSetParams failed"); |
196 return(-1); | |
197 } | |
198 | |
199 /* Open the audio port with the requested frequency */ | |
200 audio_port = NULL; | |
201 audio_config = alNewConfig(); | |
202 if ( audio_config && | |
203 (alSetSampFmt(audio_config, AL_SAMPFMT_TWOSCOMP) >= 0) && | |
204 (alSetWidth(audio_config, width) >= 0) && | |
205 (alSetQueueSize(audio_config, spec->samples*2) >= 0) && | |
206 (alSetChannels(audio_config, spec->channels) >= 0) ) { | |
605
6399f4e90211
IRIX patches from Andrea Suatoni
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
207 audio_port = alOpenPort("SDL audio", "w", audio_config); |
0 | 208 } |
209 alFreeConfig(audio_config); | |
210 if( audio_port == NULL ) { | |
211 SDL_SetError("Unable to open audio port"); | |
212 return(-1); | |
213 } | |
214 | |
215 /* Allocate mixing buffer */ | |
216 mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size); | |
217 if ( mixbuf == NULL ) { | |
218 SDL_OutOfMemory(); | |
219 return(-1); | |
220 } | |
221 memset(mixbuf, spec->silence, spec->size); | |
222 | |
223 /* We're ready to rock and roll. :-) */ | |
224 return(0); | |
225 } |