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>
36 static int tryrates[] = {44100, 22050, 11025, 8000};
38 qboolean SNDDMA_Init(void)
45 struct audio_buf_info info;
49 #if BYTE_ORDER == BIG_ENDIAN
50 format16bit = AFMT_S16_BE;
52 format16bit = AFMT_S16_LE;
56 // open /dev/dsp, confirm capability to mmap, and get size of dma buffer
57 audio_fd = open("/dev/dsp", O_RDWR);
61 Con_Print("Could not open /dev/dsp\n");
65 if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) < 0)
68 Con_Print("Could not reset /dev/dsp\n");
73 if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
76 Con_Print("Sound driver too old\n");
81 if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP))
83 Con_Print("Sorry but your soundcard can't do this\n");
88 if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
91 Con_Print("Um, can't do GETOSPACE?\n");
96 // set sample bits & speed
97 s = getenv("QUAKE_SOUND_SAMPLEBITS");
99 shm->format.width = atoi(s) / 8;
100 // COMMANDLINEOPTION: Linux OSS Sound: -sndbits <bits> chooses 8 bit or 16 bit sound output
101 else if ((i = COM_CheckParm("-sndbits")) != 0)
102 shm->format.width = atoi(com_argv[i+1]) / 8;
104 if (shm->format.width != 2 && shm->format.width != 1)
106 ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
107 if (fmt & format16bit)
108 shm->format.width = 2;
109 else if (fmt & AFMT_U8)
110 shm->format.width = 1;
113 s = getenv("QUAKE_SOUND_SPEED");
115 shm->format.speed = atoi(s);
116 // COMMANDLINEOPTION: Linux OSS Sound: -sndspeed <hz> chooses 44100 hz, 22100 hz, or 11025 hz sound output rate
117 else if ((i = COM_CheckParm("-sndspeed")) != 0)
118 shm->format.speed = atoi(com_argv[i+1]);
121 for (i = 0;i < (int) sizeof(tryrates) / 4;i++)
122 if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
125 shm->format.speed = tryrates[i];
128 s = getenv("QUAKE_SOUND_CHANNELS");
130 shm->format.channels = atoi(s);
131 // COMMANDLINEOPTION: Linux OSS Sound: -sndmono sets sound output to mono
132 else if ((i = COM_CheckParm("-sndmono")) != 0)
133 shm->format.channels = 1;
134 // COMMANDLINEOPTION: Linux OSS Sound: -sndstereo sets sound output to stereo
135 else if ((i = COM_CheckParm("-sndstereo")) != 0)
136 shm->format.channels = 2;
138 shm->format.channels = 2;
140 shm->samples = info.fragstotal * info.fragsize / shm->format.width;
142 // memory map the dma buffer
143 shm->bufferlength = info.fragstotal * info.fragsize;
144 shm->buffer = (unsigned char *) mmap(NULL, shm->bufferlength, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
145 if (!shm->buffer || shm->buffer == (unsigned char *)-1)
148 Con_Print("Could not mmap /dev/dsp\n");
154 if (shm->format.channels == 2)
157 rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
161 Con_Printf("Could not set /dev/dsp to stereo=%d\n", shm->format.channels);
166 shm->format.channels = 2;
168 shm->format.channels = 1;
170 rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->format.speed);
174 Con_Printf("Could not set /dev/dsp speed to %d\n", shm->format.speed);
179 if (shm->format.width == 2)
182 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
186 Con_Print("Could not support 16-bit data. Try 8-bit.\n");
191 else if (shm->format.width == 1)
194 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
198 Con_Print("Could not support 8-bit data.\n");
206 Con_Printf("%d-bit sound not supported.\n", shm->format.width * 8);
211 // toggle the trigger & start her up
213 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
217 Con_Print("Could not toggle.\n");
221 tmp = PCM_ENABLE_OUTPUT;
222 rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
226 Con_Print("Could not toggle.\n");
237 int SNDDMA_GetDMAPos(void)
240 struct count_info count;
242 if (!snd_inited) return 0;
244 if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
247 Con_Print("Uh, sound dead.\n");
252 shm->samplepos = count.ptr / shm->format.width;
254 return shm->samplepos;
257 void SNDDMA_Shutdown(void)
263 munmap(shm->buffer, shm->bufferlength);
266 ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
267 ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
279 Send sound to device if buffer isn't really the dma buffer
282 void SNDDMA_Submit(void)
286 void *S_LockBuffer(void)
291 void S_UnlockBuffer(void)