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.
22 #include <sys/param.h>
23 #include <sys/audioio.h>
25 # include <sys/endian.h>
27 #include <sys/ioctl.h>
38 static int audio_fd = -1;
45 Create "snd_renderbuffer" with the proper sound format if the call is successful
46 May return a suggested format if the requested format isn't available
49 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
55 // Open the audio device
59 snddev = "/dev/audio";
61 snddev = "/dev/sound";
63 audio_fd = open (snddev, O_WRONLY | O_NDELAY | O_NONBLOCK);
66 Con_Printf("Can't open the sound device (%s)\n", snddev);
70 AUDIO_INITINFO (&info);
71 #ifdef AUMODE_PLAY // NetBSD / OpenBSD
72 info.mode = AUMODE_PLAY;
74 info.play.sample_rate = requested->speed;
75 info.play.channels = requested->channels;
76 info.play.precision = requested->width * 8;
77 if (requested->width == 1)
79 info.play.encoding = AUDIO_ENCODING_LINEAR8;
81 info.play.encoding = AUDIO_ENCODING_ULINEAR;
85 info.play.encoding = AUDIO_ENCODING_LINEAR;
88 info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
90 info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
93 if (ioctl (audio_fd, AUDIO_SETINFO, &info) != 0)
95 Con_Printf("Can't set up the sound device (%s)\n", snddev);
99 // TODO: check the parameters with AUDIO_GETINFO
100 // TODO: check AUDIO_ENCODINGFLAG_EMULATED with AUDIO_GETENC
102 snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
111 Stop the sound card, delete "snd_renderbuffer" and free its other resources
114 void SndSys_Shutdown (void)
122 if (snd_renderbuffer != NULL)
124 Mem_Free(snd_renderbuffer->ring);
125 Mem_Free(snd_renderbuffer);
126 snd_renderbuffer = NULL;
135 Submit the contents of "snd_renderbuffer" to the sound card
138 void SndSys_Submit (void)
140 unsigned int startoffset, factor, limit, nbframes;
144 snd_renderbuffer->startframe == snd_renderbuffer->endframe)
147 startoffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
148 factor = snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
149 limit = snd_renderbuffer->maxframes - startoffset;
150 nbframes = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
151 if (nbframes > limit)
153 written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], limit * factor);
156 Con_Printf("SndSys_Submit: audio write returned %d!\n", written);
160 if (written % factor != 0)
161 Sys_Error("SndSys_Submit: nb of bytes written (%d) isn't aligned to a frame sample!\n", written);
163 snd_renderbuffer->startframe += written / factor;
165 if ((unsigned int)written < limit * factor)
167 Con_Printf("SndSys_Submit: audio can't keep up! (%u < %u)\n", written, limit * factor);
175 written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], nbframes * factor);
178 Con_Printf("SndSys_Submit: audio write returned %d!\n", written);
181 snd_renderbuffer->startframe += written / factor;
189 Returns the number of sample frames consumed since the sound started
192 unsigned int SndSys_GetSoundTime (void)
196 if (ioctl (audio_fd, AUDIO_GETINFO, &info) < 0)
198 Con_Print("Error: can't get audio info\n");
203 return info.play.samples;
209 SndSys_LockRenderBuffer
211 Get the exclusive lock on "snd_renderbuffer"
214 qboolean SndSys_LockRenderBuffer (void)
223 SndSys_UnlockRenderBuffer
225 Release the exclusive lock on "snd_renderbuffer"
228 void SndSys_UnlockRenderBuffer (void)
237 Send keyboard events originating from the sound system (e.g. MIDI)
240 void SndSys_SendKeyEvents(void)