fixed bumpmapping! now looks correct
[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 munmap";
218         if ((rc=snd_pcm_munmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
219                 goto error;
220
221         memset(&params, 0, sizeof(params));
222         params.channel = SND_PCM_CHANNEL_PLAYBACK;
223         params.mode = SND_PCM_MODE_BLOCK;
224         params.format.interleave=1;
225         params.format.format=format;
226         params.format.rate=rate;
227         params.format.voices=stereo+1;
228         params.start_mode = SND_PCM_START_GO;
229         params.stop_mode = SND_PCM_STOP_ROLLOVER;
230         params.buf.block.frag_size=frag_size;
231         params.buf.block.frags_min=1;
232         params.buf.block.frags_max=-1;
233         err_msg="audio params";
234         if ((rc=snd_pcm_channel_params(pcm_handle, &params))<0)
235                 goto error;
236
237         err_msg="audio mmap";
238         if ((rc=snd_pcm_mmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK, &mmap_control, (void **)&mmap_data))<0)
239                 goto error;
240         err_msg="audio prepare";
241         if ((rc=snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
242                 goto error;
243
244         memset(&setup, 0, sizeof(setup));
245         setup.mode = SND_PCM_MODE_BLOCK;
246         setup.channel = SND_PCM_CHANNEL_PLAYBACK;
247         err_msg="audio setup";
248         if ((rc=snd_pcm_channel_setup(pcm_handle, &setup))<0)
249                 goto error;
250
251         shm=&sn;
252         memset((dma_t*)shm,0,sizeof(*shm));
253     shm->splitbuffer = 0;
254         shm->channels=setup.format.voices;
255         shm->submission_chunk=128;                                      // don't mix less than this #
256         shm->samplepos=0;                                                       // in mono samples
257         shm->samplebits=setup.format.format==SND_PCM_SFMT_S16_LE?16:8;
258         shm->samples=setup.buf.block.frags*setup.buf.block.frag_size/(shm->samplebits/8);       // mono samples in buffer
259         shm->speed=setup.format.rate;
260         shm->buffer=(unsigned char*)mmap_data;
261     Con_Printf("%5d stereo\n", shm->channels - 1);
262     Con_Printf("%5d samples\n", shm->samples);
263     Con_Printf("%5d samplepos\n", shm->samplepos);
264     Con_Printf("%5d samplebits\n", shm->samplebits);
265     Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
266     Con_Printf("%5d speed\n", shm->speed);
267     Con_Printf("0x%x dma buffer\n", (int)shm->buffer);
268         Con_Printf("%5d total_channels\n", total_channels);
269
270         snd_inited=1;
271         return 1;
272  error:
273         Con_Printf("Error: %s: %s\n", err_msg, snd_strerror(rc));
274  error_2:
275         snd_pcm_close(pcm_handle);
276         return 0;
277 }
278
279 int SNDDMA_GetDMAPos(void)
280 {
281         if (!snd_inited) return 0;
282         shm->samplepos=(mmap_control->status.frag_io+1)*setup.buf.block.frag_size/(shm->samplebits/8);
283         return shm->samplepos;
284 }
285
286 void SNDDMA_Shutdown(void)
287 {
288         if (snd_inited)
289         {
290                 snd_pcm_close(pcm_handle);
291                 snd_inited = 0;
292         }
293 }
294
295 /*
296 ==============
297 SNDDMA_Submit
298
299 Send sound to device if buffer isn't really the dma buffer
300 ===============
301 */
302 void SNDDMA_Submit(void)
303 {
304         int count=paintedtime-soundtime;
305         int i,s,e;
306         int rc;
307
308         count+=setup.buf.block.frag_size-1;
309         count/=setup.buf.block.frag_size;
310         s=soundtime/setup.buf.block.frag_size;
311         e=s+count;
312         for (i=s; i<e; i++)
313                 mmap_control->fragments[i % setup.buf.block.frags].data=1;
314         switch (mmap_control->status.status) {
315         case SND_PCM_STATUS_PREPARED:
316                 if ((rc=snd_pcm_channel_go(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0) {
317                         fprintf(stderr, "unable to start playback. %s\n",
318                                         snd_strerror(rc));
319                         exit(1);
320                 }
321                 break;
322         case SND_PCM_STATUS_RUNNING:
323                 break;
324         case SND_PCM_STATUS_UNDERRUN:
325                 if ((rc=snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0) {
326                         fprintf(stderr, "underrun: playback channel prepare error. %s\n",
327                                         snd_strerror(rc));
328                         exit(1);
329                 }
330                 break;
331         default:
332                 break;
333         }
334 }
335
336 void *S_LockBuffer(void)
337 {
338         return shm->buffer;
339 }
340
341 void S_UnlockBuffer(void)
342 {
343 }
344
345 void S_Open(void)
346 {
347 }
348
349 void S_Close(void)
350 {
351 }