So to start off, I'm going to include my code at the end of the question. Basically, I've built this function a few times now and I'm kind of at my wits end here.
My code uses a for loop to iterate through an array with all the notes in it. If it finds a match to the inputted note, it executes the proper equations to spit out a proper frequency. I've tested this several times and it works.....outside of helpers.c. Once I add it there i run into the control may reach end of non-void function error. The only problem is that when i add a return statement to fix this issue it seems to stop my for loop from looping all together meaning i get nothing unless I enter A4 into it. If someone could give this a look and give me some advice I'd be very appreciative I'm so close to finishing this I can taste it.
p.s. the big issue I'm running into is with trying to test with ./synthesize test.wav like I said I can get 440 as my output when I put in A4 but it seems like with every other note the program just disregards the for loop and moves on giving me nothing. I think it's because of my return statement but i need that to even compile the thing.
int frequency(string note) {
const string NOTES[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
string f;
string n = note;
int c;
char d;
int e = 0;
int freq = 0;
int counter = 0;
for (int i = 0; i >= counter && i < 12; i++)
{
f = NOTES[i];
d = f[0];
int a = d;
int b = n[0];
c = n[1] - '0';
int str = strlen(n);
counter++;
if ( b == 65 && c == 4 )
{
counter++;
freq = 440;
return freq;
}
else if ( str == 2 && a == b)
{
e = (i - 9);
freq = pow(2, ((double)e)/12.0)*440;
switch(c)
{
case 0:
freq = freq/16;
counter++;
return freq;
case 1:
counter++;
freq = freq/8;
return freq;
case 2:
counter++;
freq = freq/4;
return freq;
case 3:
counter++;
freq = freq/2;
return freq;
case 4:
counter++;
return freq;
case 5:
counter++;
freq = freq*2;
return freq;
case 6:
counter++;
freq = freq*4;
return freq;
case 7:
counter++;
freq = freq*8;
return freq;
case 8:
counter++;
freq = freq * 16;
return freq;
}
}
else if ( a == b && str == 3)
{
int oct = n[2] - '0';
switch(c)
{
case -13:
e = ( i - 8);
freq = pow(2, ((double)e)/12.0)*440;
break;
case 50:
e = ( i - 10);
freq = pow(2, ((double)e)/12.0)*440;
break;
}
switch(oct)
{
case 0:
freq = freq/16;
counter++;
return freq;
case 1:
counter++;
freq = freq/8;
return freq;
case 2:
counter++;
freq = freq/4;
return freq;
case 3:
counter++;
freq = freq/2;
return freq;
case 4:
counter++;
return freq;
case 5:
counter++;
freq = freq*2;
return freq;
case 6:
counter++;
freq = freq*4;
return freq;
case 7:
counter++;
freq = freq*8;
return freq;
case 8:
counter++;
freq = freq * 16;
return freq;
}
}
}
return 0;
}
p.p.s please don't be rude about my sloppy code thanks.