#include "sys/asoundlib.h" #include "time.h" #include main() { snd_timer_t *th; snd_timer_general_info_t geninfo; snd_timer_select_t tselect; snd_timer_info_t info; snd_timer_params_t params; snd_timer_status_t status; snd_timer_read_t tbuf; char buf[200]; int n; snd_timer_open(&th); /* geninfo gives you the number of timers */ snd_timer_general_info(th, &geninfo); printf("Number of timers %d\n", geninfo.count); /* Then you can select each of them in turn (only the first here) */ memset(&tselect, 0, sizeof(tselect)); /* Use 0 for system 10ms timer and 1 for 1ms timer */ tselect.data.number = 1; snd_timer_select(th, &tselect); /* Find out about the selected one */ snd_timer_info(th, &info); printf("info ticks %ld res %ld\n", info.ticks, info.resolution); params.flags = SND_TIMER_PSFLG_AUTO; params.ticks = 1; params.queue_size = 64; snd_timer_params(th, ¶ms); snd_timer_status(th, &status); snd_timer_start(th); { struct sched_param param; int rc; param.sched_priority = 1; rc = sched_setscheduler(0, SCHED_FIFO, ¶m); } for (;;) { struct timeval tm; int us; int ms; n = snd_timer_read(th, &tbuf, sizeof(tbuf)); if (n) { static int firstsec; static int last; gettimeofday(&tm, NULL); if (firstsec == 0) firstsec = tm.tv_sec; if (tm.tv_sec - firstsec > 60) break; us = tm.tv_usec; ms = (tm.tv_usec + 500)/1000; if (last && ms - last > 1) printf("%ld:%03ld: Missed %d interrupts\n", tm.tv_sec, tm.tv_usec/1000, ms - last); last = ms; } else printf("=== n was 0\n"); } }