# 1 "cmus2mpu.P" 


# 1 "/usr/include/sys/types.h" 1















# 1 "/usr/include/sys/stdtypes.h" 1













typedef	int		sigset_t;	

typedef	unsigned int	speed_t;	
typedef	unsigned long	tcflag_t;	
typedef	unsigned char	cc_t;		
typedef	int		pid_t;		

typedef	unsigned short	mode_t;		
typedef	short		nlink_t;	

typedef	long		clock_t;	
typedef	long		time_t;		

typedef	int		size_t;		
typedef int		ptrdiff_t;	

typedef	unsigned short	wchar_t;	


# 17 "/usr/include/sys/types.h" 2


# 1 "/usr/include/sys/sysmacros.h" 1



















# 20 "/usr/include/sys/types.h" 2




typedef	unsigned char	u_char;
typedef	unsigned short	u_short;
typedef	unsigned int	u_int;
typedef	unsigned long	u_long;
typedef	unsigned short	ushort;		
typedef	unsigned int	uint;		


# 37 "/usr/include/sys/types.h" 

# 43 "/usr/include/sys/types.h" 


typedef	struct  _physadr_t { int r[1]; } *physadr_t;
typedef	struct label_t {
	int	val[2];
} label_t;

# 55 "/usr/include/sys/types.h" 

typedef	struct	_quad_t { long val[2]; } quad_t;
typedef	long	daddr_t;
typedef	char *	caddr_t;
typedef	unsigned long	ino_t;
typedef	short	dev_t;
typedef	long	off_t;
typedef	unsigned short	uid_t;
typedef	unsigned short	gid_t;
typedef	long	key_t;
typedef	char *	addr_t;














typedef	long	fd_mask;


# 85 "/usr/include/sys/types.h" 





typedef	struct fd_set {
	fd_mask	fds_bits[(((256)+(( (sizeof (fd_mask) * 8))-1))/( (sizeof (fd_mask) * 8)))];
} fd_set;







# 113 "/usr/include/sys/types.h" 



# 4 "cmus2mpu.P" 2
# 1 "/usr/include/carl/midi.h" 1







































extern int 	midi_out();
extern int	midi_in();
extern u_char  *midi_cmd_in();
extern char    *midi_pk();



typedef struct	midi_par {
	u_char	par_index;	
	u_char	par_val;	
	u_char	par_ub;		
	char *	par_name;	
} MidiPar;


typedef struct midi_spar {
	u_short	par_index;	
	u_short	par_val;	
	u_short	par_ub;		
	char *	par_name;	
} MidiSpar;




# 5 "cmus2mpu.P" 2
# 1 "/usr/include/carl/dx7.h" 1












































extern struct	midi_par dx7_ini_vox[];
extern u_short	dx7_ini_vox_len;
extern struct	midi_par dx7_ini_fun[];
extern u_short	dx7_ini_fun_len;
extern struct 	midi_par dx7_ch_info[];
extern u_short	dx7_ch_info_len;
extern struct	midi_par	dx7_xmit_ctl[];
extern u_short	dx7_xmit_ctl_len;
extern struct	midi_par	dx7_rcv_ctl[];
extern u_short	dx7_rcv_ctl_len;
extern struct	midi_par dx7_32v_vox[];
extern u_short	dx7_32v_vox_len;


struct dx7 {
	u_char	dx7_vox_val[(32)][(155)];	
	u_char dx7_fun_val[(14)];			
	u_char dx7_kbd_val[(127)];			
	u_char dx7_ctl_val[(127)];			
	u_char	dx7_nsv;				
	u_char	dx7_prg_val;				
	u_short	dx7_pbd_val;				
};




















































































































# 6 "cmus2mpu.P" 2
# 1 "/usr/include/carl/mpu.h" 1


typedef struct mpu_cmd {
	unsigned char time_tag;
	unsigned char arg_cnt;
	unsigned char cmd_cont;
	unsigned char cmd_type;
	long mpu_time;
	unsigned char *mpu_cmd;
} MpuCmd;
extern MpuCmd *GetMpuCmd();
extern struct   mpu_cmd *get_mpu_cmd();











































































# 7 "cmus2mpu.P" 2

elapsed_time(fd)
	int fd;
{
	static	 long	elapsed_ticks;
	register long	tticks = Now * ((((120))*(60.0))/60.0) + .5;
	register int	ticks = tticks - elapsed_ticks;
	register int	periods = ticks / (240);
	register int	phase   = ticks % (240);

	while(periods--)
		midi_out(fd, (0xF8));

	midi_out(fd, phase);
	elapsed_ticks = tticks;
}












M_start()
{
	extern int arg_index;
	extern char *arg_option;
	int flag;

	while ((flag = crack( Argc,  Argv,  "h", 1)) != NULL) {
		switch (flag) {
			case 'h':
			default:
				usage(0);
		}
	}
	Print_notes = NULL;
	SubstitutePname("play_note");
}

usage(ex)
{
fprintf(stderr, "%s%s%s%s",
"cmus2mpu: usage:\n",
"    cmus2mpu < cmusic_note_statements > mpu_code\n",
"where each note_statement is:\n",
"    note begin_time any_name duration key [[[velocity] channel] program];\n");
exit(ex);
}

M_ter()
{
	midi_out(1, 0);
	midi_out(1, 0xf9);
}

Player play_note()
{
	extern double timescale();
	Instance int sequence; 
	Instance u_char key, chan, prg;
	Instance double time;
	u_char vel;

	Kill_test(P_GE);
	if (sequence == 0) {
		key = atoi(Pi5);
		if (Hipn(Self, -1) >= 6)
			vel = atoi(Pi6);
		else
			vel = 64;
		if (Hipn(Self, -1) >= 7)
			chan = atoi(Pi7);
		else
			chan = 0;
		if (Hipn(Self, -1) >= 8) {
			prg = atoi(Pi8);
			elapsed_time(1);
			if (send_ch(1, (0xC0), chan, prg, 0) != 0)
				Pexit(1);
		}
		elapsed_time(1);
		if (send_ch(1, (0x90), chan, key, vel) != 0)
			Pexit(1);
		Wait_until(Abs_time() + atof(Pi4) - .001);
		sequence++;
	} else {
		elapsed_time(1);
		if (send_ch(1, (0x90), chan, key, 0) != 0)
		    exit(1);
		Kill(Self);
	}
}












