]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_oss.c
added TEXF_CLAMP flag on model and sprite textures (software quake did not support...
[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 < (int) sizeof(tryrates) / 4;i++)
117                         if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
118                                 break;
119
120                 shm->speed = tryrates[i];
121     }
122
123         s = getenv("QUAKE_SOUND_CHANNELS");
124         if (s)
125                 shm->channels = atoi(s);
126         else if ((i = COM_CheckParm("-sndmono")) != 0)
127                 shm->channels = 1;
128         else if ((i = COM_CheckParm("-sndstereo")) != 0)
129                 shm->channels = 2;
130         else
131                 shm->channels = 2;
132
133         shm->samples = info.fragstotal * info.fragsize / (shm->samplebits/8);
134         shm->submission_chunk = 1;
135
136         // memory map the dma buffer
137         shm->buffer = (unsigned char *) mmap(NULL, info.fragstotal
138                 * info.fragsize, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
139         if (!shm->buffer || shm->buffer == (unsigned char *)-1)
140         {
141                 perror("/dev/dsp");
142                 Con_Printf("Could not mmap /dev/dsp\n");
143                 close(audio_fd);
144                 return 0;
145         }
146
147         tmp = 0;
148         if (shm->channels == 2)
149                 tmp = 1;
150
151         rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
152         if (rc < 0)
153         {
154                 perror("/dev/dsp");
155                 Con_Printf("Could not set /dev/dsp to stereo=%d", shm->channels);
156                 close(audio_fd);
157                 return 0;
158         }
159         if (tmp)
160                 shm->channels = 2;
161         else
162                 shm->channels = 1;
163
164         rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->speed);
165         if (rc < 0)
166         {
167                 perror("/dev/dsp");
168                 Con_Printf("Could not set /dev/dsp speed to %d", shm->speed);
169                 close(audio_fd);
170                 return 0;
171         }
172
173         if (shm->samplebits == 16)
174         {
175                 rc = AFMT_S16_LE;
176                 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
177                 if (rc < 0)
178                 {
179                         perror("/dev/dsp");
180                         Con_Printf("Could not support 16-bit data.  Try 8-bit.\n");
181                         close(audio_fd);
182                         return 0;
183                 }
184         }
185         else if (shm->samplebits == 8)
186         {
187                 rc = AFMT_U8;
188                 rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
189                 if (rc < 0)
190                 {
191                         perror("/dev/dsp");
192                         Con_Printf("Could not support 8-bit data.\n");
193                         close(audio_fd);
194                         return 0;
195                 }
196         }
197         else
198         {
199                 perror("/dev/dsp");
200                 Con_Printf("%d-bit sound not supported.", shm->samplebits);
201                 close(audio_fd);
202                 return 0;
203         }
204
205         // toggle the trigger & start her up
206         tmp = 0;
207         rc  = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
208         if (rc < 0)
209         {
210                 perror("/dev/dsp");
211                 Con_Printf("Could not toggle.\n");
212                 close(audio_fd);
213                 return 0;
214         }
215         tmp = PCM_ENABLE_OUTPUT;
216         rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
217         if (rc < 0)
218         {
219                 perror("/dev/dsp");
220                 Con_Printf("Could not toggle.\n");
221                 close(audio_fd);
222                 return 0;
223         }
224
225         shm->samplepos = 0;
226
227         snd_inited = 1;
228         return 1;
229 }
230
231 int SNDDMA_GetDMAPos(void)
232 {
233
234         struct count_info count;
235
236         if (!snd_inited) return 0;
237
238         if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
239         {
240                 perror("/dev/dsp");
241                 Con_Printf("Uh, sound dead.\n");
242                 close(audio_fd);
243                 snd_inited = 0;
244                 return 0;
245         }
246         shm->samplepos = count.ptr / (shm->samplebits / 8);
247
248         return shm->samplepos;
249 }
250
251 void SNDDMA_Shutdown(void)
252 {
253         if (snd_inited)
254         {
255                 close(audio_fd);
256                 snd_inited = 0;
257         }
258 }
259
260 /*
261 ==============
262 SNDDMA_Submit
263
264 Send sound to device if buffer isn't really the dma buffer
265 ===============
266 */
267 void SNDDMA_Submit(void)
268 {
269 }
270
271 void *S_LockBuffer(void)
272 {
273         return shm->buffer;
274 }
275
276 void S_UnlockBuffer(void)
277 {
278 }
279
280
281 void S_Open(void)
282 {
283 }
284
285 void S_Close(void)
286 {
287 }