#include #include #include #include #include static double ts(void) { static struct timeval last; struct timeval now, dt; if (last.tv_sec == 0) { gettimeofday(&last, NULL); } gettimeofday(&now, NULL); timersub(&now, &last, &dt); last = now; return dt.tv_sec * 1000.0 + dt.tv_usec / 1000.0; } int main(int argc, char * argv[]) { int r; snd_pcm_t * pcm; snd_pcm_uframes_t buffer_size, period_size; char silence[88200] = {0}; if (argc != 2) { return 1; } r = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK, 0); assert(r == 0); r = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 2, 44100, 1, atoi(argv[1]) * 1000); assert(r == 0); r = snd_pcm_get_params(pcm, &buffer_size, &period_size); assert(r == 0); fprintf(stderr, "%.2f: buffer_size = %u, period_size = %u\n", ts(), (unsigned) buffer_size, (unsigned) period_size); assert(buffer_size <= sizeof(silence) / 4); for (;;) { snd_pcm_sframes_t avail, wrote = 0; snd_pcm_wait(pcm, -1); avail = snd_pcm_avail_update(pcm); if (avail == -EPIPE) { snd_pcm_recover(pcm, -EPIPE, 0); avail = snd_pcm_avail_update(pcm); } if (avail > 0) { wrote = snd_pcm_writei(pcm, silence, avail); } fprintf(stderr, "%.2f: avail = %d, wrote = %d\n", ts(), (int) avail, (int) wrote); } return 0; }