home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 611.lha / AlgoRhythms_v2.0 / Source.LZH / Source / Files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  8.7 KB  |  266 lines

  1. /* Files.c
  2.    Copyright (c) 1990,1991,1992 by Thomas E. Janzen
  3.    All Rights Reserved
  4.  
  5.    THIS SOFTWARE IS FURNISHED FREE OF CHARGE FOR STUDY AND USE AND MAY
  6.    BE COPIED ONLY FOR PERSONAL USE OR COMPLETELY AS OFFERED WITH NO
  7.    CHANGES FOR FREE DISTRIBUTION.  NO TITLE TO AND OWNERSHIP OF THE
  8.    SOFTWARE IS HEREBY TRANSFERRED.  THOMAS E. JANZEN ASSUMES NO 
  9.    RESPONSBILITY FOR THE USE OR RELIABILITY OF THIS SOFTWARE.
  10.    
  11.    Thomas E. Janzen
  12.    58A School St. Apt. 2-L
  13.    Hudson, MA 01749
  14.    (508)562-1295
  15. */
  16. /*
  17. **  FACILITY:
  18. **
  19. **    AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
  20. **    compiled with SAS/C V5.10b
  21. **
  22. **  ABSTRACT:
  23. **
  24. **    Files.c handles file reading and writing.
  25. **
  26. **  AUTHORS: Thomas E. Janzen
  27. **
  28. **  CREATION DATE:    26-MAR-1990
  29. **
  30. **  MODIFICATION HISTORY:
  31. **    DATE    NAME    DESCRIPTION
  32. **  4 Jan 92 TEJ  last changes for 2.0
  33. **--
  34. */
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <math.h>
  39. #include <stdlib.h>
  40. #include <intuition/intuition.h>
  41. #include <devices/timer.h>
  42.  
  43. #include "AlgoRhythms.h"    /* header for main function */
  44. #include "Window.h"        /* header for Window module */
  45.  
  46. #define BUFLEN 40
  47.  
  48. static int Open_Read_File (char *FileName);    /* internal function */
  49. static int Open_Save_File (char *FileName);    /* internal function */
  50.  
  51. FILE *FilePointer;    /* pointer to file  */
  52.  
  53. extern int Save_File (char *FileName,        /* name of the file        */
  54.     const struct timeval *TotalDuration, /* pointer to piece's duration    */
  55.     const int *ScaleLength,           /* ptr to num of notes in scale*/
  56.     const int Scale[],              /* musical scale        */
  57.     const int *voices,              /* pointer to number of voices    */
  58.     const int *tempo,                 /* pointer to pulses/second    */
  59.     const PARAMETER *Pitch,        /* PARAMETER struct: Pitch Form*/
  60.     const PARAMETER *Thickness,    /* PARAMETER struct: Texture Form*/
  61.     const PARAMETER *Dynamics,     /* PARAMETER struct:Dynamics Form*/
  62.     const PARAMETER *Duration,       /* PARAMETER struct: Rhythm Form*/
  63.     const NOTEEVENT *Events,       /* List of note events        */
  64.     unsigned int MinNoteLen, 
  65.    unsigned int MaxNoteLen) /* min & max note length    */
  66. {
  67.     int Status = 0;                  /* return status        */
  68.     register int      ScaleIndex,    /* Index into the scale    */
  69.                       i;               /* general purpose index    */
  70.     
  71.     Status = Open_Save_File (FileName);/* open the file        */
  72.     if (Status == 1) 
  73.    {
  74.       return Status;    /* if file opened, read it in    */
  75.    }
  76.     fprintf (FilePointer, "%4.2f\n", (double)TotalDuration->tv_secs);
  77.     fprintf (FilePointer, "%4.2f\n", ((double)MinNoteLen) / 1000.0);
  78.     fprintf (FilePointer, "%4.2f\n", ((double)MaxNoteLen) / 1000.0);
  79.     fprintf (FilePointer, "%d\n", *ScaleLength);    /* len of scale */
  80.     for (ScaleIndex = 0; ScaleIndex < *ScaleLength; ScaleIndex++)
  81.     {    
  82.       /* 
  83.       ** read in the whole scale 
  84.       */
  85.         fprintf (FilePointer, "%d\n", Scale[ScaleIndex]);
  86.     }
  87.     fprintf (FilePointer, "%d\n", *voices);    /* read number of voices */
  88.     fprintf (FilePointer, "%d\n", *tempo);    /* pulses per second */
  89.  
  90.     fprintf (FilePointer, "%4.2f\n", Pitch->CenterCycle); /* Pitch form*/
  91.     fprintf (FilePointer, "%4.2f\n", Pitch->CenterPhase);
  92.     fprintf (FilePointer, "%4.2f\n", Pitch->SpreadCycle);
  93.     fprintf (FilePointer, "%4.2f\n", Pitch->SpreadPhase);
  94.  
  95.     fprintf (FilePointer, "%4.2f\n", Duration->CenterCycle);/*RhythmForm*/
  96.     fprintf (FilePointer, "%4.2f\n", Duration->CenterPhase);
  97.     fprintf (FilePointer, "%4.2f\n", Duration->SpreadCycle);
  98.     fprintf (FilePointer, "%4.2f\n", Duration->SpreadPhase);
  99.     /*
  100.    ** Dynamics Form
  101.    */
  102.     fprintf (FilePointer, "%4.2f\n", Dynamics->CenterCycle);
  103.     fprintf (FilePointer, "%4.2f\n", Dynamics->CenterPhase);
  104.     fprintf (FilePointer, "%4.2f\n", Dynamics->SpreadCycle);
  105.     fprintf (FilePointer, "%4.2f\n", Dynamics->SpreadPhase);
  106.     /*
  107.    ** Thickness Form 
  108.    */
  109.     fprintf (FilePointer, "%4.2f\n", Thickness->SpreadCycle);
  110.     fprintf (FilePointer, "%4.2f\n", Thickness->SpreadPhase);
  111.     /* 
  112.    ** Read in MAXVOICE Event struct array parameters:
  113.     ** The lowest pitch,  the highest pitch,  the MIDI channel, whether
  114.     ** the voice is walking or random pitch 
  115.    */
  116.     for (i = 0; i < MAXVOICE; i++)
  117.     {
  118.         fprintf (FilePointer, "%d  %d  %d  %d\n",
  119.         Events[i].LowPitch, Events[i].HighPitch, Events[i].Channel,
  120.         Events[i].Walking);
  121.     }
  122.     fclose (FilePointer);
  123.     return 0;
  124. }
  125.  
  126. extern int Read_File (char *FileName, /* File Name char array   */
  127.     struct timeval *TotalDuration,        /* ptr to total piece duration    */
  128.     int *ScaleLength,             /* ptr to length of scale           */
  129.     int Scale[],                 /* ptr to musical scale array        */
  130.     int *voices,                 /* ptr to number of voices (<MAXVOICE)  */
  131.     int *tempo,                    /* ptr to pulses per second        */
  132.     PARAMETER *Pitch,             /* ptr to Pitch Form struct        */
  133.     PARAMETER *Thickness,       /* ptr to Texture Form struct        */
  134.     PARAMETER *Dynamics,          /* ptr to Dynamics Form struct     */
  135.     PARAMETER *Duration,          /* ptr to Rhythm Form struct        */
  136.     NOTEEVENT *Events,   /* ptr to array of events            */
  137.     unsigned int *MinNoteLen,          /* ptr to minimum note length        */
  138.     unsigned int *MaxNoteLen)           /* ptr to maximum note length        */
  139. {
  140.    int Status = 0;              /* Status returned by some functions*/
  141.     register int ScaleIndex,   /* Index into the scale                 */
  142.              i;                     /* general purpose counter             */
  143.     static char tempstring[BUFLEN];    /* temporary string              */
  144.     char *stringptr;              /* pointer to the temp string          */
  145.    auto double Temp_Time,
  146.                MinLen,
  147.                MaxLen;
  148.    
  149.     Status = Open_Read_File (FileName);
  150.     if (Status != 0)
  151.    {
  152.       return Status;
  153.    }
  154.     stringptr = fgets (tempstring, BUFLEN, FilePointer); /* get duration */
  155.     Temp_Time = atof (tempstring);
  156.    TotalDuration->tv_secs = (int)Temp_Time;
  157.    TotalDuration->tv_micro = 0L;
  158.       
  159.     stringptr = fgets (tempstring, BUFLEN, FilePointer); /* min note len*/
  160.    MinLen = atof (tempstring);
  161.    *MinNoteLen = (int)(MinLen * 1000.0);
  162.  
  163.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  164.    MaxLen = atof (tempstring);
  165.     *MaxNoteLen = (int)(MaxLen * 1000.0);    /* get maximum note length */
  166.  
  167.     fscanf (FilePointer, "%d", ScaleLength);    /* get scale length */
  168.     for (ScaleIndex = 0; ScaleIndex < (*ScaleLength); ScaleIndex++)
  169.     {    
  170.       /* read in the whole scale (<120 notes ) */
  171.         fscanf (FilePointer, "%d", &Scale[ScaleIndex]);
  172.     }
  173.     fscanf (FilePointer, "%d", voices);  /* get num of voices */
  174.     fscanf (FilePointer, "%d", tempo);    /* get pulse/second */
  175.  
  176.     stringptr = fgets (tempstring, BUFLEN, FilePointer); /*get junk*/
  177.  
  178.     /* Get the form structure parameter values */
  179.     /* 
  180.    ** Pitch Form 
  181.    */
  182.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  183.     Pitch->CenterCycle = atof (tempstring);
  184.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  185.     Pitch->CenterPhase = atof (tempstring);
  186.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  187.     Pitch->SpreadCycle = atof (tempstring);
  188.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  189.     Pitch->SpreadPhase = atof (tempstring);
  190.     /* 
  191.    ** Rhythm Form 
  192.    */
  193.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  194.     Duration->CenterCycle = atof (tempstring);
  195.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  196.     Duration->CenterPhase = atof (tempstring);
  197.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  198.     Duration->SpreadCycle = atof (tempstring);
  199.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  200.     Duration->SpreadPhase = atof (tempstring);
  201.     /* 
  202.    ** Dynamics Form 
  203.    */
  204.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  205.     Dynamics->CenterCycle = atof (tempstring);
  206.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  207.     Dynamics->CenterPhase = atof (tempstring);
  208.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  209.     Dynamics->SpreadCycle = atof (tempstring);
  210.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  211.     Dynamics->SpreadPhase = atof (tempstring);
  212.     /* 
  213.    ** Texture Form 
  214.    */
  215.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  216.     Thickness->SpreadCycle = atof (tempstring);
  217.     stringptr = fgets (tempstring, BUFLEN, FilePointer);
  218.     Thickness->SpreadPhase = atof (tempstring);
  219.  
  220.     /* 
  221.    ** get per-note values for lowest note,  highest note,  MIDI
  222.     ** channel, and whether it's a walking voice 
  223.    */
  224.     for (i = 0; i < MAXVOICE; i++)
  225.     {
  226.         stringptr = fgets (tempstring, BUFLEN, FilePointer);
  227.         sscanf (stringptr, "%d  %d  %d  %d", 
  228.         &(Events[i].LowPitch), &(Events[i].HighPitch), 
  229.         &(Events[i].Channel), &(Events[i].Walking));
  230.       if (feof (FilePointer))
  231.       {
  232.          break;
  233.       }
  234.     }
  235.     for (i; i < MAXVOICE; i++)
  236.    {
  237.       Events[i].LowPitch = 12;
  238.       Events[i].HighPitch = 97;
  239.       Events[i].Walking = FALSE;
  240.       Events[i].Channel = 0;
  241.    }
  242.     fclose (FilePointer);
  243.     return 0;
  244. }
  245.  
  246. static int Open_Read_File (char *FileName) 
  247. {
  248.     char *mode = "r";    /* read mode */
  249.     if ((FilePointer = fopen (FileName,  mode)) == NULL) 
  250.    {
  251.         return 1;
  252.     }
  253.     return 0;
  254. }
  255.  
  256. static int Open_Save_File (char *FileName) 
  257. {
  258.     char *mode = "w";    /* write mode */
  259.  
  260.     if ((FilePointer = fopen (FileName, mode)) == NULL) 
  261.    {
  262.         return 1;
  263.     }
  264.     return 0;
  265. }
  266.