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 ***************************************************************************/
22 #include "externals.h"
24 #define ALSA_PCM_NEW_HW_PARAMS_API
25 #define ALSA_PCM_NEW_SW_PARAMS_API
26 #include <alsa/asoundlib.h>
28 static snd_pcm_t *handle = NULL;
29 static snd_pcm_uframes_t buffer_size;
34 snd_pcm_hw_params_t *hwparams;
35 snd_pcm_status_t *status;
39 unsigned int buffer_time = 100000;
40 unsigned int period_time = buffer_time / 4;
43 if (iDisStereo) pchannels = 1;
47 format = SND_PCM_FORMAT_S16;
49 if ((err = snd_pcm_open(&handle, "default",
50 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0)
52 printf("Audio open error: %s\n", snd_strerror(err));
56 if((err = snd_pcm_nonblock(handle, 0))<0)
58 printf("Can't set blocking moded: %s\n", snd_strerror(err));
62 snd_pcm_hw_params_alloca(&hwparams);
64 if((err=snd_pcm_hw_params_any(handle, hwparams))<0)
66 printf("Broken configuration for this PCM: %s\n", snd_strerror(err));
70 if((err=snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED))<0)
72 printf("Access type not available: %s\n", snd_strerror(err));
76 if((err=snd_pcm_hw_params_set_format(handle, hwparams, format))<0)
78 printf("Sample format not available: %s\n", snd_strerror(err));
82 if((err=snd_pcm_hw_params_set_channels(handle, hwparams, pchannels))<0)
84 printf("Channels count not available: %s\n", snd_strerror(err));
88 if((err=snd_pcm_hw_params_set_rate_near(handle, hwparams, &pspeed, 0))<0)
90 printf("Rate not available: %s\n", snd_strerror(err));
94 if((err=snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0))<0)
96 printf("Buffer time error: %s\n", snd_strerror(err));
100 if((err=snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0))<0)
102 printf("Period time error: %s\n", snd_strerror(err));
106 if((err=snd_pcm_hw_params(handle, hwparams))<0)
108 printf("Unable to install hw params: %s\n", snd_strerror(err));
112 snd_pcm_status_alloca(&status);
113 if((err=snd_pcm_status(handle, status))<0)
115 printf("Unable to get status: %s\n", snd_strerror(err));
119 buffer_size = snd_pcm_status_get_avail(status);
123 void RemoveSound(void)
127 snd_pcm_drop(handle);
128 snd_pcm_close(handle);
133 // GET BYTES BUFFERED
134 unsigned long SoundGetBytesBuffered(void)
138 if (handle == NULL) // failed to open?
140 l = snd_pcm_avail(handle);
142 if (l < buffer_size / 2) // can we write in at least the half of fragments?
143 l = SOUNDSIZE; // -> no? wait
144 else l = 0; // -> else go on
150 void SoundFeedStreamData(unsigned char* pSound,long lBytes)
152 if (handle == NULL) return;
154 if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN)
155 snd_pcm_prepare(handle);
156 snd_pcm_writei(handle,pSound,
157 iDisStereo ? lBytes / 2 : lBytes / 4);