summaryrefslogtreecommitdiff
path: root/test/seq-sender.c
blob: 5d8ac92c0d760477d216fe92bb1dfc15db182940 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

#ifdef USE_PCM // XXX not yet
/*
 *  PCM timer layer
 */

int pcard = 0;
int pdevice = 0;
int period_size = 1024;

void set_hwparams(snd_pcm_t *phandle)
{
	int err;
	snd_pcm_hw_params_t *params;

	err = snd_output_stdio_attach(&log, stderr, 0);
	if (err < 0) {
		fprintf(stderr, "cannot attach output stdio\n");
		exit(0);
	}

	snd_pcm_hw_params_alloca(&params);
	err = snd_pcm_hw_params_any(phandle, params);
	if (err < 0) {
		fprintf(stderr, "Broken configuration for this PCM: no configurations available\n");
		exit(0);
	}

	err = snd_pcm_hw_params_set_access(phandle, params,
					   SND_PCM_ACCESS_RW_INTERLEAVED);
	if (err < 0) {
		fprintf(stderr, "Access type not available\n");
		exit(0);
	}
	err = snd_pcm_hw_params_set_format(phandle, params, SND_PCM_FORMAT_S16_LE);
	if (err < 0) {
		fprintf(stderr, "cannot set format\n");
		exit(0);
	}
	err = snd_pcm_hw_params_set_channels(phandle, params, 2);
	if (err < 0) {
		fprintf(stderr, "cannot set channels 2\n");
		exit(0);
	}
	err = snd_pcm_hw_params_set_rate_near(phandle, params, 44100, 0);
	if (err < 0) {
		fprintf(stderr, "cannot set rate\n");
		exit(0);
	}
	err = snd_pcm_hw_params_set_period_size_near(phandle, params, period_size);
	if (err < 0) {
		fprintf(stderr, "cannot set period size\n");
		exit(0);
	}
	err = snd_pcm_hw_params(phandle, params);
	if (err < 0) {
		fprintf(stderr, "Unable to install hw params:\n");
		exit(0);
	}
	snd_pcm_hw_params_dump(params, log);
}

#endif
/*
 *  Simple event sender
 */

void event_sender_start_timer(snd_seq_t *handle,
			      int client ATTRIBUTE_UNUSED,
			      int queue,
			      snd_pcm_t *phandle ATTRIBUTE_UNUSED)
{
	int err;
	
#ifdef USE_PCM
	if (phandle) {
		snd_pcm_playback_info_t pinfo;
		snd_seq_queue_timer_t qtimer;

		if ((err = snd_pcm_playback_info(phandle, &pinfo)) < 0) {
			fprintf(stderr, "Playback info error: %s\n", snd_strerror(err));
			exit(0);
		}
		bzero(&qtimer, sizeof(qtimer));
		qtimer.type = SND_SEQ_TIMER_MASTER;
		/* note: last bit from the subdevices specifies playback */
		/* or capture direction for the timer specification */
		qtimer.number = SND_TIMER_PCM(pcard, pdevice, pinfo.subdevice << 1);
		if ((err = snd_seq_set_queue_timer(handle, queue, &qtimer)) < 0) {
			fprintf(stderr, "Sequencer PCM timer setup failed: %s\n", snd_strerror(err));
			exit(0);
		}
	}	
#endif
	if ((err = snd_seq_start_queue(handle, queue, NULL))<0)
		fprintf(stderr, "Timer event output error: %s\n", snd_strerror(err));
	snd_seq_drain_output(handle);
}

void event_sender_filter(snd_seq_t *handle)
{
	int err;

	if ((err = snd_seq_set_client_event_filter(handle, SND_SEQ_EVENT_ECHO)) < 0) {
		fprintf(stderr, "Unable to set client info: %s\n", snd_strerror(err));
		return;
	}
}

void send_event(snd_seq_t *handle, int queue, int client, int port,
                snd_seq_addr_t *dest, int *time)
{
	int err;
	snd_seq_event_t ev;
	
	bzero(&ev, sizeof(ev));
	ev.queue = queue;
	ev.source.client = ev.dest.client = client;
	ev.source.port = ev.dest.port = port;
	ev.flags = SND_SEQ_TIME_STAMP_REAL | SND_SEQ_TIME_MODE_ABS;
	ev.time.time.tv_sec = *time; (*time)++;
	ev.type = SND_SEQ_EVENT_ECHO;
	if ((err = snd_seq_event_output(handle, &ev))<0)
		fprintf(stderr, "Event output error: %s\n", snd_strerror(err));
	ev.dest = *dest;
	ev.type = SND_SEQ_EVENT_PGMCHANGE;
	ev.data.control.channel = 0;
	ev.data.control.value = 16;
	if ((err = snd_seq_event_output(handle, &ev))<0)
		fprintf(stderr, "Event output error: %s\n", snd_strerror(err));
	ev.type = SND_SEQ_EVENT_NOTE;
	ev.data.note.channel = 0;
	ev.data.note.note = 64 + (queue*2);
	ev.data.note.velocity = 127;
	ev.data.note.off_velocity = 127;
	ev.data.note.duration = 500;	/* 0.5sec */
	if ((err = snd_seq_event_output(handle, &ev))<0)
		fprintf(stderr, "Event output error: %s\n", snd_strerror(err));
	if ((err = snd_seq_drain_output(handle))<0)
		fprintf(stderr, "Event drain error: %s\n", snd_strerror(err));
}

