Advertisement
ArtisOracle

Untitled

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