Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Helper functions for music
- #include <cs50.h>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include "helpers.h"
- // Converts a fraction formatted as X/Y to eighths
- int duration(string fraction)
- {
- if (fraction[2] == 8)
- {
- if (fraction[0] == 1)
- {
- return 1;
- }
- else if (fraction[0] == 3)
- {
- return 3;
- }
- else if (fraction[0] == 8)
- {
- return 8;
- }
- }
- else if (fraction[2] == 4)
- {
- if (fraction[0] == 1)
- {
- return 2;
- }
- }
- else if (fraction[2] == 2)
- {
- if (fraction[0] == 1)
- {
- return 4;
- }
- }
- else
- {
- return 0;
- }
- return 0;
- }
- // Calculates frequency (in Hz) of a note
- int frequency(string note)
- {
- //char Alphaofnote = note[0];
- //Detetmining the Octave NO
- int OctaveNO = note[strlen(note)-1];
- OctaveNO -= 48; // Adjust Ascii to int value
- int noteoffset = 0; int accidentaloffset = 0;
- //Calculate the Noteoffset
- switch(note[0])
- {
- case 'A' :
- noteoffset = 0;
- break;
- case 'B' :
- noteoffset = 2;
- break;
- case 'C' :
- noteoffset = -9;
- break;
- case 'D' :
- noteoffset = -7;
- break;
- case 'E' :
- noteoffset = -5;
- break;
- case 'F' :
- noteoffset = -4;
- break;
- case 'G' :
- noteoffset = -2;
- break;
- default :
- return 0;
- }
- //Adjusting the accidental offset
- if (note[1] == '#')
- {
- accidentaloffset = 1;
- }
- else if (note[1] == 'b')
- {
- accidentaloffset = -1;
- }
- else
- {
- accidentaloffset = 0;
- }
- int N = (12*(OctaveNO - 4) + noteoffset + accidentaloffset)/12; // Generalized formula to determine n
- double freq = 440.0 * (pow(2.0, N));// freequency with respect to A4 note
- int result = round(freq);
- return result;
- }
- // Determines whether a string represents a rest
- bool is_rest(string s)
- {
- if (s == NULL)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
Add Comment
Please, Sign In to add comment