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