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 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;;
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);
79 int check_card(int card)
82 snd_ctl_hw_info_t info;
85 if ((rc = snd_ctl_hw_open(&handle, card)) < 0) {
86 Con_Printf("Error: control open (%i): %s\n", card, snd_strerror(rc));
89 if ((rc = snd_ctl_hw_info(handle, &info)) < 0) {
90 Con_Printf("Error: control hardware info (%i): %s\n", card,
92 snd_ctl_close(handle);
95 snd_ctl_close(handle);
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) {
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) {
116 qboolean SNDDMA_Init(void)
120 int rate=-1,format=-1,bps,stereo=-1,frag_size,frame_size;
123 mask = snd_cards_mask();
125 Con_Printf("No sound cards detected\n");
128 if ((i=COM_CheckParm("-sndcard"))!=0) {
129 card=atoi(com_argv[i+1]);
131 if ((i=COM_CheckParm("-snddev"))!=0) {
132 dev=atoi(com_argv[i+1]);
134 if ((i=COM_CheckParm("-sndbits")) != 0) {
135 i = atoi(com_argv[i+1]);
137 format = SND_PCM_SFMT_S16_LE;
139 format = SND_PCM_SFMT_U8;
141 Con_Printf("Error: invalid sample bits: %d\n", i);
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);
152 if ((i=COM_CheckParm("-sndmono")) != 0) {
156 for (card=0; card<SND_CARDS; card++) {
157 if (!(mask & (1<<card)))
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));
182 Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
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) {
192 frag_size=256; /* assuming stereo 8 bit */
193 } else if ((rate==-1 || rate==22050) && cpinfo.rates & SND_PCM_RATE_22050) {
195 frag_size=128; /* assuming stereo 8 bit */
196 } else if ((rate==-1 || rate==11025) && cpinfo.rates & SND_PCM_RATE_11025) {
198 frag_size=64; /* assuming stereo 8 bit */
200 Con_Printf("ALSA: desired rates not supported\n");
203 if ((format==-1 || format==SND_PCM_SFMT_S16_LE) && cpinfo.formats & SND_PCM_FMT_S16_LE) {
204 format=SND_PCM_SFMT_S16_LE;
207 } else if ((format==-1 || format==SND_PCM_SFMT_U8) && cpinfo.formats & SND_PCM_FMT_U8) {
208 format=SND_PCM_SFMT_U8;
212 Con_Printf("ALSA: desired formats not supported\n");
215 //XXX can't support non-interleaved stereo
216 if (stereo && (cpinfo.flags & SND_PCM_INFO_INTERLEAVED) && cpinfo.max_channels>=2) {
223 memset(¶ms, 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;
233 params.buffer_size = (2<<16) / frame_size;
234 params.frag_size=frag_size;
235 params.avail_min = frag_size;
237 params.xrun_max = 1024;
238 params.boundary = params.buffer_size;
241 err_msg="audio params";
242 if ((rc=snd_pcm_params(pcm_handle, ¶ms))<0)
245 memset(&setup, 0, sizeof(setup));
246 err_msg="audio setup";
247 if ((rc=snd_pcm_setup(pcm_handle, &setup))<0)
249 if (setup.buffer_size == params.buffer_size)
251 params.boundary = params.buffer_size = setup.buffer_size;
254 err_msg="audio mmap";
255 if ((rc=snd_pcm_mmap(pcm_handle, (void**)&mmap_data))<0)
257 if ((rc=snd_pcm_mmap_status(pcm_handle, &mmap_status))<0)
259 if ((rc=snd_pcm_mmap_control(pcm_handle, &mmap_control))<0)
261 err_msg="audio prepare";
262 if ((rc=snd_pcm_prepare(pcm_handle))<0)
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);
287 Con_Printf("Error: %s: %s\n", err_msg, snd_strerror(rc));
289 snd_pcm_close(pcm_handle);
293 int SNDDMA_GetDMAPos(void)
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;
304 void SNDDMA_Shutdown(void)
308 snd_pcm_close(pcm_handle);
317 Send sound to device if buffer isn't really the dma buffer
320 void SNDDMA_Submit(void)
322 int count=paintedtime-soundtime;
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",
334 case SND_PCM_STATE_RUNNING:
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",