0

My code for Frequency is working well in the 4th Octave as well as any A note, but after that I am getting values all over. I used Debug50 to follow the math, and I've boiled it down to the adjustment I'm using for semitones within an octave (calculating the # of Octaves seems to work OK).

int A = 0, Bb = 1, B = 2, C= 3, Db = 4, D = 5, Eb = 6, E = 7, F = 8, Gb = 9, G = 10, Ab = 11;

This ^ seems to be the line that is causing trouble for me. Am I thinking about the values of those notes incorrectly?

string note = get_string("Type Note in form of XY or XYZ: ");
int len = strlen(note); 
int A = 0, Bb = 1, B = 2, C= 3, Db = 4, D = 5, Eb = 6, E = 7, F = 8, Gb = 9, G = 10, Ab = 11;
double n = 0; // define int value to use later in calc.
char pos = '\0';
// Determine length of Note & use to value Pos for switch Values
if(len == 2)  // n = 2 use the below. If n = 3, use the adding property.
{
    char octave = note[1]-48;
    n = n + ((octave-4)*12);
    pos = note[0];
}
else if (len == 3)
{
    char octave = note[2]-48;
    n = n + ((octave-4)*12);
    pos = (note[0]+note[1])-50; //Use -50 to stay within ASCII range but not overlap with other cases.
}

// Adjust value of N (semitones based on Pos - mapped to list of values for chords.
switch(pos)
{
 // Straight chords //
    case 65: //A
        n = n+A;
        break;
    case 66: //B
        n = n+B;
        break;
    case 67: //C
        n = n+C;
        break;
    case 68: //D
        n = n+D;
        break;
    case 69: //E
        n = n+E;
        break;
    case 70: //F
        n = n+F;
        break;
    case 71: //G
        n = n+G;
        break;
   //Sharps and flats //
    case 50: //A#
    case 114: //Bb
        n = n+Bb;
        break;
    case 52: //C#
    case 116: //Db
        n = n+Db;
        break;
    case 53: //D#
    case 117: //Eb
        n = n+Eb;
        break;
    case 55: //F#
    case 119: //Gb
        n = n+Gb;
        break;
    case 113: //Ab
    case 56: //G#
        n = n+Ab;
        break;
}
printf("n= %f \n", n);
double e = n/12;
printf("e= %f \n", e);
double result = pow(2, e)*440;
printf("hz = %f", result);
}

Note: I'm printing at the end just to test before putting this into the larger program.

2
  • I updated the accidental logic here - I removed the Switch Case, and used the following if len Note = 3: char acc = note[1]; if (acc == '#') { n = n+1; } else { n = n-1; } This seemed to work
    – Jake
    Jun 22 '18 at 13:26
  • I also adopted the following values for the notes based on what I saw in other folks comments: int A = 0, B = 2, C= -9, D = -7, E = -5, F = -4, G = -2; This works, but I don't fully understand why...any chance you could help explain?
    – Jake
    Jun 22 '18 at 13:27

You must log in to answer this question.

Browse other questions tagged .