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 (SndSys_LockRenderBuffer())
44 if (snd_usethreadedmixing)
46 S_MixToBuffer(stream, RequestedFrames);
48 memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len);
49 SndSys_UnlockRenderBuffer();
53 // Transfert up to a chunk of samples from snd_renderbuffer to stream
54 MaxFrames = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
55 if (MaxFrames > RequestedFrames)
56 FrameCount = RequestedFrames;
58 FrameCount = MaxFrames;
59 StartOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
60 EndOffset = (snd_renderbuffer->startframe + FrameCount) % snd_renderbuffer->maxframes;
61 if (StartOffset > EndOffset) // if the buffer wraps
63 unsigned int PartialLength1, PartialLength2;
65 PartialLength1 = (snd_renderbuffer->maxframes - StartOffset) * factor;
66 memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], PartialLength1);
68 PartialLength2 = FrameCount * factor - PartialLength1;
69 memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2);
72 memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor);
74 snd_renderbuffer->startframe += FrameCount;
76 if (FrameCount < RequestedFrames && developer_insane.integer && vid_activewindow)
77 Con_DPrintf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount);
79 sdlaudiotime += RequestedFrames;
81 SndSys_UnlockRenderBuffer();
90 Create "snd_renderbuffer" with the proper sound format if the call is successful
91 May return a suggested format if the requested format isn't available
94 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
96 unsigned int buffersize;
97 SDL_AudioSpec wantspec;
98 SDL_AudioSpec obtainspec;
100 snd_threaded = false;
102 Con_DPrint ("SndSys_Init: using the SDL module\n");
104 // Init the SDL Audio subsystem
105 if( SDL_InitSubSystem( SDL_INIT_AUDIO ) ) {
106 Con_Print( "Initializing the SDL Audio subsystem failed!\n" );
110 buffersize = (unsigned int)ceil((double)requested->speed / 25.0); // 2048 bytes on 24kHz to 48kHz
112 // Init the SDL Audio subsystem
113 wantspec.callback = Buffer_Callback;
114 wantspec.userdata = NULL;
115 wantspec.freq = requested->speed;
116 wantspec.format = ((requested->width == 1) ? AUDIO_U8 : AUDIO_S16SYS);
117 wantspec.channels = requested->channels;
118 wantspec.samples = CeilPowerOf2(buffersize); // needs to be a power of 2 on some platforms.
120 Con_Printf("Wanted audio Specification:\n"
125 wantspec.channels, wantspec.format, wantspec.freq, wantspec.samples);
127 if( SDL_OpenAudio( &wantspec, &obtainspec ) )
129 Con_Printf( "Failed to open the audio device! (%s)\n", SDL_GetError() );
133 Con_Printf("Obtained audio specification:\n"
138 obtainspec.channels, obtainspec.format, obtainspec.freq, obtainspec.samples);
140 // If we haven't obtained what we wanted
141 if (wantspec.freq != obtainspec.freq ||
142 wantspec.format != obtainspec.format ||
143 wantspec.channels != obtainspec.channels)
147 // Pass the obtained format as a suggested format
148 if (suggested != NULL)
150 suggested->speed = obtainspec.freq;
151 // FIXME: check the format more carefully. There are plenty of unsupported cases
152 suggested->width = ((obtainspec.format == AUDIO_U8) ? 1 : 2);
153 suggested->channels = obtainspec.channels;
161 snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
162 if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO)
163 Cvar_SetValueQuick (&snd_channellayout, SND_CHANNELLAYOUT_STANDARD);
166 SDL_PauseAudio( false );
176 Stop the sound card, delete "snd_renderbuffer" and free its other resources
179 void SndSys_Shutdown(void)
183 if (snd_renderbuffer != NULL)
185 Mem_Free(snd_renderbuffer->ring);
186 Mem_Free(snd_renderbuffer);
187 snd_renderbuffer = NULL;
196 Submit the contents of "snd_renderbuffer" to the sound card
199 void SndSys_Submit (void)
201 // Nothing to do here (this sound module is callback-based)
209 Returns the number of sample frames consumed since the sound started
212 unsigned int SndSys_GetSoundTime (void)
220 SndSys_LockRenderBuffer
222 Get the exclusive lock on "snd_renderbuffer"
225 qboolean SndSys_LockRenderBuffer (void)
234 SndSys_UnlockRenderBuffer
236 Release the exclusive lock on "snd_renderbuffer"
239 void SndSys_UnlockRenderBuffer (void)
248 Send keyboard events originating from the sound system (e.g. MIDI)
251 void SndSys_SendKeyEvents(void)