]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_alsa_0_5.c
make bbox collisions work again
[divverent/darkplaces.git] / snd_alsa_0_5.c
1 /*
2         snd_alsa_0_5.c
3
4         Support for ALSA 0.5, the old stable version of ALSA.
5
6         Copyright (C) 1999,2000  contributors of the QuakeForge project
7         Please see the file "AUTHORS" for a list of contributors
8
9         This program is free software; you can redistribute it and/or
10         modify it under the terms of the GNU General Public License
11         as published by the Free Software Foundation; either version 2
12         of the License, or (at your option) any later version.
13
14         This program is distributed in the hope that it will be useful,
15         but WITHOUT ANY WARRANTY; without even the implied warranty of
16         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18         See the GNU General Public License for more details.
19
20         You should have received a copy of the GNU General Public License
21         along with this program; if not, write to:
22
23                 Free Software Foundation, Inc.
24                 59 Temple Place - Suite 330
25                 Boston, MA  02111-1307, USA
26
27         $Id$
28 */
29
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include "quakedef.h"
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #include <fcntl.h>
42 #include <sys/types.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
45 #endif
46 #ifdef HAVE_SYS_MMAN_H
47 # include <sys/mman.h>
48 #endif
49 #if defined HAVE_SYS_SOUNDCARD_H
50 # include <sys/soundcard.h>
51 #elif defined HAVE_LINUX_SOUNDCARD_H
52 # include <linux/soundcard.h>
53 #elif HAVE_MACHINE_SOUNDCARD_H
54 # include <machine/soundcard.h>
55 #endif
56
57 #include <sys/asoundlib.h>
58
59 #ifndef MAP_FAILED
60 # define MAP_FAILED ((void*)-1)
61 #endif
62
63 extern int soundtime;
64 static int snd_inited;
65
66 static snd_pcm_t *pcm_handle;
67 static struct snd_pcm_channel_info cinfo;
68 static struct snd_pcm_channel_params params;
69 static struct snd_pcm_channel_setup setup;
70 static snd_pcm_mmap_control_t *mmap_control = NULL;
71 static char *mmap_data = NULL;
72 static int card=-1,dev=-1;
73
74 int check_card(int card)
75 {
76         snd_ctl_t *handle;
77         snd_ctl_hw_info_t info;
78         int rc;
79
80         if ((rc = snd_ctl_open(&handle, card)) < 0) {
81                 Con_Printf("Error: control open (%i): %s\n", card, snd_strerror(rc));
82                 return rc;
83         }
84         if ((rc = snd_ctl_hw_info(handle, &info)) < 0) {
85                 Con_Printf("Error: control hardware info (%i): %s\n", card,
86                                    snd_strerror(rc));
87                 snd_ctl_close(handle);
88                 return rc;
89         }
90         snd_ctl_close(handle);
91         if (dev==-1) {
92                 for (dev = 0; dev < info.pcmdevs; dev++) {
93                         if ((rc=snd_pcm_open(&pcm_handle,card,dev,
94                                                                  SND_PCM_OPEN_PLAYBACK
95                                                                  | SND_PCM_OPEN_NONBLOCK))==0) {
96                                 return 0;
97                         }
98                 }
99         } else {
100                 if (dev>=0 && dev <info.pcmdevs) {
101                         if ((rc=snd_pcm_open(&pcm_handle,card,dev,
102                                                                  SND_PCM_OPEN_PLAYBACK
103                                                                  | SND_PCM_OPEN_NONBLOCK))==0) {
104                                 return 0;
105                         }
106                 }
107         }
108         return 1;
109 }
110
111 qboolean SNDDMA_Init(void)
112 {
113         int rc=0,i;
114         char *err_msg="";
115         int rate=-1,format=-1,bps,stereo=-1,frag_size;
116         unsigned int mask;
117
118         mask = snd_cards_mask();
119         if (!mask) {
120                 Con_Printf("No sound cards detected\n");
121                 return 0;
122         }
123         if ((i=COM_CheckParm("-sndcard"))!=0) {
124                 card=atoi(com_argv[i+1]);
125         }
126         if ((i=COM_CheckParm("-snddev"))!=0) {
127                 dev=atoi(com_argv[i+1]);
128         }
129         if ((i=COM_CheckParm("-sndbits")) != 0) {
130                 i = atoi(com_argv[i+1]);
131                 if (i==16) {
132                         format = SND_PCM_SFMT_S16_LE;
133                 } else if (i==8) {
134                         format = SND_PCM_SFMT_U8;
135                 } else {
136                         Con_Printf("Error: invalid sample bits: %d\n", i);
137                         return 0;
138                 }
139         }
140         if ((i=COM_CheckParm("-sndspeed")) != 0) {
141                 rate = atoi(com_argv[i+1]);
142                 if (rate!=44100 && rate!=22050 && rate!=11025) {
143                         Con_Printf("Error: invalid sample rate: %d\n", rate);
144                         return 0;
145                 }
146         }
147         if ((i=COM_CheckParm("-sndmono")) != 0) {
148                 stereo=0;
149         }
150         if (card==-1) {
151                 for (card=0; card<SND_CARDS; card++) {
152                         if (!(mask & (1<<card)))
153                                 continue;
154                         rc=check_card(card);
155                         if (rc<0)
156                                 return 0;
157                         if (!rc)
158                                 goto dev_openned;
159                 }
160         } else {
161                 if (dev==-1) {
162                         rc=check_card(card);
163                         if (rc<0)
164                                 return 0;
165                         if (!rc)
166                                 goto dev_openned;
167                 } else {
168                         if ((rc=snd_pcm_open(&pcm_handle,card,dev,
169                                                                  SND_PCM_OPEN_PLAYBACK
170                                                                  | SND_PCM_OPEN_NONBLOCK))<0) {
171                                 Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
172                                 return 0;
173                         }
174                         goto dev_openned;
175                 }
176         }
177         Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
178         return 0;
179
180  dev_openned:
181         Con_Printf("Using card %d, device %d.\n", card, dev);
182         memset(&cinfo, 0, sizeof(cinfo));
183         cinfo.channel = SND_PCM_CHANNEL_PLAYBACK;
184         snd_pcm_channel_info(pcm_handle, &cinfo);
185         Con_Printf("%08x %08x %08x\n",cinfo.flags,cinfo.formats,cinfo.rates);
186         if ((rate==-1 || rate==44100) && cinfo.rates & SND_PCM_RATE_44100) {
187                 rate=44100;
188                 frag_size=512;  /* assuming stereo 8 bit */
189         } else if ((rate==-1 || rate==22050) && cinfo.rates & SND_PCM_RATE_22050) {
190                 rate=22050;
191                 frag_size=256;  /* assuming stereo 8 bit */
192         } else if ((rate==-1 || rate==11025) && cinfo.rates & SND_PCM_RATE_11025) {
193                 rate=11025;
194                 frag_size=128;  /* assuming stereo 8 bit */
195         } else {
196                 Con_Printf("ALSA: desired rates not supported\n");
197                 goto error_2;
198         }
199         if ((format==-1 || format==SND_PCM_SFMT_S16_LE) && cinfo.formats & SND_PCM_FMT_S16_LE) {
200                 format=SND_PCM_SFMT_S16_LE;
201                 bps=16;
202                 frag_size*=2;
203         } else if ((format==-1 || format==SND_PCM_SFMT_U8) && cinfo.formats & SND_PCM_FMT_U8) {
204                 format=SND_PCM_SFMT_U8;
205                 bps=8;
206         } else {
207                 Con_Printf("ALSA: desired formats not supported\n");
208                 goto error_2;
209         }
210         if (stereo && cinfo.max_voices>=2) {
211                 stereo=1;
212         } else {
213                 stereo=0;
214                 frag_size/=2;
215         }
216
217 //      err_msg="audio flush";
218 //      if ((rc=snd_pcm_channel_flush(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
219 //              goto error;
220         err_msg="audio munmap";
221         if ((rc=snd_pcm_munmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
222                 goto error;
223
224         memset(&params, 0, sizeof(params));
225         params.channel = SND_PCM_CHANNEL_PLAYBACK;
226         params.mode = SND_PCM_MODE_BLOCK;
227         params.format.interleave=1;
228         params.format.format=format;
229         params.format.rate=rate;
230         params.format.voices=stereo+1;
231         params.start_mode = SND_PCM_START_GO;
232         params.stop_mode = SND_PCM_STOP_ROLLOVER;
233         params.buf.block.frag_size=frag_size;
234         params.buf.block.frags_min=1;
235         params.buf.block.frags_max=-1;
236 //      err_msg="audio flush";
237 //      if ((rc=snd_pcm_channel_flush(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
238 //              goto error;
239         err_msg="audio params";
240         if ((rc=snd_pcm_channel_params(pcm_handle, &params))<0)
241                 goto error;
242
243         err_msg="audio mmap";
244         if ((rc=snd_pcm_mmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK, &mmap_control, (void **)&mmap_data))<0)
245                 goto error;
246         err_msg="audio prepare";
247         if ((rc=snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
248                 goto error;
249
250         memset(&setup, 0, sizeof(setup));
251         setup.mode = SND_PCM_MODE_BLOCK;
252         setup.channel = SND_PCM_CHANNEL_PLAYBACK;
253         err_msg="audio setup";
254         if ((rc=snd_pcm_channel_setup(pcm_handle, &setup))<0)
255                 goto error;
256
257         shm=&sn;
258         memset((dma_t*)shm,0,sizeof(*shm));
259     shm->splitbuffer = 0;
260         shm->channels=setup.format.voices;
261         shm->submission_chunk=128;                                      // don't mix less than this #
262         shm->samplepos=0;                                                       // in mono samples
263         shm->samplebits=setup.format.format==SND_PCM_SFMT_S16_LE?16:8;
264         shm->samples=setup.buf.block.frags*setup.buf.block.frag_size/(shm->samplebits/8);       // mono samples in buffer
265         shm->speed=setup.format.rate;
266         shm->buffer=(unsigned char*)mmap_data;
267     Con_Printf("%5d stereo\n", shm->channels - 1);
268     Con_Printf("%5d samples\n", shm->samples);
269     Con_Printf("%5d samplepos\n", shm->samplepos);
270     Con_Printf("%5d samplebits\n", shm->samplebits);
271     Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
272     Con_Printf("%5d speed\n", shm->speed);
273     Con_Printf("0x%x dma buffer\n", (int)shm->buffer);
274         Con_Printf("%5d total_channels\n", total_channels);
275
276         snd_inited=1;
277         return 1;
278  error:
279         Con_Printf("Error: %s: %s\n", err_msg, snd_strerror(rc));
280  error_2:
281         snd_pcm_close(pcm_handle);
282         return 0;
283 }
284
285 int SNDDMA_GetDMAPos(void)
286 {
287         if (!snd_inited) return 0;
288         shm->samplepos=(mmap_control->status.frag_io+1)*setup.buf.block.frag_size/(shm->samplebits/8);
289         return shm->samplepos;
290 }
291
292 void SNDDMA_Shutdown(void)
293 {
294         if (snd_inited)
295         {
296                 snd_pcm_close(pcm_handle);
297                 snd_inited = 0;
298         }
299 }
300
301 /*
302 ==============
303 SNDDMA_Submit
304
305 Send sound to device if buffer isn't really the dma buffer
306 ===============
307 */
308 void SNDDMA_Submit(void)
309 {
310         int count=paintedtime-soundtime;
311         int i,s,e;
312         int rc;
313
314         count+=setup.buf.block.frag_size-1;
315         count/=setup.buf.block.frag_size;
316         s=soundtime/setup.buf.block.frag_size;
317         e=s+count;
318         for (i=s; i<e; i++)
319                 mmap_control->fragments[i % setup.buf.block.frags].data=1;
320         switch (mmap_control->status.status) {
321         case SND_PCM_STATUS_PREPARED:
322                 if ((rc=snd_pcm_channel_go(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0) {
323                         fprintf(stderr, "unable to start playback. %s\n",
324                                         snd_strerror(rc));
325                         exit(1);
326                 }
327                 break;
328         case SND_PCM_STATUS_RUNNING:
329                 break;
330         case SND_PCM_STATUS_UNDERRUN:
331                 if ((rc=snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0) {
332                         fprintf(stderr, "underrun: playback channel prepare error. %s\n",
333                                         snd_strerror(rc));
334                         exit(1);
335                 }
336                 break;
337         default:
338                 break;
339         }
340 }
341