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 / DrawForm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  8.1 KB  |  252 lines

  1. /* DrawForm.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-2518
  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. **    DrawForm.c contains functions for drawing the form settings as
  25. **    a graphic representation with labels.
  26. **
  27. **  AUTHORS: Thomas E. Janzen
  28. **
  29. **  CREATION DATE:    26-MAR-1990
  30. **
  31. **  MODIFICATION HISTORY:
  32. **    DATE    NAME    DESCRIPTION
  33. **-- 12 May 91 TEJ Make a vertical line to mark time in Draw_Time()
  34. **  4 Jan 92 TEJ  last changes for 2.0
  35. */
  36.  
  37. #include <stdlib.h>
  38. #include <intuition/intuition.h>
  39. #include <graphics/text.h>
  40. #include <proto/exec.h>
  41. #include <proto/graphics.h>
  42. #include <proto/intuition.h>
  43. #include <math.h>
  44. #include <string.h>
  45. #include <devices/timer.h>
  46.  
  47. #include "Window.h"
  48. #include "AlgoRhythms.h"
  49.  
  50. /* Lables for graph */
  51. const static char PitchLabelString[16]    = "Pitch",
  52.                   RhythmLabelString[16]   = "Rhythm",
  53.                   DynamicsLabelString[16] = "Dynamics",
  54.                   TextureLabelString[16]  = "Texture";
  55. static struct  IntuiText PitchLabelTxt = 
  56.     {1, 1, JAM1, 5, 12, &font_choice, PitchLabelString, NULL},
  57.                RhythmLabelTxt = 
  58.     {1, 1, JAM1, 5, 56, NULL, RhythmLabelString, &PitchLabelTxt},
  59.                DynamicsLabelTxt = 
  60.     {1, 1, JAM1, 5, 100, NULL, DynamicsLabelString, &RhythmLabelTxt},
  61.                TextureLabelTxt = 
  62.     {1, 1, JAM1, 5, 144, NULL, TextureLabelString, &DynamicsLabelTxt};
  63.  
  64. const static char WorkingLabelString[16]  = "Working";
  65. static struct IntuiText WorkingLabelTxt = 
  66.     {2, 1, JAM2, 15, 10, &font_choice, WorkingLabelString, NULL};
  67.  
  68. static int last_inttime = 0;
  69.  
  70. void DrawForm( const struct timeval *Duration,
  71.                 const PARAMETER *PitchForm, 
  72.                 const PARAMETER *RhythmForm, 
  73.                 const PARAMETER *DynamicsForm,
  74.                 const PARAMETER *TextureForm)
  75. /* 
  76. ** DrawForm gives a graphic rendering of the Form of the piece of music.
  77. ** The inputs are the parameter form structures and the duration of
  78. ** the piece.
  79. */
  80. {
  81.     static int  Xlim, 
  82.                Ylim;                    /* Window limits in pixels */
  83.     int intLength = 631,             /* initialized horiz pixel length */
  84.        inttime4,
  85.        PitchStep,
  86.        RhythmStep,
  87.        DynStep,
  88.        TextureStep;
  89.    register int      inttime =  0,    /* horizontal pixel counter */
  90.                       Step = 22;     /* height of screen / 8 */ 
  91.     register float curtime = 0.0;   /* current time found from  */
  92.                                          /* inttime */
  93.     float    HeightRatio = 12.0,
  94.              timeratio,                 /* ratio of pixel to seconds */
  95.              Spreadfixed, 
  96.             Mean, 
  97.             Top, 
  98.              phase,
  99.             TwoPi;
  100.     SHORT    *PitchArray,
  101.             *RhythmArray,
  102.             *DynArray,
  103.             *TextureArray;
  104.    
  105.     Xlim = w->GZZWidth;
  106.     Ylim = w->GZZHeight;
  107.  
  108.     PrintIText (rp, &WorkingLabelTxt, 1, 1);
  109.  
  110.     intLength = Xlim;        /* Length of screen */
  111.     Step = Ylim / 8;        /* vertical increments on screen */
  112.    PitchStep = Step;
  113.    RhythmStep = Step * 3;
  114.    DynStep = Step * 5;
  115.    TextureStep = Step * 7;
  116.  
  117.     timeratio = ((double)intLength) / (double)Duration->tv_secs;
  118.     HeightRatio = 12 * Ylim / 200;
  119.     
  120.    PitchArray   = (SHORT *) malloc (intLength * 4 * sizeof(SHORT));
  121.    RhythmArray  = (SHORT *) malloc (intLength * 4 * sizeof(SHORT));
  122.    DynArray     = (SHORT *) malloc (intLength * 4 * sizeof(SHORT));
  123.    TextureArray = (SHORT *) malloc (intLength * 4 * sizeof(SHORT));
  124.    
  125.    TwoPi = 2.0 * PI;
  126.    
  127.     for (inttime = 0; inttime < intLength; inttime++) /* draw parameters*/
  128.     {
  129.         curtime = (double)inttime / timeratio;
  130.         /* calculate real time*/
  131.         /* from horz pixel */
  132.       inttime4 = inttime * 4;
  133.  
  134.         phase = TwoPi * curtime;    /* find phase */
  135.         /* Find range */
  136.         Spreadfixed    = (1 + sin (phase / (float)PitchForm->SpreadCycle
  137.                      + (float)PitchForm->SpreadPhase)) / 4.0;
  138.         /* find mean */
  139.         Mean  = sin (phase / (float)PitchForm->CenterCycle
  140.             + (float)PitchForm->CenterPhase);
  141.       PitchArray[inttime4 + 2] = PitchArray[inttime4] = inttime;
  142.       PitchArray[inttime4 + 1] = PitchStep 
  143.                                - (int)((Mean - Spreadfixed) * HeightRatio);
  144.       PitchArray[inttime4 + 3] = PitchStep 
  145.                                - (int)((Mean + Spreadfixed) * HeightRatio);
  146.  
  147.         Spreadfixed    = (1 + sin (phase / (float)RhythmForm->SpreadCycle
  148.                      + (float)RhythmForm->SpreadPhase)) / 4.0;
  149.         Mean  = sin (phase / (float)RhythmForm->CenterCycle
  150.             + (float)RhythmForm->CenterPhase);
  151.       RhythmArray[inttime4 + 2] = RhythmArray[inttime4] = inttime;
  152.       RhythmArray[inttime4 + 1] = RhythmStep 
  153.                                - (int)((Mean - Spreadfixed) * HeightRatio);
  154.       RhythmArray[inttime4 + 3] = RhythmStep 
  155.                                - (int)((Mean + Spreadfixed) * HeightRatio);
  156.  
  157.         Spreadfixed    = (1 + sin (phase / (float)DynamicsForm->SpreadCycle
  158.                      + (float)DynamicsForm->SpreadPhase)) / 4.0;
  159.         Mean  = sin (phase / (float)DynamicsForm->CenterCycle
  160.             + (float)DynamicsForm->CenterPhase);
  161.       DynArray[inttime4 + 2] = DynArray[inttime4] = inttime;
  162.       DynArray[inttime4 + 1] = DynStep 
  163.                                - (int)((Mean - Spreadfixed) * HeightRatio);
  164.       DynArray[inttime4 + 3] = DynStep 
  165.                                - (int)((Mean + Spreadfixed) * HeightRatio);
  166.         
  167.         Top   = (1 + sin (phase / (float)TextureForm->SpreadCycle
  168.             + (float)TextureForm->SpreadPhase)) / 2.0;
  169.       TextureArray[inttime4 + 2] = TextureArray[inttime4] = inttime;
  170.       TextureArray[inttime4 + 1] = TextureStep - (int)(-Top * HeightRatio);
  171.       TextureArray[inttime4 + 3] = TextureStep - (int)(Top * HeightRatio);
  172.     }
  173.    Move (rp, 0, 0);
  174.     SetDrMd (rp, JAM1);        /* Draw with foreground pen */
  175.     ClearScreen (rp);    /* erase screen */
  176.     SetAPen (rp, 4);            /* Set foreground pen to white */
  177.  
  178.    Move (rp, PitchArray[0], PitchArray[1]);
  179.    PolyDraw (rp, intLength * 2, PitchArray);
  180.  
  181.    Move (rp, RhythmArray[0], RhythmArray[1]);
  182.    PolyDraw (rp, intLength * 2, RhythmArray);
  183.  
  184.    Move (rp, DynArray[0], DynArray[1]);
  185.    PolyDraw (rp, intLength * 2, DynArray);
  186.  
  187.    Move (rp, TextureArray[0], TextureArray[1]);
  188.    PolyDraw (rp, intLength * 2, TextureArray);
  189.  
  190.     SetAPen (rp, 7);
  191.    SetDrMd (rp, COMPLEMENT);
  192.     for (inttime = 0; inttime < 4; inttime++)
  193.     {
  194.         Move (rp, 0, Step * ((2 * inttime) + 1));
  195.         Draw (rp, Xlim, Step * ((2 * inttime) + 1));
  196.     }
  197.     PitchLabelTxt.TopEdge = Step * (1 / 2);
  198.     RhythmLabelTxt.TopEdge = Step * (5 / 2);
  199.     DynamicsLabelTxt.TopEdge = Step * (9 / 2);
  200.     TextureLabelTxt.TopEdge = Step * (13 / 2);
  201.     PrintIText (rp, &TextureLabelTxt, 1, 1);
  202.    SetAPen (rp, 7);
  203.    if ((last_inttime != 0) && (last_inttime <= intLength))
  204.    {
  205.        SetDrMd (rp, COMPLEMENT);        /* Draw with foreground pen */
  206.       Move (rp, last_inttime, 0);
  207.       Draw (rp, last_inttime, Xlim);
  208.    }
  209.    free (PitchArray);
  210.    free (RhythmArray);
  211.    free (DynArray);
  212.    free (TextureArray);
  213. }
  214.  
  215. void DrawTime (const struct timeval *CurTime, 
  216.                const struct timeval *Duration)
  217. {
  218.     /* 
  219.    ** This function draws a line clock on the Pitch 
  220.     ** parameter graph while
  221.     ** the piece plays 
  222.    */
  223.     int   Xlim,
  224.          Ylim,
  225.          intMean;
  226.     register int inttime = 0;
  227.     double   timeratio,
  228.             HeightRatio = 12;
  229.     SetAPen (rp, 2);            /* Set foreground pen to white */
  230.     Xlim = w->GZZWidth - 1;
  231.     Ylim = w->GZZHeight - 1;
  232.     intMean = Ylim / 8;
  233.     timeratio = ((double)Xlim) / (double)Duration->tv_secs;
  234.     HeightRatio = (12.0 * Ylim) / 200.0;
  235.     inttime = (int)(((double)CurTime->tv_secs) * timeratio);
  236.    if (inttime == last_inttime)
  237.    {
  238.          return;
  239.    }
  240.    SetAPen (rp, 7);   
  241.    SetDrMd (rp, COMPLEMENT);        /* Draw with foreground pen */
  242.    Move (rp, last_inttime, 0);
  243.    Draw (rp, last_inttime, Xlim);
  244.    Move (rp, inttime, 0);
  245.    Draw (rp, inttime, Xlim);
  246.  
  247.    last_inttime = inttime;
  248.    return;
  249. }
  250.  
  251.  
  252.