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 else if ((i = COM_CheckParm("-sndbits")) != 0)
100 shm->format.width = atoi(com_argv[i+1]) / 8;
102 if (shm->format.width != 2 && shm->format.width != 1)
104 ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
105 if (fmt & format16bit)
106 shm->format.width = 2;
107 else if (fmt & AFMT_U8)
108 shm->format.width = 1;
111 s = getenv("QUAKE_SOUND_SPEED");
113 shm->format.speed = atoi(s);
114 else if ((i = COM_CheckParm("-sndspeed")) != 0)
115 shm->format.speed = atoi(com_argv[i+1]);
118 for (i = 0;i < (int) sizeof(tryrates) / 4;i++)
119 if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
122 shm->format.speed = tryrates[i];
125 s = getenv("QUAKE_SOUND_CHANNELS");
127 shm->format.channels = atoi(s);
128 else if ((i = COM_CheckParm("-sndmono")) != 0)
129 shm->format.channels = 1;
130 else if ((i = COM_CheckParm("-sndstereo")) != 0)
131 shm->format.channels = 2;
133 shm->format.channels = 2;
135 shm->samples = info.fragstotal * info.fragsize / shm->format.width;
137 // memory map the dma buffer
138 shm->bufferlength = info.fragstotal * info.fragsize;
139 shm->buffer = (unsigned char *) mmap(NULL, shm->bufferlength, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
140 if (!shm->buffer || shm->buffer == (unsigned char *)-1)
143 Con_Print("Could not mmap /dev/dsp\n");
149 if (shm->format.channels == 2)
152 rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
156 Con_Printf("Could not set /dev/dsp to stereo=%d\n", shm->format.channels);
161 shm->format.channels = 2;
163 shm->format.channels = 1;
165 rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->format.speed);
169 Con_Printf("Could not set /dev/dsp speed to %d\n", shm->format.speed);
174 if (shm->format.width == 2)
177 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
181 Con_Print("Could not support 16-bit data. Try 8-bit.\n");
186 else if (shm->format.width == 1)
189 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
193 Con_Print("Could not support 8-bit data.\n");
201 Con_Printf("%d-bit sound not supported.\n", shm->format.width * 8);
206 // toggle the trigger & start her up
208 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
212 Con_Print("Could not toggle.\n");
216 tmp = PCM_ENABLE_OUTPUT;
217 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
221 Con_Print("Could not toggle.\n");
232 int SNDDMA_GetDMAPos(void)
235 struct count_info count;
237 if (!snd_inited) return 0;
239 if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
242 Con_Print("Uh, sound dead.\n");
247 shm->samplepos = count.ptr / shm->format.width;
249 return shm->samplepos;
252 void SNDDMA_Shutdown(void)
258 munmap(shm->buffer, shm->bufferlength);
261 ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
262 ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
274 Send sound to device if buffer isn't really the dma buffer
277 void SNDDMA_Submit(void)
281 void *S_LockBuffer(void)
286 void S_UnlockBuffer(void)