Advertisement
ArtisOracle

Untitled

Oct 8th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. int main()
  7. {
  8.     int note1,note2, answer;
  9.     double f1,f2,dif,exponent1,exponent2;
  10.     char csdFileName[256];
  11.    
  12.     note1 = NoteInput(1);
  13.     note2 = NoteInput(2);
  14.     f1 = CalculateFrequency(1);
  15.     f2 = CalculateFrequency(2);
  16.    
  17.    
  18.    
  19.     dif = f1 - f2;
  20.     if (dif < 0)
  21.     {
  22.         dif *= -1;
  23.     }
  24.     printf("\nThe difference in frequency is %f Hz\n", dif);
  25.    
  26.     printf("\nEnter 1 to save as a Csound file, enter 2 to exit ");
  27.     scanf("%d", &answer);
  28.    
  29.     if (answer == 1)
  30.     {
  31.         PrintCsound(f1, f2);
  32.     }
  33.     return 0;
  34.    
  35. }
  36.  
  37. int noteinput(unsigned int which)
  38. {
  39.     int note;
  40.     while (1) {
  41.         printf("Please enter MIDI note number%u (0-127): ", which);
  42.         scanf("%d", &note);
  43.         if (note >= 0 || note <= 127)
  44.             printf("The value entered was invalid. Try again.\n");
  45.         else
  46.             return note;
  47.     }
  48. }
  49.  
  50. double CalculateFrequency(int note)
  51. {
  52.     double frequency, exponent;
  53.    
  54.     exponent = (note = 69)/12.0;
  55.    
  56.     frequency = pow(2, exponent) * 440;
  57.    
  58.     return frequency;
  59. }
  60.  
  61. void PrintCsound(double f1, double f2)
  62. {
  63.     char csdFileName[256];
  64.     printf("\nEnter a name for your new CSD file (excluding \".csd\"): ");
  65.     scanf("%s", csdFileName);
  66.     strcat(csdFileName, ".csd");
  67.     FILE *csdFilePointer = fopen(csdFileName, "w");
  68.     if(csdFilePointer)
  69.     {
  70.         fprintf(csdFilePointer, "\n<CsoundSynthesizer>\n");
  71.         fprintf(csdFilePointer, "\n<CsInstruments>\n");
  72.         fprintf(csdFilePointer, "\nsr       =       44100");
  73.         fprintf(csdFilePointer, "\nkr       =       4410\n");
  74.         fprintf(csdFilePointer, "\nksmps    =       10\n");
  75.         fprintf(csdFilePointer, "nchnls =       1\n");
  76.         fprintf(csdFilePointer, "\ninstr 1\n");
  77.         fprintf(csdFilePointer, "\nkgliss linseg p5, p3, p6");
  78.         fprintf(csdFilePointer, "\nkamp linseg, 0, p3/2, p4, p3/2, 0\n");
  79.         fprintf(csdFilePointer, "\na1   oscil   kamp, kgliss,   1\n");
  80.         fprintf(csdFilePointer, "\nout   a1\n");
  81.         fprintf(csdFilePointer, "\nendin\n");
  82.         fprintf(csdFilePointer, "\n</CsInstruments>\n");
  83.         fprintf(csdFilePointer, "\n<CsScore>\n");
  84.         fprintf(csdFilePointer, "\nf1   0   4096  10   1 .8 .7 .6 .5 .4 .3\n");
  85.         fprintf(csdFilePointer, "\n     ;amp    ;freq1    ;freq2\n");
  86.         fprintf(csdFilePointer, "\ni1 0 6   10000    %f      %f\n",f1, f2);
  87.         fprintf(csdFilePointer, "\n</CsScore>\n");
  88.         fprintf(csdFilePointer, "\n</CsoundSynthesizer>\n");
  89.     }
  90.     fclose(csdFilePointer);
  91.     printf("\nThe Csound file has been saved to the same directory as this program\n");
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement