2 #include "audio-alsa.h"
4 PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
7 snd_pcm_hw_params_t *params;
10 if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
13 snd_pcm_hw_params_malloc (¶ms);
14 snd_pcm_hw_params_any (dev, params);
15 snd_pcm_hw_params_set_access (dev, params, SND_PCM_ACCESS_RW_INTERLEAVED);
16 snd_pcm_hw_params_set_format (dev, params, SND_PCM_FORMAT_S16_LE);
17 snd_pcm_hw_params_set_rate_near (dev, params, &ctx->rate, 0);
18 snd_pcm_hw_params_set_channels (dev, params, ctx->channels);
19 if (snd_pcm_hw_params (dev, params) < 0) {
20 snd_pcm_hw_params_free (params);
23 snd_pcm_prepare (dev);
25 /* allocate the global array if it hasn't been done */
27 dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T));
28 dev_ctx[0] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
29 dev_ctx[0]->name = NULL;
30 dev_ctx[0]->dev = NULL;
33 /* is a card with similar name already opened ? */
34 for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) {
35 if (dev_ctx[num]->name &&
36 !strcmp (dev_ctx[num]->name, name))
41 /* it's not... let us add it to the global array */
42 dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
43 dev_ctx[num]->name = strdup (name);
44 dev_ctx[num]->dev = dev;
45 dev_ctx[num]->params = params;
50 PUBLIC void _alsa_free (const char *name) {
54 for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) {
55 if (dev_ctx[num]->name &&
56 !strcmp (dev_ctx[num]->name, name)) {
57 snd_pcm_close (dev_ctx[num]->dev);
58 snd_pcm_hw_params_free (dev_ctx[num]->params);
59 free (dev_ctx[num]->name);
60 dev_ctx[num]->name = NULL;
61 dev_ctx[num]->dev = NULL;
68 PUBLIC void _alsa_play (unsigned int num, void *buf, int len) {
70 if (!dev_ctx || !dev_ctx[num])
73 int16_t *cbuf = (int16_t *)buf;
77 if ((res = snd_pcm_writei (dev_ctx[num]->dev, cbuf, frames)) != frames) {
78 snd_pcm_recover (dev_ctx[num]->dev, res, 0);
79 snd_pcm_prepare (dev_ctx[num]->dev);
81 /* snd_pcm_drain (dev_ctx[num]->dev); */