/* files.c */
/* Copyright 1990 Thomas E. Janzen All Rights Reserved */
/*
**  FACILITY:
**
**	AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
**	compiled with Lattice (TM) C 5.05
**
**  ABSTRACT:
**
**	Algorhythms improvises music over the MIDI serial port.
**
**  AUTHORS: Thomas E. Janzen
**
**  CREATION DATE:	26-MAR-1990
**
**  MODIFICATION HISTORY:
**    DATE	NAME	DESCRIPTION
**--
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdlib.h>
#include "AlgoRhythms.h"

extern struct GfxBase *GfxBase;
extern struct IntuitionBase *IntuitionBase;
extern struct DOSBase *DOSBase;
extern struct MathBase *MathBase;

static int Open_Read_File(char *FileName);
static int Open_Save_File(char *FileName);

FILE *FilePointer;

struct Parameter {
	double CenterCycle;
	double CenterPhase;
	double SpreadCycle;
	double SpreadPhase;
};

int Save_File(char *FileName, const double *TotalDuration,
	const int *ScaleLength, const int Scale[], const int *voices,
	const int *tempo,
	const struct Parameter *Pitch, 
	const struct Parameter *Thickness,
	const struct Parameter *Dynamics, 
	const struct Parameter *Duration,
	const struct NoteEvent *Events,
	double MinNoteLen, double MaxNoteLen){

	int Status=0;
	register int ScaleIndex;
	register int i;
	
	Status=Open_Save_File(FileName);
	if(Status == 1) return Status;

	fprintf(FilePointer,"%4.2f\n",*TotalDuration);
	fprintf(FilePointer,"%4.2f\n",MinNoteLen);
	fprintf(FilePointer,"%4.2f\n",MaxNoteLen);
	fprintf(FilePointer,"%d\n",*ScaleLength);
	for(ScaleIndex = 0; ScaleIndex<(*ScaleLength); ScaleIndex++) {
		fprintf(FilePointer,"%d\n",Scale[ScaleIndex]);
	}

	fprintf(FilePointer,"%d\n",*voices);
	fprintf(FilePointer,"%d\n",*tempo);

	fprintf(FilePointer,"%4.2f\n",Pitch->CenterCycle);
	fprintf(FilePointer,"%4.2f\n",Pitch->CenterPhase);
	fprintf(FilePointer,"%4.2f\n",Pitch->SpreadCycle);
	fprintf(FilePointer,"%4.2f\n",Pitch->SpreadPhase);

	fprintf(FilePointer,"%4.2f\n",Duration->CenterCycle);
	fprintf(FilePointer,"%4.2f\n",Duration->CenterPhase);
	fprintf(FilePointer,"%4.2f\n",Duration->SpreadCycle);
	fprintf(FilePointer,"%4.2f\n",Duration->SpreadPhase);

	fprintf(FilePointer,"%4.2f\n",Dynamics->CenterCycle);
	fprintf(FilePointer,"%4.2f\n",Dynamics->CenterPhase);
	fprintf(FilePointer,"%4.2f\n",Dynamics->SpreadCycle);
	fprintf(FilePointer,"%4.2f\n",Dynamics->SpreadPhase);

	fprintf(FilePointer,"%4.2f\n",Thickness->SpreadCycle);
	fprintf(FilePointer,"%4.2f\n",Thickness->SpreadPhase);

	for(i=0;i<MAXVOICE;i++) {
		fprintf(FilePointer,"%d  %d  %d  %d\n",
		Events[i].LowPitch,Events[i].HighPitch,Events[i].Channel,
		Events[i].Walking);
	}
	fclose(FilePointer);
	return(0);
}

int Read_File(char *FileName, double *TotalDuration, 
	int *ScaleLength, int Scale[], int *voices, int *tempo,
	struct Parameter *Pitch, struct Parameter *Thickness,
	struct Parameter *Dynamics, struct Parameter *Duration,
	const struct NoteEvent *Events, double *MinNoteLen,
	double *MaxNoteLen) {
	int Status=0;
	register int ScaleIndex;
	static char tempstring[40];
	char *stringptr;
	int buflen=40;
	register i;
	Status=Open_Read_File(FileName);
	if (Status != 0) {
		return Status;
	}
	
	stringptr=fgets(tempstring,buflen,FilePointer);
	*TotalDuration=atof(tempstring);

	stringptr=fgets(tempstring,buflen,FilePointer);
	*MinNoteLen=atof(tempstring);

	stringptr=fgets(tempstring,buflen,FilePointer);
	*MaxNoteLen=atof(tempstring);


	fscanf(FilePointer,"%d",ScaleLength);
	for(ScaleIndex = 0; ScaleIndex<(*ScaleLength); ScaleIndex++) {
		fscanf(FilePointer,"%d",&Scale[ScaleIndex]);
	}

	fscanf(FilePointer,"%d",voices);
	fscanf(FilePointer,"%d",tempo);

	stringptr=fgets(tempstring,buflen,FilePointer); /*junk*/

	stringptr=fgets(tempstring,buflen,FilePointer);
	Pitch->CenterCycle=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Pitch->CenterPhase=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Pitch->SpreadCycle=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Pitch->SpreadPhase=atof(tempstring);

	stringptr=fgets(tempstring,buflen,FilePointer);
	Duration->CenterCycle=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Duration->CenterPhase=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Duration->SpreadCycle=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Duration->SpreadPhase=atof(tempstring);

	stringptr=fgets(tempstring,buflen,FilePointer);
	Dynamics->CenterCycle=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Dynamics->CenterPhase=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Dynamics->SpreadCycle=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Dynamics->SpreadPhase=atof(tempstring);

	stringptr=fgets(tempstring,buflen,FilePointer);
	Thickness->SpreadCycle=atof(tempstring);
	stringptr=fgets(tempstring,buflen,FilePointer);
	Thickness->SpreadPhase=atof(tempstring);

	for(i=0;i<MAXVOICE;i++) {
		stringptr=fgets(tempstring,buflen,FilePointer);
		sscanf(stringptr,"%d  %d  %d  %d",
		&(Events[i].LowPitch),&(Events[i].HighPitch),
		&(Events[i].Channel),&(Events[i].Walking));
	}
	fclose(FilePointer);
	return 0;
}

static int Open_Read_File(char *FileName) {
	char *mode="r";
	if ((FilePointer = fopen(FileName, mode)) == NULL) {
		return(1);
	}
	return 0;
}

static int Open_Save_File(char *FileName) {
	char *mode="w";
	if ((FilePointer = fopen(FileName, mode)) == NULL) {
		return(1);
	}
	return 0;
}
