]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_oss.c
389 items.
[divverent/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         else if ((i = COM_CheckParm("-sndbits")) != 0)
100                 shm->format.width = atoi(com_argv[i+1]) / 8;
101
102         if (shm->format.width != 2 && shm->format.width != 1)
103         {
104                 ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
105                 if (fmt & format16bit)
106                         shm->format.width = 2;
107                 else if (fmt & AFMT_U8)
108                         shm->format.width = 1;
109     }
110
111         s = getenv("QUAKE_SOUND_SPEED");
112         if (s)
113                 shm->format.speed = atoi(s);
114         else if ((i = COM_CheckParm("-sndspeed")) != 0)
115                 shm->format.speed = atoi(com_argv[i+1]);
116         else
117         {
118                 for (i = 0;i < (int) sizeof(tryrates) / 4;i++)
119                         if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
120                                 break;
121
122                 shm->format.speed = tryrates[i];
123     }
124
125         s = getenv("QUAKE_SOUND_CHANNELS");
126         if (s)
127                 shm->format.channels = atoi(s);
128         else if ((i = COM_CheckParm("-sndmono")) != 0)
129                 shm->format.channels = 1;
130         else if ((i = COM_CheckParm("-sndstereo")) != 0)
131                 shm->format.channels = 2;
132         else
133                 shm->format.channels = 2;
134
135         shm->samples = info.fragstotal * info.fragsize / shm->format.width;
136
137         // memory map the dma buffer
138         shm->bufferlength = info.fragstotal * info.fragsize;
139         shm->buffer = (unsigned char *) mmap(NULL, shm->bufferlength, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
140         if (!shm->buffer || shm->buffer == (unsigned char *)-1)
141         {
142                 perror("/dev/dsp");
143                 Con_Print("Could not mmap /dev/dsp\n");
144                 close(audio_fd);
145                 return 0;
146         }
147
148         tmp = 0;
149         if (shm->format.channels == 2)
150                 tmp = 1;
151
152         rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
153         if (rc < 0)
154         {
155                 perror("/dev/dsp");
156                 Con_Printf("Could not set /dev/dsp to stereo=%d\n", shm->format.channels);
157                 close(audio_fd);
158                 return 0;
159         }
160         if (tmp)
161                 shm->format.channels = 2;
162         else
163                 shm->format.channels = 1;
164
165         rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->format.speed);
166         if (rc < 0)
167         {
168                 perror("/dev/dsp");
169                 Con_Printf("Could not set /dev/dsp speed to %d\n", shm->format.speed);
170                 close(audio_fd);
171                 return 0;
172         }
173
174         if (shm->format.width == 2)
175         {
176                 rc = format16bit;
177                 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
178                 if (rc < 0)
179                 {
180                         perror("/dev/dsp");
181                         Con_Print("Could not support 16-bit data.  Try 8-bit.\n");
182                         close(audio_fd);
183                         return 0;
184                 }
185         }
186         else if (shm->format.width == 1)
187         {
188                 rc = AFMT_U8;
189                 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
190                 if (rc < 0)
191                 {
192                         perror("/dev/dsp");
193                         Con_Print("Could not support 8-bit data.\n");
194                         close(audio_fd);
195                         return 0;
196                 }
197         }
198         else
199         {
200                 perror("/dev/dsp");
201                 Con_Printf("%d-bit sound not supported.\n", shm->format.width * 8);
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