Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************** MIDIFreqDif ********************************************************
- * MIDIFreqDif *
- * ==================== *
- * *
- * By: Beau Wright *
- * *
- * Description: *
- * ------ *
- * This program takes the input of two MIDI note numbers between 0 and 127 and computes the *
- * frequency difference between them. It can also output a Csound file with the audible sweep between *
- * the two frequencies. *
- * *
- ********************************************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- int main()
- {
- int note1,note2, answer;
- double f1,f2,dif,exponent1,exponent2;
- char csdFileName[256];
- /*Takes in values for MIDI notes and checks to make sure
- they are valid (between 0 and 127). It will ask the user again for valid numbers
- if they are not in this range.*/
- note1 = acceptAndValidateInput(1);
- note2 = acceptAndValidateInput(2);
- f1 = calculateFrequency(note1);
- f2 = calculateFrequency(note2);
- /*dif (difference) is first entered note's frequency (f1)
- subtracted from the second entered note's frequency (f2).*/
- dif = f1 - f2;
- if (dif < 0)
- {
- dif *= -1;
- }
- printf("\nThe difference in frequency is %f Hz\n", dif);
- /*Now asking the user to save a Csound file or exit*/
- printf("\nEnter 1 to save as a Csound file, enter 2 to exit ");
- scanf("%d", &answer);
- switch (answer)
- {
- case 1:
- printToFile(f1, f2);
- default:
- break;
- }
- return 0;
- }
- /**
- * Takes in values for MIDI notes and checks to make sure
- * they are valid (between 0 and 127). It will ask the user again for valid numbers.
- * If valid, this will return the note entered.
- */
- int acceptAndValidateInput(unsigned int which)
- {
- int note;
- while (1)
- {
- printf("Please enter MIDI note #%u (0-127): ", which);
- scanf("%d", ¬e);
- if (note < 0 && note > 127)
- printf("The value entered was invalid. Try again.\n");
- else
- return note;
- }
- }
- /**
- * Calculates a note's frequency using the formula to calculate frequency from MIDI notes.
- */
- double calculateFrequency(int note)
- {
- double frequency, exponent;
- /*Here is the formula for calculating frequency from MIDI notes. First we find the exponent.*/
- exponent = (note - 69)/12.0;
- // Now, calculate the actual frequency
- frequency = pow(2, exponent) * 440;
- return frequency;
- }
- /**
- * Prints to a Csound file and replaces the proper frequency variables with the input
- */
- void printToFile(double f1, double f2)
- {
- /*Case 1 prints to a Csound file and replaces the proper frequency variables with the input from above*/
- printf("\nEnter a name for your new csd file (excluding \".csd\"): ");
- scanf("%s", csdFileName);
- strcat(csdFileName, ".csd");
- FILE *csdFilePointer = fopen(csdFileName, "w");
- if(csdFilePointer)
- {
- fprintf(csdFilePointer, "\n<CsoundSynthesizer>\n");
- fprintf(csdFilePointer, "\n<CsInstruments>\n");
- fprintf(csdFilePointer, "\nsr = 44100");
- fprintf(csdFilePointer, "\nkr = 4410\n");
- fprintf(csdFilePointer, "\nksmps = 10\n");
- fprintf(csdFilePointer, "nchnls = 1\n");
- fprintf(csdFilePointer, "\ninstr 1\n");
- fprintf(csdFilePointer, "\nkgliss linseg p5, p3, p6");
- fprintf(csdFilePointer, "\nkamp linseg, 0, p3/2, p4, p3/2, 0\n");
- fprintf(csdFilePointer, "\na1 oscil kamp, kgliss, 1\n");
- fprintf(csdFilePointer, "\nout a1\n");
- fprintf(csdFilePointer, "\nendin\n");
- fprintf(csdFilePointer, "\n</CsInstruments>\n");
- fprintf(csdFilePointer, "\n<CsScore>\n");
- fprintf(csdFilePointer, "\nf1 0 4096 10 1 .8 .7 .6 .5 .4 .3\n");
- fprintf(csdFilePointer, "\n ;amp ;freq1 ;freq2\n");
- fprintf(csdFilePointer, "\ni1 0 6 10000 %f %f\n",f1, f2);
- fprintf(csdFilePointer, "\n</CsScore>\n");
- fprintf(csdFilePointer, "\n</CsoundSynthesizer>\n");
- }
- fclose(csdFilePointer);
- printf("\nThe Csound file has been saved to the same directory as this program\n");
- else
- {
- printf("Writing to file failed. :(\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement