]> git.xonotic.org Git - xonotic/darkplaces.git/blob - snd_sdl.c
patch from div to fix icc warnings
[xonotic/darkplaces.git] / snd_sdl.c
1 /*
2 Copyright (C) 2004 Andreas Kirsch
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18 */
19
20 #include <math.h>
21 #include <SDL.h>
22
23 #include "quakedef.h"
24 #include "snd_main.h"
25
26
27 static unsigned int sdlaudiotime = 0;
28
29
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)
32 {
33         unsigned int factor, RequestedFrames, MaxFrames, FrameCount;
34         unsigned int StartOffset, EndOffset;
35
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);
39
40         RequestedFrames = (unsigned int)len / factor;
41         
42         // Transfert up to a chunk of samples from snd_renderbuffer to stream
43         MaxFrames = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
44         if (MaxFrames > RequestedFrames)
45                 FrameCount = RequestedFrames;
46         else
47                 FrameCount = MaxFrames;
48         StartOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
49         EndOffset = (snd_renderbuffer->startframe + FrameCount) % snd_renderbuffer->maxframes;
50         if (StartOffset > EndOffset)  // if the buffer wraps
51         {
52                 unsigned int PartialLength1, PartialLength2;
53
54                 PartialLength1 = (snd_renderbuffer->maxframes - StartOffset) * factor;
55                 memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], PartialLength1);
56                 
57                 PartialLength2 = FrameCount * factor - PartialLength1;
58                 memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2);
59         }
60         else
61                 memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor);
62
63         snd_renderbuffer->startframe += FrameCount;
64
65         if (FrameCount < RequestedFrames && developer.integer >= 100)
66                 Con_DPrintf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount);
67
68         sdlaudiotime += RequestedFrames;
69 }
70
71
72 /*
73 ====================
74 SndSys_Init
75
76 Create "snd_renderbuffer" with the proper sound format if the call is successful
77 May return a suggested format if the requested format isn't available
78 ====================
79 */
80 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
81 {
82         unsigned int buffersize;
83         SDL_AudioSpec wantspec;
84         SDL_AudioSpec obtainspec;
85
86         Con_Print ("SndSys_Init: using the SDL module\n");
87
88         // Init the SDL Audio subsystem
89         if( SDL_InitSubSystem( SDL_INIT_AUDIO ) ) {
90                 Con_Print( "Initializing the SDL Audio subsystem failed!\n" );
91                 return false;
92         }
93
94         buffersize = (unsigned int)ceil((double)requested->speed / 20.0);
95
96         // Init the SDL Audio subsystem
97         wantspec.callback = Buffer_Callback;
98         wantspec.userdata = NULL;
99         wantspec.freq = requested->speed;
100         wantspec.format = ((requested->width == 1) ? AUDIO_U8 : AUDIO_S16SYS);
101         wantspec.channels = requested->channels;
102         wantspec.samples = CeilPowerOf2(buffersize);  // needs to be a power of 2 on some platforms.
103
104         Con_DPrintf("Wanted audio Specification:\n"
105                                 "\tChannels  : %i\n"
106                                 "\tFormat    : 0x%X\n"
107                                 "\tFrequency : %i\n"
108                                 "\tSamples   : %i\n",
109                                 wantspec.channels, wantspec.format, wantspec.freq, wantspec.samples);
110
111         if( SDL_OpenAudio( &wantspec, &obtainspec ) )
112         {       
113                 Con_Printf( "Failed to open the audio device! (%s)\n", SDL_GetError() );
114                 return false;
115         }
116
117         Con_DPrintf("Obtained audio specification:\n"
118                                 "\tChannels  : %i\n"
119                                 "\tFormat    : 0x%X\n"
120                                 "\tFrequency : %i\n"
121                                 "\tSamples   : %i\n",
122                                 obtainspec.channels, obtainspec.format, obtainspec.freq, obtainspec.samples);
123
124         // If we haven't obtained what we wanted
125         if (wantspec.freq != obtainspec.freq ||
126                 wantspec.format != obtainspec.format ||
127                 wantspec.channels != obtainspec.channels)
128         {
129                 SDL_CloseAudio();
130
131                 // Pass the obtained format as a suggested format
132                 if (suggested != NULL)
133                 {
134                         suggested->speed = obtainspec.freq;
135                         // FIXME: check the format more carefully. There are plenty of unsupported cases
136                         suggested->width = ((obtainspec.format == AUDIO_U8) ? 1 : 2);
137                         suggested->channels = obtainspec.channels;
138                 }
139
140                 return false;
141         }
142
143         snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
144         if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO)
145         {
146                 int newlayout;
147                 
148 #ifdef __linux__
149                 newlayout = SND_CHANNELLAYOUT_ALSA;
150 #else
151                 newlayout = SND_CHANNELLAYOUT_STANDARD;
152 #endif
153                 Cvar_SetValueQuick (&snd_channellayout, newlayout);
154         }
155
156         sdlaudiotime = 0;
157         SDL_PauseAudio( false );
158         
159         return true;
160 }
161
162
163 /*
164 ====================
165 SndSys_Shutdown
166
167 Stop the sound card, delete "snd_renderbuffer" and free its other resources
168 ====================
169 */
170 void SndSys_Shutdown(void)
171 {
172         SDL_CloseAudio();
173
174         if (snd_renderbuffer != NULL)
175         {
176                 Mem_Free(snd_renderbuffer->ring);
177                 Mem_Free(snd_renderbuffer);
178                 snd_renderbuffer = NULL;
179         }
180 }
181
182
183 /*
184 ====================
185 SndSys_Submit
186
187 Submit the contents of "snd_renderbuffer" to the sound card
188 ====================
189 */
190 void SndSys_Submit (void)
191 {
192         // Nothing to do here (this sound module is callback-based)
193 }
194
195
196 /*
197 ====================
198 SndSys_GetSoundTime
199
200 Returns the number of sample frames consumed since the sound started
201 ====================
202 */
203 unsigned int SndSys_GetSoundTime (void)
204 {
205         return sdlaudiotime;
206 }
207
208
209 /*
210 ====================
211 SndSys_LockRenderBuffer
212
213 Get the exclusive lock on "snd_renderbuffer"
214 ====================
215 */
216 qboolean SndSys_LockRenderBuffer (void)
217 {
218         SDL_LockAudio();
219         return true;
220 }
221
222
223 /*
224 ====================
225 SndSys_UnlockRenderBuffer
226
227 Release the exclusive lock on "snd_renderbuffer"
228 ====================
229 */
230 void SndSys_UnlockRenderBuffer (void)
231 {
232         SDL_UnlockAudio();
233 }