Advertisement
ArtisOracle

Untitled

Oct 8th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************** MIDIFreqDif ********************************************************
  2.  *  MIDIFreqDif                                                                                         *
  3.  *  ====================                                                                                *
  4.  *                                                                                                      *
  5.  *  By: Beau Wright                                                                                     *
  6.  *                                                                                                      *
  7.  *  Description:                                                                                        *
  8.  *  ------                                                                                              *
  9.  *  This program takes the input of two MIDI note numbers between 0 and 127 and computes the            *
  10.  *  frequency difference between them. It can also output a Csound file with the audible sweep between  *
  11.  *  the two frequencies.                                                                                *
  12.  *                                                                                                      *
  13.  ********************************************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include <string.h>
  19.  
  20. int main()
  21. {
  22.     int note1,note2, answer;
  23.     double f1,f2,dif,exponent1,exponent2;
  24.     char csdFileName[256];
  25.    
  26.     /*Takes in values for MIDI notes and checks to make sure
  27.      they are valid (between 0 and 127). It will ask the user again for valid numbers
  28.      if they are not in this range.*/
  29.     note1 = acceptAndValidateInput(1);
  30.     note2 = acceptAndValidateInput(2);
  31.     f1 = calculateFrequency(note1);
  32.     f2 = calculateFrequency(note2);
  33.    
  34.     /*dif (difference) is first entered note's frequency (f1)
  35.      subtracted from the second entered note's frequency (f2).*/
  36.     dif = f1 - f2;
  37.     if (dif < 0)
  38.     {
  39.         dif *= -1;
  40.     }
  41.     printf("\nThe difference in frequency is %f Hz\n", dif);
  42.    
  43.     /*Now asking the user to save a Csound file or exit*/
  44.     printf("\nEnter 1 to save as a Csound file, enter 2 to exit ");
  45.     scanf("%d", &answer);
  46.    
  47.     switch (answer)
  48.     {
  49.         case 1:
  50.             printToFile(f1, f2);
  51.         default:
  52.             break;
  53.     }
  54.     return 0;
  55. }
  56.  
  57. /**
  58.  * Takes in values for MIDI notes and checks to make sure
  59.  * they are valid (between 0 and 127). It will ask the user again for valid numbers.
  60.  * If valid, this will return the note entered.
  61.  */
  62. int acceptAndValidateInput(unsigned int which)
  63. {
  64.     int note;
  65.     while (1)
  66.     {
  67.         printf("Please enter MIDI note #%u (0-127): ", which);
  68.         scanf("%d", &note);
  69.         if (note < 0 && note > 127)
  70.             printf("The value entered was invalid. Try again.\n");
  71.        
  72.         else
  73.             return note;
  74.     }
  75. }
  76.  
  77. /**
  78.  * Calculates a note's frequency using the formula to calculate frequency from MIDI notes.
  79.  */
  80. double calculateFrequency(int note)
  81. {
  82.     double frequency, exponent;
  83.    
  84.     /*Here is the formula for calculating frequency from MIDI notes. First we find the exponent.*/
  85.     exponent = (note - 69)/12.0;
  86.    
  87.     // Now, calculate the actual frequency
  88.     frequency = pow(2, exponent) * 440;
  89.    
  90.     return frequency;
  91. }
  92.  
  93. /**
  94.  * Prints to a Csound file and replaces the proper frequency variables with the input
  95.  */
  96. void printToFile(double f1, double f2)
  97. {
  98.     /*Case 1 prints to a Csound file and replaces the proper frequency variables with the input from above*/
  99.     printf("\nEnter a name for your new csd file (excluding \".csd\"): ");
  100.     scanf("%s", csdFileName);
  101.     strcat(csdFileName, ".csd");
  102.     FILE *csdFilePointer = fopen(csdFileName, "w");
  103.     if(csdFilePointer)
  104.     {
  105.         fprintf(csdFilePointer, "\n<CsoundSynthesizer>\n");
  106.         fprintf(csdFilePointer, "\n<CsInstruments>\n");
  107.         fprintf(csdFilePointer, "\nsr       =       44100");
  108.         fprintf(csdFilePointer, "\nkr       =       4410\n");
  109.         fprintf(csdFilePointer, "\nksmps    =       10\n");
  110.         fprintf(csdFilePointer, "nchnls =       1\n");
  111.         fprintf(csdFilePointer, "\ninstr 1\n");
  112.         fprintf(csdFilePointer, "\nkgliss linseg p5, p3, p6");
  113.         fprintf(csdFilePointer, "\nkamp linseg, 0, p3/2, p4, p3/2, 0\n");
  114.         fprintf(csdFilePointer, "\na1   oscil   kamp, kgliss,   1\n");
  115.         fprintf(csdFilePointer, "\nout   a1\n");
  116.         fprintf(csdFilePointer, "\nendin\n");
  117.         fprintf(csdFilePointer, "\n</CsInstruments>\n");
  118.         fprintf(csdFilePointer, "\n<CsScore>\n");
  119.         fprintf(csdFilePointer, "\nf1   0   4096  10   1 .8 .7 .6 .5 .4 .3\n");
  120.         fprintf(csdFilePointer, "\n     ;amp    ;freq1    ;freq2\n");
  121.         fprintf(csdFilePointer, "\ni1 0 6   10000    %f      %f\n",f1, f2);
  122.         fprintf(csdFilePointer, "\n</CsScore>\n");
  123.         fprintf(csdFilePointer, "\n</CsoundSynthesizer>\n");
  124.     }
  125.     fclose(csdFilePointer);
  126.     printf("\nThe Csound file has been saved to the same directory as this program\n");
  127.     else
  128.     {
  129.         printf("Writing to file failed. :(\n");
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement