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
36 #include <sys/asoundlib.h>
43 static int snd_inited;
45 static snd_pcm_t *pcm_handle;
46 static snd_pcm_params_info_t cpinfo;
47 static snd_pcm_params_t params;
48 static snd_pcm_setup_t setup;
49 static snd_pcm_channel_area_t mmap_stopped_areas[2];
50 static snd_pcm_channel_area_t mmap_running_areas[2];
51 static int card=-1,dev=-1,subdev=-1;;
53 int check_card(int card)
56 snd_ctl_hw_info_t info;
59 if ((rc = snd_ctl_hw_open(&handle, 0, card)) < 0) {
60 Con_Printf("Error: control open (%i): %s\n", card, snd_strerror(rc));
63 if ((rc = snd_ctl_hw_info(handle, &info)) < 0) {
64 Con_Printf("Error: control hardware info (%i): %s\n", card,
66 snd_ctl_close(handle);
69 snd_ctl_close(handle);
71 for (dev = 0; dev < info.pcmdevs; dev++) {
72 if ((rc=snd_pcm_hw_open_subdevice(&pcm_handle,card,dev,subdev,
73 SND_PCM_STREAM_PLAYBACK,
74 SND_PCM_NONBLOCK))==0) {
79 if (dev>=0 && dev <info.pcmdevs) {
80 if ((rc=snd_pcm_hw_open_subdevice(&pcm_handle,card,dev,subdev,
81 SND_PCM_STREAM_PLAYBACK,
82 SND_PCM_NONBLOCK))==0) {
90 qboolean SNDDMA_Init(void)
94 int rate=-1,format=-1,bps,stereo=-1,frag_size,frame_size;
97 mask = snd_cards_mask();
99 Con_Printf("No sound cards detected\n");
102 if ((i=COM_CheckParm("-sndcard"))!=0) {
103 card=atoi(com_argv[i+1]);
105 if ((i=COM_CheckParm("-snddev"))!=0) {
106 dev=atoi(com_argv[i+1]);
108 if ((i=COM_CheckParm("-sndbits")) != 0) {
109 i = atoi(com_argv[i+1]);
111 format = SND_PCM_SFMT_S16_LE;
113 format = SND_PCM_SFMT_U8;
115 Con_Printf("Error: invalid sample bits: %d\n", i);
119 if ((i=COM_CheckParm("-sndspeed")) != 0) {
120 rate = atoi(com_argv[i+1]);
121 if (rate!=44100 && rate!=22050 && rate!=11025) {
122 Con_Printf("Error: invalid sample rate: %d\n", rate);
126 if ((i=COM_CheckParm("-sndmono")) != 0) {
130 for (card=0; card<SND_CARDS; card++) {
131 if (!(mask & (1<<card)))
147 if ((rc=snd_pcm_hw_open_subdevice(&pcm_handle,card,dev,subdev,
148 SND_PCM_STREAM_PLAYBACK,
149 SND_PCM_NONBLOCK))<0) {
150 Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
156 Con_Printf("Error: audio open error: %s\n", snd_strerror(rc));
160 Con_Printf("Using card %d, device %d.\n", card, dev);
161 memset(&cpinfo, 0, sizeof(cpinfo));
162 snd_pcm_params_info(pcm_handle, &cpinfo);
163 Con_Printf("%08x %08x %08x\n",cpinfo.flags,cpinfo.formats,cpinfo.rates);
164 if ((rate==-1 || rate==44100) && cpinfo.rates & SND_PCM_RATE_44100) {
166 frag_size=256; /* assuming stereo 8 bit */
167 } else if ((rate==-1 || rate==22050) && cpinfo.rates & SND_PCM_RATE_22050) {
169 frag_size=128; /* assuming stereo 8 bit */
170 } else if ((rate==-1 || rate==11025) && cpinfo.rates & SND_PCM_RATE_11025) {
172 frag_size=64; /* assuming stereo 8 bit */
174 Con_Printf("ALSA: desired rates not supported\n");
177 if ((format==-1 || format==SND_PCM_SFMT_S16_LE) && cpinfo.formats & SND_PCM_FMT_S16_LE) {
178 format=SND_PCM_SFMT_S16_LE;
181 } else if ((format==-1 || format==SND_PCM_SFMT_U8) && cpinfo.formats & SND_PCM_FMT_U8) {
182 format=SND_PCM_SFMT_U8;
186 Con_Printf("ALSA: desired formats not supported\n");
189 //XXX can't support non-interleaved stereo
190 if (stereo && (cpinfo.flags & SND_PCM_INFO_INTERLEAVED) && cpinfo.max_channels>=2) {
197 memset(¶ms, 0, sizeof(params));
198 //XXX can't support non-interleaved stereo
199 params.xfer_mode = stereo ? SND_PCM_XFER_INTERLEAVED
200 : SND_PCM_XFER_NONINTERLEAVED;
201 params.mmap_shape = stereo ? SND_PCM_XFER_INTERLEAVED
202 : SND_PCM_XFER_NONINTERLEAVED;
203 params.format.sfmt=format;
204 params.format.rate=rate;
205 params.format.channels=stereo+1;
206 params.start_mode = SND_PCM_START_EXPLICIT;
207 params.xrun_mode = SND_PCM_XRUN_NONE;
209 params.buffer_size = (2<<16) / frame_size;
210 params.frag_size=frag_size;
211 params.avail_min = frag_size;
213 params.boundary = params.buffer_size;
216 err_msg="audio params";
217 if ((rc=snd_pcm_params(pcm_handle, ¶ms))<0)
220 memset(&setup, 0, sizeof(setup));
221 err_msg="audio setup";
222 if ((rc=snd_pcm_setup(pcm_handle, &setup))<0)
224 if (setup.buffer_size == params.buffer_size)
226 params.boundary = params.buffer_size = setup.buffer_size;
229 err_msg="audio mmap";
230 if ((rc=snd_pcm_mmap(pcm_handle))<0)
232 if ((rc=snd_pcm_mmap_get_areas(pcm_handle, mmap_stopped_areas, mmap_running_areas)))
234 err_msg="audio prepare";
235 if ((rc=snd_pcm_prepare(pcm_handle))<0)
239 memset((dma_t*)shm,0,sizeof(*shm));
240 shm->splitbuffer = 0;
241 shm->channels=setup.format.channels;
242 shm->submission_chunk=frag_size; // don't mix less than this #
243 shm->samplepos=0; // in mono samples
244 shm->samplebits=setup.format.sfmt==SND_PCM_SFMT_S16_LE?16:8;
245 shm->samples=setup.buffer_size*shm->channels; // mono samples in buffer
246 shm->speed=setup.format.rate;
247 shm->buffer=(unsigned char*)mmap_running_areas->addr;
248 Con_Printf("%5d stereo\n", shm->channels - 1);
249 Con_Printf("%5d samples\n", shm->samples);
250 Con_Printf("%5d samplepos\n", shm->samplepos);
251 Con_Printf("%5d samplebits\n", shm->samplebits);
252 Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
253 Con_Printf("%5d speed\n", shm->speed);
254 Con_Printf("0x%x dma buffer\n", (int)shm->buffer);
255 Con_Printf("%5d total_channels\n", total_channels);
260 Con_Printf("Error: %s: %s\n", err_msg, snd_strerror(rc));
262 snd_pcm_close(pcm_handle);
273 if (snd_pcm_state (pcm_handle) != SND_PCM_STATE_RUNNING)
275 app_ptr = snd_pcm_mmap_offset (pcm_handle);
276 snd_pcm_delay (pcm_handle, &delay);
277 hw_ptr = app_ptr - delay;
281 int SNDDMA_GetDMAPos(void)
285 if (!snd_inited) return 0;
287 hw_ptr = get_hw_ptr();
288 hw_ptr *= shm->channels;
289 shm->samplepos = hw_ptr;
290 return shm->samplepos;
293 void SNDDMA_Shutdown(void)
297 snd_pcm_close(pcm_handle);
306 Send sound to device if buffer isn't really the dma buffer
309 void SNDDMA_Submit(void)
311 int count = paintedtime - soundtime;
318 state = snd_pcm_state (pcm_handle);
321 case SND_PCM_STATE_PREPARED:
322 snd_pcm_mmap_forward (pcm_handle, count);
323 snd_pcm_start (pcm_handle);
325 case SND_PCM_STATE_RUNNING:
326 hw_ptr = get_hw_ptr();
327 missed = hw_ptr - shm->samplepos / shm->channels;
329 offset = snd_pcm_mmap_offset (pcm_handle);
331 count -= (offset - hw_ptr);
333 count -= (setup.buffer_size - hw_ptr + offset);
335 snd_pcm_rewind (pcm_handle, -count);
336 } else if (count > 0) {
337 avail = snd_pcm_avail_update(pcm_handle);
338 if (avail > 0 && count > avail) {
339 snd_pcm_mmap_forward (pcm_handle, avail);
342 snd_pcm_mmap_forward (pcm_handle, count);