Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Helper functions for music
- #include <cs50.h>
- #include <math.h>
- #include "helpers.h"
- int scaleCal(int _f, char _scale) //4+
- {
- if (_scale == '#'){
- _f = round(_f * pow(2, 1/12));
- }
- else if(_scale == 'b'){
- _f = round(_f / pow(2, 1/12));
- }
- return _f;
- }
- int letterMov(char l, int _f) //2
- {
- if(l == 'A')
- _f = 440;
- if(l == 'B')
- _f = round(_f * pow(2, 2/12));
- if(l == 'G')
- _f = round(_f / pow(2, 2/12));
- if(l == 'F')
- _f = round(_f / pow(2, 4/12));
- if(l == 'E')
- _f = round(_f / pow(2, 5/12));
- if(l == 'D')
- _f = round(_f / pow(2, 7/12));
- if(l == 'C')
- _f = round(_f / pow(2, 9/12));
- return _f;
- }
- int octaveFreq(int oct, int _f) //3
- {
- double a = oct - 4;
- if (oct > 4)
- {
- _f = _f * pow(2, a);
- }
- else if(oct < 4 )
- {
- _f = round(_f / pow(2, a));
- }
- return _f;
- }
- char octaveCheck(char a, char b) //1
- {
- if(a == '#' || a == 'b'){ //Checks for # or b;
- return b;
- }
- else{
- return a;
- }
- }
- // Converts a fraction formatted as X/Y to eighths
- int duration(string fraction)
- {
- int up = (int)fraction[0];
- int down = (int)fraction[2];
- return up * 8 / down;
- }
- // Calculates frequency (in Hz) of a note
- int frequency(string note)
- {
- int octave, f = 0; char letter, scale;
- octave = octaveCheck(note[1],note[2]);
- letter = note[0];
- scale = note[1];
- f = letterMov(letter, f);
- f = octaveFreq(octave, f);
- f = scaleCal(f, scale);
- return f;
- }
- // Determines whether a string represents a rest
- bool is_rest(string s)
- {
- if(s[0] == '\0')
- {
- return true;
- }
- else
- {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement