summaryrefslogtreecommitdiffstats
path: root/src/base/test/seq/generator.c
blob: 9f64d61a143618dab0d5a3d2ede9234def4a8931 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

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

int
main(int argc, char **argv)
{
    snd_seq_t *handle;
    int portid;
    int npfd;
    struct pollfd *pfd;
    int queue;
    int i;
    int rval;
    int target;
    snd_seq_queue_timer_t *timer;
    snd_timer_id_t *timerid;

    if (argc != 2) {
	fprintf(stderr, "usage: generator <target-client-id>\n");
	exit(2);
    }
    target = atoi(argv[1]);

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

    snd_seq_set_client_name(handle, "generator");

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

    if ((queue = snd_seq_alloc_queue(handle)) < 0) {
	fprintf(stderr, "failed to create ALSA sequencer queue\n");
	return 1;
    }
/*
    snd_seq_queue_timer_alloca(&timer);
    snd_seq_get_queue_timer(handle, queue, timer);
    snd_timer_id_alloca(&timerid);
    snd_timer_id_set_class(timerid, SND_TIMER_CLASS_PCM);
    snd_timer_id_set_sclass(timerid, SND_TIMER_SCLASS_NONE);
    snd_timer_id_set_card(timerid, 0);
    snd_timer_id_set_device(timerid, 0);
    snd_timer_id_set_subdevice(timerid, 0);
    snd_seq_queue_timer_set_id(timer, timerid);
    snd_seq_set_queue_timer(handle, queue, timer);
*/
    snd_seq_start_queue(handle, queue, 0);

    // stuff two minutes worth of events on the queue
    for (i = 0; i < 240; ++i) {
	snd_seq_real_time_t rtime;
	rtime.tv_sec = i / 2;
	rtime.tv_nsec = (i % 2) * 500000000;
	snd_seq_event_t ev;
	snd_seq_ev_clear(&ev);
	snd_seq_ev_set_source(&ev, portid);
	snd_seq_ev_set_dest(&ev, target, 0);
	snd_seq_ev_schedule_real(&ev, queue, 0, &rtime);
	snd_seq_ev_set_noteon(&ev, 0, 64, 127);
	if ((rval = snd_seq_event_output(handle, &ev)) < 0) {
	    fprintf(stderr, "failed to write event: %s", snd_strerror(rval));
	}
    }

    snd_seq_drain_output(handle);

    for (i = 0; i < 120; ++i) {
	snd_seq_queue_status_t *status;
	const snd_seq_real_time_t *rtime;
	struct timeval tv;

	snd_seq_queue_status_alloca(&status);

	snd_seq_get_queue_status(handle, queue, status);
	rtime = snd_seq_queue_status_get_real_time(status);

	gettimeofday(&tv, 0);

	fprintf(stderr, " real time: %ld sec, %ld usec\nqueue time: %ld sec, %ld usec (diff to real time %ld sec %ld usec)\n",
		tv.tv_sec, tv.tv_usec,
		rtime->tv_sec, rtime->tv_nsec / 1000,
		tv.tv_sec - rtime->tv_sec, tv.tv_usec - (rtime->tv_nsec / 1000));

	sleep(1);
    }
}