Advertisement
ArtisOracle

Untitled

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