#define PCM_DEVICE "hw:0,0" /* gcc -Wall -o bug start_stop_threshold_bug.c -lasound ./bug */ /* * Simple sound playback using ALSA API and libasound. * * Compile: * $ cc -o play sound_playback.c -lasound * * Usage: * $ ./play < * * Examples: * $ ./play 44100 2 5 < /dev/urandom * $ ./play 22050 1 8 < /path/to/file.wav * * Copyright (C) 2009 Alessandro Ghedini * -------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * Alessandro Ghedini wrote this file. As long as you retain this * notice you can do whatever you want with this stuff. If we * meet some day, and you think this stuff is worth it, you can * buy me a beer in return. * -------------------------------------------------------------- */ #include #include int main(int argc, char **argv) { unsigned int pcm, tmp; unsigned rate; int channels, seconds; snd_pcm_t *pcm_handle; snd_pcm_hw_params_t *params; snd_pcm_uframes_t frames, bufsz; char *buff; int buff_size, loops; rate = 44100; channels = 2; seconds = 10; /* Open the PCM device in playback mode */ if ((pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) < 0)) { printf("ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(pcm)); abort(); } /* Allocate parameters object and fill it with default values*/ snd_pcm_hw_params_alloca(¶ms); snd_pcm_hw_params_any(pcm_handle, params); /* Set parameters */ snd_pcm_hw_params_set_rate_resample(pcm_handle, params, 0); if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0)) { printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm)); abort(); } if ((pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE) < 0)) { printf("ERROR: Can't set format. %s\n", snd_strerror(pcm)); abort(); } if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0)) { printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm)); abort(); } if ((pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)) { printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm)); abort(); } /* Write parameters */ if ((pcm = snd_pcm_hw_params(pcm_handle, params) < 0)) { printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm)); abort(); } if ((pcm = snd_pcm_hw_params_get_buffer_size(params, &bufsz) < 0)) { printf("ERROR: Can't get buffer size. %s\n", snd_strerror(pcm)); abort(); } int start_threshold = 100; int stop_threshold = bufsz - 50; snd_pcm_sw_params_t *swparams; snd_pcm_sw_params_alloca(&swparams); pcm = snd_pcm_sw_params_current(pcm_handle, swparams); if (pcm < 0) { printf("Unable to determine current swparams: %s", snd_strerror(pcm)); abort(); } pcm = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, start_threshold); if (pcm < 0) { printf("Unable to set start threshold mode: %s", snd_strerror(pcm)); abort(); } pcm = snd_pcm_sw_params_set_stop_threshold(pcm_handle, swparams, stop_threshold); if (pcm < 0) { printf("Unable to set stop threshold mode: %s", snd_strerror(pcm)); abort(); } pcm = snd_pcm_sw_params(pcm_handle, swparams); if (pcm < 0) { printf("Unable to set sw params: %s", snd_strerror(pcm)); abort(); } /* Resume information */ printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle)); printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle))); snd_pcm_hw_params_get_channels(params, &tmp); printf("channels: %i ", tmp); if (tmp == 1) printf("(mono)\n"); else if (tmp == 2) printf("(stereo)\n"); snd_pcm_hw_params_get_rate(params, &tmp, 0); printf("rate: %d\n", tmp); printf("seconds: %d\n", seconds); printf("bufsz: %ld\n", bufsz); printf("start_threshold: %d\n", start_threshold); printf("stop_threshold: %d\n", stop_threshold); frames = 44; buff_size = frames * channels * 2 /* 2 -> sample size */; buff = (char *) malloc(buff_size); memset(buff, 0, buff_size); snd_pcm_hw_params_get_period_time(params, &tmp, NULL); for (loops = (seconds * 1000000) / tmp; loops > 0; loops--) { printf("==== loop ====\n"); printf("(before write) PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle))); pcm = snd_pcm_writei(pcm_handle, buff, frames); printf("written %d\n", pcm); printf("(after write) PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle))); if (pcm == -EPIPE) { printf("XRUN.\n"); snd_pcm_prepare(pcm_handle); abort(); } else if (pcm < 0) { printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm)); } } snd_pcm_drain(pcm_handle); snd_pcm_close(pcm_handle); free(buff); return 0; }