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;
28 static int alsa_init(void)
30 snd_pcm_hw_params_t *hwparams;
31 snd_pcm_status_t *status;
35 unsigned int buffer_time = 100000;
36 unsigned int period_time = buffer_time / 4;
42 format = SND_PCM_FORMAT_S16;
44 if ((err = snd_pcm_open(&handle, "default",
45 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0)
47 printf("Audio open error: %s\n", snd_strerror(err));
51 if((err = snd_pcm_nonblock(handle, 0))<0)
53 printf("Can't set blocking moded: %s\n", snd_strerror(err));
57 snd_pcm_hw_params_alloca(&hwparams);
59 if((err=snd_pcm_hw_params_any(handle, hwparams))<0)
61 printf("Broken configuration for this PCM: %s\n", snd_strerror(err));
65 if((err=snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED))<0)
67 printf("Access type not available: %s\n", snd_strerror(err));
71 if((err=snd_pcm_hw_params_set_format(handle, hwparams, format))<0)
73 printf("Sample format not available: %s\n", snd_strerror(err));
77 if((err=snd_pcm_hw_params_set_channels(handle, hwparams, pchannels))<0)
79 printf("Channels count not available: %s\n", snd_strerror(err));
83 if((err=snd_pcm_hw_params_set_rate_near(handle, hwparams, &pspeed, 0))<0)
85 printf("Rate not available: %s\n", snd_strerror(err));
89 if((err=snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0))<0)
91 printf("Buffer time error: %s\n", snd_strerror(err));
95 if((err=snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0))<0)
97 printf("Period time error: %s\n", snd_strerror(err));
101 if((err=snd_pcm_hw_params(handle, hwparams))<0)
103 printf("Unable to install hw params: %s\n", snd_strerror(err));
107 snd_pcm_status_alloca(&status);
108 if((err=snd_pcm_status(handle, status))<0)
110 printf("Unable to get status: %s\n", snd_strerror(err));
114 buffer_size = snd_pcm_status_get_avail(status);
119 static void alsa_finish(void)
123 snd_pcm_drop(handle);
124 snd_pcm_close(handle);
129 // GET BYTES BUFFERED
130 static int alsa_busy(void)
134 if (handle == NULL) // failed to open?
136 l = snd_pcm_avail(handle);
138 if (l < buffer_size / 2) // can we write in at least the half of fragments?
139 l = 1; // -> no? wait
140 else l = 0; // -> else go on
146 static void alsa_feed(void *pSound, int lBytes)
148 if (handle == NULL) return;
150 if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN)
151 snd_pcm_prepare(handle);
152 snd_pcm_writei(handle,pSound, lBytes / 4);
155 void out_register_alsa(struct out_driver *drv)
158 drv->init = alsa_init;
159 drv->finish = alsa_finish;
160 drv->busy = alsa_busy;
161 drv->feed = alsa_feed;