#include "midi.h"
#include <stdio.h>
/* 
Copright 1988, G.E.S. Consulting

NORMAL MODE tutorial, WANT_TO_SEND_DATA

Read a file, make Midi events out of the data, use want_to_send_data() 
	to play them.
*/
main(ac,av)
char **av;
	{
	int i,v,l;
	FILE *f;
	if (f=fopen(av[1],"rb")) /* if there is a file */
		{
		mpu_init(); /* install the driver, NOTE: no need to init_event_list()
					 for this program, since we won't be reading anything from
					 the MPU */
		reset_mpu(); /* get to known state */
		
		/* stuff can sound pretty funky */
		
		printf("Press a key when you can't take it anymore!\n");
		
		/* get three chars to a clump */
		
		while((i=getc(f)) != EOF && 
			(v = getc(f)) != EOF &&
			(l = getc(f)) != EOF)
			{
			/* keep values in range */
			i &= 0x7f; /* i == note value, v = velocity, l = duration */
			v &= 0x7f;
			if (i < 21 || i > 108)
				continue; /* skip unplayable notes */
			play_note(i,v,l);
			if (kbhit()) /* user couldn't stand it */
				{
				getch();
				break;
				}
			}
		mpu_close(); /* Uninstall Driver */
		}
	}

play_note(x,v,l)
	{
	int i;
	unsigned char buf[3];
	
	/* set up a Channel 1 NOTE ON Midi Message */
	
	buf[0] = 0x90; /* midi note on  CH 1*/
	buf[1] = x;  /* note value */;
	buf[2] = v /* velocity */;
	
	want_to_send_data(0,buf,3); /* send on midi track 1 */
	
	/* leave it on a while */
	while(l--)
		for (i = 0; i < 200; i++)
		; /* let it sound a bit */
	
	/* make a NOTE OFF event */
	
	buf[2] = 0; /* velocity 0 == NOTE OFF */
	
	want_to_send_data(0,buf,3); /* send on midi track 1 */
	}
