#import <musickit/musickit.h>
#import <sys/file.h>
#import "SmsInstrument.h"

void main(int ac, char *av[])
{
	int i,partCount;
	id aSFPerformer,smsIns;
	double fSamplingRate = 22050;
	int inFd,outFd;
	id scoreInfo,noteSenders;
	NXStream *inStream,*outStream;
	if (ac != 3) 
	{
		fprintf(stderr,
			"Usage: smsMk <input score file> <output snd file>.\n");
		exit(1);
	}
	inFd = open(av[1],O_RDONLY,0660); 
	if (inFd == -1) 
	{
		fprintf(stderr,"Can't open %s\n.",av[1]);
		exit(1);
	} 
	else 
		fprintf(stderr,"Input file: %s\n",av[1]);

	outFd = open(av[2], O_RDWR | O_CREAT | O_TRUNC, 0660);
	if (outFd == -1) 
	{
		fprintf(stderr,"Can't create %s\n.",av[2]);
		exit(1);
	} 
	else 
		fprintf(stderr,"Output file: %s\n",av[2]);
	inStream = NXOpenFile(inFd,NX_READONLY);
	outStream = NXOpenFile(outFd,NX_READWRITE);
	aSFPerformer = [[ScorefilePerformer alloc] init];
	[aSFPerformer setStream:inStream];
	[aSFPerformer activate]; 
	scoreInfo = [aSFPerformer info];
	if (scoreInfo) 
	{ 
		/* Configure performance as specified in info. */ 
		if ([scoreInfo isParPresent:MK_samplingRate]) 
			fSamplingRate = [scoreInfo parAsDouble:MK_samplingRate];
	}
	smsIns = [[SmsInstrument alloc] init];
	[smsIns setSamplingRate:fSamplingRate stream:outStream];
	noteSenders = [aSFPerformer noteSenders];
	partCount = [noteSenders count];
	for (i = 0; i < partCount; i++) 
		[[noteSenders objectAt:i] connect:[smsIns noteReceiver]];
	[noteSenders free];
	[Conductor setClocked:NO];     /* User process runs as fast as it can. */
	fprintf(stderr,"synthesizing...\n");
	[Conductor startPerformance];  /* Start sending Notes, loops till done.*/
	fprintf(stderr,"...done\n");
	NXClose(outStream);
	exit(0);
}