]> git.xonotic.org Git - xonotic/darkplaces.git/blob - snd_sdl.c
Remove the model parameter from NativeContentsFromSuperContents/SuperContentsFromNati...
[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 #include <math.h>
20 #include <SDL.h>
21
22 #include "quakedef.h"
23
24 #include "snd_main.h"
25
26
27 static unsigned int sdlaudiotime = 0;
28 static int audio_device = 0;
29
30
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)
33 {
34         unsigned int factor, RequestedFrames, MaxFrames, FrameCount;
35         unsigned int StartOffset, EndOffset;
36
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);
40
41         RequestedFrames = (unsigned int)len / factor;
42
43         if (SndSys_LockRenderBuffer())
44         {
45                 if (snd_usethreadedmixing)
46                 {
47                         S_MixToBuffer(stream, RequestedFrames);
48                         if (snd_blocked)
49                                 memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len);
50                         SndSys_UnlockRenderBuffer();
51                         return;
52                 }
53
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;
58                 else
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
63                 {
64                         unsigned int PartialLength1, PartialLength2;
65
66                         PartialLength1 = (snd_renderbuffer->maxframes - StartOffset) * factor;
67                         memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], PartialLength1);
68
69                         PartialLength2 = FrameCount * factor - PartialLength1;
70                         memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2);
71
72                         // As of SDL 2.0 buffer needs to be fully initialized, so fill leftover part with silence
73                         // FIXME this is another place that assumes 8bit is always unsigned and others always signed
74                         memset(&stream[PartialLength1 + PartialLength2], snd_renderbuffer->format.width == 1 ? 0x80 : 0, len - (PartialLength1 + PartialLength2));
75                 }
76                 else
77                 {
78                         memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor);
79
80                         // As of SDL 2.0 buffer needs to be fully initialized, so fill leftover part with silence
81                         // FIXME this is another place that assumes 8bit is always unsigned and others always signed
82                         memset(&stream[FrameCount * factor], snd_renderbuffer->format.width == 1 ? 0x80 : 0, len - (FrameCount * factor));
83                 }
84
85                 snd_renderbuffer->startframe += FrameCount;
86
87                 if (FrameCount < RequestedFrames && developer_insane.integer && vid_activewindow)
88                         Con_DPrintf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount);
89
90                 sdlaudiotime += RequestedFrames;
91
92                 SndSys_UnlockRenderBuffer();
93         }
94 }
95
96
97 /*
98 ====================
99 SndSys_Init
100
101 Create "snd_renderbuffer" with the proper sound format if the call is successful
102 May return a suggested format if the requested format isn't available
103 ====================
104 */
105 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
106 {
107         unsigned int buffersize;
108         SDL_AudioSpec wantspec;
109         SDL_AudioSpec obtainspec;
110
111         snd_threaded = false;
112
113         Con_DPrint ("SndSys_Init: using the SDL module\n");
114
115         // Init the SDL Audio subsystem
116         if( SDL_InitSubSystem( SDL_INIT_AUDIO ) ) {
117                 Con_Print( "Initializing the SDL Audio subsystem failed!\n" );
118                 return false;
119         }
120
121         buffersize = (unsigned int)ceil((double)requested->speed / 25.0); // 2048 bytes on 24kHz to 48kHz
122
123         // Init the SDL Audio subsystem
124         wantspec.callback = Buffer_Callback;
125         wantspec.userdata = NULL;
126         wantspec.freq = requested->speed;
127         wantspec.format = ((requested->width == 1) ? AUDIO_U8 : AUDIO_S16SYS);
128         wantspec.channels = requested->channels;
129         wantspec.samples = CeilPowerOf2(buffersize);  // needs to be a power of 2 on some platforms.
130
131         Con_Printf("Wanted audio Specification:\n"
132                                 "\tChannels  : %i\n"
133                                 "\tFormat    : 0x%X\n"
134                                 "\tFrequency : %i\n"
135                                 "\tSamples   : %i\n",
136                                 wantspec.channels, wantspec.format, wantspec.freq, wantspec.samples);
137
138         if ((audio_device = SDL_OpenAudioDevice(NULL, 0, &wantspec, &obtainspec, 0)) == 0)
139         {
140                 Con_Printf( "Failed to open the audio device! (%s)\n", SDL_GetError() );
141                 return false;
142         }
143
144         Con_Printf("Obtained audio specification:\n"
145                                 "\tChannels  : %i\n"
146                                 "\tFormat    : 0x%X\n"
147                                 "\tFrequency : %i\n"
148                                 "\tSamples   : %i\n",
149                                 obtainspec.channels, obtainspec.format, obtainspec.freq, obtainspec.samples);
150
151         // If we haven't obtained what we wanted
152         if (wantspec.freq != obtainspec.freq ||
153                 wantspec.format != obtainspec.format ||
154                 wantspec.channels != obtainspec.channels)
155         {
156                 SDL_CloseAudioDevice(audio_device);
157
158                 // Pass the obtained format as a suggested format
159                 if (suggested != NULL)
160                 {
161                         suggested->speed = obtainspec.freq;
162                         // FIXME: check the format more carefully. There are plenty of unsupported cases
163                         suggested->width = ((obtainspec.format == AUDIO_U8) ? 1 : 2);
164                         suggested->channels = obtainspec.channels;
165                 }
166
167                 return false;
168         }
169
170         snd_threaded = true;
171
172         snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
173         if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO)
174                 Cvar_SetValueQuick (&snd_channellayout, SND_CHANNELLAYOUT_STANDARD);
175
176         sdlaudiotime = 0;
177         SDL_PauseAudioDevice(audio_device, 0);
178
179         return true;
180 }
181
182
183 /*
184 ====================
185 SndSys_Shutdown
186
187 Stop the sound card, delete "snd_renderbuffer" and free its other resources
188 ====================
189 */
190 void SndSys_Shutdown(void)
191 {
192         if (audio_device > 0) {
193                 SDL_CloseAudioDevice(audio_device);
194                 audio_device = 0;
195         }
196         if (snd_renderbuffer != NULL)
197         {
198                 Mem_Free(snd_renderbuffer->ring);
199                 Mem_Free(snd_renderbuffer);
200                 snd_renderbuffer = NULL;
201         }
202 }
203
204
205 /*
206 ====================
207 SndSys_Submit
208
209 Submit the contents of "snd_renderbuffer" to the sound card
210 ====================
211 */
212 void SndSys_Submit (void)
213 {
214         // Nothing to do here (this sound module is callback-based)
215 }
216
217
218 /*
219 ====================
220 SndSys_GetSoundTime
221
222 Returns the number of sample frames consumed since the sound started
223 ====================
224 */
225 unsigned int SndSys_GetSoundTime (void)
226 {
227         return sdlaudiotime;
228 }
229
230
231 /*
232 ====================
233 SndSys_LockRenderBuffer
234
235 Get the exclusive lock on "snd_renderbuffer"
236 ====================
237 */
238 qboolean SndSys_LockRenderBuffer (void)
239 {
240         SDL_LockAudioDevice(audio_device);
241         return true;
242 }
243
244
245 /*
246 ====================
247 SndSys_UnlockRenderBuffer
248
249 Release the exclusive lock on "snd_renderbuffer"
250 ====================
251 */
252 void SndSys_UnlockRenderBuffer (void)
253 {
254         SDL_UnlockAudioDevice(audio_device);
255 }
256
257 /*
258 ====================
259 SndSys_SendKeyEvents
260
261 Send keyboard events originating from the sound system (e.g. MIDI)
262 ====================
263 */
264 void SndSys_SendKeyEvents(void)
265 {
266         // not supported
267 }