Blame test/seq-sender.c

Packit 4a16fb
Packit 4a16fb
#ifdef USE_PCM // XXX not yet
Packit 4a16fb
/*
Packit 4a16fb
 *  PCM timer layer
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
int pcard = 0;
Packit 4a16fb
int pdevice = 0;
Packit 4a16fb
int period_size = 1024;
Packit 4a16fb
Packit 4a16fb
void set_hwparams(snd_pcm_t *phandle)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_pcm_hw_params_t *params;
Packit 4a16fb
Packit 4a16fb
	err = snd_output_stdio_attach(&log, stderr, 0);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		fprintf(stderr, "cannot attach output stdio\n");
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	snd_pcm_hw_params_alloca(&params);
Packit 4a16fb
	err = snd_pcm_hw_params_any(phandle, params);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		fprintf(stderr, "Broken configuration for this PCM: no configurations available\n");
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	err = snd_pcm_hw_params_set_access(phandle, params,
Packit 4a16fb
					   SND_PCM_ACCESS_RW_INTERLEAVED);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		fprintf(stderr, "Access type not available\n");
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params_set_format(phandle, params, SND_PCM_FORMAT_S16_LE);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		fprintf(stderr, "cannot set format\n");
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params_set_channels(phandle, params, 2);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		fprintf(stderr, "cannot set channels 2\n");
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params_set_rate_near(phandle, params, 44100, 0);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		fprintf(stderr, "cannot set rate\n");
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params_set_period_size_near(phandle, params, period_size);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		fprintf(stderr, "cannot set period size\n");
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_pcm_hw_params(phandle, params);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		fprintf(stderr, "Unable to install hw params:\n");
Packit 4a16fb
		exit(0);
Packit 4a16fb
	}
Packit 4a16fb
	snd_pcm_hw_params_dump(params, log);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#endif
Packit 4a16fb
/*
Packit 4a16fb
 *  Simple event sender
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
void event_sender_start_timer(snd_seq_t *handle,
Packit 4a16fb
			      int client ATTRIBUTE_UNUSED,
Packit 4a16fb
			      int queue,
Packit 4a16fb
			      snd_pcm_t *phandle ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
#ifdef USE_PCM
Packit 4a16fb
	if (phandle) {
Packit 4a16fb
		snd_pcm_playback_info_t pinfo;
Packit 4a16fb
		snd_seq_queue_timer_t qtimer;
Packit 4a16fb
Packit 4a16fb
		if ((err = snd_pcm_playback_info(phandle, &pinfo)) < 0) {
Packit 4a16fb
			fprintf(stderr, "Playback info error: %s\n", snd_strerror(err));
Packit 4a16fb
			exit(0);
Packit 4a16fb
		}
Packit 4a16fb
		bzero(&qtimer, sizeof(qtimer));
Packit 4a16fb
		qtimer.type = SND_SEQ_TIMER_MASTER;
Packit 4a16fb
		/* note: last bit from the subdevices specifies playback */
Packit 4a16fb
		/* or capture direction for the timer specification */
Packit 4a16fb
		qtimer.number = SND_TIMER_PCM(pcard, pdevice, pinfo.subdevice << 1);
Packit 4a16fb
		if ((err = snd_seq_set_queue_timer(handle, queue, &qtimer)) < 0) {
Packit 4a16fb
			fprintf(stderr, "Sequencer PCM timer setup failed: %s\n", snd_strerror(err));
Packit 4a16fb
			exit(0);
Packit 4a16fb
		}
Packit 4a16fb
	}	
Packit 4a16fb
#endif
Packit 4a16fb
	if ((err = snd_seq_start_queue(handle, queue, NULL))<0)
Packit 4a16fb
		fprintf(stderr, "Timer event output error: %s\n", snd_strerror(err));
