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;
28 static int audio_device = 0;
31 // Note: SDL calls SDL_LockAudio() right before this function, so no need to lock the audio data here
32 static void Buffer_Callback (void *userdata, Uint8 *stream, int len)
34 unsigned int factor, RequestedFrames, MaxFrames, FrameCount;
35 unsigned int StartOffset, EndOffset;
37 factor = snd_renderbuffer->format.channels * snd_renderbuffer->format.width;
38 if ((unsigned int)len % factor != 0)
39 Sys_Error("SDL sound: invalid buffer length passed to Buffer_Callback (%d bytes)\n", len);
41 RequestedFrames = (unsigned int)len / factor;
43 if (SndSys_LockRenderBuffer())
45 if (snd_usethreadedmixing)
47 S_MixToBuffer(stream, RequestedFrames);
49 memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len);
50 SndSys_UnlockRenderBuffer();
54 // Transfert up to a chunk of samples from snd_renderbuffer to stream
55 MaxFrames = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
56 if (MaxFrames > RequestedFrames)
57 FrameCount = RequestedFrames;
59 FrameCount = MaxFrames;
60 StartOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
61 EndOffset = (snd_renderbuffer->startframe + FrameCount) % snd_renderbuffer->maxframes;
62 if (StartOffset > EndOffset) // if the buffer wraps
64 unsigned int PartialLength1, PartialLength2;
66 PartialLength1 = (snd_renderbuffer->maxframes - StartOffset) * factor;
67 memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], PartialLength1);
69 PartialLength2 = FrameCount * factor - PartialLength1;
70 memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2);
73 memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor);
75 snd_renderbuffer->startframe += FrameCount;
77 if (FrameCount < RequestedFrames && developer_insane.integer && vid_activewindow)
78 Con_DPrintf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount);
80 sdlaudiotime += RequestedFrames;
82 SndSys_UnlockRenderBuffer();
91 Create "snd_renderbuffer" with the proper sound format if the call is successful
92 May return a suggested format if the requested format isn't available
95 qboolean SndSys_Init (snd_format_t* fmt)
97 unsigned int buffersize;
98 SDL_AudioSpec wantspec;
99 SDL_AudioSpec obtainspec;
101 snd_threaded = false;
103 Con_DPrint ("SndSys_Init: using the SDL module\n");
105 // Init the SDL Audio subsystem
106 if( SDL_InitSubSystem( SDL_INIT_AUDIO ) ) {
107 Con_Print( "Initializing the SDL Audio subsystem failed!\n" );
111 buffersize = (unsigned int)ceil((double)fmt->speed / 25.0); // 2048 bytes on 24kHz to 48kHz
113 // Init the SDL Audio subsystem
114 memset(&wantspec, 0, sizeof(wantspec));
115 wantspec.callback = Buffer_Callback;
116 wantspec.userdata = NULL;
117 wantspec.freq = fmt->speed;
118 wantspec.format = fmt->width == 1 ? AUDIO_U8 : (fmt->width == 2 ? AUDIO_S16SYS : AUDIO_F32);
119 wantspec.channels = fmt->channels;
120 wantspec.samples = CeilPowerOf2(buffersize); // needs to be a power of 2 on some platforms.
122 Con_Printf("Wanted audio Specification:\n"
127 wantspec.channels, wantspec.format, wantspec.freq, wantspec.samples);
129 if ((audio_device = SDL_OpenAudioDevice(NULL, 0, &wantspec, &obtainspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE)) == 0)
131 Con_Printf( "Failed to open the audio device! (%s)\n", SDL_GetError() );
135 Con_Printf("Obtained audio specification:\n"
140 obtainspec.channels, obtainspec.format, obtainspec.freq, obtainspec.samples);
142 fmt->speed = obtainspec.freq;
143 fmt->channels = obtainspec.channels;
147 snd_renderbuffer = Snd_CreateRingBuffer(fmt, 0, NULL);
148 if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO)
149 Cvar_SetValueQuick (&snd_channellayout, SND_CHANNELLAYOUT_STANDARD);
152 SDL_PauseAudioDevice(audio_device, 0);
162 Stop the sound card, delete "snd_renderbuffer" and free its other resources
165 void SndSys_Shutdown(void)
167 if (audio_device > 0) {
168 SDL_CloseAudioDevice(audio_device);
171 if (snd_renderbuffer != NULL)
173 Mem_Free(snd_renderbuffer->ring);
174 Mem_Free(snd_renderbuffer);
175 snd_renderbuffer = NULL;
184 Submit the contents of "snd_renderbuffer" to the sound card
187 void SndSys_Submit (void)
189 // Nothing to do here (this sound module is callback-based)
197 Returns the number of sample frames consumed since the sound started
200 unsigned int SndSys_GetSoundTime (void)
208 SndSys_LockRenderBuffer
210 Get the exclusive lock on "snd_renderbuffer"
213 qboolean SndSys_LockRenderBuffer (void)
215 SDL_LockAudioDevice(audio_device);
222 SndSys_UnlockRenderBuffer
224 Release the exclusive lock on "snd_renderbuffer"
227 void SndSys_UnlockRenderBuffer (void)
229 SDL_UnlockAudioDevice(audio_device);
236 Send keyboard events originating from the sound system (e.g. MIDI)
239 void SndSys_SendKeyEvents(void)