Advertisement
Guest User

CS50 : Music

a guest
Mar 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. // Helper functions for music
  2. #include <ctype.h>
  3. #include <cs50.h>
  4. #include <string.h>
  5. #include "helpers.h"
  6. #include <stdlib.h>
  7. #include <math.h>
  8. // Converts a fraction formatted as X/Y to eighths
  9. int duration(string fraction)
  10. {
  11.   // The 1st number
  12.   int first = ((int)fraction[0] - 48);
  13.   // The 2nd number
  14.   int second = ((int)fraction[2] - 48);
  15.   //get the answer and return it!
  16.   int answer = (first * 8)/second;
  17.   return answer;
  18. }
  19.  
  20. // Calculates frequency (in Hz) of a note
  21. int frequency(string note)
  22. {
  23.     int diff = ((int)note[1] - 48) - 4;
  24.     //int diff2 = ((int)note[2] - 48) - 4;
  25.     int pow1/*, noteadd*/;
  26.     //if accidental #
  27.     if(note[1] == '#')
  28.         pow1 = pow(2, 1/12);
  29.     // if accidental b
  30.     if(note[1] == 'b')
  31.        pow1 = pow(2, -1/12);
  32.     //if key : A
  33.  
  34.     if(note[0] == 'A')
  35.     {
  36.         //If it not is an accidental
  37.          if(note[1] != 'b' && note[1] != '#')
  38.           return round(440 * pow(2, diff));
  39.          //If it is an accidental
  40.          else
  41.           return round(440 * pow(2, diff) * pow1);
  42.     }
  43.     else if(note[0] == 'B')
  44.     {
  45.         //If it not is an accidental
  46.          if(note[1] != 'b' && note[1] != '#')
  47.           return round(440 * pow(2, diff + 2));
  48.          //If it is an accidental
  49.          if(note[1] == 'b' || note[1] == '#')
  50.           return round(440 * pow(2, diff + 2) * pow1);
  51.     }
  52.     else if(note[0] == 'G')
  53.     {
  54.         //If it not is an accidental
  55.          if(note[1] != 'b' && note[1] != '#')
  56.           return round(440 * pow(2, diff - 2));
  57.          //If it is an accidental
  58.          if(note[1] == 'b' || note[1] == '#')
  59.           return round(440 * pow(2, diff - 2) * pow1);
  60.     }
  61.     else if(note[0] == 'F')
  62.     {
  63.         //If it not is an accidental
  64.          if(note[1] != 'b' && note[1] != '#')
  65.           return round(440 * pow(2, diff - 4));
  66.          //If it is an accidental
  67.          if(note[1] == 'b' || note[1] == '#')
  68.           return round(440 * pow(2, diff - 4) * pow1);
  69.     }
  70.     else if(note[0] == 'E')
  71.     {
  72.         //If it not is an accidental
  73.          if(note[1] != 'b' && note[1] != '#')
  74.           return round(440 * pow(2, diff - 6));
  75.          //If it is an accidental
  76.          if(note[1] == 'b' || note[1] == '#')
  77.           return round(440 * pow(2, diff - 6) * pow1);
  78.     }
  79.     else if(note[0] == 'D')
  80.     {
  81.         //If it not is an accidental
  82.          if(note[1] != 'b' && note[1] != '#')
  83.           return round(440 * pow(2, diff - 8));
  84.          //If it is an accidental
  85.          if(note[1] == 'b' || note[1] == '#')
  86.           return round(440 * pow(2, diff - 8) * pow1);
  87.     }
  88.     else if(note[0] == 'C')
  89.     {
  90.         //If it not is an accidental
  91.          if(note[1] != 'b' && note[1] != '#')
  92.           return round(440 * pow(2, diff - 10));
  93.          //If it is an accidental
  94.          if(note[1] == 'b' || note[1] == '#')
  95.           return round(440 * pow(2, diff - 10) * pow1);
  96.     }/*char values[5] = {'C', 'D', 'E', 'F', 'G'};
  97.       for(int a = 0; a < 5; a++)
  98.       {
  99.        if(note[0] == values[a])
  100.         noteadd = 2 * (7 - ((int)values[a] - (int)"A"));
  101.        }
  102.         //If it is not an accidental
  103.         if(note[1] != 'b' && note[1] != '#')
  104.          return round(440 * pow(2, diff2) * pow(2, noteadd/12));
  105.          //If it is an accidental
  106.          if(note[1] == 'b'|| note[1] == '#')
  107.           return round(440 * pow(2, diff2) * pow(2, noteadd/12) * pow1);*/
  108.      return 0;
  109. }
  110.  
  111. // Determines whether a string represents a rest
  112. bool is_rest(string s)
  113. {
  114.     //Is it a rest?
  115.     if(strcmp(s,"") == 0)
  116.         {
  117.         //It's a rest!
  118.         return true;
  119.         }
  120.     else
  121.        {
  122.         //Nope, not a rest..
  123.         return false;
  124.        }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement