0

here's the code: // Helper functions for music

#include <cs50.h>
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
#include "helpers.h"
#include <math.h>
#include<ctype.h>

// Converts a fraction formatted as X/Y to eighths
int duration(string fraction)
{
    int numerator;
    int denominator;
    int duration;
    numerator = fraction[0];
    denominator = fraction[2];

    if(denominator==8)
    {
        duration = numerator / 1;       //the "duration" represents the 
number of eighths, each eighth is a musical representation
    }                                   //of a period of time.
    else if(denominator!=8)
    {
        duration = 8 / denominator;
    }
    return 0;
    }

// Calculates frequency (in Hz) of a note
int frequency(string note)
{
    int octave = 0;             
    int n = 0;                  
    char noteAndAcc[2];
    noteAndAcc[0] = note[0];
    if(note[1]=='#' || note[1]=='b')
    {
        noteAndAcc[1] = note[1];
        octave = atoi(&note[2]);
    }
    else
    {
        octave = atoi(&note[1]);
    }

    switch(noteAndAcc[0])        //default n values are at octave = 4
    {
        case 'B':
        n = 2;
        break;
        case 'A':
        n = 0;
        break;
        case 'G':
        n = -2;
        break;
        case 'F':
        n = -4;
        break;
        case 'E':
        n = -5;
        break;
        case 'D':
        n = -7;
        break;
        case 'C':
        n = -9;
        break;
    }

    if(noteAndAcc[1]=='#')
    {
        n = n + 1;
    }

    if(noteAndAcc[1]=='b')
    {
        n = n - 1;
    }

    if(octave == 5)
    {
        n = n + 12;
    }
    else if(octave == 6)
    {
        n = n + 24;
    }
    else if(octave == 7)
    {
        n = n + 36;
    }
    else if(octave == 8)
    {
        n = n + 48;
    }
    else if(octave == 3)
    {
        n = n - 12;
    }
    else if(octave == 2)
    {
        n = n - 24;
    }
    else if(octave == 1)
    {
        n = n - 36;
    }
    else if(octave == 0)
    {
        n = n - 48;
    }
    else if(octave ==4)
    {
        n = n + 0;
    }

    float freqWithDecimals = 0.00;
    int frequency = 0;
    freqWithDecimals = pow(2, (n/12.0))*440;
    frequency = round(freqWithDecimals);
    return 0;
}

// Determines whether a string represents a rest
bool is_rest(string s)
{
   if(s[0]=='\0')
    {
       return true;
    }
    else
    {
       return false;
    }
    return 0;
}

and here's what i get in the terminal after compiling both notes and synthesize using "make", and attempting to synthesize jeopardy.wav from jeopardy.txt :

~/workspace/pset3/music/ $ make
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 - 
O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow -o notes 
helpers.c notes.c wav.c -lcrypt -lcs50 -lm
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 - 
O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow -o 
synthesize helpers.c synthesize.c wav.c -lcrypt -lcs50 -lm
~/workspace/pset3/music/ $ ./synthesize jeopardy.wav < 
songs/jeopardy.txt
Segmentation fault
~/workspace/pset3/music/ $ 

As you can see, i'm getting a segmentation fault. Is something wrong with my is_rest function? Looking forward to your help. Thank you.

1 Answer 1

0

./synthesize will give a segmentation fault if duration is 0. (It comes from the song_close() function in wav.c). This duration function always returns 0. That goes for frequency too (which makes for a very boring song :).

Another problem in duration is you forgot to turn the char that is fraction[x] into a number (which will also surely provoke the seg fault).

3
  • So, I did this to convert the chars that are fraction[x] into numbers: numerator = fraction[0] - 48; denominator = fraction[2] - 48; I get a jeopardy wav file now, that is 8 seconds long, but has no sound when i play it. Given these things, I would think my duration can't be 0 now. It couldn't, right? Or am I missing something?
    – John
    Commented Aug 22, 2018 at 18:34
  • just to clarify, i am no longer getting a segmentation fault, after i adjusted is_rest to: bool is_rest(string s) { if(strcmp(s,"\n")) { return true; } else { return false; } return 0; } however, i'm now at the stage of getting this 8 seconds-long jeopardy wav file, and i can play it, but it has no sound. is this a problem because i'm using a macbook pro?
    – John
    Commented Aug 22, 2018 at 18:46
  • strcmp does not return a boolean, it returns an int. So your is_rest is always returning true, so it's not processing any notes. And btw, once you fix it, it will never be true because get_string never includes the delimiter/terminator. The "old" version (s[0] == '\0') should work. I do not think the macbook is implicated at all. I think the problem is the return values from duration and frequency. It will fail (seg fault) again after you "fix" is_rest. Time to break out debug50, dude. Commented Aug 22, 2018 at 19:55

You must log in to answer this question.

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