0

I am doing the pset3 (music). However, my code appears to be very buggy. Although it compiles just fine, the frequency outputs are all 440Hz. Also, it seems like it can't read the duration either. I am not sure what's the problem with the code as I am completely new to programming. Thanks!

**UPDATED CODE enter image description here enter image description here enter image description here enter image description here enter image description here

1
  1. Your problem is not with the duration, but with is_rest. The string does not come with a '\r' or '\n', you just have to test for the empty string (which starts with '\0', the null terminator, as it has length zero)
  2. 1/12 is 0 (integer division truncates the result to result in an integer again). 1.0/12 or 1/12.0 would do the division in floating point.
  3. You truncate 440*power by assigning it to an integer, but you should round it before any truncation.
  4. Personal opinion: The result of log2 is a floating-point number. By assigning it to an integer, you are truncating the number (discarding the fractional part). Yeah, the denominator is always a power of two, but still. What keeps you from just writing numerator * 8 / denominator? * and / have same precedence, and are evaluated left to right.
  5. Also personal opinion: The translation letter->frequency looks a bit complex, though it again seems as if it could work, I guess letters C..G might be off by an octave. I used a lookup array that would tell me how many semitones a given letter was away from that octave's A (using note[0]-65 as the index), this made my code much shorter and especially more uniform (no different cases to consider, as I had considered them in creating the lookup array).
10
  • Thanks, but I realize that the frequency outputs are still incorrect. They are either 440 or 441. After making a test code in another file, I realized the problem is that pow(2/12) or any other powers all equal to 1.0000 in my code. Thus, the output is always 440. How can I solve this inaccuracy problem? I am using double for pow(2/12). Jun 6 '18 at 1:15
  • I have solved the previous problem by adding .0 to all the numbers, but I now have another problem with running './synthesize test.wav'. There's a 'runtime error: load of null pointer of type 'char''. This error refers to my code of 'int numerator = (int)fraction[0] - 48;'. What does it mean? Jun 6 '18 at 1:37
  • Probably related to point 1 of my answer. Not detecting a rest would result in NULL being passed for a string (thanks to strtok).
    – Blauelf
    Jun 6 '18 at 4:12
  • But I have already changed it to detecting for \0 instead? Why does the problem still persist? Jun 6 '18 at 9:59
  • Could you maybe post your current code for that part?
    – Blauelf
    Jun 6 '18 at 12:37

You must log in to answer this question.

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