-
Notifications
You must be signed in to change notification settings - Fork 3
/
composer-app.c
253 lines (240 loc) · 7.41 KB
/
composer-app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <dos.h>
#define MAX 1000
void line(int x)
{
int i;
for(i=1;i<=x;i++)
printf("-");
printf("\n");
}
void cleanBuffer() //to clean upto newline charcter in buffer
{
int c;
while((c=getchar())!='\n' && c!=EOF);
}
int main(void)
{
//Data elements-------------------------------------------------------------------
char c,cont='y',*notes,input[MAX];
int i,j,option;
float oct4freq[7]={261.626,293.665,329.628,349.228,391.995,440.000,493.883};
float oct4Freq[5]={277.183,311.127,369.994,415.305,466.164};
int octSeq[7]={4,100,0,1,100,2,3};
char notesCollection[][MAX]={
"ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc", //Twinkle
"ddedgF ddedag dddbgFe ccbgag", //H Bday-1
"ccdcfe ccdcgf cccafed AAafgff ccdcfe ccdcgf cccafed AAafgff", //H Bday-2
"begFebaF egFDfb begFebdCc GcbAAge gbgbgcbA FgbAAbb gbgbgdCc GcbAAge", //HP Theme
"eFGFeeFF eeFGFeeFC CeFGeeFF eFGFeFC FGbGFeF CeFGFeFC CCCCeFGCeFF eFGFeFC CCeeFFGG eFGFeFC" //Shape of You
};
int numNotesColl=sizeof(notesCollection)/(sizeof(char)*MAX);
//----------------------------------------------------------------------------------
while(cont=='y')
{
system("cls");
line(70);
printf("\t\tWELCOME to <<< FREQUENCY COMPOSER >>>\n\nIt is used to compose Sound by only using Changes in Frequency!!!\n");
line(70);
printf("\nChoose what would you like to enjoy:\n1.-> Create a Sound Show on basis of your Input\n2.-> Listen to some Pre-existing Music\n3.-> Compose Music using Musical Notes\n\n");
printf("Input the Option No.: ");
gets(input);
option=atoi(input); //Validating integer input
printf("\n");
if(option==1)
{
int *num=malloc(1*sizeof(int)),size;
i=0,j=1; //For capturing two digit no.s out of the input string - i to move in num array, j to toggle b/w 1st & 2nd digit
printf("\nInput a string made of Digits only:\n(Longer the string, longer will be the Sound Show)\n");
while((c=getchar())!='\n')
{
if(c<'0' || c>'9')
{
printf("Only Digits are allowed! Input has terminated with 1st occurance of Non-Digit character.\n");
cleanBuffer();
break;
}
if(j) //1st Digit
{
num=realloc(num,(1+i)*sizeof(int));
num[i]=(c-'0')*10;
}
else //2nd Digit
{
num[i]+=(c-'0');
i++;
}
j=(j+1)%2; //toggle j (1 or 0)
}
//Determining size of array
if(j)
size=i;
else
size=i+1;
if(size) //Validating that num array (of 2 digit no.s) is not empty
{
int mode,f1,f2,rLow,rHigh;
printf("\nThe Frequencies will now be calculated by extracting 2 digits numbers \nfrom the input string!\n\nMeanwhile, select one from the available modes:\n");
printf("1.-> Customized Range\n2.-> Low Frequency (0-1,000)\n3.-> High Frequency(12,000-18,000)\nANY NO.-> General\n\n");
printf("Input the Mode No.: ");
gets(input);
mode=atoi(input); //Validating integer input
switch(mode)
{
case 1:
printf("\nInput max. value (must be between 100 and 20,000): ");
gets(input);
rHigh=atoi(input); //highest range
if(rHigh>=20000 || rHigh<=100) //When invalid input
{
printf("Invalid Input! Taking it =20,000\n");
rHigh=20000;
}
printf("\nInput min. value (must be between 0 and (max. value - 100)): ");
gets(input);
rLow=atoi(input); //lowest range
if(rLow<=0 || rLow>=rHigh-100) //When invalid input
{
printf("Invalid Input! Taking it =0\n");
rLow=0;
sleep(2);
}
break;
case 2:
rLow=0;
rHigh=1000;
break;
case 3:
rLow=12000;
rHigh=18000;
break;
default:
rLow=0;
rHigh=18000; //>18,000 (upto 20,000) are very feeble
break;
}
system("cls");
f1=(rHigh-rLow)/100; //Factor1 = Difference in ranges scaled down to 100
f2=rLow; //Factor2 = The equivalent of zero in new scale
line(70);
printf("GET READY FOR THE SOUND SHOW CREATED BY YOU!...\n");
line(70);
printf("\n");
sleep(2);
for(i=0;i<size;i++)
{
int barMax=num[i]*0.70; //Scaling 0-100 to 0-70 (Max. characters that can be printed in a line = ~75)
int freq=num[i]*f1+f2; //Scaling 0-100 to rLow-rHigh Hz
printf("%5d Hz ",freq);
for(j=1;j<=barMax;j++) //Drawing bar graph
printf("%c",220); //ASCII-220 is bottom half BLOCK (Extended ASCII char)
Beep(freq,800); //Play the beep of coressponding frequency
printf("\n");
}
printf("\n\nNow, How do your ears feel?\t...LOL :P\n");
}
}
else if(option==2 || option==3) //Both handle musical notes
{
if(option==2)
{
int noteNum;
printf("Choose one of these:\n1.-> Twinkle Twinkle\n2.-> Happy Birthday-1\n3.-> Happy Birthday-2\n4.-> Harry Potter Theme\n5.-> Shape of You\n\n");
printf("Input the Option No.: ");
gets(input);
noteNum=atoi(input);
if(noteNum<=0 || noteNum>numNotesColl)
{
printf("Invalid input! Taking it =1\n");
noteNum=1;
sleep(2);
}
notes=¬esCollection[noteNum-1][0];
}
else
{
char noteStr[MAX];
printf("Let\'s Guide you first:\nIn Music- A,B,C,D,E,F,G are Normal Notes and A#,C#,#D,#F,#G are Sharp Notes\nEach note has an Octave No. from 1 to 7\nHere Octave No. 4 will be used\n\n");
printf("Input Example-\nTo represent\t\t\t\tInput in this way\nD D E D G F#\nD D E D A G\nD D D B G F# E ----------------> ddedgF ddedag dddbgFe ccbgag\nC C B G A G\n\n");
printf("Input the Notes to Compose Music:\n");
gets(noteStr);
while(1) //Validating Input
{
for(i=0;noteStr[i]!='\0';i++)
{
char z=noteStr[i];
if(!(z>='a' && z<='g') && !(z>='A' && z<='G' && z!='B' && z!='E') && z!=' ')
{
printf("\nInvalid Input! \'%c\' is not allowed\n",z);
break;
}
}
if(i==strlen(noteStr))
break;
printf("Input the Notes again:\n");
gets(noteStr);
}
notes=¬eStr[0];
}
int size;
float *m=malloc(sizeof(float));
i=0;
while((c=notes[i])!='\0') //Converting notes into frequencies & storing into array m
{
m=realloc(m,(1+i)*sizeof(float));
if(c>='a' && c<='g') //Fetching frequency value corresponding to normal notes
m[i]=oct4freq[(c-'a'+5)%7];
else if(c>='A' && c<='G') //Fetching frequency value corresponding to sharp notes
m[i]=oct4Freq[octSeq[c-'A']]; //octSeq is array for sharp notes' indices
else if(c==' ')
m[i]=0; //Storing 0 freq for pauses (time-gaps in music)
i++;
}
size=i;
system("cls");
line(70);
printf("GET READY TO ENJOY THE MUSIC!...\n");
line(70);
printf("\n");
sleep(2);
char noteLtr[3]; //String for Note Letter
for(i=0;i<size;i++)
{
int barMax=m[i]*0.07; //Scaling 0-1000 to 0-70 (Coz Notes Octave-3 or 4's Frequency range 200-800)
//Preparing noteLtr string-------------
if(notes[i]>='a' && notes[i]<='g')
{
noteLtr[0]=notes[i]-('a'-'A');
noteLtr[1]='\0';
}
else if(notes[i]>='A' && notes[i]<='G')
{
noteLtr[0]=notes[i];
noteLtr[1]='#';
noteLtr[2]='\0';
}
else if(notes[i]==' ')
strcpy(noteLtr," ");
printf("%2s ",noteLtr);
for(j=1;j<=barMax;j++) //Drawing bar graph
printf("%c",220);
Beep(m[i],450); //Play Beep
printf("\n");
}
printf("\n\nThat's how we can play with Numbers!\nUnlike original music, this music was composed by the changes \nonly in frequency values!\n");
}
else
printf("Invalid Input!\n");
line(70);
printf("Wanna continue the Fun? - (y/n): "); //Re-directing to the 1st menu
cont=getchar();
cleanBuffer();
}
line(70);
printf("Thank You!\nBye-Bye!!!\n"); //Terminating the program
line(70);
return 0;
}