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