Packit 4a16fb
	snd_seq_drain_output(handle);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void event_sender_filter(snd_seq_t *handle)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if ((err = snd_seq_set_client_event_filter(handle, SND_SEQ_EVENT_ECHO)) < 0) {
Packit 4a16fb
		fprintf(stderr, "Unable to set client info: %s\n", snd_strerror(err));
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void send_event(snd_seq_t *handle, int queue, int client, int port,
Packit 4a16fb
                snd_seq_addr_t *dest, int *time)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
	
Packit 4a16fb
	bzero(&ev, sizeof(ev));
Packit 4a16fb
	ev.queue = queue;
Packit 4a16fb
	ev.source.client = ev.dest.client = client;
Packit 4a16fb
	ev.source.port = ev.dest.port = port;
Packit 4a16fb
	ev.flags = SND_SEQ_TIME_STAMP_REAL | SND_SEQ_TIME_MODE_ABS;
Packit 4a16fb
	ev.time.time.tv_sec = *time; (*time)++;
Packit 4a16fb
	ev.type = SND_SEQ_EVENT_ECHO;
Packit 4a16fb
	if ((err = snd_seq_event_output(handle, &ev))<0)
Packit 4a16fb
		fprintf(stderr, "Event output error: %s\n", snd_strerror(err));
Packit 4a16fb
	ev.dest = *dest;
Packit 4a16fb
	ev.type = SND_SEQ_EVENT_PGMCHANGE;
Packit 4a16fb
	ev.data.control.channel = 0;
Packit 4a16fb
	ev.data.control.value = 16;
Packit 4a16fb
	if ((err = snd_seq_event_output(handle, &ev))<0)
Packit 4a16fb
		fprintf(stderr, "Event output error: %s\n", snd_strerror(err));
Packit 4a16fb
	ev.type = SND_SEQ_EVENT_NOTE;
Packit 4a16fb
	ev.data.note.channel = 0;
Packit 4a16fb
	ev.data.note.note = 64 + (queue*2);
Packit 4a16fb
	ev.data.note.velocity = 127;
Packit 4a16fb
	ev.data.note.off_velocity = 127;
Packit 4a16fb
	ev.data.note.duration = 500;	/* 0.5sec */
Packit 4a16fb
	if ((err = snd_seq_event_output(handle, &ev))<0)
Packit 4a16fb
		fprintf(stderr, "Event output error: %s\n", snd_strerror(err));
Packit 4a16fb
	if ((err = snd_seq_drain_output(handle))<0)
Packit 4a16fb
		fprintf(stderr, "Event drain error: %s\n", snd_strerror(err));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void event_sender(snd_seq_t *handle, int argc, char *argv[])
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t *ev;
Packit 4a16fb
	snd_seq_port_info_t *pinfo;
Packit 4a16fb
	snd_seq_port_subscribe_t *sub;
Packit 4a16fb
	snd_seq_addr_t addr;
Packit 4a16fb
	struct pollfd *pfds;
Packit 4a16fb
	int client, port, queue, max, err, v1, v2, time = 0, pcm_flag = 0;
Packit 4a16fb
	char *ptr;
Packit 4a16fb
	snd_pcm_t *phandle = NULL;
Packit 4a16fb
Packit 4a16fb
	if (argc < 1) {
Packit 4a16fb
		fprintf(stderr, "Invalid destination...\n");
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if ((client = snd_seq_client_id(handle))<0) {
Packit 4a16fb
		fprintf(stderr, "Cannot determine client number: %s\n", snd_strerror(client));
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	printf("Client ID = %i\n", client);
Packit 4a16fb
	if ((queue = snd_seq_alloc_queue(handle))<0) {
Packit 4a16fb
		fprintf(stderr, "Cannot allocate queue: %s\n", snd_strerror(queue));
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	printf("Queue ID = %i\n", queue);
Packit 4a16fb
	event_sender_filter(handle);
Packit 4a16fb
	if ((err = snd_seq_nonblock(handle, 1))<0)
Packit 4a16fb
		fprintf(stderr, "Cannot set nonblock mode: %s\n", snd_strerror(err));
Packit 4a16fb
Packit 4a16fb
	snd_seq_port_info_alloca(&pinfo);
Packit 4a16fb
	snd_seq_port_info_set_capability(pinfo, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_READ);
Packit 4a16fb
	snd_seq_port_info_set_name(pinfo, "Output");
Packit 4a16fb
	if ((err = snd_seq_create_port(handle, pinfo)) < 0) {
Packit 4a16fb
		fprintf(stderr, "Cannot create output port: %s\n", snd_strerror(err));
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	port = snd_seq_port_info_get_port(pinfo);
Packit 4a16fb
Packit 4a16fb
	snd_seq_port_subscribe_alloca(&sub);
Packit 4a16fb
	addr.client = client;
Packit 4a16fb
	addr.port = port;
Packit 4a16fb
	snd_seq_port_subscribe_set_sender(sub, &addr);
Packit 4a16fb
Packit 4a16fb
	for (max = 0; max < argc; max++) {
Packit 4a16fb
		ptr = argv[max];
Packit 4a16fb
		if (!ptr)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (!strcmp(ptr, "pcm")) {
Packit 4a16fb
			pcm_flag = 1;
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		if (sscanf(ptr, "%i.%i", &v1, &v2) != 2) {
Packit 4a16fb
			fprintf(stderr, "Wrong argument '%s'...\n", argv[max]);
Packit 4a16fb
			return;
Packit 4a16fb
		}
Packit 4a16fb
		addr.client = v1;
Packit 4a16fb
		addr.port = v2;
Packit 4a16fb
		snd_seq_port_subscribe_set_dest(sub, &addr);
Packit 4a16fb
		if ((err = snd_seq_subscribe_port(handle, sub))<0) {
Packit 4a16fb
			fprintf(stderr, "Cannot subscribe port %i from client %i: %s\n", v2, v1, snd_strerror(err));
Packit 4a16fb
			return;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	printf("Destination client = %i, port = %i\n", addr.client, addr.port);
Packit 4a16fb
Packit 4a16fb
#ifdef USE_PCM
Packit 4a16fb
	if (pcm_flag) {
Packit 4a16fb
		if ((err = snd_pcm_open(&phandle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
Packit 4a16fb
			fprintf(stderr, "Playback open error: %s\n", snd_strerror(err));
Packit 4a16fb
			exit(0);
Packit 4a16fb
		}
Packit 4a16fb
		set_hwparams(phandle);
Packit 4a16fb
		pbuf = calloc(1, period_size * 4);
Packit 4a16fb
		if (pbuf == NULL) {
Packit 4a16fb
			fprintf(stderr, "No enough memory...\n");
Packit 4a16fb
			exit(0);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
	event_sender_start_timer(handle, client, queue, phandle);
Packit 4a16fb
	
Packit 4a16fb
	/* send the first event */
Packit 4a16fb
	send_event(handle, queue, client, port, &addr, &time);
Packit 4a16fb
#ifdef USE_PCM
Packit 4a16fb
	if (phandle)
Packit 4a16fb
		max += snd_pcm_poll_descriptors_count(phandle);
Packit 4a16fb
#endif
Packit 4a16fb
	pfds = alloca(sizeof(*pfds) * max);
Packit 4a16fb
	while (1) {
Packit 4a16fb
		int nseqs = snd_seq_poll_descriptors_count(handle, POLLOUT|POLLIN);
Packit 4a16fb
		if (snd_seq_event_output_pending(handle))
Packit 4a16fb
			snd_seq_poll_descriptors(handle, pfds, nseqs, POLLOUT|POLLIN);
Packit 4a16fb
		else
Packit 4a16fb
			snd_seq_poll_descriptors(handle, pfds, nseqs, POLLIN);
Packit 4a16fb
		max = nseqs;
Packit 4a16fb
#ifdef USE_PCM
Packit 4a16fb
		if (phandle) {
Packit 4a16fb
			int pmax = snd_pcm_poll_descriptors_count(phandle);
Packit 4a16fb
			snd_seq_poll_descriptors(phandle, pfds + max, pmax);
Packit 4a16fb
			max += pmax;
Packit 4a16fb
		}
Packit 4a16fb
#endif
Packit 4a16fb
		if (poll(pfds, max, -1) < 0)
Packit 4a16fb
			break;
Packit 4a16fb
#ifdef USE_PCM
Packit 4a16fb
		if (phandle && (pfds[nseqs].revents & POLLOUT)) {
Packit 4a16fb
			if (snd_pcm_writei(phandle, pbuf, period_size) != period_size) {
Packit 4a16fb
				fprintf(stderr, "Playback write error!!\n");
Packit 4a16fb
				exit(0);
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
#endif
Packit 4a16fb
		if (pfds[0].revents & POLLOUT)
Packit 4a16fb
			snd_seq_drain_output(handle);
Packit 4a16fb
		if (pfds[0].revents & POLLIN) {
Packit 4a16fb
			do {
Packit 4a16fb
				if ((err = snd_seq_event_input(handle, &ev))<0)
Packit 4a16fb
					break;
Packit 4a16fb
				if (!ev)
Packit 4a16fb
					continue;
Packit 4a16fb
				if (ev->type == SND_SEQ_EVENT_ECHO)
Packit 4a16fb
					send_event(handle, queue, client, port, &addr, &time);
Packit 4a16fb
				decode_event(ev);
Packit 4a16fb
				snd_seq_free_event(ev);
Packit 4a16fb
			} while (err > 0);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
}