Whenever I attempt to synthesize a new .wav file, the notes come out extremely long (over 1:30 each when played in windows media player) and my .wav file is over 200 MB when inputting the notes to play Happy Birthday.
The program initially worked the first few times I ran it; however, despite not changing anything in my code, it can no longer synthesize and play the .wav files correctly. I've been using new file names each time I test to avoid cached previous attempts.
I figure it must be an error in my duration function, but I'm not sure why the synthesizing worked the first few times if there was, and I don't see anything wrong with it.
Thanks for any help!
// Helper functions for music
#include <cs50.h>
#include <string.h>
#include <math.h>
#include "helpers.h"
// Converts a fraction formatted as X/Y to eighths
int duration(string fraction)
{
int number1;
int num = fraction[0];
int den = fraction[2];
if (den == 8)
{
number1 = num;
}
else if (den == 4)
{
number1 = num * 2;
}
else if (den == 2)
{
number1 = num * 4;
}
else
{
number1 = num * 8;
}
return number1;
}
// Calculates frequency (in Hz) of a note
int frequency(string note)
{
double number;
int note_value;
if (note[0] == 'A')
{
number = 55;
}
else if (note[0] == 'B')
{
number = 61.735;
}
else if (note[0] == 'C')
{
number = 32.703;
}
else if (note[0] == 'D')
{
number = 36.708;
}
else if (note[0] == 'E')
{
number = 41.203;
}
else if (note[0] == 'F')
{
number = 43.654;
}
else
{
number = 48.999;
}
if (note[1] == 'b')
{
note_value = atoi(¬e[2]);
number *= pow(2, (note_value - 1));
number /= pow(2, (1 / 12));
}
else if (note[1] == '#')
{
note_value = atoi(¬e[2]);
number *= pow(2, (note_value - 1));
number *= pow(2, (1 / 12));
}
else
{
note_value = atoi(¬e[1]);
number *= pow(2, (note_value - 1));
}
return number;
}
// Determines whether a string represents a rest
bool is_rest(string s)
{
if (s[0] == '\0')
{
return true;
}
return false;
}