/* C program to generate score for "A Nother Quarter" */
/* Copyright 2009 John ffitch                         */
/* Licenced under any of BSD, LGPL or Creative        */
/*                Commons Share/Atrrib                */

#include <stdio.h>
#include <stdlib.h>

/* A global amplitude scale */
#define AMP (1.8)

/* Output a simple note */
void outnoise(double start, double dur, double vol,
              double pitch, double attack, double span, double epan)
{
    printf("i1 %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f\n",
           start, dur, AMP*vol, pitch, pitch, attack, span, epan);
}

/* Output a more complex note */
void outnoise1(double start, double dur, double vol,
               double pitch1, double pitch2, double attack, double span, double epan)
{
    printf("i1 %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f\n",
           start, dur, AMP*vol, pitch1, pitch2, attack, span, epan);
}

/* Scalled random number generator */
double myrand(double r)
{
    return (double)(random())*r/(double)RAND_MAX;
}


int main(void)
{
    double tt = 0.0;
    double pitch = 333.0/2.0;   /* Base pitch */
    double dur = 2.0*60.0;      /* Total length of each section -- + extras */
    int NUMH = (int)(22050.0/pitch); /* Number of harminics */
    int i;
    double max= 0.0;

    while (tt<=dur) {           /* Until the end.... */
      double ave = 0.0;
      double mm = -0.0;
      for (i=2; i<NUMH; i++) {
        double t = myrand(10.0); /* delay */
        double d = myrand((double)i/6.0); /* duration */
        ave += t;
        outnoise(tt+t, d, myrand(10000.0/(double)i), i*pitch,
                 d*0.5, myrand(1.0), myrand(1.0));
        t = myrand(10.0); /* delay */
        d = myrand(6.0); /* duration */
        ave += t;
        outnoise(tt+t, d, myrand(10000.0/(double)i), i*pitch,
                 d*0.717, myrand(0.75), myrand(0.25));
        if (tt+t+d>max) max = tt+t+d;
      }
      tt += ave/NUMH;
    }
    outnoise(0.0, max+5.0, 10000.0, pitch, dur*0.717,
             0.5, 0.5); /* Main note */
    /* Now second section -- like first with glissando */
    printf("b 135\n");
    tt = 0.0;
    max = 0.0;
    while (tt<=dur) {
      double ave = 0.0;
      for (i=2; i<NUMH; i++) {
        double t = myrand(9.0); /* delay */
        double d = myrand((double)i/5.5); /* duration */
        double p = (double)i*pitch;
        double pp = myrand(20.0);
        ave += t;
        outnoise(tt+t, d, myrand(18000.0/(double)i), p-10.0+pp,
                 d*0.5, myrand(1.0), myrand(1.0));
        t = myrand(15); /* delay */
        d = myrand(12.0); /* duration */
        ave += t;
        pp = myrand(100.0);
        outnoise1(tt+t, d, myrand(17000.0/(double)i), p-50.0+pp, p-50.0+myrand(100.0),
                 d*0.717, myrand(0.75), myrand(0.25));
        if (tt+t+d>max) max = tt+t+d;
      }
      tt += ave/NUMH;
    }
    outnoise(0.0, max+6.0, 18000.0, pitch, max, 0.5, 0.5); /* Main note */
    printf("e\n");              /* and we are done */
    return 0;
}
