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 munmap";
218 if ((rc=snd_pcm_munmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
221 memset(¶ms, 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, ¶ms))<0)
237 err_msg="audio mmap";
238 if ((rc=snd_pcm_mmap(pcm_handle, SND_PCM_CHANNEL_PLAYBACK, &mmap_control, (void **)&mmap_data))<0)
240 err_msg="audio prepare";
241 if ((rc=snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK))<0)
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)
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);
273 Con_Printf("Error: %s: %s\n", err_msg, snd_strerror(rc));
275 snd_pcm_close(pcm_handle);
279 int SNDDMA_GetDMAPos(void)
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;
286 void SNDDMA_Shutdown(void)
290 snd_pcm_close(pcm_handle);
299 Send sound to device if buffer isn't really the dma buffer
302 void SNDDMA_Submit(void)
304 int count=paintedtime-soundtime;
308 count+=setup.buf.block.frag_size-1;
309 count/=setup.buf.block.frag_size;
310 s=soundtime/setup.buf.block.frag_size;
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",
322 case SND_PCM_STATUS_RUNNING:
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",
336 void *S_LockBuffer(void)
341 void S_UnlockBuffer(void)