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.    
  25.     /*Takes in values for MIDI notes and checks to make sure
  26.      they are valid (between 0 and 127). It will ask the user again for valid numbers
  27.      if they are not in this range.*/
  28.     note1 = acceptAndValidateInput(1);
  29.     note2 = acceptAndValidateInput(2);
  30.     f1 = calculateFrequency(note1);
  31.     f2 = calculateFrequency(note2);
  32.    
  33.     /*dif (difference) is first entered note's frequency (f1)
  34.      subtracted from the second entered note's frequency (f2).*/
  35.     dif = f1 - f2;
  36.     if (dif < 0)
  37.     {
  38.         dif *= -1;
  39.     }
  40.     printf("\nThe difference in frequency is %f Hz\n", dif);
  41.    
  42.     /*Now asking the user to save a Csound file or exit*/
  43.     printf("\nEnter 1 to save as a Csound file, enter 2 to exit ");
  44.     scanf("%d", &answer);
  45.    
  46.     switch (answer)
  47.     {
  48.         case 1:
  49.             printToFile(f1, f2);
  50.         default:
  51.             break;
  52.     }
  53.     return 0;
  54. }
  55.  
  56. /**
  57.  * Takes in values for MIDI notes and checks to make sure
  58.  * they are valid (between 0 and 127). It will ask the user again for valid numbers.
  59.  * If valid, this will return the note entered.
  60.  */
  61. int acceptAndValidateInput(unsigned int which)
  62. {
  63.     int note;
  64.     while (1)
  65.     {
  66.         printf("Please enter MIDI note #%u (0-127): ", which);
  67.         scanf("%d", &note);
  68.         if (note < 0 || note > 127)
  69.             printf("The value entered was invalid. Try again.\n");
  70.        
  71.         else
  72.             return note;
  73.     }
  74. }
  75.  
  76. /**
  77.  * Calculates a note's frequency using the formula to calculate frequency from MIDI notes.
  78.  */
  79. double calculateFrequency(int note)
  80. {
  81.     double frequency, exponent;
  82.    
  83.     /*Here is the formula for calculating frequency from MIDI notes. First we find the exponent.*/
  84.     exponent = (note - 69)/12.0;
  85.    
  86.     // Now, calculate the actual frequency
  87.     frequency = pow(2, exponent) * 440;
  88.    
  89.     return frequency;
  90. }
  91.  
  92. /**
  93.  * Prints to a Csound file and replaces the proper frequency variables with the input
  94.  */
  95. void printToFile(double f1, double f2)
  96. {
  97.     /*Case 1 prints to a Csound file and replaces the proper frequency variables with the input from above*/
  98.     char csdFileName[256];
  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