4 Support for ALSA 0.9 sound driver (cvs development version)
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>
42 static int snd_inited;
44 static snd_pcm_t *pcm;
45 static const snd_pcm_channel_area_t *mmap_areas;
46 static char *pcmname = NULL;
49 qboolean SNDDMA_Init(void)
52 int rate=-1,bps=-1,stereo=-1,frag_size;
53 snd_pcm_hw_params_t *hw;
54 snd_pcm_sw_params_t *sw;
55 snd_pcm_hw_params_alloca(&hw);
56 snd_pcm_sw_params_alloca(&sw);
58 if ((i=COM_CheckParm("-sndpcm"))!=0) {
59 pcmname=com_argv[i+1];
61 if ((i=COM_CheckParm("-sndbits")) != 0) {
62 bps = atoi(com_argv[i+1]);
63 if (bps != 16 && bps != 8) {
64 Con_Printf("Error: invalid sample bits: %d\n", i);
68 if ((i=COM_CheckParm("-sndspeed")) != 0) {
69 rate = atoi(com_argv[i+1]);
70 if (rate!=44100 && rate!=22050 && rate!=11025) {
71 Con_Printf("Error: invalid sample rate: %d\n", rate);
75 if ((i=COM_CheckParm("-sndmono")) != 0) {
78 if ((i=COM_CheckParm("-sndstereo")) != 0) {
83 if ((err=snd_pcm_open(&pcm, pcmname,
84 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK))<0) {
85 Con_Printf("Error: audio open error: %s\n", snd_strerror(err));
89 Con_Printf("Using PCM %s.\n", pcmname);
90 snd_pcm_hw_params_any(pcm, hw);
95 if (snd_pcm_hw_params_set_rate_near(pcm, hw, 44100, 0) >= 0) {
96 frag_size = 256; /* assuming stereo 8 bit */
98 } else if (snd_pcm_hw_params_set_rate_near(pcm, hw, 22050, 0) >= 0) {
99 frag_size = 128; /* assuming stereo 8 bit */
101 } else if (snd_pcm_hw_params_set_rate_near(pcm, hw, 11025, 0) >= 0) {
102 frag_size = 64; /* assuming stereo 8 bit */
105 Con_Printf("ALSA: no useable rates\n");
112 if (snd_pcm_hw_params_set_rate_near(pcm, hw, rate, 0) >= 0) {
113 frag_size = 64 * rate / 11025; /* assuming stereo 8 bit */
118 Con_Printf("ALSA: desired rate not supported\n");
124 if (snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_S16_LE) >= 0) {
126 } else if (snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_U8) >= 0) {
129 Con_Printf("ALSA: no useable formats\n");
135 if (snd_pcm_hw_params_set_format(pcm, hw,
136 bps == 8 ? SND_PCM_FORMAT_U8 :
137 SND_PCM_FORMAT_S16) >= 0) {
142 Con_Printf("ALSA: desired format not supported\n");
146 if (snd_pcm_hw_params_set_access(pcm, hw,
147 SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) {
148 Con_Printf("ALSA: interleaved is not supported\n");
154 if (snd_pcm_hw_params_set_channels(pcm, hw, 2) >= 0) {
156 } else if (snd_pcm_hw_params_set_channels(pcm, hw, 1) >= 0) {
159 Con_Printf("ALSA: no useable channels\n");
165 if (snd_pcm_hw_params_set_channels(pcm, hw, stereo ? 2 : 1) >= 0)
169 Con_Printf("ALSA: desired channels not supported\n");
173 snd_pcm_hw_params_set_period_size_near(pcm, hw, frag_size, 0);
175 err = snd_pcm_hw_params(pcm, hw);
177 Con_Printf("ALSA: unable to install hw params\n");
181 snd_pcm_sw_params_current(pcm, sw);
182 snd_pcm_sw_params_set_start_mode(pcm, sw, SND_PCM_START_EXPLICIT);
183 snd_pcm_sw_params_set_xrun_mode(pcm, sw, SND_PCM_XRUN_NONE);
185 err = snd_pcm_sw_params(pcm, sw);
187 Con_Printf("ALSA: unable to install sw params\n");
191 mmap_areas = snd_pcm_mmap_running_areas(pcm);
194 memset((dma_t*)shm,0,sizeof(*shm));
195 shm->splitbuffer = 0;
196 shm->channels=stereo+1;
197 shm->submission_chunk=snd_pcm_hw_params_get_period_size(hw, 0); // don't mix less than this #
198 shm->samplepos=0; // in mono samples
200 buffer_size = snd_pcm_hw_params_get_buffer_size(hw);
201 shm->samples=buffer_size*shm->channels; // mono samples in buffer
203 shm->buffer=(unsigned char*)mmap_areas->addr;
204 Con_Printf("%5d stereo\n", shm->channels - 1);
205 Con_Printf("%5d samples\n", shm->samples);
206 Con_Printf("%5d samplepos\n", shm->samplepos);
207 Con_Printf("%5d samplebits\n", shm->samplebits);
208 Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
209 Con_Printf("%5d speed\n", shm->speed);
210 Con_Printf("0x%x dma buffer\n", (int)shm->buffer);
211 Con_Printf("%5d total_channels\n", total_channels);
224 snd_pcm_sframes_t delay;
227 if (snd_pcm_state (pcm) != SND_PCM_STATE_RUNNING)
229 app_ptr = snd_pcm_mmap_offset (pcm);
230 snd_pcm_delay (pcm, &delay);
231 hw_ptr = app_ptr - delay;
233 hw_ptr += buffer_size;
237 int SNDDMA_GetDMAPos(void)
241 if (!snd_inited) return 0;
243 hw_ptr = get_hw_ptr();
244 hw_ptr *= shm->channels;
245 shm->samplepos = hw_ptr;
246 return shm->samplepos;
249 void SNDDMA_Shutdown(void)
262 Send sound to device if buffer isn't really the dma buffer
265 void SNDDMA_Submit(void)
267 int count = paintedtime - soundtime;
274 state = snd_pcm_state (pcm);
277 case SND_PCM_STATE_PREPARED:
278 snd_pcm_mmap_forward (pcm, count);
281 case SND_PCM_STATE_RUNNING:
282 hw_ptr = get_hw_ptr();
283 missed = hw_ptr - shm->samplepos / shm->channels;
285 missed += buffer_size;
287 offset = snd_pcm_mmap_offset (pcm);
289 count -= (offset - hw_ptr);
291 count -= (buffer_size - hw_ptr + offset);
293 snd_pcm_rewind (pcm, -count);
295 avail = snd_pcm_avail_update(pcm);
300 snd_pcm_mmap_forward (pcm, count);
308 void *S_LockBuffer(void)
313 void S_UnlockBuffer(void)