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;
46 static const snd_pcm_channel_area_t *mmap_areas;
47 static char *pcmname = NULL;
50 qboolean SNDDMA_Init(void)
53 int rate=-1,bps=-1,stereo=-1,frag_size;
54 snd_pcm_hw_params_t *hw;
55 snd_pcm_sw_params_t *sw;
56 snd_pcm_hw_params_alloca(&hw);
57 snd_pcm_sw_params_alloca(&sw);
59 if ((i=COM_CheckParm("-sndpcm"))!=0) {
60 pcmname=com_argv[i+1];
62 if ((i=COM_CheckParm("-sndbits")) != 0) {
63 bps = atoi(com_argv[i+1]);
64 if (bps != 16 && bps != 8) {
65 Con_Printf("Error: invalid sample bits: %d\n", i);
69 if ((i=COM_CheckParm("-sndspeed")) != 0) {
70 rate = atoi(com_argv[i+1]);
71 if (rate!=44100 && rate!=22050 && rate!=11025) {
72 Con_Printf("Error: invalid sample rate: %d\n", rate);
76 if ((i=COM_CheckParm("-sndmono")) != 0) {
79 if ((i=COM_CheckParm("-sndstereo")) != 0) {
84 if ((err=snd_pcm_open(&pcm, pcmname,
85 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK))<0) {
86 Con_Printf("Error: audio open error: %s\n", snd_strerror(err));
90 Con_Printf("Using PCM %s.\n", pcmname);
91 snd_pcm_hw_params_any(pcm, hw);
96 if (snd_pcm_hw_params_set_rate_near(pcm, hw, 44100, 0) >= 0) {
97 frag_size = 256; /* assuming stereo 8 bit */
99 } else if (snd_pcm_hw_params_set_rate_near(pcm, hw, 22050, 0) >= 0) {
100 frag_size = 128; /* assuming stereo 8 bit */
102 } else if (snd_pcm_hw_params_set_rate_near(pcm, hw, 11025, 0) >= 0) {
103 frag_size = 64; /* assuming stereo 8 bit */
106 Con_Printf("ALSA: no useable rates\n");
113 if (snd_pcm_hw_params_set_rate_near(pcm, hw, rate, 0) >= 0) {
114 frag_size = 64 * rate / 11025; /* assuming stereo 8 bit */
119 Con_Printf("ALSA: desired rate not supported\n");
125 if (snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_S16_LE) >= 0) {
127 } else if (snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_U8) >= 0) {
130 Con_Printf("ALSA: no useable formats\n");
136 if (snd_pcm_hw_params_set_format(pcm, hw,
137 bps == 8 ? SND_PCM_FORMAT_U8 :
138 SND_PCM_FORMAT_S16) >= 0) {
143 Con_Printf("ALSA: desired format not supported\n");
147 if (snd_pcm_hw_params_set_access(pcm, hw,
148 SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) {
149 Con_Printf("ALSA: interleaved is not supported\n");
155 if (snd_pcm_hw_params_set_channels(pcm, hw, 2) >= 0) {
157 } else if (snd_pcm_hw_params_set_channels(pcm, hw, 1) >= 0) {
160 Con_Printf("ALSA: no useable channels\n");
166 if (snd_pcm_hw_params_set_channels(pcm, hw, stereo ? 2 : 1) >= 0)
170 Con_Printf("ALSA: desired channels not supported\n");
174 snd_pcm_hw_params_set_period_size_near(pcm, hw, frag_size, 0);
176 err = snd_pcm_hw_params(pcm, hw);
178 Con_Printf("ALSA: unable to install hw params\n");
182 snd_pcm_sw_params_current(pcm, sw);
183 snd_pcm_sw_params_set_start_mode(pcm, sw, SND_PCM_START_EXPLICIT);
184 snd_pcm_sw_params_set_xrun_mode(pcm, sw, SND_PCM_XRUN_NONE);
186 err = snd_pcm_sw_params(pcm, sw);
188 Con_Printf("ALSA: unable to install sw params\n");
192 mmap_areas = snd_pcm_mmap_running_areas(pcm);
195 memset((dma_t*)shm,0,sizeof(*shm));
196 shm->splitbuffer = 0;
197 shm->channels=stereo+1;
198 shm->submission_chunk=snd_pcm_hw_params_get_period_size(hw, 0); // don't mix less than this #
199 shm->samplepos=0; // in mono samples
201 buffer_size = snd_pcm_hw_params_get_buffer_size(hw);
202 shm->samples=buffer_size*shm->channels; // mono samples in buffer
204 shm->buffer=(unsigned char*)mmap_areas->addr;
205 Con_Printf("%5d stereo\n", shm->channels - 1);
206 Con_Printf("%5d samples\n", shm->samples);
207 Con_Printf("%5d samplepos\n", shm->samplepos);
208 Con_Printf("%5d samplebits\n", shm->samplebits);
209 Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
210 Con_Printf("%5d speed\n", shm->speed);
211 Con_Printf("0x%x dma buffer\n", (int)shm->buffer);
212 Con_Printf("%5d total_channels\n", total_channels);
225 snd_pcm_sframes_t delay;
228 if (snd_pcm_state (pcm) != SND_PCM_STATE_RUNNING)
230 app_ptr = snd_pcm_mmap_offset (pcm);
231 snd_pcm_delay (pcm, &delay);
232 hw_ptr = app_ptr - delay;
234 hw_ptr += buffer_size;
238 int SNDDMA_GetDMAPos(void)
242 if (!snd_inited) return 0;
244 hw_ptr = get_hw_ptr();
245 hw_ptr *= shm->channels;
246 shm->samplepos = hw_ptr;
247 return shm->samplepos;
250 void SNDDMA_Shutdown(void)
263 Send sound to device if buffer isn't really the dma buffer
266 void SNDDMA_Submit(void)
268 int count = paintedtime - soundtime;
275 state = snd_pcm_state (pcm);
278 case SND_PCM_STATE_PREPARED:
279 snd_pcm_mmap_forward (pcm, count);
282 case SND_PCM_STATE_RUNNING:
283 hw_ptr = get_hw_ptr();
284 missed = hw_ptr - shm->samplepos / shm->channels;
286 missed += buffer_size;
288 offset = snd_pcm_mmap_offset (pcm);
290 count -= (offset - hw_ptr);
292 count -= (buffer_size - hw_ptr + offset);
294 snd_pcm_rewind (pcm, -count);
296 avail = snd_pcm_avail_update(pcm);
301 snd_pcm_mmap_forward (pcm, count);