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, 22051, 11025, 8000};
37 qboolean SNDDMA_Init(void)
44 struct audio_buf_info info;
47 // LordHavoc: a quick patch to support big endian cpu, I hope
56 format16bit = AFMT_S16_BE;
58 format16bit = AFMT_S16_LE;
62 // open /dev/dsp, confirm capability to mmap, and get size of dma buffer
63 audio_fd = open("/dev/dsp", O_RDWR);
67 Con_Print("Could not open /dev/dsp\n");
71 if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) < 0)
74 Con_Print("Could not reset /dev/dsp\n");
79 if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
82 Con_Print("Sound driver too old\n");
87 if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP))
89 Con_Print("Sorry but your soundcard can't do this\n");
94 if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
97 Con_Print("Um, can't do GETOSPACE?\n");
102 // set sample bits & speed
103 s = getenv("QUAKE_SOUND_SAMPLEBITS");
105 shm->samplebits = atoi(s);
106 else if ((i = COM_CheckParm("-sndbits")) != 0)
107 shm->samplebits = atoi(com_argv[i+1]);
109 if (shm->samplebits != 16 && shm->samplebits != 8)
111 ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
112 if (fmt & format16bit)
113 shm->samplebits = 16;
114 else if (fmt & AFMT_U8)
118 s = getenv("QUAKE_SOUND_SPEED");
120 shm->speed = atoi(s);
121 else if ((i = COM_CheckParm("-sndspeed")) != 0)
122 shm->speed = atoi(com_argv[i+1]);
125 for (i = 0;i < (int) sizeof(tryrates) / 4;i++)
126 if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
129 shm->speed = tryrates[i];
132 s = getenv("QUAKE_SOUND_CHANNELS");
134 shm->channels = atoi(s);
135 else if ((i = COM_CheckParm("-sndmono")) != 0)
137 else if ((i = COM_CheckParm("-sndstereo")) != 0)
142 shm->samples = info.fragstotal * info.fragsize / (shm->samplebits/8);
144 // memory map the dma buffer
145 shm->bufferlength = info.fragstotal * info.fragsize;
146 shm->buffer = (unsigned char *) mmap(NULL, shm->bufferlength, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
147 if (!shm->buffer || shm->buffer == (unsigned char *)-1)
150 Con_Print("Could not mmap /dev/dsp\n");
156 if (shm->channels == 2)
159 rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
163 Con_Printf("Could not set /dev/dsp to stereo=%d\n", shm->channels);
172 rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->speed);
176 Con_Printf("Could not set /dev/dsp speed to %d\n", shm->speed);
181 if (shm->samplebits == 16)
184 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
188 Con_Print("Could not support 16-bit data. Try 8-bit.\n");
193 else if (shm->samplebits == 8)
196 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
200 Con_Print("Could not support 8-bit data.\n");
208 Con_Printf("%d-bit sound not supported.\n", shm->samplebits);
213 // toggle the trigger & start her up
215 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
219 Con_Print("Could not toggle.\n");
223 tmp = PCM_ENABLE_OUTPUT;
224 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
228 Con_Print("Could not toggle.\n");
239 int SNDDMA_GetDMAPos(void)
242 struct count_info count;
244 if (!snd_inited) return 0;
246 if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
249 Con_Print("Uh, sound dead.\n");
254 shm->samplepos = count.ptr / (shm->samplebits / 8);
256 return shm->samplepos;
259 void SNDDMA_Shutdown(void)
265 munmap(shm->buffer, shm->bufferlength);
268 ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
269 ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
281 Send sound to device if buffer isn't really the dma buffer
284 void SNDDMA_Submit(void)
288 void *S_LockBuffer(void)
293 void S_UnlockBuffer(void)