2 Copyright (C) 1996-1997 Id Software, Inc.
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.
21 // OSS module, used by Linux and FreeBSD
25 #include <sys/ioctl.h>
26 #include <sys/soundcard.h>
33 #define NB_FRAGMENTS 4
35 static int audio_fd = -1;
36 static int old_osstime = 0;
37 static unsigned int osssoundtime;
44 Create "snd_renderbuffer" with the proper sound format if the call is successful
45 May return a suggested format if the requested format isn't available
48 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
50 int flags, ioctl_param, prev_value;
51 unsigned int fragmentsize;
53 Con_DPrint("SndSys_Init: using the OSS module\n");
55 // Check the requested sound format
56 if (requested->width < 1 || requested->width > 2)
58 Con_Printf("SndSys_Init: invalid sound width (%hu)\n",
61 if (suggested != NULL)
63 memcpy(suggested, requested, sizeof(suggested));
65 if (requested->width < 1)
75 audio_fd = open("/dev/dsp", O_WRONLY);
79 Con_Print("SndSys_Init: could not open /dev/dsp\n");
83 // Use non-blocking IOs if possible
84 flags = fcntl(audio_fd, F_GETFL);
87 if (fcntl(audio_fd, F_SETFL, flags | O_NONBLOCK) == -1)
88 Con_Print("SndSys_Init : fcntl(F_SETFL, O_NONBLOCK) failed!\n");
91 Con_Print("SndSys_Init: fcntl(F_GETFL) failed!\n");
93 // Set the fragment size (up to "NB_FRAGMENTS" fragments of "fragmentsize" bytes)
94 fragmentsize = requested->speed * requested->channels * requested->width / 5;
95 fragmentsize = (unsigned int)ceilf((float)fragmentsize / (float)NB_FRAGMENTS);
96 fragmentsize = CeilPowerOf2(fragmentsize);
97 ioctl_param = (NB_FRAGMENTS << 16) | log2i(fragmentsize);
98 if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &ioctl_param) == -1)
100 Con_Print ("SndSys_Init: could not set the fragment size\n");
105 // Set the sound width
106 if (requested->width == 1)
107 ioctl_param = AFMT_U8;
109 ioctl_param = AFMT_S16_NE;
110 prev_value = ioctl_param;
111 if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &ioctl_param) == -1 ||
112 ioctl_param != prev_value)
114 if (ioctl_param != prev_value && suggested != NULL)
116 memcpy(suggested, requested, sizeof(suggested));
118 if (ioctl_param == AFMT_S16_NE)
119 suggested->width = 2;
121 suggested->width = 1;
124 Con_Printf("SndSys_Init: could not set the sound width to %hu\n",
130 // Set the sound channels
131 ioctl_param = requested->channels;
132 if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &ioctl_param) == -1 ||
133 ioctl_param != requested->channels)
135 if (ioctl_param != requested->channels && suggested != NULL)
137 memcpy(suggested, requested, sizeof(suggested));
138 suggested->channels = ioctl_param;
141 Con_Printf("SndSys_Init: could not set the number of channels to %hu\n",
142 requested->channels);
147 // Set the sound speed
148 ioctl_param = requested->speed;
149 if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &ioctl_param) == -1 ||
150 (unsigned int)ioctl_param != requested->speed)
152 if ((unsigned int)ioctl_param != requested->speed && suggested != NULL)
154 memcpy(suggested, requested, sizeof(suggested));
155 suggested->speed = ioctl_param;
158 Con_Printf("SndSys_Init: could not set the sound speed to %u\n",
165 alsaspeakerlayout = true;
167 alsaspeakerlayout = false;
172 snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
181 Stop the sound card, delete "snd_renderbuffer" and free its other resources
184 void SndSys_Shutdown (void)
186 // Stop the sound and close the device
189 ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
194 if (snd_renderbuffer != NULL)
196 Mem_Free(snd_renderbuffer->ring);
197 Mem_Free(snd_renderbuffer);
198 snd_renderbuffer = NULL;
207 Submit the contents of "snd_renderbuffer" to the sound card
210 void SndSys_Submit (void)
212 unsigned int startoffset, factor, limit, nbframes;
216 snd_renderbuffer->startframe == snd_renderbuffer->endframe)
219 startoffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
220 factor = snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
221 limit = snd_renderbuffer->maxframes - startoffset;
222 nbframes = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
223 if (nbframes > limit)
225 written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], limit * factor);
228 Con_Printf("SndSys_Submit: audio write returned %d!\n", written);
232 if (written % factor != 0)
233 Sys_Error("SndSys_Submit: nb of bytes written (%d) isn't aligned to a frame sample!\n", written);
235 snd_renderbuffer->startframe += written / factor;
237 if ((unsigned int)written < limit * factor)
239 Con_Printf("SndSys_Submit: audio can't keep up! (%u < %u)\n", written, limit * factor);
247 written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], nbframes * factor);
250 Con_Printf("SndSys_Submit: audio write returned %d!\n", written);
254 if (written % factor != 0)
255 Sys_Error("SndSys_Submit: nb of bytes written (%d) isn't aligned to a frame sample!\n", written);
257 snd_renderbuffer->startframe += written / factor;
259 if ((unsigned int)written < nbframes * factor)
260 Con_Printf("SndSys_Submit: audio can't keep up! (%u < %u)\n", written, nbframes * factor);
268 Returns the number of sample frames consumed since the sound started
271 unsigned int SndSys_GetSoundTime (void)
273 struct count_info count;
275 unsigned int timediff;
277 // TODO: use SNDCTL_DSP_GETODELAY instead
278 if (ioctl (audio_fd, SNDCTL_DSP_GETOPTR, &count) == -1)
280 Con_Print ("SndSys_GetSoundTimeDiff: can't ioctl (SNDCTL_DSP_GETOPTR)\n");
283 new_osstime = count.bytes / (snd_renderbuffer->format.width * snd_renderbuffer->format.channels);
285 if (new_osstime >= old_osstime)
286 timediff = new_osstime - old_osstime;
289 Con_Print ("SndSys_GetSoundTime: osstime wrapped\n");
293 old_osstime = new_osstime;
294 osssoundtime += timediff;
301 SndSys_LockRenderBuffer
303 Get the exclusive lock on "snd_renderbuffer"
306 qboolean SndSys_LockRenderBuffer (void)
315 SndSys_UnlockRenderBuffer
317 Release the exclusive lock on "snd_renderbuffer"
320 void SndSys_UnlockRenderBuffer (void)