void event_sender(snd_seq_t *handle, int argc, char *argv[])
{
	snd_seq_event_t *ev;
	snd_seq_port_info_t *pinfo;
	snd_seq_port_subscribe_t *sub;
	snd_seq_addr_t addr;
	struct pollfd *pfds;
	int client, port, queue, max, err, v1, v2, time = 0, pcm_flag = 0;
	char *ptr;
	snd_pcm_t *phandle = NULL;

	if (argc < 1) {
		fprintf(stderr, "Invalid destination...\n");
		return;
	}

	if ((client = snd_seq_client_id(handle))<0) {
		fprintf(stderr, "Cannot determine client number: %s\n", snd_strerror(client));
		return;
	}
	printf("Client ID = %i\n", client);
	if ((queue = snd_seq_alloc_queue(handle))<0) {
		fprintf(stderr, "Cannot allocate queue: %s\n", snd_strerror(queue));
		return;
	}
	printf("Queue ID = %i\n", queue);
	event_sender_filter(handle);
	if ((err = snd_seq_nonblock(handle, 1))<0)
		fprintf(stderr, "Cannot set nonblock mode: %s\n", snd_strerror(err));

	snd_seq_port_info_alloca(&pinfo);
	snd_seq_port_info_set_capability(pinfo, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_READ);
	snd_seq_port_info_set_name(pinfo, "Output");
	if ((err = snd_seq_create_port(handle, pinfo)) < 0) {
		fprintf(stderr, "Cannot create output port: %s\n", snd_strerror(err));
		return;
	}
	port = snd_seq_port_info_get_port(pinfo);

	snd_seq_port_subscribe_alloca(&sub);
	addr.client = client;
	addr.port = port;
	snd_seq_port_subscribe_set_sender(sub, &addr);

	for (max = 0; max < argc; max++) {
		ptr = argv[max];
		if (!ptr)
			continue;
		if (!strcmp(ptr, "pcm")) {
			pcm_flag = 1;
			continue;
		}
		if (sscanf(ptr, "%i.%i", &v1, &v2) != 2) {
			fprintf(stderr, "Wrong argument '%s'...\n", argv[max]);
			return;
		}
		addr.client = v1;
		addr.port = v2;
		snd_seq_port_subscribe_set_dest(sub, &addr);
		if ((err = snd_seq_subscribe_port(handle, sub))<0) {
			fprintf(stderr, "Cannot subscribe port %i from client %i: %s\n", v2, v1, snd_strerror(err));
			return;
		}
	}

	printf("Destination client = %i, port = %i\n", addr.client, addr.port);

#ifdef USE_PCM
	if (pcm_flag) {
		if ((err = snd_pcm_open(&phandle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
			fprintf(stderr, "Playback open error: %s\n", snd_strerror(err));
			exit(0);
		}
		set_hwparams(phandle);
		pbuf = calloc(1, period_size * 4);
		if (pbuf == NULL) {
			fprintf(stderr, "No enough memory...\n");
			exit(0);
		}
	}
#endif
	event_sender_start_timer(handle, client, queue, phandle);
	
	/* send the first event */
	send_event(handle, queue, client, port, &addr, &time);
#ifdef USE_PCM
	if (phandle)
		max += snd_pcm_poll_descriptors_count(phandle);
#endif
	pfds = alloca(sizeof(*pfds) * max);
	while (1) {
		int nseqs = snd_seq_poll_descriptors_count(handle, POLLOUT|POLLIN);
		if (snd_seq_event_output_pending(handle))
			snd_seq_poll_descriptors(handle, pfds, nseqs, POLLOUT|POLLIN);
		else
			snd_seq_poll_descriptors(handle, pfds, nseqs, POLLIN);
		max = nseqs;
#ifdef USE_PCM
		if (phandle) {
			int pmax = snd_pcm_poll_descriptors_count(phandle);
			snd_seq_poll_descriptors(phandle, pfds + max, pmax);
			max += pmax;
		}
#endif
		if (poll(pfds, max, -1) < 0)
			break;
#ifdef USE_PCM
		if (phandle && (pfds[nseqs].revents & POLLOUT)) {
			if (snd_pcm_writei(phandle, pbuf, period_size) != period_size) {
				fprintf(stderr, "Playback write error!!\n");
				exit(0);
			}
		}
#endif
		if (pfds[0].revents & POLLOUT)
			snd_seq_drain_output(handle);
		if (pfds[0].revents & POLLIN) {
			do {
				if ((err = snd_seq_event_input(handle, &ev))<0)
					break;
				if (!ev)
					continue;
				if (ev->type == SND_SEQ_EVENT_ECHO)
					send_event(handle, queue, client, port, &addr, &time);
				decode_event(ev);
				snd_seq_free_event(ev);
			} while (err > 0);
		}
	}
}