]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_oss.c
make profile now works
[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[] = { 11025, 22051, 44100, 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
47         snd_inited = 0;
48
49         // open /dev/dsp, confirm capability to mmap, and get size of dma buffer
50     audio_fd = open("/dev/dsp", O_RDWR);
51         if (audio_fd < 0)
52         {
53                 perror("/dev/dsp");
54                 Con_Printf("Could not open /dev/dsp\n");
55                 return 0;
56         }
57
58         rc = ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
59         if (rc < 0)
60         {
61                 perror("/dev/dsp");
62                 Con_Printf("Could not reset /dev/dsp\n");
63                 close(audio_fd);
64                 return 0;
65         }
66
67         if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
68         {
69                 perror("/dev/dsp");
70                 Con_Printf("Sound driver too old\n");
71                 close(audio_fd);
72                 return 0;
73         }
74
75         if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP))
76         {
77                 Con_Printf("Sorry but your soundcard can't do this\n");
78                 close(audio_fd);
79                 return 0;
80         }
81
82         if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
83         {   
84                 perror("GETOSPACE");
85                 Con_Printf("Um, can't do GETOSPACE?\n");
86                 close(audio_fd);
87                 return 0;
88         }
89     
90         shm = &sn;
91         shm->splitbuffer = 0;
92
93         // set sample bits & speed
94         s = getenv("QUAKE_SOUND_SAMPLEBITS");
95         if (s)
96                 shm->samplebits = atoi(s);
97         else if ((i = COM_CheckParm("-sndbits")) != 0)
98                 shm->samplebits = atoi(com_argv[i+1]);
99
100         if (shm->samplebits != 16 && shm->samplebits != 8)
101         {
102                 ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
103                 if (fmt & AFMT_S16_LE)
104                         shm->samplebits = 16;
105                 else if (fmt & AFMT_U8)
106                         shm->samplebits = 8;
107     }
108
109         s = getenv("QUAKE_SOUND_SPEED");
110         if (s)
111                 shm->speed = atoi(s);
112         else if ((i = COM_CheckParm("-sndspeed")) != 0)
113                 shm->speed = atoi(com_argv[i+1]);
114         else
115         {
116                 for (i=0 ; i<sizeof(tryrates)/4 ; i++) {
117                         if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
118                                 break;
119                 }
120
121                 shm->speed = tryrates[i];
122     }
123
124         s = getenv("QUAKE_SOUND_CHANNELS");
125         if (s)
126                 shm->channels = atoi(s);
127         else if ((i = COM_CheckParm("-sndmono")) != 0)
128                 shm->channels = 1;
129         else if ((i = COM_CheckParm("-sndstereo")) != 0)
130                 shm->channels = 2;
131         else
132                 shm->channels = 2;
133
134         shm->samples = info.fragstotal * info.fragsize / (shm->samplebits/8);
135         shm->submission_chunk = 1;
136
137         // memory map the dma buffer
138         shm->buffer = (unsigned char *) mmap(NULL, info.fragstotal
139                 * info.fragsize, 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_Printf("Could not mmap /dev/dsp\n");
144                 close(audio_fd);
145                 return 0;
146         }
147
148         tmp = 0;
149         if (shm->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", shm->channels);
157                 close(audio_fd);
158                 return 0;
159         }
160         if (tmp)
161                 shm->channels = 2;
162         else
163                 shm->channels = 1;
164
165         rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->speed);
166         if (rc < 0)
167         {
168                 perror("/dev/dsp");
169                 Con_Printf("Could not set /dev/dsp speed to %d", shm->speed);
170                 close(audio_fd);
171                 return 0;
172         }
173
174         if (shm->samplebits == 16)
175         {
176                 rc = AFMT_S16_LE;
177                 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
178                 if (rc < 0)
179                 {
180                         perror("/dev/dsp");
181                         Con_Printf("Could not support 16-bit data.  Try 8-bit.\n");
182                         close(audio_fd);
183                         return 0;
184                 }
185         }
186         else if (shm->samplebits == 8)
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_Printf("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.", shm->samplebits);
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_Printf("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_Printf("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_Printf("Uh, sound dead.\n");
243                 close(audio_fd);
244                 snd_inited = 0;
245                 return 0;
246         }
247         shm->samplepos = count.ptr / (shm->samplebits / 8);
248
249         return shm->samplepos;
250 }
251
252 void SNDDMA_Shutdown(void)
253 {
254         if (snd_inited)
255         {
256                 close(audio_fd);
257                 snd_inited = 0;
258         }
259 }
260
261 /*
262 ==============
263 SNDDMA_Submit
264
265 Send sound to device if buffer isn't really the dma buffer
266 ===============
267 */
268 void SNDDMA_Submit(void)
269 {
270 }
271
272 void *S_LockBuffer(void)
273 {
274         return shm->buffer;
275 }
276
277 void S_UnlockBuffer(void)
278 {
279 }
280
281
282 void S_Open(void)
283 {
284 }
285
286 void S_Close(void)
287 {
288 }