home *** CD-ROM | disk | FTP | other *** search
- /* Files.c
- Copyright (c) 1990,1991,1992 by Thomas E. Janzen
- All Rights Reserved
-
- THIS SOFTWARE IS FURNISHED FREE OF CHARGE FOR STUDY AND USE AND MAY
- BE COPIED ONLY FOR PERSONAL USE OR COMPLETELY AS OFFERED WITH NO
- CHANGES FOR FREE DISTRIBUTION. NO TITLE TO AND OWNERSHIP OF THE
- SOFTWARE IS HEREBY TRANSFERRED. THOMAS E. JANZEN ASSUMES NO
- RESPONSBILITY FOR THE USE OR RELIABILITY OF THIS SOFTWARE.
-
- Thomas E. Janzen
- 58A School St. Apt. 2-L
- Hudson, MA 01749
- (508)562-1295
- */
- /*
- ** FACILITY:
- **
- ** AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
- ** compiled with SAS/C V5.10b
- **
- ** ABSTRACT:
- **
- ** Files.c handles file reading and writing.
- **
- ** AUTHORS: Thomas E. Janzen
- **
- ** CREATION DATE: 26-MAR-1990
- **
- ** MODIFICATION HISTORY:
- ** DATE NAME DESCRIPTION
- ** 4 Jan 92 TEJ last changes for 2.0
- **--
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <stdlib.h>
- #include <intuition/intuition.h>
- #include <devices/timer.h>
-
- #include "AlgoRhythms.h" /* header for main function */
- #include "Window.h" /* header for Window module */
-
- #define BUFLEN 40
-
- static int Open_Read_File (char *FileName); /* internal function */
- static int Open_Save_File (char *FileName); /* internal function */
-
- FILE *FilePointer; /* pointer to file */
-
- extern int Save_File (char *FileName, /* name of the file */
- const struct timeval *TotalDuration, /* pointer to piece's duration */
- const int *ScaleLength, /* ptr to num of notes in scale*/
- const int Scale[], /* musical scale */
- const int *voices, /* pointer to number of voices */
- const int *tempo, /* pointer to pulses/second */
- const PARAMETER *Pitch, /* PARAMETER struct: Pitch Form*/
- const PARAMETER *Thickness, /* PARAMETER struct: Texture Form*/
- const PARAMETER *Dynamics, /* PARAMETER struct:Dynamics Form*/
- const PARAMETER *Duration, /* PARAMETER struct: Rhythm Form*/
- const NOTEEVENT *Events, /* List of note events */
- unsigned int MinNoteLen,
- unsigned int MaxNoteLen) /* min & max note length */
- {
- int Status = 0; /* return status */
- register int ScaleIndex, /* Index into the scale */
- i; /* general purpose index */
-
- Status = Open_Save_File (FileName);/* open the file */
- if (Status == 1)
- {
- return Status; /* if file opened, read it in */
- }
- fprintf (FilePointer, "%4.2f\n", (double)TotalDuration->tv_secs);
- fprintf (FilePointer, "%4.2f\n", ((double)MinNoteLen) / 1000.0);
- fprintf (FilePointer, "%4.2f\n", ((double)MaxNoteLen) / 1000.0);
- fprintf (FilePointer, "%d\n", *ScaleLength); /* len of scale */
- for (ScaleIndex = 0; ScaleIndex < *ScaleLength; ScaleIndex++)
- {
- /*
- ** read in the whole scale
- */
- fprintf (FilePointer, "%d\n", Scale[ScaleIndex]);
- }
- fprintf (FilePointer, "%d\n", *voices); /* read number of voices */
- fprintf (FilePointer, "%d\n", *tempo); /* pulses per second */
-
- fprintf (FilePointer, "%4.2f\n", Pitch->CenterCycle); /* Pitch form*/
- 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);/*RhythmForm*/
- fprintf (FilePointer, "%4.2f\n", Duration->CenterPhase);
- fprintf (FilePointer, "%4.2f\n", Duration->SpreadCycle);
- fprintf (FilePointer, "%4.2f\n", Duration->SpreadPhase);
- /*
- ** Dynamics Form
- */
- 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);
- /*
- ** Thickness Form
- */
- fprintf (FilePointer, "%4.2f\n", Thickness->SpreadCycle);
- fprintf (FilePointer, "%4.2f\n", Thickness->SpreadPhase);
- /*
- ** Read in MAXVOICE Event struct array parameters:
- ** The lowest pitch, the highest pitch, the MIDI channel, whether
- ** the voice is walking or random pitch
- */
- 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;
- }
-
- extern int Read_File (char *FileName, /* File Name char array */
- struct timeval *TotalDuration, /* ptr to total piece duration */
- int *ScaleLength, /* ptr to length of scale */
- int Scale[], /* ptr to musical scale array */
- int *voices, /* ptr to number of voices (<MAXVOICE) */
- int *tempo, /* ptr to pulses per second */
- PARAMETER *Pitch, /* ptr to Pitch Form struct */
- PARAMETER *Thickness, /* ptr to Texture Form struct */
- PARAMETER *Dynamics, /* ptr to Dynamics Form struct */
- PARAMETER *Duration, /* ptr to Rhythm Form struct */
- NOTEEVENT *Events, /* ptr to array of events */
- unsigned int *MinNoteLen, /* ptr to minimum note length */
- unsigned int *MaxNoteLen) /* ptr to maximum note length */
- {
- int Status = 0; /* Status returned by some functions*/
- register int ScaleIndex, /* Index into the scale */
- i; /* general purpose counter */
- static char tempstring[BUFLEN]; /* temporary string */
- char *stringptr; /* pointer to the temp string */
- auto double Temp_Time,
- MinLen,
- MaxLen;
-
- Status = Open_Read_File (FileName);
- if (Status != 0)
- {
- return Status;
- }
- stringptr = fgets (tempstring, BUFLEN, FilePointer); /* get duration */
- Temp_Time = atof (tempstring);
- TotalDuration->tv_secs = (int)Temp_Time;
- TotalDuration->tv_micro = 0L;
-
- stringptr = fgets (tempstring, BUFLEN, FilePointer); /* min note len*/
- MinLen = atof (tempstring);
- *MinNoteLen = (int)(MinLen * 1000.0);
-
- stringptr = fgets (tempstring, BUFLEN, FilePointer);
- MaxLen = atof (tempstring);
- *MaxNoteLen = (int)(MaxLen * 1000.0); /* get maximum note length */
-
- fscanf (FilePointer, "%d", ScaleLength); /* get scale length */
- for (ScaleIndex = 0; ScaleIndex < (*ScaleLength); ScaleIndex++)
- {
- /* read in the whole scale (<120 notes ) */
- fscanf (FilePointer, "%d", &Scale[ScaleIndex]);
- }
- fscanf (FilePointer, "%d", voices); /* get num of voices */
- fscanf (FilePointer, "%d", tempo); /* get pulse/second */
-
- stringptr = fgets (tempstring, BUFLEN, FilePointer); /*get junk*/
-
- /* Get the form structure parameter values */
- /*
- ** Pitch Form
- */
- 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);
- /*
- ** Rhythm Form
- */
- 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);
- /*
- ** Dynamics Form
- */
- 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);
- /*
- ** Texture Form
- */
- stringptr = fgets (tempstring, BUFLEN, FilePointer);
- Thickness->SpreadCycle = atof (tempstring);
- stringptr = fgets (tempstring, BUFLEN, FilePointer);
- Thickness->SpreadPhase = atof (tempstring);
-
- /*
- ** get per-note values for lowest note, highest note, MIDI
- ** channel, and whether it's a walking voice
- */
- 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));
- if (feof (FilePointer))
- {
- break;
- }
- }
- for (i; i < MAXVOICE; i++)
- {
- Events[i].LowPitch = 12;
- Events[i].HighPitch = 97;
- Events[i].Walking = FALSE;
- Events[i].Channel = 0;
- }
- fclose (FilePointer);
- return 0;
- }
-
- static int Open_Read_File (char *FileName)
- {
- char *mode = "r"; /* read mode */
- if ((FilePointer = fopen (FileName, mode)) == NULL)
- {
- return 1;
- }
- return 0;
- }
-
- static int Open_Save_File (char *FileName)
- {
- char *mode = "w"; /* write mode */
-
- if ((FilePointer = fopen (FileName, mode)) == NULL)
- {
- return 1;
- }
- return 0;
- }
-