2 Copyright (C) 2004 Andreas Kirsch
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 static unsigned int sdlaudiotime = 0;
30 // Note: SDL calls SDL_LockAudio() right before this function, so no need to lock the audio data here
31 static void Buffer_Callback (void *userdata, Uint8 *stream, int len)
33 unsigned int factor, RequestedFrames, MaxFrames, FrameCount;
34 unsigned int StartOffset, EndOffset;
36 factor = snd_renderbuffer->format.channels * snd_renderbuffer->format.width;
37 if ((unsigned int)len % factor != 0)
38 Sys_Error("SDL sound: invalid buffer length passed to Buffer_Callback (%d bytes)\n", len);
40 RequestedFrames = (unsigned int)len / factor;
42 if (snd_usethreadedmixing)
44 S_MixToBuffer(stream, RequestedFrames);
46 memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len);
50 // Transfert up to a chunk of samples from snd_renderbuffer to stream
51 MaxFrames = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
52 if (MaxFrames > RequestedFrames)
53 FrameCount = RequestedFrames;
55 FrameCount = MaxFrames;
56 StartOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
57 EndOffset = (snd_renderbuffer->startframe + FrameCount) % snd_renderbuffer->maxframes;
58 if (StartOffset > EndOffset) // if the buffer wraps
60 unsigned int PartialLength1, PartialLength2;
62 PartialLength1 = (snd_renderbuffer->maxframes - StartOffset) * factor;
63 memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], PartialLength1);
65 PartialLength2 = FrameCount * factor - PartialLength1;
66 memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2);
69 memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor);
71 snd_renderbuffer->startframe += FrameCount;
73 if (FrameCount < RequestedFrames && developer.integer >= 1000 && vid_activewindow)
74 Con_Printf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount);
76 sdlaudiotime += RequestedFrames;
84 Create "snd_renderbuffer" with the proper sound format if the call is successful
85 May return a suggested format if the requested format isn't available
88 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
90 unsigned int buffersize;
91 SDL_AudioSpec wantspec;
92 SDL_AudioSpec obtainspec;
96 Con_DPrint ("SndSys_Init: using the SDL module\n");
98 // Init the SDL Audio subsystem
99 if( SDL_InitSubSystem( SDL_INIT_AUDIO ) ) {
100 Con_Print( "Initializing the SDL Audio subsystem failed!\n" );
104 buffersize = (unsigned int)ceil((double)requested->speed / 25.0); // 2048 bytes on 24kHz to 48kHz
106 // Init the SDL Audio subsystem
107 wantspec.callback = Buffer_Callback;
108 wantspec.userdata = NULL;
109 wantspec.freq = requested->speed;
110 wantspec.format = ((requested->width == 1) ? AUDIO_U8 : AUDIO_S16SYS);
111 wantspec.channels = requested->channels;
112 wantspec.samples = CeilPowerOf2(buffersize); // needs to be a power of 2 on some platforms.
114 Con_Printf("Wanted audio Specification:\n"
119 wantspec.channels, wantspec.format, wantspec.freq, wantspec.samples);
121 if( SDL_OpenAudio( &wantspec, &obtainspec ) )
123 Con_Printf( "Failed to open the audio device! (%s)\n", SDL_GetError() );
127 Con_Printf("Obtained audio specification:\n"
132 obtainspec.channels, obtainspec.format, obtainspec.freq, obtainspec.samples);
134 // If we haven't obtained what we wanted
135 if (wantspec.freq != obtainspec.freq ||
136 wantspec.format != obtainspec.format ||
137 wantspec.channels != obtainspec.channels)
141 // Pass the obtained format as a suggested format
142 if (suggested != NULL)
144 suggested->speed = obtainspec.freq;
145 // FIXME: check the format more carefully. There are plenty of unsupported cases
146 suggested->width = ((obtainspec.format == AUDIO_U8) ? 1 : 2);
147 suggested->channels = obtainspec.channels;
155 snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
156 if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO)
161 newlayout = SND_CHANNELLAYOUT_ALSA;
163 newlayout = SND_CHANNELLAYOUT_STANDARD;
165 Cvar_SetValueQuick (&snd_channellayout, newlayout);
169 SDL_PauseAudio( false );
179 Stop the sound card, delete "snd_renderbuffer" and free its other resources
182 void SndSys_Shutdown(void)
186 if (snd_renderbuffer != NULL)
188 Mem_Free(snd_renderbuffer->ring);
189 Mem_Free(snd_renderbuffer);
190 snd_renderbuffer = NULL;
199 Submit the contents of "snd_renderbuffer" to the sound card
202 void SndSys_Submit (void)
204 // Nothing to do here (this sound module is callback-based)
212 Returns the number of sample frames consumed since the sound started
215 unsigned int SndSys_GetSoundTime (void)
223 SndSys_LockRenderBuffer
225 Get the exclusive lock on "snd_renderbuffer"
228 qboolean SndSys_LockRenderBuffer (void)
237 SndSys_UnlockRenderBuffer
239 Release the exclusive lock on "snd_renderbuffer"
242 void SndSys_UnlockRenderBuffer (void)