Next: lorenz.c: For solving and Up: Lorenz Attractor Source Code Previous: music.c: For producing chaotic

# music2.c: For producing chaotic music with note durations

This program is an attempt at producing music with not only pitches but non-arbitrary note durations. Its operation is the same as music.c, except for the addition of a note duration handler. The durations come from any of the system's variables, and are quantized down to four binarily weighted levels corresponding to eighth, quarter, half and whole note durations.

```/*
* Lorenz attractor chaotic music generator
* \$Id\$
* Mike Andrews
*
*/

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

#define DATASIZE  100                /* Number of data points in output  */
#define TIMESTEP  0.0001             /* Numerical timestep for dt        */
#define XSCALE    0.65               /* Appropriate scaling factors for  */
#define YSCALE    0.65               /*   each axis.                     */
#define ZSCALE    0.65
#define TSCALE    20                 /* Scaling factor for the time axis */
#define QUANTIZE  3                  /* Quantization interval for the
time axis */

void main()
{
double x1, y1, z1, x, y, z, t, dt, dx, dy, dz, initial;
int    xdat[DATASIZE], ydat[DATASIZE], zdat[DATASIZE];
int    dxdat[DATASIZE], dydat[DATASIZE], dzdat[DATASIZE];
int    qindex=0, hindex=0, eindex=0, windex=0;
int    duration[DATASIZE], melody[DATASIZE], i=0;
int    tp=0, tpo=-1, stuff[4];
long int tmp=0;

FILE  *song;

song=fopen("output", "w");

printf("\nEnter initial conditions (x1=y1=z1=t= ?):");
scanf("%lf", &initial);
x1=(y1=(z1=(t=initial)));              /* Set initial conditions */
melody[i]=(duration[i]=0);

dt=TIMESTEP;

while(tp<((DATASIZE*QUANTIZE)-1))
{
x=x1 + ( -10.0*x1  + 10.0*y1    ) * dt;
y=y1 + ( 28.0*x1   - y1 - x1*z1 ) * dt;
z=z1 + ( -(8/3)*z1 + x1*y1      ) * dt;
t=t  +                              dt;
dx= -10.0*x  + 10.0*y;
dy=  28.0*x  - y      - x*z;
dz= -(8/3)*z + x*y;

tp=(int)floor(TSCALE*t);

x1=x;
y1=y;
z1=z;

if ((!(tp%QUANTIZE))&&(tp!=tpo))
{
tpo=tp;
xdat[i]=(int)floor(XSCALE*x);
ydat[i]=(int)floor(YSCALE*y);
zdat[i]=(int)floor(ZSCALE*z);
dxdat[i]= (int)floor(dx);
dydat[i]= (int)floor(dy);
dzdat[i]= (int)floor(dz);
i++;
}
}

for (i=0; i<DATASIZE; i++)
{
melody[i]=xdat[i];
duration[i]=1;
}

eindex=0;
qindex=1;
hindex=2;
windex=3;

for (i=0; i<4; i++) stuff[i]=0;   /* Stylistically, set
probabilities in this order:
eighth, quarter, half, whole
*/
for (i=0; i<DATASIZE; i++) stuff[duration[i]]++;
tmp=0;
for (i=0; i<4; i++)
if (stuff[i]>stuff[tmp]) tmp=i;
eindex=tmp;
stuff[tmp]=-1;
tmp=0;
for (i=0; i<4; i++)
if (stuff[i]>stuff[tmp]) tmp=i;
qindex=tmp;
stuff[tmp]=-1;
tmp=0;
for (i=0; i<4; i++)
if (stuff[i]>stuff[tmp]) tmp=i;
hindex=tmp;
stuff[tmp]=-1;
tmp=0;
for (i=0; i<4; i++)
if (stuff[i]>stuff[tmp]) tmp=i;
windex=tmp;
tmp=0;

for (i=0; i<DATASIZE; i++)
{
if (duration[i]==eindex) duration[i]=1;
else if (duration[i]==qindex) duration[i]=2;
else if (duration[i]==hindex) duration[i]=4;
else if (duration[i]==windex) duration[i]=8;
fprintf(song, "%d %d\n", tmp, melody[i]);
tmp+=duration[i];
}

fclose(song);
}```

Mike Andrews
Wed Oct 23 01:18:29 EDT 1996