4 Support for the ALSA 1.0.1 sound driver
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
29 #include <alsa/asoundlib.h>
34 static int snd_inited;
35 static snd_pcm_uframes_t buffer_size;
37 static const char *pcmname = NULL;
38 static snd_pcm_t *pcm;
40 qboolean SNDDMA_Init (void)
43 int bps = -1, stereo = -1;
44 unsigned int rate = 0;
45 snd_pcm_hw_params_t *hw;
46 snd_pcm_sw_params_t *sw;
47 snd_pcm_uframes_t frag_size;
49 snd_pcm_hw_params_alloca (&hw);
50 snd_pcm_sw_params_alloca (&sw);
52 // COMMANDLINEOPTION: Linux ALSA Sound: -sndpcm <devicename> selects which pcm device to us, default is "default"
53 if ((i=COM_CheckParm("-sndpcm"))!=0)
54 pcmname=com_argv[i+1];
58 // COMMANDLINEOPTION: Linux ALSA Sound: -sndbits <number> sets sound precision to 8 or 16 bit (email me if you want others added)
59 if ((i=COM_CheckParm("-sndbits")) != 0)
61 bps = atoi(com_argv[i+1]);
62 if (bps != 16 && bps != 8)
64 Con_Printf("Error: invalid sample bits: %d\n", bps);
69 // COMMANDLINEOPTION: Linux ALSA Sound: -sndspeed <hz> chooses 44100 hz, 22100 hz, or 11025 hz sound output rate
70 if ((i=COM_CheckParm("-sndspeed")) != 0)
72 rate = atoi(com_argv[i+1]);
73 if (rate!=44100 && rate!=22050 && rate!=11025)
75 Con_Printf("Error: invalid sample rate: %d\n", rate);
80 // COMMANDLINEOPTION: Linux ALSA Sound: -sndmono sets sound output to mono
81 if ((i=COM_CheckParm("-sndmono")) != 0)
83 // COMMANDLINEOPTION: Linux ALSA Sound: -sndstereo sets sound output to stereo
84 if ((i=COM_CheckParm("-sndstereo")) != 0)
87 err = snd_pcm_open (&pcm, pcmname, SND_PCM_STREAM_PLAYBACK,
90 Con_Printf ("Error: audio open error: %s\n", snd_strerror (err));
93 Con_Printf ("ALSA: Using PCM %s.\n", pcmname);
95 err = snd_pcm_hw_params_any (pcm, hw);
97 Con_Printf ("ALSA: error setting hw_params_any. %s\n",
102 err = snd_pcm_hw_params_set_access (pcm, hw,
103 SND_PCM_ACCESS_MMAP_INTERLEAVED);
105 Con_Printf ("ALSA: Failure to set noninterleaved PCM access. %s\n"
106 "Note: Interleaved is not supported\n",
113 err = snd_pcm_hw_params_set_format (pcm, hw,
117 } else if (0 <= (err = snd_pcm_hw_params_set_format (pcm, hw,
118 SND_PCM_FORMAT_U8))) {
121 Con_Printf ("ALSA: no useable formats. %s\n",
128 err = snd_pcm_hw_params_set_format (pcm, hw, bps == 8 ?
132 Con_Printf ("ALSA: no usable formats. %s\n",
138 Con_Printf ("ALSA: desired format not supported\n");
144 err = snd_pcm_hw_params_set_channels (pcm, hw, 2);
147 } else if (0 <= (err = snd_pcm_hw_params_set_channels (pcm, hw,
151 Con_Printf ("ALSA: no usable channels. %s\n",
158 err = snd_pcm_hw_params_set_channels (pcm, hw, stereo ? 2 : 1);
160 Con_Printf ("ALSA: no usable channels. %s\n",
166 Con_Printf ("ALSA: desired channels not supported\n");
173 err = snd_pcm_hw_params_set_rate_near (pcm, hw, &rate, 0);
175 frag_size = 32 * bps;
178 err = snd_pcm_hw_params_set_rate_near (pcm, hw, &rate, 0);
180 frag_size = 16 * bps;
183 err = snd_pcm_hw_params_set_rate_near (pcm, hw, &rate,
188 Con_Printf ("ALSA: no usable rates. %s\n",
198 err = snd_pcm_hw_params_set_rate_near (pcm, hw, &rate, 0);
200 Con_Printf ("ALSA: desired rate %i not supported. %s\n", rate,
204 frag_size = 8 * bps * rate / 11025;
207 Con_Printf ("ALSA: desired rate %i not supported.\n", rate);
211 err = snd_pcm_hw_params_set_period_size_near (pcm, hw, &frag_size, 0);
213 Con_Printf ("ALSA: unable to set period size near %i. %s\n",
214 (int) frag_size, snd_strerror (err));
217 err = snd_pcm_hw_params (pcm, hw);
219 Con_Printf ("ALSA: unable to install hw params: %s\n",
223 err = snd_pcm_sw_params_current (pcm, sw);
225 Con_Printf ("ALSA: unable to determine current sw params. %s\n",
229 err = snd_pcm_sw_params_set_start_threshold (pcm, sw, ~0U);
231 Con_Printf ("ALSA: unable to set playback threshold. %s\n",
235 err = snd_pcm_sw_params_set_stop_threshold (pcm, sw, ~0U);
237 Con_Printf ("ALSA: unable to set playback stop threshold. %s\n",
241 err = snd_pcm_sw_params (pcm, sw);
243 Con_Printf ("ALSA: unable to install sw params. %s\n",
248 shm->format.channels = stereo + 1;
250 shm->format.width = bps / 8;
252 err = snd_pcm_hw_params_get_buffer_size (hw, &buffer_size);
254 Con_Printf ("ALSA: unable to get buffer size. %s\n",
259 shm->samples = buffer_size * shm->format.channels; // mono samples in buffer
260 shm->format.speed = rate;
261 SNDDMA_GetDMAPos (); // sets shm->buffer
271 int SNDDMA_GetDMAPos (void)
273 const snd_pcm_channel_area_t *areas;
274 snd_pcm_uframes_t offset;
275 snd_pcm_uframes_t nframes = shm->samples/shm->format.channels;
280 snd_pcm_avail_update (pcm);
281 snd_pcm_mmap_begin (pcm, &areas, &offset, &nframes);
282 offset *= shm->format.channels;
283 nframes *= shm->format.channels;
284 shm->samplepos = offset;
285 shm->buffer = (unsigned char *)areas->addr;
286 return shm->samplepos;
289 void SNDDMA_Shutdown (void)
300 Send sound to device if buffer isn't really the dma buffer
302 void SNDDMA_Submit (void)
305 int count = paintedtime - soundtime;
306 const snd_pcm_channel_area_t *areas;
307 snd_pcm_uframes_t nframes;
308 snd_pcm_uframes_t offset;
310 nframes = count / shm->format.channels;
312 snd_pcm_avail_update (pcm);
313 snd_pcm_mmap_begin (pcm, &areas, &offset, &nframes);
315 state = snd_pcm_state (pcm);
318 case SND_PCM_STATE_PREPARED:
319 snd_pcm_mmap_commit (pcm, offset, nframes);
322 case SND_PCM_STATE_RUNNING:
323 snd_pcm_mmap_commit (pcm, offset, nframes);
330 void *S_LockBuffer(void)
335 void S_UnlockBuffer(void)