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.
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
28 #include <sys/soundcard.h>
35 static int tryrates[] = {44100, 22050, 11025, 8000};
37 qboolean SNDDMA_Init(void)
44 struct audio_buf_info info;
48 #if BYTE_ORDER == BIG_ENDIAN
49 format16bit = AFMT_S16_BE;
51 format16bit = AFMT_S16_LE;
55 // open /dev/dsp, confirm capability to mmap, and get size of dma buffer
56 audio_fd = open("/dev/dsp", O_RDWR);
60 Con_Print("Could not open /dev/dsp\n");
64 if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) < 0)
67 Con_Print("Could not reset /dev/dsp\n");
72 if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
75 Con_Print("Sound driver too old\n");
80 if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP))
82 Con_Print("Sorry but your soundcard can't do this\n");
87 if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
90 Con_Print("Um, can't do GETOSPACE?\n");
95 // set sample bits & speed
96 s = getenv("QUAKE_SOUND_SAMPLEBITS");
98 shm->format.width = atoi(s) / 8;
99 // COMMANDLINEOPTION: -sndbits <bits> chooses 8 bit or 16 bit sound output
100 else if ((i = COM_CheckParm("-sndbits")) != 0)
101 shm->format.width = atoi(com_argv[i+1]) / 8;
103 if (shm->format.width != 2 && shm->format.width != 1)
105 ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
106 if (fmt & format16bit)
107 shm->format.width = 2;
108 else if (fmt & AFMT_U8)
109 shm->format.width = 1;
112 s = getenv("QUAKE_SOUND_SPEED");
114 shm->format.speed = atoi(s);
115 // COMMANDLINEOPTION: -sndspeed <hz> chooses 44100 hz, 22100 hz, or 11025 hz sound output rate
116 else if ((i = COM_CheckParm("-sndspeed")) != 0)
117 shm->format.speed = atoi(com_argv[i+1]);
120 for (i = 0;i < (int) sizeof(tryrates) / 4;i++)
121 if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
124 shm->format.speed = tryrates[i];
127 s = getenv("QUAKE_SOUND_CHANNELS");
129 shm->format.channels = atoi(s);
130 // COMMANDLINEOPTION: -sndmono sets sound output to mono
131 else if ((i = COM_CheckParm("-sndmono")) != 0)
132 shm->format.channels = 1;
133 // COMMANDLINEOPTION: -sndstereo sets sound output to stereo
134 else if ((i = COM_CheckParm("-sndstereo")) != 0)
135 shm->format.channels = 2;
137 shm->format.channels = 2;
139 shm->samples = info.fragstotal * info.fragsize / shm->format.width;
141 // memory map the dma buffer
142 shm->bufferlength = info.fragstotal * info.fragsize;
143 shm->buffer = (unsigned char *) mmap(NULL, shm->bufferlength, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
144 if (!shm->buffer || shm->buffer == (unsigned char *)-1)
147 Con_Print("Could not mmap /dev/dsp\n");
153 if (shm->format.channels == 2)
156 rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
160 Con_Printf("Could not set /dev/dsp to stereo=%d\n", shm->format.channels);
165 shm->format.channels = 2;
167 shm->format.channels = 1;
169 rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->format.speed);
173 Con_Printf("Could not set /dev/dsp speed to %d\n", shm->format.speed);
178 if (shm->format.width == 2)
181 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
185 Con_Print("Could not support 16-bit data. Try 8-bit.\n");
190 else if (shm->format.width == 1)
193 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
197 Con_Print("Could not support 8-bit data.\n");
205 Con_Printf("%d-bit sound not supported.\n", shm->format.width * 8);
210 // toggle the trigger & start her up
212 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
216 Con_Print("Could not toggle.\n");
220 tmp = PCM_ENABLE_OUTPUT;
221 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
225 Con_Print("Could not toggle.\n");
236 int SNDDMA_GetDMAPos(void)
239 struct count_info count;
241 if (!snd_inited) return 0;
243 if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
246 Con_Print("Uh, sound dead.\n");
251 shm->samplepos = count.ptr / shm->format.width;
253 return shm->samplepos;
256 void SNDDMA_Shutdown(void)
262 munmap(shm->buffer, shm->bufferlength);
265 ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
266 ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
278 Send sound to device if buffer isn't really the dma buffer
281 void SNDDMA_Submit(void)
285 void *S_LockBuffer(void)
290 void S_UnlockBuffer(void)