5 #include "../include/asoundlib.h"
7 void show_status(void *handle)
10 snd_timer_status_t *status;
12 snd_timer_status_alloca(&status);
13 if ((err = snd_timer_status(handle, status)) < 0) {
14 fprintf(stderr, "timer status %i (%s)\n", err, snd_strerror(err));
18 printf(" resolution = %li\n", snd_timer_status_get_resolution(status));
19 printf(" lost = %li\n", snd_timer_status_get_lost(status));
20 printf(" overrun = %li\n", snd_timer_status_get_overrun(status));
21 printf(" queue = %li\n", snd_timer_status_get_queue(status));
24 void read_loop(void *handle, int master_ticks, int timeout)
30 count = snd_timer_poll_descriptors_count(handle);
31 fds = calloc(count, sizeof(struct pollfd));
33 fprintf(stderr, "malloc error\n");
36 while (master_ticks-- > 0) {
37 if ((err = snd_timer_poll_descriptors(handle, fds, count)) < 0) {
38 fprintf(stderr, "snd_timer_poll_descriptors error: %s\n", snd_strerror(err));
41 if ((err = poll(fds, count, timeout)) < 0) {
42 fprintf(stderr, "poll error %i (%s)\n", err, strerror(err));
46 fprintf(stderr, "timer time out!!\n");
49 while (snd_timer_read(handle, &tr, sizeof(tr)) == sizeof(tr)) {
50 printf("TIMER: resolution = %uns, ticks = %u\n",
51 tr.resolution, tr.ticks);
57 int main(int argc, char *argv[])
60 int class = SND_TIMER_CLASS_GLOBAL;
61 int sclass = SND_TIMER_CLASS_NONE;
63 int device = SND_TIMER_GLOBAL_SYSTEM;
68 snd_timer_info_t *info;
69 snd_timer_params_t *params;
72 snd_timer_id_alloca(&id);
73 snd_timer_info_alloca(&info);
74 snd_timer_params_alloca(¶ms);
78 if (!strncmp(argv[idx], "class=", 5)) {
79 class = atoi(argv[idx]+6);
80 } else if (!strncmp(argv[idx], "sclass=", 6)) {
81 sclass = atoi(argv[idx]+7);
82 } else if (!strncmp(argv[idx], "card=", 5)) {
83 card = atoi(argv[idx]+5);
84 } else if (!strncmp(argv[idx], "device=", 7)) {
85 device = atoi(argv[idx]+7);
86 } else if (!strncmp(argv[idx], "subdevice=", 10)) {
87 subdevice = atoi(argv[idx]+10);
88 } else if (!strcmp(argv[idx], "list")) {
93 if (class == SND_TIMER_CLASS_SLAVE && sclass == SND_TIMER_SCLASS_NONE) {
94 fprintf(stderr, "slave class is not set\n");
98 snd_timer_query_t *qhandle;
99 if ((err = snd_timer_query_open(&qhandle, "hw", 0)) < 0) {
100 fprintf(stderr, "snd_timer_query_open error: %s\n", snd_strerror(err));
103 snd_timer_id_set_class(id, SND_TIMER_CLASS_NONE);
105 if ((err = snd_timer_query_next_device(qhandle, id)) < 0) {
106 fprintf(stderr, "timer next device error: %s\n", snd_strerror(err));
109 if (snd_timer_id_get_class(id) < 0)
111 printf("Timer device: class %i, sclass %i, card %i, device %i, subdevice %i\n",
112 snd_timer_id_get_class(id),
113 snd_timer_id_get_sclass(id),
114 snd_timer_id_get_card(id),
115 snd_timer_id_get_device(id),
116 snd_timer_id_get_subdevice(id));
118 snd_timer_query_close(qhandle);
121 sprintf(timername, "hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i", class, sclass, card, device, subdevice);
122 if ((err = snd_timer_open(&handle, timername, SND_TIMER_OPEN_NONBLOCK))<0) {
123 fprintf(stderr, "timer open %i (%s)\n", err, snd_strerror(err));
126 printf("Using timer class %i, slave class %i, card %i, device %i, subdevice %i\n", class, sclass, card, device, subdevice);
127 if ((err = snd_timer_info(handle, info)) < 0) {
128 fprintf(stderr, "timer info %i (%s)\n", err, snd_strerror(err));
131 printf("Timer info:\n");
132 printf(" slave = %s\n", snd_timer_info_is_slave(info) ? "yes" : "no");
133 printf(" card = %i\n", snd_timer_info_get_card(info));
134 printf(" id = '%s'\n", snd_timer_info_get_id(info));
135 printf(" name = '%s'\n", snd_timer_info_get_name(info));
136 printf(" average resolution = %li\n", snd_timer_info_get_resolution(info));
137 snd_timer_params_set_auto_start(params, 1);
138 if (!snd_timer_info_is_slave(info)) {
139 snd_timer_params_set_ticks(params, (1000000000 / snd_timer_info_get_resolution(info)) / 50); /* 50Hz */
140 if (snd_timer_params_get_ticks(params) < 1)
141 snd_timer_params_set_ticks(params, 1);
142 printf("Using %li tick(s)\n", snd_timer_params_get_ticks(params));
144 snd_timer_params_set_ticks(params, 1);
146 if ((err = snd_timer_params(handle, params)) < 0) {
147 fprintf(stderr, "timer params %i (%s)\n", err, snd_strerror(err));
151 if ((err = snd_timer_start(handle)) < 0) {
152 fprintf(stderr, "timer start %i (%s)\n", err, snd_strerror(err));
155 read_loop(handle, 25, snd_timer_info_is_slave(info) ? 10000 : 25);
157 snd_timer_close(handle);