//getchordinfo -- opens and retrieves chord data file. Info retrieved:
// the chord root, whether its a flat or sharp, the chord type
// (Major, minor, diminished, augmented, dominant) and whether
// the chord is a triad or seventh (this not yet implemented).
//-----------------------------------------------------------------------

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

int Major[3] = { 4, 3, 4};
int minor[3] = { 3, 4, 3};
int dim[3] = { 3, 3, 3};
int dom[3] = { 4, 3, 3};

int aug[2] = { 4, 4};

struct chord curchord[MAXCHORDS];

double getchordinfo(float *p, short n_args, double *pp){

FILE *f;
char *filename;
char temp[7];
int i, num = 0, decext;

//this is the "weird" way of grabbing the string from input
i = (int) pp[0];
filename = (char *) i;

//open file with chord progressions and durations
f = fopen(filename, "r");
if( f == NULL )

fprintf(stderr, "Cannot find %s...not opened.\n", filename);

else

printf("Datafile %s opened\n", filename);

//count number of chords in file
while( fscanf(f, "%s", temp) != EOF){

//check the pitch......
switch(temp[0]){

case 'C':

curchord[num].root = 712;
break;

case 'D':

curchord[num].root = 802;
break;

case 'E':

curchord[num].root = 804;
break;

case 'F':

curchord[num].root = 805;
break;

case 'G':

curchord[num].root = 807;
break;

case 'A':

curchord[num].root = 809;
break;

case 'B':

curchord[num].root = 811;
break;

default:

curchord[num].root = 712;
break;

}//end switch root

//check whether b for flat or s for sharp or neither...
switch(temp[1]){

case 'b':

curchord[num].root--;
if( (curchord[num].root % 100) == 0)

curchord[num].root -= 88;

break;

case 's':


curchord[num].root++;
decext = curchord[num].root%100;
if( (decext%12) == 1)

curchord[num].root += 88;

break;


case '_':

default:

break;

}//end switch

//check for which type of chord.......

switch(temp[2]){

case 'M':

curchord[num].triadtype = Major;
break;

case 'm':

curchord[num].triadtype = minor;
break;

case 'd':

curchord[num].triadtype = dim;
break;

case 'a':

curchord[num].triadtype = aug;
break;

case '_':

default:

curchord[num].triadtype = dom;
break;

}//end switch type

//scan again for dur and increment num
fscanf(f, "%s", temp);

if(strcmp(temp, "whole") == 0)

curchord[num].dur = 4.0;

else if(strcmp(temp, "half") == 0)

curchord[num].dur = 2.0;

else if(strcmp(temp, "quarter") == 0)

curchord[num].dur = 1.0;

else if(strcmp(temp, "eigth") == 0)

curchord[num].dur = 0.5;

else

printf("Duration error\n");

//increment number of chords
num++;

}//end scanning of file

fclose(f);
return num;

}//end getchordinfo