/test/timer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "../include/asoundlib.h"
void show_status(
void *handle)
{
int err;
snd_timer_status_t *status;
snd_timer_status_alloca(&status);
if ((err =
snd_timer_status(handle, status)) < 0) {
fprintf(stderr,
"timer status %i (%s)\n", err,
snd_strerror(err));
return;
}
printf(
"STATUS:\n");
printf(
" resolution = %li\n",
snd_timer_status_get_resolution(status));
printf(
" lost = %li\n",
snd_timer_status_get_lost(status));
printf(
" overrun = %li\n",
snd_timer_status_get_overrun(status));
printf(
" queue = %li\n",
snd_timer_status_get_queue(status));
}
void read_loop(
void *handle,
int master_ticks,
int timeout)
{
int count, err;
struct pollfd *fds;
snd_timer_read_t tr;
count =
snd_timer_poll_descriptors_count(handle);
fds = calloc(count,
sizeof(
struct pollfd));
if (fds == NULL) {
fprintf(stderr,
"malloc error\n");
exit(EXIT_FAILURE);
}
while (master_ticks-- > 0) {
if ((err =
snd_timer_poll_descriptors(handle, fds, count)) < 0) {
fprintf(stderr,
"snd_timer_poll_descriptors error: %s\n",
snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = poll(fds, count, timeout)) < 0) {
fprintf(stderr,
"poll error %i (%s)\n", err, strerror(err));
exit(EXIT_FAILURE);
}
if (err == 0) {
fprintf(stderr,
"timer time out!!\n");
exit(EXIT_FAILURE);
}
while (
snd_timer_read(handle, &tr,
sizeof(tr)) ==
sizeof(tr)) {
printf(
"TIMER: resolution = %uns, ticks = %u\n",
tr.
resolution, tr.
ticks);
}
}
free(fds);
}
int main(
int argc,
char *argv[])
{
int idx, err;
int class =
SND_TIMER_CLASS_GLOBAL;
int sclass =
SND_TIMER_CLASS_NONE;
int card = 0;
int device =
SND_TIMER_GLOBAL_SYSTEM;
int subdevice = 0;
int list = 0;
snd_timer_t *handle;
snd_timer_id_t *
id;
snd_timer_info_t *info;
snd_timer_params_t *params;
char timername[64];
snd_timer_id_alloca(&
id);
snd_timer_info_alloca(&info);
snd_timer_params_alloca(¶ms);
idx = 1;
while (idx < argc) {
if (!strncmp(argv[idx],
"class=", 5)) {
class = atoi(argv[idx]+6);
}
else if (!strncmp(argv[idx],
"sclass=", 6)) {
sclass = atoi(argv[idx]+7);
}
else if (!strncmp(argv[idx],
"card=", 5)) {
card = atoi(argv[idx]+5);
}
else if (!strncmp(argv[idx],
"device=", 7)) {
device = atoi(argv[idx]+7);
}
else if (!strncmp(argv[idx],
"subdevice=", 10)) {
subdevice = atoi(argv[idx]+10);
}
else if (!strcmp(argv[idx],
"list")) {
list = 1;
}
idx++;
}
if (
class ==
SND_TIMER_CLASS_SLAVE && sclass ==
SND_TIMER_SCLASS_NONE) {
fprintf(stderr,
"slave class is not set\n");
exit(EXIT_FAILURE);
}
if (list) {
snd_timer_query_t *qhandle;
if ((err =
snd_timer_query_open(&qhandle,
"hw", 0)) < 0) {
fprintf(stderr,
"snd_timer_query_open error: %s\n",
snd_strerror(err));
exit(EXIT_FAILURE);
}
snd_timer_id_set_class(
id, SND_TIMER_CLASS_NONE);
while (1) {
if ((err =
snd_timer_query_next_device(qhandle,
id)) < 0) {
fprintf(stderr,
"timer next device error: %s\n",
snd_strerror(err));
break;
}
if (
snd_timer_id_get_class(
id) < 0)
break;
printf(
"Timer device: class %i, sclass %i, card %i, device %i, subdevice %i\n",
snd_timer_id_get_class(
id),
snd_timer_id_get_sclass(
id),
snd_timer_id_get_card(
id),
snd_timer_id_get_device(
id),
snd_timer_id_get_subdevice(
id));
}
snd_timer_query_close(qhandle);
exit(EXIT_SUCCESS);
}
sprintf(timername,
"hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i",
class, sclass, card, device, subdevice);
if ((err =
snd_timer_open(&handle, timername, SND_TIMER_OPEN_NONBLOCK))<0) {
fprintf(stderr,
"timer open %i (%s)\n", err,
snd_strerror(err));
exit(EXIT_FAILURE);
}
printf(
"Using timer class %i, slave class %i, card %i, device %i, subdevice %i\n",
class, sclass, card, device, subdevice);
if ((err =
snd_timer_info(handle, info)) < 0) {
fprintf(stderr,
"timer info %i (%s)\n", err,
snd_strerror(err));
exit(0);
}
printf(
"Timer info:\n");
printf(
" slave = %s\n",
snd_timer_info_is_slave(info) ?
"yes" :
"no");
printf(
" card = %i\n",
snd_timer_info_get_card(info));
printf(
" id = '%s'\n",
snd_timer_info_get_id(info));
printf(
" name = '%s'\n",
snd_timer_info_get_name(info));
printf(
" average resolution = %li\n",
snd_timer_info_get_resolution(info));
snd_timer_params_set_auto_start(params, 1);
if (!
snd_timer_info_is_slave(info)) {
snd_timer_params_set_ticks(params, (1000000000 /
snd_timer_info_get_resolution(info)) / 50);
if (
snd_timer_params_get_ticks(params) < 1)
snd_timer_params_set_ticks(params, 1);
printf(
"Using %li tick(s)\n",
snd_timer_params_get_ticks(params));
}
else {
snd_timer_params_set_ticks(params, 1);
}
if ((err =
snd_timer_params(handle, params)) < 0) {
fprintf(stderr,
"timer params %i (%s)\n", err,
snd_strerror(err));
exit(0);
}
show_status(handle);
if ((err =
snd_timer_start(handle)) < 0) {
fprintf(stderr,
"timer start %i (%s)\n", err,
snd_strerror(err));
exit(EXIT_FAILURE);
}
read_loop(handle, 25,
snd_timer_info_is_slave(info) ? 10000 : 25);
show_status(handle);
snd_timer_close(handle);
printf(
"Done\n");
return EXIT_SUCCESS;
}
00001
#include <stdio.h>
00002
#include <stdlib.h>
00003
#include <string.h>
00004
#include <sys/time.h>
00005
#include "../include/asoundlib.h"
00006
00007
void show_status(
void *handle)
00008 {
00009
int err;
00010
snd_timer_status_t *status;
00011
00012
snd_timer_status_alloca(&status);
00013
if ((err =
snd_timer_status(handle, status)) < 0) {
00014 fprintf(stderr,
"timer status %i (%s)\n", err,
snd_strerror(err));
00015
return;
00016 }
00017 printf(
"STATUS:\n");
00018 printf(
" resolution = %li\n",
snd_timer_status_get_resolution(status));
00019 printf(
" lost = %li\n",
snd_timer_status_get_lost(status));
00020 printf(
" overrun = %li\n",
snd_timer_status_get_overrun(status));
00021 printf(
" queue = %li\n",
snd_timer_status_get_queue(status));
00022 }
00023
00024
void read_loop(
void *handle,
int master_ticks,
int timeout)
00025 {
00026
int count, err;
00027
struct pollfd *fds;
00028
snd_timer_read_t tr;
00029
00030 count =
snd_timer_poll_descriptors_count(handle);
00031 fds = calloc(count,
sizeof(
struct pollfd));
00032
if (fds == NULL) {
00033 fprintf(stderr,
"malloc error\n");
00034 exit(EXIT_FAILURE);
00035 }
00036
while (master_ticks-- > 0) {
00037
if ((err =
snd_timer_poll_descriptors(handle, fds, count)) < 0) {
00038 fprintf(stderr,
"snd_timer_poll_descriptors error: %s\n",
snd_strerror(err));
00039 exit(EXIT_FAILURE);
00040 }
00041
if ((err = poll(fds, count, timeout)) < 0) {
00042 fprintf(stderr,
"poll error %i (%s)\n", err, strerror(err));
00043 exit(EXIT_FAILURE);
00044 }
00045
if (err == 0) {
00046 fprintf(stderr,
"timer time out!!\n");
00047 exit(EXIT_FAILURE);
00048 }
00049
while (
snd_timer_read(handle, &tr,
sizeof(tr)) ==
sizeof(tr)) {
00050 printf(
"TIMER: resolution = %uns, ticks = %u\n",
00051 tr.
resolution, tr.
ticks);
00052 }
00053 }
00054 free(fds);
00055 }
00056
00057
int main(
int argc,
char *argv[])
00058 {
00059
int idx, err;
00060
int class =
SND_TIMER_CLASS_GLOBAL;
00061
int sclass =
SND_TIMER_CLASS_NONE;
00062
int card = 0;
00063
int device =
SND_TIMER_GLOBAL_SYSTEM;
00064
int subdevice = 0;
00065
int list = 0;
00066
snd_timer_t *handle;
00067
snd_timer_id_t *
id;
00068
snd_timer_info_t *info;
00069
snd_timer_params_t *params;
00070
char timername[64];
00071
00072
snd_timer_id_alloca(&
id);
00073
snd_timer_info_alloca(&info);
00074
snd_timer_params_alloca(¶ms);
00075
00076 idx = 1;
00077
while (idx < argc) {
00078
if (!strncmp(argv[idx],
"class=", 5)) {
00079
class = atoi(argv[idx]+6);
00080 }
else if (!strncmp(argv[idx],
"sclass=", 6)) {
00081 sclass = atoi(argv[idx]+7);
00082 }
else if (!strncmp(argv[idx],
"card=", 5)) {
00083 card = atoi(argv[idx]+5);
00084 }
else if (!strncmp(argv[idx],
"device=", 7)) {
00085 device = atoi(argv[idx]+7);
00086 }
else if (!strncmp(argv[idx],
"subdevice=", 10)) {
00087 subdevice = atoi(argv[idx]+10);
00088 }
else if (!strcmp(argv[idx],
"list")) {
00089 list = 1;
00090 }
00091 idx++;
00092 }
00093
if (
class ==
SND_TIMER_CLASS_SLAVE && sclass ==
SND_TIMER_SCLASS_NONE) {
00094 fprintf(stderr,
"slave class is not set\n");
00095 exit(EXIT_FAILURE);
00096 }
00097
if (list) {
00098
snd_timer_query_t *qhandle;
00099
if ((err =
snd_timer_query_open(&qhandle,
"hw", 0)) < 0) {
00100 fprintf(stderr,
"snd_timer_query_open error: %s\n",
snd_strerror(err));
00101 exit(EXIT_FAILURE);
00102 }
00103
snd_timer_id_set_class(
id, SND_TIMER_CLASS_NONE);
00104
while (1) {
00105
if ((err =
snd_timer_query_next_device(qhandle,
id)) < 0) {
00106 fprintf(stderr,
"timer next device error: %s\n",
snd_strerror(err));
00107
break;
00108 }
00109
if (
snd_timer_id_get_class(
id) < 0)
00110
break;
00111 printf(
"Timer device: class %i, sclass %i, card %i, device %i, subdevice %i\n",
00112
snd_timer_id_get_class(
id),
00113
snd_timer_id_get_sclass(
id),
00114
snd_timer_id_get_card(
id),
00115
snd_timer_id_get_device(
id),
00116
snd_timer_id_get_subdevice(
id));
00117 }
00118
snd_timer_query_close(qhandle);
00119 exit(EXIT_SUCCESS);
00120 }
00121 sprintf(timername,
"hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i",
class, sclass, card, device, subdevice);
00122
if ((err =
snd_timer_open(&handle, timername, SND_TIMER_OPEN_NONBLOCK))<0) {
00123 fprintf(stderr,
"timer open %i (%s)\n", err,
snd_strerror(err));
00124 exit(EXIT_FAILURE);
00125 }
00126 printf(
"Using timer class %i, slave class %i, card %i, device %i, subdevice %i\n",
class, sclass, card, device, subdevice);
00127
if ((err =
snd_timer_info(handle, info)) < 0) {
00128 fprintf(stderr,
"timer info %i (%s)\n", err,
snd_strerror(err));
00129 exit(0);
00130 }
00131 printf(
"Timer info:\n");
00132 printf(
" slave = %s\n",
snd_timer_info_is_slave(info) ?
"yes" :
"no");
00133 printf(
" card = %i\n",
snd_timer_info_get_card(info));
00134 printf(
" id = '%s'\n",
snd_timer_info_get_id(info));
00135 printf(
" name = '%s'\n",
snd_timer_info_get_name(info));
00136 printf(
" average resolution = %li\n",
snd_timer_info_get_resolution(info));
00137
snd_timer_params_set_auto_start(params, 1);
00138
if (!
snd_timer_info_is_slave(info)) {
00139
snd_timer_params_set_ticks(params, (1000000000 /
snd_timer_info_get_resolution(info)) / 50);
00140
if (
snd_timer_params_get_ticks(params) < 1)
00141
snd_timer_params_set_ticks(params, 1);
00142 printf(
"Using %li tick(s)\n",
snd_timer_params_get_ticks(params));
00143 }
else {
00144
snd_timer_params_set_ticks(params, 1);
00145 }
00146
if ((err =
snd_timer_params(handle, params)) < 0) {
00147 fprintf(stderr,
"timer params %i (%s)\n", err,
snd_strerror(err));
00148 exit(0);
00149 }
00150 show_status(handle);
00151
if ((err =
snd_timer_start(handle)) < 0) {
00152 fprintf(stderr,
"timer start %i (%s)\n", err,
snd_strerror(err));
00153 exit(EXIT_FAILURE);
00154 }
00155 read_loop(handle, 25,
snd_timer_info_is_slave(info) ? 10000 : 25);
00156 show_status(handle);
00157
snd_timer_close(handle);
00158 printf(
"Done\n");
00159
return EXIT_SUCCESS;
00160 }
Generated on Thu Sep 30 04:13:33 2004 for ALSA project - the C library reference by
1.3.7