]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_alsa_0_6.c
fix for blob particle crash
[divverent/darkplaces.git] / snd_alsa_0_6.c
1 /*
2         snd_alsa.c
3
4         (description)
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 snd_pcm_params_info_t cpinfo;
68 static snd_pcm_params_t params;
69 static snd_pcm_setup_t setup;
70 static snd_pcm_mmap_control_t *mmap_control = NULL;
71 static snd_pcm_mmap_status_t *mmap_status = NULL;
72 static char *mmap_data = NULL;
73 static int card=-1,dev=-1,subdev=-1;;
74
75 //XXX ugh, not defined in asoundlib.h
76 int snd_pcm_mmap_status(snd_pcm_t *pcm, snd_pcm_mmap_status_t **status);
77 int snd_pcm_mmap_control(snd_pcm_t *pcm, snd_pcm_mmap_control_t **control);
78
79 int check_card(int card)
80 {
81         snd_ctl_t *handle;
82         snd_ctl_hw_info_t info;
83         int rc;
84
85         if ((rc = snd_ctl_hw_open(&handle, card)) < 0) {
86                 Con_Printf("Error: control open (%i): %s\n", card, snd_strerror(rc));
87                 return rc;
88         }
89         if ((rc = snd_ctl_hw_info(handle, &info)) < 0) {
90                 Con_Printf("Error: control hardware info (%i): %s\n", card,
91                                    snd_strerror(rc));
92                 snd_ctl_close(handle);
93                 return rc;
94         }
95         snd_ctl_close(handle);
96         if (dev==-1) {
97                 for (dev = 0; dev < info.pcmdevs; dev++) {
98                         if ((rc=snd_pcm_hw_open_subdevice(&pcm_handle,card,dev,subdev,
99                                                                  SND_PCM_STREAM_PLAYBACK,
100                                                                  SND_PCM_NONBLOCK))==0) {
101                                 return 0;
102                         }
103                 }
104         } else {
105                 if (dev>=0 && dev <info.pcmdevs) {
106                         if ((rc=snd_pcm_hw_open_subdevice(&pcm_handle,card,dev,subdev,
107                                                                  SND_PCM_STREAM_PLAYBACK,
108                                                                  SND_PCM_NONBLOCK))==0) {
109                                 return 0;
110                         }
111                 }
112         }
113         return 1;
114 }
115
116 qboolean SNDDMA_Init(void)
117 {
118         int rc=0,i;
119         char *err_msg="";
120         int rate=-1,format=-1,bps,stereo=-1,frag_size,frame_size;
121         unsigned int mask;
122
123         mask = snd_cards_mask();
124         if (!mask) {
125                 Con_Printf("No sound cards detected\n");
126                 return 0;
127         }
128         if ((i=COM_CheckParm("-sndcard"))!=0) {
129                 card=atoi(com_argv[i+1]);
130         }
131         if ((i=COM_CheckParm("-snddev"))!=0) {
132                 dev=atoi(com_argv[i+1]);
133         }
134         if ((i=COM_CheckParm("-sndbits")) != 0) {
135                 i = atoi(com_argv[i+1]);
136                 if (i==16) {
137                         format = SND_PCM_SFMT_S16_LE;
138                 } else if (i==8) {
139                         format = SND_PCM_SFMT_U8;
140                 } else {
141                         Con_Printf("Error: invalid sample bits: %d\n", i);
142                         return 0;
143                 }
144         }
145         if ((i=COM_CheckParm("-sndspeed")) != 0) {
146                 rate = atoi(com_argv[i+1]);
147                 if (rate!=44100 && rate!=22050 && rate!=11025) {
148                         Con_Printf("Error: invalid sample rate: %d\n", rate);
149                         return 0;
150                 }
151         }
152         if ((i=COM_CheckParm("-sndmono")) != 0) {
153                 stereo=0;
154         }
155         if (card==-1) {
156                 for (card=0; card<SND_CARDS; card++) {
157                         if (!(mask & (1<<card)))
158                                 continue;
159                         rc=check_card(card);
160                         if (rc<0)
161                                 return 0;
162                         if (!rc)
163                                 goto dev_openned;
164                 }
165         } else {
166                 if (dev==-1) {
167                         rc=check_card(card);
168                         if (rc<0)
169                                 return 0;
170                         if (!rc)
171                                 goto dev_openned;
172                 } else {
173                         if ((rc=snd_pcm_hw_open_subdevice(&pcm_handle,card,dev,subdev,
174                                                                  SND_PCM_STREAM_PLAYBACK,
175                                                                  SND_PCM_NONBLOCK))<0) {
176                                 Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
177                                 return 0;
178                         }
179                         goto dev_openned;
180                 }
181         }
182         Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
183         return 0;
184
185  dev_openned:
186         Con_Printf("Using card %d, device %d.\n", card, dev);
187         memset(&cpinfo, 0, sizeof(cpinfo));
188         snd_pcm_params_info(pcm_handle, &cpinfo);
189         Con_Printf("%08x %08x %08x\n",cpinfo.flags,cpinfo.formats,cpinfo.rates);
190         if ((rate==-1 || rate==44100) && cpinfo.rates & SND_PCM_RATE_44100) {
191                 rate=44100;
192                 frag_size=256;  /* assuming stereo 8 bit */
193         } else if ((rate==-1 || rate==22050) && cpinfo.rates & SND_PCM_RATE_22050) {
194                 rate=22050;
195                 frag_size=128;  /* assuming stereo 8 bit */
196         } else if ((rate==-1 || rate==11025) && cpinfo.rates & SND_PCM_RATE_11025) {
197                 rate=11025;
198                 frag_size=64;   /* assuming stereo 8 bit */
199         } else {
200                 Con_Printf("ALSA: desired rates not supported\n");
201                 goto error_2;
202         }
203         if ((format==-1 || format==SND_PCM_SFMT_S16_LE) && cpinfo.formats & SND_PCM_FMT_S16_LE) {
204                 format=SND_PCM_SFMT_S16_LE;
205                 bps=16;
206                 frame_size=2;
207         } else if ((format==-1 || format==SND_PCM_SFMT_U8) && cpinfo.formats & SND_PCM_FMT_U8) {
208                 format=SND_PCM_SFMT_U8;
209                 bps=8;
210                 frame_size=1;
211         } else {
212                 Con_Printf("ALSA: desired formats not supported\n");
213                 goto error_2;
214         }
215         //XXX can't support non-interleaved stereo
216         if (stereo && (cpinfo.flags & SND_PCM_INFO_INTERLEAVED) && cpinfo.max_channels>=2) {
217                 stereo=1;
218                 frame_size*=2;
219         } else {
220                 stereo=0;
221         }
222
223         memset(&params, 0, sizeof(params));
224         //XXX can't support non-interleaved stereo
225         params.xfer_mode = stereo ? SND_PCM_XFER_INTERLEAVED
226                                                           : SND_PCM_XFER_NONINTERLEAVED;
227         params.format.sfmt=format;
228         params.format.rate=rate;
229         params.format.channels=stereo+1;
230         params.start_mode = SND_PCM_START_EXPLICIT;
231         params.xrun_mode = SND_PCM_XRUN_NONE;
232
233         params.buffer_size = (2<<16) / frame_size;
234         params.frag_size=frag_size;
235         params.avail_min = frag_size;
236
237         params.xrun_max = 1024;
238         params.boundary = params.buffer_size;
239
240         while (1) {
241                 err_msg="audio params";
242                 if ((rc=snd_pcm_params(pcm_handle, &params))<0)
243                         goto error;
244
245                 memset(&setup, 0, sizeof(setup));
246                 err_msg="audio setup";
247                 if ((rc=snd_pcm_setup(pcm_handle, &setup))<0)
248                         goto error;
249                 if (setup.buffer_size == params.buffer_size)
250                         break;
251                 params.boundary = params.buffer_size = setup.buffer_size;
252         }
253
254         err_msg="audio mmap";
255         if ((rc=snd_pcm_mmap(pcm_handle, (void**)&mmap_data))<0)
256                 goto error;
257         if ((rc=snd_pcm_mmap_status(pcm_handle, &mmap_status))<0)
258                 goto error;
259         if ((rc=snd_pcm_mmap_control(pcm_handle, &mmap_control))<0)
260                 goto error;
261         err_msg="audio prepare";
262         if ((rc=snd_pcm_prepare(pcm_handle))<0)
263                 goto error;
264
265         shm=&sn;
266         memset((dma_t*)shm,0,sizeof(*shm));
267     shm->splitbuffer = 0;
268         shm->channels=setup.format.channels;
269         shm->submission_chunk=frag_size;                        // don't mix less than this #
270         shm->samplepos=0;                                                       // in mono samples
271         shm->samplebits=setup.format.sfmt==SND_PCM_SFMT_S16_LE?16:8;
272         shm->samples=setup.buffer_size*shm->channels;   // mono samples in buffer
273         shm->speed=setup.format.rate;
274         shm->buffer=(unsigned char*)mmap_data;
275     Con_Printf("%5d stereo\n", shm->channels - 1);
276     Con_Printf("%5d samples\n", shm->samples);
277     Con_Printf("%5d samplepos\n", shm->samplepos);
278     Con_Printf("%5d samplebits\n", shm->samplebits);
279     Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
280     Con_Printf("%5d speed\n", shm->speed);
281     Con_Printf("0x%x dma buffer\n", (int)shm->buffer);
282         Con_Printf("%5d total_channels\n", total_channels);
283
284         snd_inited=1;
285         return 1;
286  error:
287         Con_Printf("Error: %s: %s\n", err_msg, snd_strerror(rc));
288  error_2:
289         snd_pcm_close(pcm_handle);
290         return 0;
291 }
292
293 int SNDDMA_GetDMAPos(void)
294 {
295         size_t hw_ptr;
296         if (!snd_inited) return 0;
297         hw_ptr = mmap_status->hw_ptr;
298         //printf("%7d %7d\n", mmap_control->appl_ptr, hw_ptr);
299         hw_ptr *= shm->channels;
300         shm->samplepos = hw_ptr;
301         return shm->samplepos;
302 }
303
304 void SNDDMA_Shutdown(void)
305 {
306         if (snd_inited)
307         {
308                 snd_pcm_close(pcm_handle);
309                 snd_inited = 0;
310         }
311 }
312
313 /*
314 ==============
315 SNDDMA_Submit
316
317 Send sound to device if buffer isn't really the dma buffer
318 ===============
319 */
320 void SNDDMA_Submit(void)
321 {
322         int count=paintedtime-soundtime;
323         int rc;
324
325         mmap_control->appl_ptr=mmap_status->hw_ptr+count;
326         switch (mmap_status->state) {
327         case SND_PCM_STATE_PREPARED:
328                 if ((rc=snd_pcm_start(pcm_handle))<0) {
329                         fprintf(stderr, "unable to start playback. %s\n",
330                                         snd_strerror(rc));
331                         exit(1);
332                 }
333                 break;
334         case SND_PCM_STATE_RUNNING:
335                 break;
336         case SND_PCM_STATE_UNDERRUN:
337                 printf("sound underrun\n");
338                 if ((rc=snd_pcm_prepare (pcm_handle))<0) {
339                         fprintf(stderr, "underrun: playback channel prepare error. %s\n",
340                                 snd_strerror(rc));
341                         exit(1);
342                 }
343                 break;
344         }
345 }
346