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 ***************************************************************************/
19 #define ALSA_PCM_NEW_HW_PARAMS_API
20 #define ALSA_PCM_NEW_SW_PARAMS_API
21 #include <alsa/asoundlib.h>
24 static snd_pcm_t *handle = NULL;
25 static snd_pcm_uframes_t buffer_size;
27 static void alsa_finish(void);
30 static int alsa_init(void)
32 snd_pcm_hw_params_t *hwparams;
33 snd_pcm_status_t *status;
34 snd_ctl_t *ctl_handle = NULL;
35 snd_ctl_card_info_t *info;
39 unsigned int buffer_time = 100000;
40 unsigned int period_time = buffer_time / 4;
41 const char *alsa_name = "default";
46 name = getenv("ALSA_NAME");
50 snd_ctl_card_info_alloca(&info);
51 if ((err = snd_ctl_open(&ctl_handle, alsa_name, 0)) < 0) {
52 printf("control open: %s\n", snd_strerror(err));
54 else if ((err = snd_ctl_card_info(ctl_handle, info)) < 0) {
55 printf("control info: %s\n", snd_strerror(err));
56 snd_ctl_card_info_clear(info);
58 if (ctl_handle != NULL)
59 snd_ctl_close(ctl_handle);
61 name = snd_ctl_card_info_get_name(info);
63 if (strcasecmp(name, "PulseAudio") == 0) {
64 // PulseAudio's ALSA emulation is known to be broken..
65 printf("alsa: refusing to run under PulseAudio's emulation\n");
69 printf("alsa: using '%s', set ALSA_NAME to change\n", name);
76 format = SND_PCM_FORMAT_S16;
78 if ((err = snd_pcm_open(&handle, alsa_name,
79 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0)
81 printf("Audio open error: %s\n", snd_strerror(err));
85 if((err = snd_pcm_nonblock(handle, 0))<0)
87 printf("Can't set blocking moded: %s\n", snd_strerror(err));
91 snd_pcm_hw_params_alloca(&hwparams);
93 if((err=snd_pcm_hw_params_any(handle, hwparams))<0)
95 printf("Broken configuration for this PCM: %s\n", snd_strerror(err));
99 if((err=snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED))<0)
101 printf("Access type not available: %s\n", snd_strerror(err));
105 if((err=snd_pcm_hw_params_set_format(handle, hwparams, format))<0)
107 printf("Sample format not available: %s\n", snd_strerror(err));
111 if((err=snd_pcm_hw_params_set_channels(handle, hwparams, pchannels))<0)
113 printf("Channels count not available: %s\n", snd_strerror(err));
117 if((err=snd_pcm_hw_params_set_rate_near(handle, hwparams, &pspeed, 0))<0)
119 printf("Rate not available: %s\n", snd_strerror(err));
123 if((err=snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0))<0)
125 printf("Buffer time error: %s\n", snd_strerror(err));
129 if((err=snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0))<0)
131 printf("Period time error: %s\n", snd_strerror(err));
135 if((err=snd_pcm_hw_params(handle, hwparams))<0)
137 printf("Unable to install hw params: %s\n", snd_strerror(err));
141 snd_pcm_status_alloca(&status);
142 if((err=snd_pcm_status(handle, status))<0)
144 printf("Unable to get status: %s\n", snd_strerror(err));
148 buffer_size = snd_pcm_status_get_avail(status);
158 static void alsa_finish(void)
162 snd_pcm_drop(handle);
163 snd_pcm_close(handle);
168 // GET BYTES BUFFERED
169 static int alsa_busy(void)
173 if (handle == NULL) // failed to open?
175 l = snd_pcm_avail(handle);
177 if (l < buffer_size / 2) // can we write in at least the half of fragments?
178 l = 1; // -> no? wait
179 else l = 0; // -> else go on
185 static void alsa_feed(void *pSound, int lBytes)
187 if (handle == NULL) return;
189 if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN)
190 snd_pcm_prepare(handle);
191 snd_pcm_writei(handle,pSound, lBytes / 4);
194 void out_register_alsa(struct out_driver *drv)
197 drv->init = alsa_init;
198 drv->finish = alsa_finish;
199 drv->busy = alsa_busy;
200 drv->feed = alsa_feed;