31 #include <alsa/asoundlib.h> 33 static snd_pcm_t* playback_handle = 0;
34 static snd_pcm_sw_params_t* sw_params = 0;
35 static snd_pcm_hw_params_t* hw_params = 0;
36 static snd_pcm_sframes_t buffer_size = 0;
38 static snd_pcm_sframes_t written = 0;
39 static snd_pcm_sframes_t delay = 0;
41 static unsigned int rate = 44100;
43 static int audio_initialised = 0;
48 snd_pcm_delay(playback_handle, &delay);
50 return written - delay;
56 unsigned int buffer_time = 50000;
63 audio_initialised = 1;
65 device = getenv(
"ALSA_DEVICE");
70 if(0 > (err = snd_pcm_open(&playback_handle, device,
71 SND_PCM_STREAM_PLAYBACK, 0)))
72 errx(EXIT_FAILURE,
"Audio: Cannot open device %s: %s", device, snd_strerror(err));
74 if(0 > (err = snd_pcm_sw_params_malloc(&sw_params)))
75 errx(EXIT_FAILURE,
"Audio: Could not allocate software parameter structure: %s",
78 if(0 > (err = snd_pcm_hw_params_malloc(&hw_params)))
79 errx(EXIT_FAILURE,
"Audio: Could not allocate hardware parameter structure: %s",
82 if(0 > (err = snd_pcm_hw_params_any(playback_handle, hw_params)))
83 errx(EXIT_FAILURE,
"Audio: Could not initializa hardware parameters: %s",
86 if(0 > (err = snd_pcm_hw_params_set_access(playback_handle, hw_params,
87 SND_PCM_ACCESS_RW_INTERLEAVED)))
88 errx(EXIT_FAILURE,
"Audio: Could not set access type: %s", snd_strerror(err));
90 if(0 > (err = snd_pcm_hw_params_set_format(playback_handle, hw_params,
92 errx(EXIT_FAILURE,
"Audio: Could not set sample format to signed 16 bit " 93 "native endian: %s", snd_strerror(err));
95 if(0 > (err = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params,
97 errx(EXIT_FAILURE,
"Audio: Could not set sample rate %uHz: %s", rate,
100 if(0 > (err = snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2)))
101 errx(EXIT_FAILURE,
"Audio: Could not set channel count to %u: %s",
102 2, snd_strerror(err));
104 snd_pcm_hw_params_set_buffer_time_near(playback_handle, hw_params, &buffer_time, 0);
106 if(0 > (err = snd_pcm_hw_params(playback_handle, hw_params)))
107 errx(EXIT_FAILURE,
"Audio: Could not set hardware parameters: %s", snd_strerror(err));
109 fprintf(stderr,
"Buffer time is %.3f seconds\n", buffer_time / 1.0e6);
111 if(0 > (err = snd_pcm_sw_params_current(playback_handle, sw_params)))
112 errx(EXIT_FAILURE,
"Audio: Could not initialise software parameters: %s",
115 snd_pcm_sw_params_set_start_threshold(playback_handle, sw_params, 0);
116 snd_pcm_sw_params_set_avail_min(playback_handle, sw_params, 1024);
118 snd_pcm_uframes_t min;
119 snd_pcm_sw_params_get_avail_min(sw_params, &min);
120 fprintf(stderr,
"Minimum %u\n", (
unsigned) min);
122 if(0 > (err = snd_pcm_sw_params(playback_handle, sw_params)))
123 errx(EXIT_FAILURE,
"Audio: Could not set software parameters: %s",
126 buffer_size = snd_pcm_avail_update(playback_handle);
130 audio_write(
const void* data,
size_t amount)
138 err = snd_pcm_writei(playback_handle, data, amount);
145 err = snd_pcm_recover(playback_handle, err, 0);
148 errx(EXIT_FAILURE,
"Audio playback failed: %s", strerror(-err));
163 audio_start(
unsigned int rate,
unsigned int channel_count)
167 snd_pcm_prepare(playback_handle);
173 snd_pcm_drain(playback_handle);
Interfacing to ALSA sound output.