/* to compile: gcc -O2 -Wall -o alsa_quickcrash alsa_quickcrash.c -lasound */ #include #include #include #define RATE 22050 #define CHANNELS 1 #define FORMAT SND_PCM_FORMAT_S16_LE #define BUF_MS 60 #define DO(x) if (x < 0) { fprintf(stderr, "%s failed\n", #x); exit(1); } int main() { unsigned int rate = RATE; snd_pcm_uframes_t frames = RATE * BUF_MS / 1000; snd_pcm_t *handle; snd_pcm_hw_params_t *params; DO(snd_pcm_hw_params_malloc(¶ms)); DO(snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0)); DO(snd_pcm_hw_params_any(handle, params)); DO(snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)); DO(snd_pcm_hw_params_set_format(handle, params, FORMAT)); DO(snd_pcm_hw_params_set_rate_near(handle, params, &rate, 0)); DO(snd_pcm_hw_params_set_channels(handle, params, CHANNELS)); DO(snd_pcm_hw_params_set_buffer_size_near(handle, params, &frames)); DO(snd_pcm_hw_params(handle, params)); DO(snd_pcm_prepare(handle)); printf("PCM config dump:\n"); snd_output_t *output = NULL; DO(snd_output_stdio_attach(&output, stdout, 0)); DO(snd_pcm_dump(handle, output)); printf("\n"); printf("frames = %ld\n", (long)frames); short data[frames * CHANNELS]; memset(data, 0, sizeof(data)); printf("writing whole buffer\n"); DO(snd_pcm_writei(handle, data, frames)); printf("writing whole buffer\n"); DO(snd_pcm_writei(handle, data, frames)); printf("writing whole buffer minus 1 frame\n"); DO(snd_pcm_writei(handle, data, frames - 1)); printf("draining audio\n"); DO(snd_pcm_drain(handle)); printf("done\n"); DO(snd_pcm_close(handle)); return 0; }