2 Copyright (C) 2006 Mathieu Olivier
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to:
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA
24 // ALSA module, used by Linux
28 #include <alsa/asoundlib.h>
35 static snd_pcm_t* pcm_handle = NULL;
36 static snd_pcm_sframes_t expected_delay = 0;
37 static unsigned int alsasoundtime;
39 static snd_seq_t* seq_handle = NULL;
45 Create "snd_renderbuffer" with the proper sound format if the call is successful
46 May return a suggested format if the requested format isn't available
49 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested)
51 const char* pcm_name, *seq_name;
52 int i, err, seq_client, seq_port;
53 snd_pcm_hw_params_t* hw_params = NULL;
54 snd_pcm_format_t snd_pcm_format;
55 snd_pcm_uframes_t buffer_size;
57 Con_Print ("SndSys_Init: using the ALSA module\n");
60 // COMMANDLINEOPTION: Linux ALSA Sound: -sndseqin <client>:<port> selects which sequencer port to use for input, by default no sequencer port is used (MIDI note events from that port get mapped to MIDINOTE<n> keys that can be bound)
61 i = COM_CheckParm ("-sndseqin"); // TODO turn this into a cvar, maybe
62 if (i != 0 && i < com_argc - 1)
63 seq_name = com_argv[i + 1];
66 seq_client = atoi(seq_name);
68 if(strchr(seq_name, ':'))
69 seq_port = atoi(strchr(seq_name, ':') + 1);
70 Con_Printf ("SndSys_Init: seq input port has been set to \"%d:%d\". Enabling sequencer input...\n", seq_client, seq_port);
71 err = snd_seq_open (&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0);
74 Con_Print ("SndSys_Init: can't open seq device\n");
77 err = snd_seq_set_client_name(seq_handle, gamename);
80 Con_Print ("SndSys_Init: can't set name of seq device\n");
83 err = snd_seq_create_simple_port(seq_handle, gamename, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
86 Con_Print ("SndSys_Init: can't create seq port\n");
89 err = snd_seq_connect_from(seq_handle, 0, seq_client, seq_port);
92 Con_Printf ("SndSys_Init: can't connect to seq port \"%d:%d\"\n", seq_client, seq_port);
95 err = snd_seq_nonblock(seq_handle, 1);
98 Con_Print ("SndSys_Init: can't make seq nonblocking\n");
105 snd_seq_close(seq_handle);
110 // Check the requested sound format
111 if (requested->width < 1 || requested->width > 2)
113 Con_Printf ("SndSys_Init: invalid sound width (%hu)\n",
116 if (suggested != NULL)
118 memcpy (suggested, requested, sizeof (*suggested));
120 if (requested->width < 1)
121 suggested->width = 1;
123 suggested->width = 2;
125 Con_Printf ("SndSys_Init: suggesting sound width = %hu\n",
132 if (pcm_handle != NULL)
134 Con_Print ("SndSys_Init: WARNING: Init called before Shutdown!\n");
138 // Determine the name of the PCM handle we'll use
139 switch (requested->channels)
142 pcm_name = "surround40";
145 pcm_name = "surround51";
148 pcm_name = "surround71";
151 pcm_name = "default";
154 // COMMANDLINEOPTION: Linux ALSA Sound: -sndpcm <devicename> selects which pcm device to use, default is "default"
155 i = COM_CheckParm ("-sndpcm");
156 if (i != 0 && i < com_argc - 1)
157 pcm_name = com_argv[i + 1];
159 // Open the audio device
160 Con_Printf ("SndSys_Init: PCM device is \"%s\"\n", pcm_name);
161 err = snd_pcm_open (&pcm_handle, pcm_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
164 Con_Printf ("SndSys_Init: can't open audio device \"%s\" (%s)\n",
165 pcm_name, snd_strerror (err));
169 // Allocate the hardware parameters
170 err = snd_pcm_hw_params_malloc (&hw_params);
173 Con_Printf ("SndSys_Init: can't allocate hardware parameters (%s)\n",
177 err = snd_pcm_hw_params_any (pcm_handle, hw_params);
180 Con_Printf ("SndSys_Init: can't initialize hardware parameters (%s)\n",
185 // Set the access type
186 err = snd_pcm_hw_params_set_access (pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
189 Con_Printf ("SndSys_Init: can't set access type (%s)\n",
194 // Set the sound width
195 if (requested->width == 1)
196 snd_pcm_format = SND_PCM_FORMAT_U8;
198 snd_pcm_format = SND_PCM_FORMAT_S16;
199 err = snd_pcm_hw_params_set_format (pcm_handle, hw_params, snd_pcm_format);
202 Con_Printf ("SndSys_Init: can't set sound width to %hu (%s)\n",
203 requested->width, snd_strerror (err));
207 // Set the sound channels
208 err = snd_pcm_hw_params_set_channels (pcm_handle, hw_params, requested->channels);
211 Con_Printf ("SndSys_Init: can't set sound channels to %hu (%s)\n",
212 requested->channels, snd_strerror (err));
216 // Set the sound speed
217 err = snd_pcm_hw_params_set_rate (pcm_handle, hw_params, requested->speed, 0);
220 Con_Printf ("SndSys_Init: can't set sound speed to %u (%s)\n",
221 requested->speed, snd_strerror (err));
225 // pick a buffer size that is a power of 2 (by masking off low bits)
226 buffer_size = i = (int)(requested->speed * 0.15f);
227 while (buffer_size & (buffer_size-1))
228 buffer_size &= (buffer_size-1);
229 // then check if it is the nearest power of 2 and bump it up if not
230 if (i - buffer_size >= buffer_size >> 1)
233 err = snd_pcm_hw_params_set_buffer_size_near (pcm_handle, hw_params, &buffer_size);
236 Con_Printf ("SndSys_Init: can't set sound buffer size to %lu (%s)\n",
237 buffer_size, snd_strerror (err));
241 // pick a period size near the buffer_size we got from ALSA
242 snd_pcm_hw_params_get_buffer_size (hw_params, &buffer_size);
243 buffer_size /= NB_PERIODS;
244 err = snd_pcm_hw_params_set_period_size_near(pcm_handle, hw_params, &buffer_size, 0);
247 Con_Printf ("SndSys_Init: can't set sound period size to %lu (%s)\n",
248 buffer_size, snd_strerror (err));
252 err = snd_pcm_hw_params (pcm_handle, hw_params);
255 Con_Printf ("SndSys_Init: can't set hardware parameters (%s)\n",
260 snd_pcm_hw_params_free (hw_params);
262 snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL);
265 if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO)
266 Cvar_SetValueQuick (&snd_channellayout, SND_CHANNELLAYOUT_ALSA);
271 // It's not very clean, but it avoids a lot of duplicated code.
274 if (hw_params != NULL)
275 snd_pcm_hw_params_free (hw_params);
277 snd_pcm_close(pcm_handle);
288 Stop the sound card, delete "snd_renderbuffer" and free its other resources
291 void SndSys_Shutdown (void)
293 if (seq_handle != NULL)
295 snd_seq_close(seq_handle);
299 if (pcm_handle != NULL)
301 snd_pcm_close(pcm_handle);
305 if (snd_renderbuffer != NULL)
307 Mem_Free(snd_renderbuffer->ring);
308 Mem_Free(snd_renderbuffer);
309 snd_renderbuffer = NULL;
318 Try to recover from errors
321 static qboolean SndSys_Recover (int err_num)
325 // We can only do something on underrun ("broken pipe") errors
326 if (err_num != -EPIPE)
329 err = snd_pcm_prepare (pcm_handle);
332 Con_Printf ("SndSys_Recover: unable to recover (%s)\n",
335 // TOCHECK: should we stop the playback ?
349 static snd_pcm_sframes_t SndSys_Write (const unsigned char* buffer, unsigned int nbframes)
351 snd_pcm_sframes_t written;
353 written = snd_pcm_writei (pcm_handle, buffer, nbframes);
356 if (developer_insane.integer && vid_activewindow)
357 Con_DPrintf ("SndSys_Write: audio write returned %ld (%s)!\n",
358 written, snd_strerror (written));
360 if (SndSys_Recover (written))
362 written = snd_pcm_writei (pcm_handle, buffer, nbframes);
364 Con_DPrintf ("SndSys_Write: audio write failed again (error %ld: %s)!\n",
365 written, snd_strerror (written));
370 snd_renderbuffer->startframe += written;
371 expected_delay += written;
382 Submit the contents of "snd_renderbuffer" to the sound card
385 void SndSys_Submit (void)
387 unsigned int startoffset, factor;
388 snd_pcm_uframes_t limit, nbframes;
389 snd_pcm_sframes_t written;
391 if (pcm_handle == NULL ||
392 snd_renderbuffer->startframe == snd_renderbuffer->endframe)
395 startoffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
396 factor = snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
397 limit = snd_renderbuffer->maxframes - startoffset;
398 nbframes = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
400 if (nbframes > limit)
402 written = SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], limit);
403 if (written < 0 || (snd_pcm_uframes_t)written != limit)
410 written = SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], nbframes);
420 Returns the number of sample frames consumed since the sound started
423 unsigned int SndSys_GetSoundTime (void)
425 snd_pcm_sframes_t delay, timediff;
428 if (pcm_handle == NULL)
431 err = snd_pcm_delay (pcm_handle, &delay);
434 if (developer_insane.integer && vid_activewindow)
435 Con_DPrintf ("SndSys_GetSoundTime: can't get playback delay (%s)\n",
438 if (! SndSys_Recover (err))
441 err = snd_pcm_delay (pcm_handle, &delay);
444 Con_DPrintf ("SndSys_GetSoundTime: can't get playback delay, again (%s)\n",
450 if (expected_delay < delay)
452 Con_DPrintf ("SndSys_GetSoundTime: expected_delay(%ld) < delay(%ld)\n",
453 expected_delay, delay);
457 timediff = expected_delay - delay;
458 expected_delay = delay;
460 alsasoundtime += (unsigned int)timediff;
462 return alsasoundtime;
468 SndSys_LockRenderBuffer
470 Get the exclusive lock on "snd_renderbuffer"
473 qboolean SndSys_LockRenderBuffer (void)
482 SndSys_UnlockRenderBuffer
484 Release the exclusive lock on "snd_renderbuffer"
487 void SndSys_UnlockRenderBuffer (void)
496 Send keyboard events originating from the sound system (e.g. MIDI)
499 void SndSys_SendKeyEvents(void)
501 snd_seq_event_t *event;
506 if(snd_seq_event_input(seq_handle, &event) <= 0)
512 case SND_SEQ_EVENT_NOTEON:
513 if(event->data.note.velocity)
515 Key_Event(K_MIDINOTE0 + event->data.note.note, 0, true);
518 case SND_SEQ_EVENT_NOTEOFF:
519 Key_Event(K_MIDINOTE0 + event->data.note.note, 0, false);