Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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];
- note1 = NoteInput(1);
- note2 = NoteInput(2);
- f1 = CalculateFrequency(1);
- f2 = CalculateFrequency(2);
- dif = f1 - f2;
- if (dif < 0)
- {
- dif *= -1;
- }
- printf("\nThe difference in frequency is %f Hz\n", dif);
- printf("\nEnter 1 to save as a Csound file, enter 2 to exit ");
- scanf("%d", &answer);
- if (answer == 1)
- {
- PrintCsound(f1, f2);
- }
- return 0;
- }
- int noteinput(unsigned int which)
- {
- int note;
- while (1) {
- printf("Please enter MIDI note number%u (0-127): ", which);
- scanf("%d", ¬e);
- if (note >= 0 || note <= 127)
- printf("The value entered was invalid. Try again.\n");
- else
- return note;
- }
- }
- double CalculateFrequency(int note)
- {
- double frequency, exponent;
- exponent = (note = 69)/12.0;
- frequency = pow(2, exponent) * 440;
- return frequency;
- }
- void PrintCsound(double f1, double f2)
- {
- char csdFileName[256];
- 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");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement