Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Helper functions for music
- #include <ctype.h>
- #include <cs50.h>
- #include <string.h>
- #include "helpers.h"
- #include <stdlib.h>
- #include <math.h>
- // Converts a fraction formatted as X/Y to eighths
- int duration(string fraction)
- {
- // The 1st number
- int first = ((int)fraction[0] - 48);
- // The 2nd number
- int second = ((int)fraction[2] - 48);
- //get the answer and return it!
- int answer = (first * 8)/second;
- return answer;
- }
- // Calculates frequency (in Hz) of a note
- int frequency(string note)
- {
- int diff = ((int)note[1] - 48) - 4;
- //int diff2 = ((int)note[2] - 48) - 4;
- int pow1/*, noteadd*/;
- //if accidental #
- if(note[1] == '#')
- pow1 = pow(2, 1/12);
- // if accidental b
- if(note[1] == 'b')
- pow1 = pow(2, -1/12);
- //if key : A
- if(note[0] == 'A')
- {
- //If it not is an accidental
- if(note[1] != 'b' && note[1] != '#')
- return round(440 * pow(2, diff));
- //If it is an accidental
- else
- return round(440 * pow(2, diff) * pow1);
- }
- else if(note[0] == 'B')
- {
- //If it not is an accidental
- if(note[1] != 'b' && note[1] != '#')
- return round(440 * pow(2, diff + 2));
- //If it is an accidental
- if(note[1] == 'b' || note[1] == '#')
- return round(440 * pow(2, diff + 2) * pow1);
- }
- else if(note[0] == 'G')
- {
- //If it not is an accidental
- if(note[1] != 'b' && note[1] != '#')
- return round(440 * pow(2, diff - 2));
- //If it is an accidental
- if(note[1] == 'b' || note[1] == '#')
- return round(440 * pow(2, diff - 2) * pow1);
- }
- else if(note[0] == 'F')
- {
- //If it not is an accidental
- if(note[1] != 'b' && note[1] != '#')
- return round(440 * pow(2, diff - 4));
- //If it is an accidental
- if(note[1] == 'b' || note[1] == '#')
- return round(440 * pow(2, diff - 4) * pow1);
- }
- else if(note[0] == 'E')
- {
- //If it not is an accidental
- if(note[1] != 'b' && note[1] != '#')
- return round(440 * pow(2, diff - 6));
- //If it is an accidental
- if(note[1] == 'b' || note[1] == '#')
- return round(440 * pow(2, diff - 6) * pow1);
- }
- else if(note[0] == 'D')
- {
- //If it not is an accidental
- if(note[1] != 'b' && note[1] != '#')
- return round(440 * pow(2, diff - 8));
- //If it is an accidental
- if(note[1] == 'b' || note[1] == '#')
- return round(440 * pow(2, diff - 8) * pow1);
- }
- else if(note[0] == 'C')
- {
- //If it not is an accidental
- if(note[1] != 'b' && note[1] != '#')
- return round(440 * pow(2, diff - 10));
- //If it is an accidental
- if(note[1] == 'b' || note[1] == '#')
- return round(440 * pow(2, diff - 10) * pow1);
- }/*char values[5] = {'C', 'D', 'E', 'F', 'G'};
- for(int a = 0; a < 5; a++)
- {
- if(note[0] == values[a])
- noteadd = 2 * (7 - ((int)values[a] - (int)"A"));
- }
- //If it is not an accidental
- if(note[1] != 'b' && note[1] != '#')
- return round(440 * pow(2, diff2) * pow(2, noteadd/12));
- //If it is an accidental
- if(note[1] == 'b'|| note[1] == '#')
- return round(440 * pow(2, diff2) * pow(2, noteadd/12) * pow1);*/
- return 0;
- }
- // Determines whether a string represents a rest
- bool is_rest(string s)
- {
- //Is it a rest?
- if(strcmp(s,"") == 0)
- {
- //It's a rest!
- return true;
- }
- else
- {
- //Nope, not a rest..
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement