0

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(&note[2]);
    number *= pow(2, (note_value - 1));
    number /= pow(2, (1 / 12));
}
else if (note[1] == '#')
{
    note_value = atoi(&note[2]);
    number *= pow(2, (note_value - 1));
    number *= pow(2, (1 / 12));
}
else
{
    note_value = atoi(&note[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;
}

1 Answer 1

1

duration always returns 392.

4 is not the same as '4'. The latter is an ASCII character of value 52.

You don't need atoi to convert a single digit character, just subtract '0' like you probably subtracted 'A' or its ASCII value 65 in caesar and vigenere.

BTW, 1/12 is 0 (integer division). If instead at least one of the two is a floating point (like 1/12.0), it's a floating point division.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .