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 <linux/soundcard.h>
35 static int tryrates[] = { 11025, 22051, 44100, 8000 };
37 qboolean SNDDMA_Init(void)
45 struct audio_buf_info info;
50 // open /dev/dsp, confirm capability to mmap, and get size of dma buffer
52 audio_fd = open("/dev/dsp", O_RDWR);
56 Con_Printf("Could not open /dev/dsp\n");
60 rc = ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
64 Con_Printf("Could not reset /dev/dsp\n");
69 if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
72 Con_Printf("Sound driver too old\n");
77 if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP))
79 Con_Printf("Sorry but your soundcard can't do this\n");
84 if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
87 Con_Printf("Um, can't do GETOSPACE?\n");
95 // set sample bits & speed
97 s = getenv("QUAKE_SOUND_SAMPLEBITS");
98 if (s) shm->samplebits = atoi(s);
99 else if ((i = COM_CheckParm("-sndbits")) != 0)
100 shm->samplebits = atoi(com_argv[i+1]);
101 if (shm->samplebits != 16 && shm->samplebits != 8)
103 ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
104 if (fmt & AFMT_S16_LE) shm->samplebits = 16;
105 else if (fmt & AFMT_U8) shm->samplebits = 8;
108 s = getenv("QUAKE_SOUND_SPEED");
109 if (s) shm->speed = atoi(s);
110 else if ((i = COM_CheckParm("-sndspeed")) != 0)
111 shm->speed = atoi(com_argv[i+1]);
114 for (i=0 ; i<sizeof(tryrates)/4 ; i++)
115 if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i])) break;
116 shm->speed = tryrates[i];
119 s = getenv("QUAKE_SOUND_CHANNELS");
120 if (s) shm->channels = atoi(s);
121 else if ((i = COM_CheckParm("-sndmono")) != 0)
123 else if ((i = COM_CheckParm("-sndstereo")) != 0)
125 else shm->channels = 2;
127 shm->samples = info.fragstotal * info.fragsize / (shm->samplebits/8);
128 shm->submission_chunk = 1;
130 // memory map the dma buffer
132 shm->buffer = (unsigned char *) mmap(NULL, info.fragstotal
133 * info.fragsize, PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
134 if (!shm->buffer || shm->buffer == (unsigned char *)-1)
137 Con_Printf("Could not mmap /dev/dsp\n");
143 if (shm->channels == 2)
145 rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
149 Con_Printf("Could not set /dev/dsp to stereo=%d", shm->channels);
158 rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->speed);
162 Con_Printf("Could not set /dev/dsp speed to %d", shm->speed);
167 if (shm->samplebits == 16)
170 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
174 Con_Printf("Could not support 16-bit data. Try 8-bit.\n");
179 else if (shm->samplebits == 8)
182 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
186 Con_Printf("Could not support 8-bit data.\n");
194 Con_Printf("%d-bit sound not supported.", shm->samplebits);
199 // toggle the trigger & start her up
202 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
206 Con_Printf("Could not toggle.\n");
210 tmp = PCM_ENABLE_OUTPUT;
211 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
215 Con_Printf("Could not toggle.\n");
227 int SNDDMA_GetDMAPos(void)
230 struct count_info count;
232 if (!snd_inited) return 0;
234 if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
237 Con_Printf("Uh, sound dead.\n");
242 shm->samplepos = count.ptr / (shm->samplebits / 8);
244 return shm->samplepos;
248 void SNDDMA_Shutdown(void)
261 Send sound to device if buffer isn't really the dma buffer
264 void SNDDMA_Submit(void)
268 void *S_LockBuffer(void)
273 void S_UnlockBuffer(void)