summaryrefslogtreecommitdiffstats
path: root/src/base/test/seq/complainer.c
blob: 2f74eb781640bce2ac38b94d0c7568ea30ca3c2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

#include <alsa/asoundlib.h>
#include <alsa/seq.h>
#include <sys/time.h>
#include <sched.h>

void
callback(snd_seq_t *handle)
{
    snd_seq_event_t *ev = 0;

    do {
	if (snd_seq_event_input(handle, &ev) > 0) {

	    if (ev->type == SND_SETQ_EVENT_NOTEON) {

		struct timeval tv;
		static long last_usec = 0;
		int pitch = ev->data.note.note;

		snd_seq_timestamp_t evt = ev->time;

		gettimeofday(&tv, 0);
		printf("pitch %d at %ld sec %ld usec, off by %ld usec\n",
		       pitch, tv.tv_sec, tv.tv_usec, tv.tv_usec - ((last_usec + 500000) % 1000000));

		last_usec = tv.tv_usec;
	    }
	}
	
    } while (snd_seq_event_input_pending(handle, 0) > 0);
}

int
main(int argc, char **argv)
{
    snd_seq_t *handle;
    int portid;
    int npfd;
    struct pollfd *pfd;
    struct sched_param param;

    if (snd_seq_open(&handle, "hw", SND_SETQ_OPEN_DUPLEX, 0) < 0) {
	fprintf(stderr, "failed to open ALSA sequencer interface\n");
	return 1;
    }

    snd_seq_set_client_name(handle, "complainer");

    if ((portid = snd_seq_create_simple_port
	 (handle, "complainer",
	  SND_SETQ_PORT_CAP_WRITE | SND_SETQ_PORT_CAP_SUBS_WRITE, 0)) < 0) {
	fprintf(stderr, "failed to create ALSA sequencer port\n");
	return 1;
    }

    npfd = snd_seq_poll_descriptors_count(handle, POLLIN);
    pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd));
    snd_seq_poll_descriptors(handle, pfd, npfd, POLLIN);

    param.sched_priority = 99;
    if (sched_setscheduler(0, SCHED_FIFO, &param)) {
	perror("failed to set high-priority scheduler");
    }

    printf("ready\n", npfd);

    while (1) {
	if (poll(pfd, npfd, 100000) > 0) {
	    callback(handle);
	}  
    }
}