Blame test/playmidi1.c

Packit 4a16fb
/*
Packit 4a16fb
 *   MIDI file player for ALSA sequencer 
Packit 4a16fb
 *   (type 0 only!, the library that is used doesn't support merging of tracks)
Packit 4a16fb
 *
Packit 4a16fb
 *   Copyright (c) 1998 by Frank van de Pol <F.K.W.van.de.Pol@inter.nl.net>
Packit 4a16fb
 *
Packit 4a16fb
 *   Modified so that this uses alsa-lib
Packit 4a16fb
 *   1999 Jan. by Isaku Yamahata <yamahata@kusm.kyoto-u.ac.jp>
Packit 4a16fb
 *
Packit 4a16fb
 *   19990604	Takashi Iwai <iwai@ww.uni-erlangen.de>
Packit 4a16fb
 *	- use blocking mode
Packit 4a16fb
 *	- fix tempo event bug
Packit 4a16fb
 *	- add command line options
Packit 4a16fb
 *
Packit 4a16fb
 *   19990827	Takashi Iwai <iwai@ww.uni-erlangen.de>
Packit 4a16fb
 *	- use snd_seq_alloc_queue()
Packit 4a16fb
 *
Packit 4a16fb
 *   19990916	Takashi Iwai <iwai@ww.uni-erlangen.de>
Packit 4a16fb
 *	- use middle-level sequencer routines and macros
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU General Public License as published by
Packit 4a16fb
 *   the Free Software Foundation; either version 2 of the License, or
Packit 4a16fb
 *   (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU General Public License
Packit 4a16fb
 *   along with this program; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <ctype.h>
Packit 4a16fb
#include <fcntl.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <sys/ioctl.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <errno.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
Packit 4a16fb
#include "midifile.h"		/* SMF library header */
Packit 4a16fb
#include "midifile.c"		/* SMF library code */
Packit 4a16fb
Packit 4a16fb
#include "../include/asoundlib.h"
Packit 4a16fb
Packit 4a16fb
/* send the real-time time stamps (instead of midi ticks) to the ALSA sequencer */
Packit 4a16fb
static int use_realtime = 0;
Packit 4a16fb
Packit 4a16fb
/* control the event buffering by using a blocking mode */
Packit 4a16fb
static int use_blocking_mode = 1;
Packit 4a16fb
Packit 4a16fb
/* default destination queue, client and port numbers */
Packit 4a16fb
#define DEST_CLIENT_NUMBER	65
Packit 4a16fb
#define DEST_PORT_NUMBER	0
Packit 4a16fb
Packit 4a16fb
/* event pool size */
Packit 4a16fb
#define WRITE_POOL_SIZE		200
Packit 4a16fb
#define WRITE_POOL_SPACE	10
Packit 4a16fb
#define READ_POOL_SIZE		10	/* we need to read the pool only for echoing */
Packit 4a16fb
Packit 4a16fb
static FILE *F;
Packit 4a16fb
static snd_seq_t *seq_handle = NULL;
Packit 4a16fb
static int ppq = 96;
Packit 4a16fb
static int slave_ppq = 96;
Packit 4a16fb
Packit 4a16fb
static double local_secs = 0;
Packit 4a16fb
static int local_ticks = 0;
Packit 4a16fb
static int local_tempo = 500000;
Packit 4a16fb
Packit 4a16fb
static int dest_queue = -1;
Packit 4a16fb
static int shared_queue = 0;
Packit 4a16fb
static int tick_offset = 0;
Packit 4a16fb
static int dest_client = DEST_CLIENT_NUMBER;
Packit 4a16fb
static int dest_port = DEST_PORT_NUMBER;
Packit 4a16fb
static int my_port = 0;
Packit 4a16fb
Packit 4a16fb
static int verbose = 0;
Packit 4a16fb
static int slave   = 0;		/* allow external sync */
Packit 4a16fb
Packit 4a16fb
#define VERB_INFO	1
Packit 4a16fb
#define VERB_MUCH	2
Packit 4a16fb
#define VERB_EVENT	3
Packit 4a16fb
Packit 4a16fb
static void alsa_start_timer(void);
Packit 4a16fb
static void alsa_stop_timer(void);
Packit 4a16fb
static void wait_start(void);
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
static inline double tick2time_dbl(int tick)
Packit 4a16fb
{
Packit 4a16fb
	return local_secs + ((double) (tick - local_ticks) * (double) local_tempo * 1.0E-6 / (double) ppq);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void tick2time(snd_seq_real_time_t * tm, int tick)
Packit 4a16fb
{
Packit 4a16fb
	double secs = tick2time_dbl(tick);
Packit 4a16fb
	tm->tv_sec = secs;
Packit 4a16fb
	tm->tv_nsec = (secs - tm->tv_sec) * 1.0E9;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void write_ev(snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	int rc;
Packit 4a16fb
Packit 4a16fb
	if (use_blocking_mode) {
Packit 4a16fb
		rc = snd_seq_event_output(seq_handle, ev);
Packit 4a16fb
		if (rc < 0) {
Packit 4a16fb
			printf("written = %i (%s)\n", rc, snd_strerror(rc));
Packit 4a16fb
			exit(1);
Packit 4a16fb
		}
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	while ((rc = snd_seq_event_output(seq_handle, ev)) < 0) {
Packit 4a16fb
		int npfds = snd_seq_poll_descriptors_count(seq_handle, POLLOUT);
Packit 4a16fb
		struct pollfd *pfds = alloca(sizeof(*pfds) * npfds);
Packit 4a16fb
		snd_seq_poll_descriptors(seq_handle, pfds, npfds, POLLOUT);
Packit 4a16fb
		if ((rc = poll(pfds, npfds, -1)) < 0) {
Packit 4a16fb
			printf("poll error = %i (%s)\n", rc, snd_strerror(errno));
Packit 4a16fb
			exit(1);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* read the byte */
Packit 4a16fb
static int mygetc(void)
Packit 4a16fb
{
Packit 4a16fb
	return getc(F);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* print out the text */
Packit 4a16fb
static void mytext(int type ATTRIBUTE_UNUSED, int leng, char *msg)
Packit 4a16fb
{
Packit 4a16fb
	char *p;
Packit 4a16fb
	char *ep = msg + leng;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_INFO) {
Packit 4a16fb
		for (p = msg; p < ep; p++)
Packit 4a16fb
			putchar(isprint(*p) ? *p : '?');
Packit 4a16fb
		putchar('\n');
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void do_header(int format, int ntracks, int division)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_queue_tempo_t *tempo;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_INFO)
Packit 4a16fb
		printf("smf format %d, %d tracks, %d ppq\n", format, ntracks, division);
Packit 4a16fb
	ppq = division;
Packit 4a16fb
Packit 4a16fb
	if (format != 0 || ntracks != 1) {
Packit 4a16fb
		printf("This player does not support merging of tracks.\n");
Packit 4a16fb
		if (! shared_queue)
Packit 4a16fb
			alsa_stop_timer();
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	/* set the ppq */
Packit 4a16fb
	snd_seq_queue_tempo_alloca(&tempo);
Packit 4a16fb
	/* ppq must be set before starting the timer */
Packit 4a16fb
	if (snd_seq_get_queue_tempo(seq_handle, dest_queue, tempo) < 0) {
Packit 4a16fb
    		perror("get_queue_tempo");
Packit 4a16fb
    		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	if ((slave_ppq = snd_seq_queue_tempo_get_ppq(tempo)) != ppq) {
Packit 4a16fb
		snd_seq_queue_tempo_set_ppq(tempo, ppq);
Packit 4a16fb
		if (snd_seq_set_queue_tempo(seq_handle, dest_queue, tempo) < 0) {
Packit 4a16fb
    			perror("set_queue_tempo");
Packit 4a16fb
    			if (!slave && !shared_queue)
Packit 4a16fb
    				exit(1);
Packit 4a16fb
			else
Packit 4a16fb
				printf("different PPQ %d in SMF from queue PPQ %d\n", ppq, slave_ppq);
Packit 4a16fb
		} else
Packit 4a16fb
			slave_ppq = ppq;
Packit 4a16fb
		if (verbose >= VERB_INFO)
Packit 4a16fb
			printf("ALSA Timer updated, PPQ = %d\n", snd_seq_queue_tempo_get_ppq(tempo));
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	/* start playing... */
Packit 4a16fb
	if (slave) {
Packit 4a16fb
		if (verbose >= VERB_INFO)
Packit 4a16fb
			printf("Wait till timer starts...\n");	
Packit 4a16fb
		wait_start();
Packit 4a16fb
		if (verbose >= VERB_INFO)
Packit 4a16fb
			printf("Go!\n");	
Packit 4a16fb
	} else if (shared_queue) {
Packit 4a16fb
		snd_seq_queue_status_t *stat;
Packit 4a16fb
		snd_seq_queue_status_alloca(&stat;;
Packit 4a16fb
		snd_seq_get_queue_status(seq_handle, dest_queue, stat);
Packit 4a16fb
		tick_offset = snd_seq_queue_status_get_tick_time(stat);
Packit 4a16fb
		fprintf(stderr, "tick offset = %d\n", tick_offset);
Packit 4a16fb
	} else {
Packit 4a16fb
		alsa_start_timer();
Packit 4a16fb
		tick_offset = 0;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* fill the event time */
Packit 4a16fb
static void set_event_time(snd_seq_event_t *ev, unsigned int currtime)
Packit 4a16fb
{
Packit 4a16fb
	if (use_realtime) {
Packit 4a16fb
		snd_seq_real_time_t rtime;
Packit 4a16fb
		if (ppq != slave_ppq)
Packit 4a16fb
			currtime = (currtime * slave_ppq) / ppq;
Packit 4a16fb
		tick2time(&rtime, currtime);
Packit 4a16fb
		snd_seq_ev_schedule_real(ev, dest_queue, 0, &rtime);
Packit 4a16fb
	} else {
Packit 4a16fb
		if (ppq != slave_ppq)
Packit 4a16fb
			currtime = (currtime * slave_ppq) / ppq;
Packit 4a16fb
		currtime += tick_offset;
Packit 4a16fb
		snd_seq_ev_schedule_tick(ev, dest_queue, 0, currtime);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* fill the normal event header */
Packit 4a16fb
static void set_event_header(snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_ev_clear(ev);
Packit 4a16fb
	snd_seq_ev_set_dest(ev, dest_client, dest_port);
Packit 4a16fb
	snd_seq_ev_set_source(ev, my_port);
Packit 4a16fb
	set_event_time(ev, Mf_currtime);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* start the timer */
Packit 4a16fb
static void alsa_start_timer(void)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_start_queue(seq_handle, dest_queue, NULL);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* stop the timer */
Packit 4a16fb
static void alsa_stop_timer(void)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_stop_queue(seq_handle, dest_queue, &ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* change the tempo */
Packit 4a16fb
static void do_tempo(int us)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_MUCH) {
Packit 4a16fb
		double bpm;
Packit 4a16fb
		bpm = 60.0E6 / (double) us;
Packit 4a16fb
		printf("Tempo %d us/beat, %.2f bpm\n", us, bpm);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	/* store the new tempo and timestamp of the tempo change */
Packit 4a16fb
	local_secs = tick2time_dbl(Mf_currtime);
Packit 4a16fb
	local_ticks = Mf_currtime;
Packit 4a16fb
	local_tempo = us;
Packit 4a16fb
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	if (!slave)
Packit 4a16fb
		snd_seq_change_queue_tempo(seq_handle, dest_queue, us, &ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void do_noteon(int chan, int pitch, int vol)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_EVENT)
Packit 4a16fb
		printf("%lu: NoteOn (%d) %d %d\n", Mf_currtime, chan, pitch, vol);
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_ev_set_noteon(&ev, chan, pitch, vol);
Packit 4a16fb
	write_ev(&ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
static void do_noteoff(int chan, int pitch, int vol)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_EVENT)
Packit 4a16fb
		printf("%lu: NoteOff (%d) %d %d\n", Mf_currtime, chan, pitch, vol);
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_ev_set_noteoff(&ev, chan, pitch, vol);
Packit 4a16fb
	write_ev(&ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
static void do_program(int chan, int program)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_EVENT)
Packit 4a16fb
		printf("%lu: Program (%d) %d\n", Mf_currtime, chan, program);
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_ev_set_pgmchange(&ev, chan, program);
Packit 4a16fb
	write_ev(&ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
static void do_parameter(int chan, int control, int value)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_EVENT)
Packit 4a16fb
		printf("%lu: Control (%d) %d %d\n", Mf_currtime, chan, control, value);
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_ev_set_controller(&ev, chan, control, value);
Packit 4a16fb
	write_ev(&ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
static void do_pitchbend(int chan, int lsb, int msb)
Packit 4a16fb
{	/* !@#$% lsb & msb are in the wrong order in docs */
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_EVENT)
Packit 4a16fb
		printf("%lu: Pitchbend (%d) %d %d\n", Mf_currtime, chan, lsb, msb);
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_ev_set_pitchbend(&ev, chan, (lsb + (msb << 7)) - 8192);
Packit 4a16fb
	write_ev(&ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void do_pressure(int chan, int pitch, int pressure)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_EVENT)
Packit 4a16fb
		printf("%lu: KeyPress (%d) %d %d\n", Mf_currtime, chan, pitch, pressure);
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_ev_set_keypress(&ev, chan, pitch, pressure);
Packit 4a16fb
	write_ev(&ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void do_chanpressure(int chan, int pressure)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_EVENT)
Packit 4a16fb
		printf("%lu: ChanPress (%d) %d\n", Mf_currtime, chan, pressure);
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_ev_set_chanpress(&ev, chan, pressure);
Packit 4a16fb
	write_ev(&ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void do_sysex(int len, char *msg)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t ev;
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_MUCH) {
Packit 4a16fb
		int c;
Packit 4a16fb
		printf("%lu: Sysex, len=%d\n", Mf_currtime, len);
Packit 4a16fb
		for (c = 0; c < len; c++) {
Packit 4a16fb
			printf(" %02x", (unsigned char)msg[c]);
Packit 4a16fb
			if (c % 16 == 15)
Packit 4a16fb
				putchar('\n');
Packit 4a16fb
		}
Packit 4a16fb
		if (c % 16 != 15)
Packit 4a16fb
			putchar('\n');
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	set_event_header(&ev;;
Packit 4a16fb
	snd_seq_ev_set_sysex(&ev, len, msg);
Packit 4a16fb
	write_ev(&ev;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static snd_seq_event_t *wait_for_event(void)
Packit 4a16fb
{
Packit 4a16fb
	int left;
Packit 4a16fb
	snd_seq_event_t *input_event;
Packit 4a16fb
  
Packit 4a16fb
	if (use_blocking_mode) {
Packit 4a16fb
		/* read the event - blocked until any event is read */
Packit 4a16fb
		left = snd_seq_event_input(seq_handle, &input_event);
Packit 4a16fb
	} else {
Packit 4a16fb
		/* read the event - using select syscall */
Packit 4a16fb
		while ((left = snd_seq_event_input(seq_handle, &input_event)) >= 0 &&
Packit 4a16fb
		       input_event == NULL) {
Packit 4a16fb
			int npfds = snd_seq_poll_descriptors_count(seq_handle, POLLIN);
Packit 4a16fb
			struct pollfd *pfds = alloca(sizeof(*pfds) * npfds);
Packit 4a16fb
			snd_seq_poll_descriptors(seq_handle, pfds, npfds, POLLIN);
Packit 4a16fb
			if ((left = poll(pfds, npfds, -1)) < 0) {
Packit 4a16fb
				printf("poll error = %i (%s)\n", errno, snd_strerror(errno));
Packit 4a16fb
				exit(1);
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (left < 0) {
Packit 4a16fb
		printf("alsa_sync error!:%s\n", snd_strerror(left));
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return input_event;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* synchronize to the end of the event */
Packit 4a16fb
static void alsa_sync(void)
Packit 4a16fb
{
Packit 4a16fb
	/* send the echo event to the self client. */
Packit 4a16fb
	if (verbose >= VERB_MUCH)
Packit 4a16fb
		printf("alsa_sync syncing...\n");
Packit 4a16fb
	/* dump the buffer */
Packit 4a16fb
	snd_seq_drain_output(seq_handle);
Packit 4a16fb
	snd_seq_sync_output_queue(seq_handle);
Packit 4a16fb
	if (verbose >= VERB_MUCH)
Packit 4a16fb
		printf("alsa_sync synced\n");
Packit 4a16fb
	sleep(1); /* give a time for note releasing.. */
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
/* wait for the start of the queue */
Packit 4a16fb
static void wait_start(void)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t *input_event;
Packit 4a16fb
Packit 4a16fb
	/* wait for the start event from the system timer */
Packit 4a16fb
	for (;;) {
Packit 4a16fb
		input_event = wait_for_event();
Packit 4a16fb
		if (input_event) {
Packit 4a16fb
			if (verbose >= VERB_MUCH)
Packit 4a16fb
				printf("wait_start got event. type=%d, flags=%d\n",
Packit 4a16fb
				       input_event->type, input_event->flags);
Packit 4a16fb
			if (input_event->type == SND_SEQ_EVENT_START &&
Packit 4a16fb
			    input_event->data.queue.queue == dest_queue) {
Packit 4a16fb
				snd_seq_free_event(input_event);
Packit 4a16fb
				break;
Packit 4a16fb
			}
Packit 4a16fb
			snd_seq_free_event(input_event);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (verbose >= VERB_MUCH)
Packit 4a16fb
		printf("start received\n");
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
/* print the usage */
Packit 4a16fb
static void usage(void)
Packit 4a16fb
{
Packit 4a16fb
	fprintf(stderr, "usage: playmidi1 [options] [file]\n");
Packit 4a16fb
	fprintf(stderr, "  options:\n");
Packit 4a16fb
	fprintf(stderr, "  -v: verbose mode\n");
Packit 4a16fb
	fprintf(stderr, "  -a client:port : set destination address (default=%d:%d)\n",
Packit 4a16fb
		DEST_CLIENT_NUMBER, DEST_PORT_NUMBER);
Packit 4a16fb
	fprintf(stderr, "  -q queue: use the specified queue\n");
Packit 4a16fb
	fprintf(stderr, "  -s queue: slave mode (allow external clock synchronization)\n");
Packit 4a16fb
	fprintf(stderr, "  -r : play on real-time mode\n");
Packit 4a16fb
	fprintf(stderr, "  -b : play on non-blocking mode\n");
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int main(int argc, char *argv[])
Packit 4a16fb
{
Packit 4a16fb
	int tmp;
Packit 4a16fb
	int c;
Packit 4a16fb
	snd_seq_addr_t dest_addr;
Packit 4a16fb
	const char *addr = "65:0";
Packit 4a16fb
Packit 4a16fb
	while ((c = getopt(argc, argv, "s:a:p:q:vrb")) != -1) {
Packit 4a16fb
		switch (c) {
Packit 4a16fb
		case 'v':
Packit 4a16fb
			verbose++;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'a':
Packit 4a16fb
		case 'p':
Packit 4a16fb
			addr = optarg;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'q':
Packit 4a16fb
			dest_queue = atoi(optarg);
Packit 4a16fb
			if (dest_queue < 0) {
Packit 4a16fb
				fprintf(stderr, "invalid queue number %d\n", dest_queue);
Packit 4a16fb
				exit(1);
Packit 4a16fb
			}
Packit 4a16fb
			break;
Packit 4a16fb
		case 's':
Packit 4a16fb
			slave = 1;
Packit 4a16fb
			dest_queue = atoi(optarg);
Packit 4a16fb
			if (dest_queue < 0) {
Packit 4a16fb
				fprintf(stderr, "invalid queue number %d\n", dest_queue);
Packit 4a16fb
				exit(1);
Packit 4a16fb
			}
Packit 4a16fb
			break;
Packit 4a16fb
		case 'r':
Packit 4a16fb
			use_realtime = 1;
Packit 4a16fb
			break;
Packit 4a16fb
		case 'b':
Packit 4a16fb
			use_blocking_mode = 0;
Packit 4a16fb
			break;
Packit 4a16fb
		default:
Packit 4a16fb
			usage();
Packit 4a16fb
			exit(1);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_INFO) {
Packit 4a16fb
		if (use_realtime)
Packit 4a16fb
			printf("ALSA MIDI Player, feeding events to real-time queue\n");
Packit 4a16fb
		else
Packit 4a16fb
			printf("ALSA MIDI Player, feeding events to song queue\n");
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	/* open the sequencer device */
Packit 4a16fb
	/* Here we open the device in read/write for slave mode. */
Packit 4a16fb
	tmp = snd_seq_open(&seq_handle, "hw", slave ? SND_SEQ_OPEN_DUPLEX : SND_SEQ_OPEN_OUTPUT, 0);
Packit 4a16fb
	if (tmp < 0) {
Packit 4a16fb
		perror("open /dev/snd/seq");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	tmp = snd_seq_nonblock(seq_handle, !use_blocking_mode);
Packit 4a16fb
	if (tmp < 0) {
Packit 4a16fb
		perror("block_mode");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
			
Packit 4a16fb
	/* set the name */
Packit 4a16fb
	/* set the event filter to receive only the echo event */
Packit 4a16fb
	/* if running in slave mode, also listen for a START event */
Packit 4a16fb
	if (slave)
Packit 4a16fb
		snd_seq_set_client_event_filter(seq_handle, SND_SEQ_EVENT_START);
Packit 4a16fb
	snd_seq_set_client_name(seq_handle, "MIDI file player");
Packit 4a16fb
Packit 4a16fb
	/* create the port */
Packit 4a16fb
	my_port = snd_seq_create_simple_port(seq_handle, "Port 0",
Packit 4a16fb
					     SND_SEQ_PORT_CAP_WRITE |
Packit 4a16fb
					     SND_SEQ_PORT_CAP_READ,
Packit 4a16fb
					     SND_SEQ_PORT_TYPE_MIDI_GENERIC);
Packit 4a16fb
	if (my_port < 0) {
Packit 4a16fb
		perror("create port");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	if (snd_seq_parse_address(seq_handle, &dest_addr, addr) < 0) {
Packit 4a16fb
		perror("invalid destination address");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	dest_client = dest_addr.client;
Packit 4a16fb
	dest_port = dest_addr.port;
Packit 4a16fb
Packit 4a16fb
	/* set the queue */
Packit 4a16fb
	if (dest_queue >= 0) {
Packit 4a16fb
		shared_queue = 1;
Packit 4a16fb
		if (snd_seq_set_queue_usage(seq_handle, dest_queue, 1) < 0) {
Packit 4a16fb
			perror("use queue");
Packit 4a16fb
			exit(1);
Packit 4a16fb
		}
Packit 4a16fb
	} else {
Packit 4a16fb
		shared_queue = 0;
Packit 4a16fb
		dest_queue = snd_seq_alloc_queue(seq_handle);
Packit 4a16fb
		if (dest_queue < 0) {
Packit 4a16fb
			perror("alloc queue");
Packit 4a16fb
			exit(1);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	/* set the subscriber */
Packit 4a16fb
	tmp = snd_seq_connect_to(seq_handle, my_port, dest_client, dest_port);
Packit 4a16fb
	if (tmp < 0) {
Packit 4a16fb
		perror("subscribe");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	/* subscribe for the timer START event */	
Packit 4a16fb
	if (slave) {	
Packit 4a16fb
		tmp = snd_seq_connect_from(seq_handle, my_port,
Packit 4a16fb
					   SND_SEQ_CLIENT_SYSTEM,
Packit 4a16fb
					   dest_queue + 16 /*snd_seq_queue_sync_port(dest_queue)*/);
Packit 4a16fb
		if (tmp < 0) {
Packit 4a16fb
			perror("subscribe");
Packit 4a16fb
			exit(1);
Packit 4a16fb
		}	
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	/* change the pool size */
Packit 4a16fb
	if (snd_seq_set_client_pool_output(seq_handle, WRITE_POOL_SIZE) < 0 ||
Packit 4a16fb
	    snd_seq_set_client_pool_input(seq_handle, READ_POOL_SIZE) < 0 ||
Packit 4a16fb
	    snd_seq_set_client_pool_output_room(seq_handle, WRITE_POOL_SPACE) < 0) {
Packit 4a16fb
		perror("pool");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	if (optind < argc) {
Packit 4a16fb
		F = fopen(argv[optind], "r");
Packit 4a16fb
		if (F == NULL) {
Packit 4a16fb
			fprintf(stderr, "playmidi1: can't open file %s\n", argv[optind]);
Packit 4a16fb
			exit(1);
Packit 4a16fb
		}
Packit 4a16fb
	} else
Packit 4a16fb
		F = stdin;
Packit 4a16fb
Packit 4a16fb
	Mf_header = do_header;
Packit 4a16fb
	Mf_tempo = do_tempo;
Packit 4a16fb
	Mf_getc = mygetc;
Packit 4a16fb
	Mf_text = mytext;
Packit 4a16fb
Packit 4a16fb
	Mf_noteon = do_noteon;
Packit 4a16fb
	Mf_noteoff = do_noteoff;
Packit 4a16fb
	Mf_program = do_program;
Packit 4a16fb
	Mf_parameter = do_parameter;
Packit 4a16fb
	Mf_pitchbend = do_pitchbend;
Packit 4a16fb
	Mf_pressure = do_pressure;
Packit 4a16fb
	Mf_chanpressure = do_chanpressure;
Packit 4a16fb
	Mf_sysex = do_sysex;
Packit 4a16fb
Packit 4a16fb
	/* go.. go.. go.. */
Packit 4a16fb
	mfread();
Packit 4a16fb
Packit 4a16fb
	alsa_sync();
Packit 4a16fb
	if (! shared_queue)
Packit 4a16fb
		alsa_stop_timer();
Packit 4a16fb
Packit 4a16fb
	snd_seq_close(seq_handle);
Packit 4a16fb
Packit 4a16fb
	if (verbose >= VERB_INFO) {
Packit 4a16fb
		printf("Stopping at %f s,  tick %f\n",
Packit 4a16fb
		       tick2time_dbl(Mf_currtime + 1), (double) (Mf_currtime + 1));
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	exit(0);
Packit 4a16fb
}