]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - snd_oss.c
added COMMANDLINEOPTION comments for every commandline option, these will be listed...
[xonotic/darkplaces.git] / snd_oss.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <sys/mman.h>
26 #include <sys/shm.h>
27 #include <sys/wait.h>
28 #include <sys/soundcard.h>
29 #include <stdio.h>
30 #include "quakedef.h"
31
32 int audio_fd;
33 int snd_inited;
34
35 static int tryrates[] = {44100, 22050, 11025, 8000};
36
37 qboolean SNDDMA_Init(void)
38 {
39         int rc;
40         int fmt;
41         int tmp;
42         int i;
43         char *s;
44         struct audio_buf_info info;
45         int caps;
46         int format16bit;
47
48 #if BYTE_ORDER == BIG_ENDIAN
49         format16bit = AFMT_S16_BE;
50 #else
51         format16bit = AFMT_S16_LE;
52 #endif
53         snd_inited = 0;
54
55         // open /dev/dsp, confirm capability to mmap, and get size of dma buffer
56     audio_fd = open("/dev/dsp", O_RDWR);
57         if (audio_fd < 0)
58         {
59                 perror("/dev/dsp");
60                 Con_Print("Could not open /dev/dsp\n");
61                 return 0;
62         }
63
64         if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) < 0)
65         {
66                 perror("/dev/dsp");
67                 Con_Print("Could not reset /dev/dsp\n");
68                 close(audio_fd);
69                 return 0;
70         }
71
72         if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
73         {
74                 perror("/dev/dsp");
75                 Con_Print("Sound driver too old\n");
76                 close(audio_fd);
77                 return 0;
78         }
79
80         if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP))
81         {
82                 Con_Print("Sorry but your soundcard can't do this\n");
83                 close(audio_fd);
84                 return 0;
85         }
86
87         if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
88         {
89                 perror("GETOSPACE");
90                 Con_Print("Um, can't do GETOSPACE?\n");
91                 close(audio_fd);
92                 return 0;
93         }
94
95         // set sample bits & speed
96         s = getenv("QUAKE_SOUND_SAMPLEBITS");
97         if (s)
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;
102
103         if (shm->format.width != 2 && shm->format.width != 1)
104         {
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;
110     }
111
112         s = getenv("QUAKE_SOUND_SPEED");
113         if (s)
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]);
118         else
119         {
120                 for (i = 0;i < (int) sizeof(tryrates) / 4;i++)
121                         if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
122                                 break;
123
124                 shm->format.speed = tryrates[i];
125     }
126
127         s = getenv("QUAKE_SOUND_CHANNELS");
128         if (s)
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;
136         else
137                 shm->format.channels = 2;
138
139         shm->samples = info.fragstotal * info.fragsize / shm->format.width;
140
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)
145         {
146                 perror("/dev/dsp");
147                 Con_Print("Could not mmap /dev/dsp\n");
148                 close(audio_fd);
149                 return 0;
150         }
151
152         tmp = 0;
153         if (shm->format.channels == 2)
154                 tmp = 1;
155
156         rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
157         if (rc < 0)
158         {
159                 perror("/dev/dsp");
160                 Con_Printf("Could not set /dev/dsp to stereo=%d\n", shm->format.channels);
161                 close(audio_fd);
162                 return 0;
163         }
164         if (tmp)
165                 shm->format.channels = 2;
166         else
167                 shm->format.channels = 1;
168
169         rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->format.speed);
170         if (rc < 0)
171         {
172                 perror("/dev/dsp");
173                 Con_Printf("Could not set /dev/dsp speed to %d\n", shm->format.speed);
174                 close(audio_fd);
175                 return 0;
176         }
177
178         if (shm->format.width == 2)
179         {
180                 rc = format16bit;
181                 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
182                 if (rc < 0)
183                 {
184                         perror("/dev/dsp");
185                         Con_Print("Could not support 16-bit data.  Try 8-bit.\n");
186                         close(audio_fd);
187                         return 0;
188                 }
189         }
190         else if (shm->format.width == 1)
191         {
192                 rc = AFMT_U8;
193                 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
194                 if (rc < 0)
195                 {
196                         perror("/dev/dsp");
197                         Con_Print("Could not support 8-bit data.\n");
198                         close(audio_fd);
199                         return 0;
200                 }
201         }
202         else
203         {
204                 perror("/dev/dsp");
205                 Con_Printf("%d-bit sound not supported.\n", shm->format.width * 8);
206                 close(audio_fd);
207                 return 0;
208         }
209
210         // toggle the trigger & start her up
211         tmp = 0;
212         rc  = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
213         if (rc < 0)
214         {
215                 perror("/dev/dsp");
216                 Con_Print("Could not toggle.\n");
217                 close(audio_fd);
218                 return 0;
219         }
220         tmp = PCM_ENABLE_OUTPUT;
221         rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
222         if (rc < 0)
223         {
224                 perror("/dev/dsp");
225                 Con_Print("Could not toggle.\n");
226                 close(audio_fd);
227                 return 0;
228         }
229
230         shm->samplepos = 0;
231
232         snd_inited = 1;
233         return 1;
234 }
235
236 int SNDDMA_GetDMAPos(void)
237 {
238
239         struct count_info count;
240
241         if (!snd_inited) return 0;
242
243         if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
244         {
245                 perror("/dev/dsp");
246                 Con_Print("Uh, sound dead.\n");
247                 close(audio_fd);
248                 snd_inited = 0;
249                 return 0;
250         }
251         shm->samplepos = count.ptr / shm->format.width;
252
253         return shm->samplepos;
254 }
255
256 void SNDDMA_Shutdown(void)
257 {
258         int tmp;
259         if (snd_inited)
260         {
261                 // unmap the memory
262                 munmap(shm->buffer, shm->bufferlength);
263                 // stop the sound
264                 tmp = 0;
265                 ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
266                 ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
267                 // close the device
268                 close(audio_fd);
269                 audio_fd = -1;
270                 snd_inited = 0;
271         }
272 }
273
274 /*
275 ==============
276 SNDDMA_Submit
277
278 Send sound to device if buffer isn't really the dma buffer
279 ===============
280 */
281 void SNDDMA_Submit(void)
282 {
283 }
284
285 void *S_LockBuffer(void)
286 {
287         return shm->buffer;
288 }
289
290 void S_UnlockBuffer(void)
291 {
292 }
293