Skip to content

Instantly share code, notes, and snippets.

@changtimwu
Created December 22, 2010 07:49
Show Gist options
  • Select an option

  • Save changtimwu/751243 to your computer and use it in GitHub Desktop.

Select an option

Save changtimwu/751243 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
static const char *major_tune_strs[2][12]=
{
{"C", "Db", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"},
{"C", "C#", "D", "D#", "E", "F", "Gb", "G", "G#", "A", "A#", "B"},
};
static const char *minor_tune_strs[2][12] =
{
{"Am", "Am#", "Bm", "Cm", "Cm#", "Dm", "Dm#", "Em", "Fm", "Fm#", "Gm", "Gm#"},
{"Am", "Bmb", "Bm", "Cm", "Dmb", "Dm", "Emb", "Em", "Fm", "Gmb", "Gm", "Amb"}
};
static int tunestr2idx( const char *str)
{
int i,j;
const char **alltunes[4];
alltunes[0]= major_tune_strs[0];
alltunes[1] = major_tune_strs[1];
alltunes[2] = minor_tune_strs[0];
alltunes[3] = minor_tune_strs[1];
for ( j=0; j<4; j++) {
const char **tunestr = alltunes[j];
for ( i=0; i<12; i++) {
if ( strcmp( tunestr[i], str)==0)
return i;
}
}
return -1;
}
int tune_subtract( const char *comptune, const char *basetune)
{
int bt, ct;
bt = tunestr2idx( basetune);
ct = tunestr2idx( comptune);
if ( bt==-1 || ct==-1)
return -999;
return ct-bt;
}
int main ( int argc, char *argv[])
{
const char *basetune, *comptune, *cp;
int compd, pitchd;
basetune = argv[2];
comptune = argv[1];
switch (comptune[0]) {
case '+':
cp = comptune +1;
compd=12;
break;
case '-':
cp = comptune + 1;
compd=-12;
default:
cp = comptune;
compd=0;
}
pitchd = tune_subtract( cp, basetune) + compd;
printf("comptune(%s) - basetune(%s) = %d\n", comptune, basetune, pitchd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment