add ALSA support
[divverent/darkplaces.git] / snd_alsa.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 <sys/soundcard.h>
37 #include <linux/soundcard.h>
38 #include <sys/asoundlib.h>
39
40 #ifndef MAP_FAILED
41 # define MAP_FAILED ((void*)-1)
42 #endif
43
44 extern int soundtime;
45
46 static int snd_inited;
47
48 static snd_pcm_t *pcm_handle;
49 static struct snd_pcm_channel_info cinfo;
50 static struct snd_pcm_channel_params params;
51 static struct snd_pcm_channel_setup setup;
52 static snd_pcm_mmap_control_t *mmap_control = NULL;
53 static char *mmap_data = NULL;
54 static int card=-1,dev=-1;
55
56 int check_card(int card)
57 {
58         snd_ctl_t *handle;
59         snd_ctl_hw_info_t info;
60         int rc;
61
62         if ((rc = snd_ctl_open(&handle, card)) < 0) {
63                 Con_Printf("Error: control open (%i): %s\n", card, snd_strerror(rc));
64                 return rc;
65         }
66         if ((rc = snd_ctl_hw_info(handle, &info)) < 0) {
67                 Con_Printf("Error: control hardware info (%i): %s\n", card,
68                                    snd_strerror(rc));
69                 snd_ctl_close(handle);
70                 return rc;
71         }
72         snd_ctl_close(handle);
73         if (dev==-1) {
74                 for (dev = 0; dev < info.pcmdevs; dev++) {
75                         if ((rc=snd_pcm_open(&pcm_handle,card,dev,
76                                                                  SND_PCM_OPEN_PLAYBACK
77                                                                  | SND_PCM_OPEN_NONBLOCK))==0) {
78                                 return 0;
79                         }
80                 }
81         } else {
82                 if (dev>=0 && dev <info.pcmdevs) {
83                         if ((rc=snd_pcm_open(&pcm_handle,card,dev,
84                                                                  SND_PCM_OPEN_PLAYBACK
85                                                                  | SND_PCM_OPEN_NONBLOCK))==0) {
86                                 return 0;
87                         }
88                 }
89         }
90         return 1;
91 }
92
93 qboolean SNDDMA_Init(void)
94 {
95         int rc=0,i;
96         char *err_msg="";
97         int rate,format,bps,stereo,frag_size;
98         unsigned int mask;
99
100         mask = snd_cards_mask();
101         if (!mask) {
102                 Con_Printf("No sound cards detected\n");
103                 return 0;
104         }
105         if ((i=COM_CheckParm("-sndcard"))!=0) {
106                 card=atoi(com_argv[i+1]);
107         }
108         if ((i=COM_CheckParm("-snddev"))!=0) {
109                 dev=atoi(com_argv[i+1]);
110         }
111         if (card==-1) {
112                 for (card=0; card<SND_CARDS; card++) {
113                         if (!(mask & (1<<card)))
114                                 continue;
115                         rc=check_card(card);
116                         if (rc<0)
117                                 return 0;
118                         if (!rc)
119                                 goto dev_openned;
120                 }
121         } else {
122                 if (dev==-1) {
123                         rc=check_card(card);
124                         if (rc<0)
125                                 return 0;
126                         if (!rc)
127                                 goto dev_openned;
128                 } else {
129                         if ((rc=snd_pcm_open(&pcm_handle,card,dev,
130                                                                  SND_PCM_OPEN_PLAYBACK
131                                                                  | SND_PCM_OPEN_NONBLOCK))<0) {
132                                 Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
133                                 return 0;
134                         }
135                         goto dev_openned;
136                 }
137         }
138         Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
139         return 0;
140
141  dev_openned:
142         Con_Printf("Using card %d, device %d.\n", card, dev);
143         memset(&cinfo, 0, sizeof(cinfo));
144         cinfo.channel = SND_PCM_CHANNEL_PLAYBACK;
145         snd_pcm_channel_info(pcm_handle, &cinfo);
146         Con_Printf("%08x %08x %08x\n",cinfo.flags,cinfo.formats,cinfo.rates);
147         if (cinfo.rates & SND_PCM_RATE_44100) {
148                 rate=44100;
149                 frag_size=512;  /* assuming stereo 8 bit */
150         } else if (cinfo.rates & SND_PCM_RATE_22050) {
151                 rate=22050;
152                 frag_size=256;  /* assuming stereo 8 bit */
153         } else if (cinfo.rates & SND_PCM_RATE_11025) {
154                 rate=11025;
155                 frag_size=128;  /* assuming stereo 8 bit */
156         } else {
157                 Con_Printf("ALSA: desired rates not supported\n");
158                 goto error_2;
159         }
160         if (cinfo.formats & SND_PCM_FMT_S16_LE) {
161                 format=SND_PCM_SFMT_S16_LE;
162                 bps=16;
163                 frag_size*=2;
164         } else if (cinfo.formats & SND_PCM_FMT_U8) {
165                 format=SND_PCM_SFMT_U8;
166                 bps=8;
167         } else {
168                 Con_Printf("ALSA: desired formats not supported\n");
169                 goto error_2;
170         }
171         if (cinfo.max_voices>=2) {
172                 stereo=1;
173         } else {
174                 stereo=0;
175                 frag_size/=2;
176         }
177
178 //      err_msg="audio flush";
179 //      if ((rc=snd_pcm_channel_flush(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
180 //              goto error;
181         err_msg="audio munmap";
182         if ((rc=snd_pcm_munmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
183                 goto error;
184
185         memset(&params, 0, sizeof(params));
186         params.channel = SND_PCM_CHANNEL_PLAYBACK;
187         params.mode = SND_PCM_MODE_BLOCK;
188         params.format.interleave=1;
189         params.format.format=format;
190         params.format.rate=rate;
191         params.format.voices=stereo+1;
192         params.start_mode = SND_PCM_START_GO;
193         params.stop_mode = SND_PCM_STOP_ROLLOVER;
194         params.buf.block.frag_size=frag_size;
195         params.buf.block.frags_min=1;
196         params.buf.block.frags_max=-1;
197 //      err_msg="audio flush";
198 //      if ((rc=snd_pcm_channel_flush(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
199 //              goto error;
200         err_msg="audio params";
201         if ((rc=snd_pcm_channel_params(pcm_handle, &params))<0)
202                 goto error;
203
204         err_msg="audio mmap";
205         if ((rc=snd_pcm_mmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK, &mmap_control, (void **)&mmap_data))<0)
206                 goto error;
207         err_msg="audio prepare";
208         if ((rc=snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
209                 goto error;
210
211         memset(&setup, 0, sizeof(setup));
212         setup.mode = SND_PCM_MODE_BLOCK;
213         setup.channel = SND_PCM_CHANNEL_PLAYBACK;
214         err_msg="audio setup";
215         if ((rc=snd_pcm_channel_setup(pcm_handle, &setup))<0)
216                 goto error;
217
218         shm=&sn;
219         memset((dma_t*)shm,0,sizeof(*shm));
220     shm->splitbuffer = 0;
221         shm->channels=setup.format.voices;
222         shm->submission_chunk=128;                                      // don't mix less than this #
223         shm->samplepos=0;                                                       // in mono samples
224         shm->samplebits=setup.format.format==SND_PCM_SFMT_S16_LE?16:8;
225         shm->samples=setup.buf.block.frags*setup.buf.block.frag_size/(shm->samplebits/8);       // mono samples in buffer
226         shm->speed=setup.format.rate;
227         shm->buffer=(unsigned char*)mmap_data;
228     Con_Printf("%5d stereo\n", shm->channels - 1);
229     Con_Printf("%5d samples\n", shm->samples);
230     Con_Printf("%5d samplepos\n", shm->samplepos);
231     Con_Printf("%5d samplebits\n", shm->samplebits);
232     Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
233     Con_Printf("%5d speed\n", shm->speed);
234     Con_Printf("0x%x dma buffer\n", (int)shm->buffer);
235         Con_Printf("%5d total_channels\n", total_channels);
236
237         snd_inited=1;
238         return 1;
239  error:
240         Con_Printf("Error: %s: %s\n", err_msg, snd_strerror(rc));
241  error_2:
242         snd_pcm_close(pcm_handle);
243         return 0;
244 }
245
246 int SNDDMA_GetDMAPos(void)
247 {
248         if (!snd_inited) return 0;
249         shm->samplepos=(mmap_control->status.frag_io+1)*setup.buf.block.frag_size/(shm->samplebits/8);
250         return shm->samplepos;
251 }
252
253 void SNDDMA_Shutdown(void)
254 {
255         if (snd_inited)
256         {
257                 snd_pcm_close(pcm_handle);
258                 snd_inited = 0;
259         }
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         int count=paintedtime-soundtime;
272         int i,s,e;
273         int rc;
274
275         count+=setup.buf.block.frag_size-1;
276         count/=setup.buf.block.frag_size;
277         s=soundtime/setup.buf.block.frag_size;
278         e=s+count;
279         for (i=s; i<e; i++)
280                 mmap_control->fragments[i % setup.buf.block.frags].data=1;
281         switch (mmap_control->status.status) {
282         case SND_PCM_STATUS_PREPARED:
283                 if ((rc=snd_pcm_channel_go(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0) {
284                         fprintf(stderr, "unable to start playback. %s\n",
285                                         snd_strerror(rc));
286                         exit(1);
287                 }
288                 break;
289         case SND_PCM_STATUS_RUNNING:
290                 break;
291         case SND_PCM_STATUS_UNDERRUN:
292                 if ((rc=snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0) {
293                         fprintf(stderr, "underrun: playback channel prepare error. %s\n",
294                                         snd_strerror(rc));
295                         exit(1);
296                 }
297                 break;
298         default:
299                 break;
300         }
301 }
302