4 Support for ALSA 0.5, the old stable version of ALSA.
6 Copyright (C) 1999,2000 contributors of the QuakeForge project
7 Please see the file "AUTHORS" for a list of contributors
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.
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.
18 See the GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to:
23 Free Software Foundation, Inc.
24 59 Temple Place - Suite 330
25 Boston, MA 02111-1307, USA
42 #include <sys/types.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
46 #ifdef HAVE_SYS_MMAN_H
47 # include <sys/mman.h>
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>
57 #include <sys/asoundlib.h>
60 # define MAP_FAILED ((void*)-1)
64 static int snd_inited;
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;
74 int check_card(int card)
77 snd_ctl_hw_info_t info;
80 if ((rc = snd_ctl_open(&handle, card)) < 0) {
81 Con_Printf("Error: control open (%i): %s\n", card, snd_strerror(rc));
84 if ((rc = snd_ctl_hw_info(handle, &info)) < 0) {
85 Con_Printf("Error: control hardware info (%i): %s\n", card,
87 snd_ctl_close(handle);
90 snd_ctl_close(handle);
92 for (dev = 0; dev < info.pcmdevs; dev++) {
93 if ((rc=snd_pcm_open(&pcm_handle,card,dev,
95 | SND_PCM_OPEN_NONBLOCK))==0) {
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) {
111 qboolean SNDDMA_Init(void)
115 int rate=-1,format=-1,bps,stereo=-1,frag_size;
118 mask = snd_cards_mask();
120 Con_Printf("No sound cards detected\n");
123 if ((i=COM_CheckParm("-sndcard"))!=0) {
124 card=atoi(com_argv[i+1]);
126 if ((i=COM_CheckParm("-snddev"))!=0) {
127 dev=atoi(com_argv[i+1]);
129 if ((i=COM_CheckParm("-sndbits")) != 0) {
130 i = atoi(com_argv[i+1]);
132 format = SND_PCM_SFMT_S16_LE;
134 format = SND_PCM_SFMT_U8;
136 Con_Printf("Error: invalid sample bits: %d\n", i);
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);
147 if ((i=COM_CheckParm("-sndmono")) != 0) {
151 for (card=0; card<SND_CARDS; card++) {
152 if (!(mask & (1<<card)))
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));
177 Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
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) {
188 frag_size=512; /* assuming stereo 8 bit */
189 } else if ((rate==-1 || rate==22050) && cinfo.rates & SND_PCM_RATE_22050) {
191 frag_size=256; /* assuming stereo 8 bit */
192 } else if ((rate==-1 || rate==11025) && cinfo.rates & SND_PCM_RATE_11025) {
194 frag_size=128; /* assuming stereo 8 bit */
196 Con_Printf("ALSA: desired rates not supported\n");
199 if ((format==-1 || format==SND_PCM_SFMT_S16_LE) && cinfo.formats & SND_PCM_FMT_S16_LE) {
200 format=SND_PCM_SFMT_S16_LE;
203 } else if ((format==-1 || format==SND_PCM_SFMT_U8) && cinfo.formats & SND_PCM_FMT_U8) {
204 format=SND_PCM_SFMT_U8;
207 Con_Printf("ALSA: desired formats not supported\n");
210 if (stereo && cinfo.max_voices>=2) {
217 // err_msg="audio flush";
218 // if ((rc=snd_pcm_channel_flush(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
220 err_msg="audio munmap";
221 if ((rc=snd_pcm_munmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
224 memset(¶ms, 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)
239 err_msg="audio params";
240 if ((rc=snd_pcm_channel_params(pcm_handle, ¶ms))<0)
243 err_msg="audio mmap";
244 if ((rc=snd_pcm_mmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK, &mmap_control, (void **)&mmap_data))<0)
246 err_msg="audio prepare";
247 if ((rc=snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
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)
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);
279 Con_Printf("Error: %s: %s\n", err_msg, snd_strerror(rc));
281 snd_pcm_close(pcm_handle);
285 int SNDDMA_GetDMAPos(void)
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;
292 void SNDDMA_Shutdown(void)
296 snd_pcm_close(pcm_handle);
305 Send sound to device if buffer isn't really the dma buffer
308 void SNDDMA_Submit(void)
310 int count=paintedtime-soundtime;
314 count+=setup.buf.block.frag_size-1;
315 count/=setup.buf.block.frag_size;
316 s=soundtime/setup.buf.block.frag_size;
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",
328 case SND_PCM_STATUS_RUNNING:
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",