SaiTejaAdda

Music helpers.c

Jan 7th, 2018
1,337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. // Helper functions for music
  2.  
  3. #include <cs50.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <string.h>
  7.  
  8. #include "helpers.h"
  9.  
  10. // Converts a fraction formatted as X/Y to eighths
  11. int duration(string fraction)
  12. {
  13.     if (fraction[2] == 8)
  14.     {
  15.         if (fraction[0] == 1)
  16.         {
  17.             return 1;
  18.  
  19.         }
  20.         else if (fraction[0] == 3)
  21.         {
  22.             return 3;
  23.  
  24.         }
  25.         else if (fraction[0] == 8)
  26.         {
  27.             return 8;
  28.  
  29.         }
  30.  
  31.     }
  32.  
  33.     else if (fraction[2] == 4)
  34.     {
  35.  
  36.     if (fraction[0] == 1)
  37.     {
  38.         return 2;
  39.     }
  40.  
  41.     }
  42.  
  43.     else if (fraction[2] == 2)
  44.     {
  45.     if (fraction[0] == 1)
  46.     {
  47.         return 4;
  48.     }
  49.  
  50.     }
  51.     else
  52.     {
  53.         return 0;
  54.     }
  55.  return 0;
  56. }
  57.  
  58. // Calculates frequency (in Hz) of a note
  59. int frequency(string note)
  60. {
  61.     //char Alphaofnote = note[0];
  62.  
  63.     //Detetmining the Octave NO
  64.     int OctaveNO = note[strlen(note)-1];
  65.     OctaveNO -= 48; // Adjust Ascii to int value
  66.  
  67.     int noteoffset = 0; int accidentaloffset = 0;
  68.  
  69.  
  70.     //Calculate the Noteoffset
  71.     switch(note[0])
  72.     {
  73.         case 'A' :
  74.         noteoffset = 0;
  75.         break;
  76.  
  77.         case 'B' :
  78.         noteoffset = 2;
  79.         break;
  80.  
  81.         case 'C' :
  82.         noteoffset = -9;
  83.         break;
  84.  
  85.         case 'D' :
  86.         noteoffset = -7;
  87.         break;
  88.  
  89.         case 'E' :
  90.         noteoffset = -5;
  91.         break;
  92.  
  93.         case 'F' :
  94.         noteoffset = -4;
  95.         break;
  96.  
  97.         case 'G' :
  98.         noteoffset = -2;
  99.         break;
  100.  
  101.         default :
  102.         return 0;
  103.     }
  104.  
  105.     //Adjusting the accidental offset
  106.     if (note[1] == '#')
  107.     {
  108.         accidentaloffset = 1;
  109.     }
  110.  
  111.     else if (note[1] == 'b')
  112.     {
  113.         accidentaloffset = -1;
  114.     }
  115.  
  116.     else
  117.     {
  118.         accidentaloffset = 0;
  119.     }
  120.  
  121. int N = (12*(OctaveNO - 4) + noteoffset  + accidentaloffset)/12; // Generalized formula to determine n
  122.     double freq = 440.0 * (pow(2.0, N));// freequency with respect to A4 note
  123.  
  124. int result = round(freq);
  125. return result;
  126.  
  127. }
  128.  
  129.  
  130. // Determines whether a string represents a rest
  131. bool is_rest(string s)
  132. {
  133.     if (s == NULL)
  134.     {
  135.         return false;
  136.     }
  137.  
  138.     else
  139.     {
  140.         return true;
  141.     }
  142. }
Add Comment
Please, Sign In to add comment