1 /***************************************************************************
4 begin : Sat Mar 01 2003
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8 /***************************************************************************
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. See also the license.txt file for *
14 * additional informations. *
16 ***************************************************************************/
20 #define ALSA_PCM_NEW_HW_PARAMS_API
21 #define ALSA_PCM_NEW_SW_PARAMS_API
22 #include <alsa/asoundlib.h>
25 static snd_pcm_t *handle = NULL;
26 static snd_pcm_uframes_t buffer_size;
28 static void alsa_finish(void);
31 static int alsa_init(void)
33 snd_pcm_hw_params_t *hwparams;
34 snd_pcm_status_t *status;
35 snd_ctl_t *ctl_handle = NULL;
36 snd_ctl_card_info_t *info;
40 unsigned int buffer_time = 100000;
41 unsigned int period_time = buffer_time / 4;
42 const char *alsa_name = "default";
47 name = getenv("ALSA_NAME");
51 snd_ctl_card_info_alloca(&info);
52 if ((err = snd_ctl_open(&ctl_handle, alsa_name, 0)) < 0) {
53 printf("control open: %s\n", snd_strerror(err));
55 else if ((err = snd_ctl_card_info(ctl_handle, info)) < 0) {
56 printf("control info: %s\n", snd_strerror(err));
57 snd_ctl_card_info_clear(info);
59 if (ctl_handle != NULL)
60 snd_ctl_close(ctl_handle);
62 name = snd_ctl_card_info_get_name(info);
64 if (strcasecmp(name, "PulseAudio") == 0) {
65 // PulseAudio's ALSA emulation is known to be broken..
66 printf("alsa: refusing to run under PulseAudio's emulation\n");
70 printf("alsa: using '%s', set ALSA_NAME to change\n", name);
77 format = SND_PCM_FORMAT_S16;
79 if ((err = snd_pcm_open(&handle, alsa_name,
80 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0)
82 printf("Audio open error: %s\n", snd_strerror(err));
86 if((err = snd_pcm_nonblock(handle, 0))<0)
88 printf("Can't set blocking moded: %s\n", snd_strerror(err));
92 snd_pcm_hw_params_alloca(&hwparams);
94 if((err=snd_pcm_hw_params_any(handle, hwparams))<0)
96 printf("Broken configuration for this PCM: %s\n", snd_strerror(err));
100 if((err=snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED))<0)
102 printf("Access type not available: %s\n", snd_strerror(err));
106 if((err=snd_pcm_hw_params_set_format(handle, hwparams, format))<0)
108 printf("Sample format not available: %s\n", snd_strerror(err));
112 if((err=snd_pcm_hw_params_set_channels(handle, hwparams, pchannels))<0)
114 printf("Channels count not available: %s\n", snd_strerror(err));
118 if((err=snd_pcm_hw_params_set_rate_near(handle, hwparams, &pspeed, 0))<0)
120 printf("Rate not available: %s\n", snd_strerror(err));
124 if((err=snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0))<0)
126 printf("Buffer time error: %s\n", snd_strerror(err));
130 if((err=snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0))<0)
132 printf("Period time error: %s\n", snd_strerror(err));
136 if((err=snd_pcm_hw_params(handle, hwparams))<0)
138 printf("Unable to install hw params: %s\n", snd_strerror(err));
142 snd_pcm_status_alloca(&status);
143 if((err=snd_pcm_status(handle, status))<0)
145 printf("Unable to get status: %s\n", snd_strerror(err));
149 buffer_size = snd_pcm_status_get_avail(status);
159 static void alsa_finish(void)
163 snd_pcm_drop(handle);
164 snd_pcm_close(handle);
169 // GET BYTES BUFFERED
170 static int alsa_busy(void)
174 if (handle == NULL) // failed to open?
176 l = snd_pcm_avail(handle);
178 if (l < buffer_size / 2) // can we write in at least the half of fragments?
179 l = 1; // -> no? wait
180 else l = 0; // -> else go on
186 static void alsa_feed(void *pSound, int lBytes)
190 if (handle == NULL) return;
192 if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN)
194 memset(sbuf, 0, sizeof(sbuf));
195 snd_pcm_prepare(handle);
196 snd_pcm_writei(handle, sbuf, sizeof(sbuf) / 4);
197 snd_pcm_writei(handle, sbuf, sizeof(sbuf) / 4);
198 snd_pcm_writei(handle, sbuf, sizeof(sbuf) / 4);
202 int l = snd_pcm_avail(handle);
212 snd_pcm_writei(handle, pSound, lBytes / 4);
215 void out_register_alsa(struct out_driver *drv)
218 drv->init = alsa_init;
219 drv->finish = alsa_finish;
220 drv->busy = alsa_busy;
221 drv->feed = alsa_feed;