-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusic.c
123 lines (92 loc) · 2.66 KB
/
music.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#define SAMPLE_RATE 44100.0 //read from file samples per second
#define DISPLAY_RATE 10.0
#define DISPLAY_MOD 50.0
void importFreq(double raw[], double refine[], int songLength);//FINISH_ME
void playDisplay(double refine[], int songLength);
double minFreq(double array[]);
double maxFreq(double array[]);
double avgFreq(double array[]);
void refineArray(double array[], length);
int main(void)
{
int songLength;//10*seconds
//songLength = 20 * SAMPLE_RATE;//test
scanf("%d\n", &songLength);//real
songLength = songLength / SAMPLE_RATE * DISPLAY_RATE;
double rawFreq[(int)(SAMPLE_RATE/DISPLAY_RATE)];
double refineFreq[songLength];
importFreq(rawFreq, refineFreq, songLength);
//refineArray(refineFreq);
playDisplay(refineFreq, songLength);
return 0;
}
void importFreq(double raw[], double refine[], int songLength){//FINISH_ME
srand(time(NULL));
int i, j;
double rawImport=0.0;
double max, min, average;
for(i=0;i< songLength;i++){
for(j=0;j<SAMPLE_RATE/DISPLAY_RATE;j++){//importing sound
//raw[j] = 2.0*(double)rand()/(double)RAND_MAX - 1;//test
scanf("%lf\n", &raw[j]);//real import
rawImport += fabs(raw[j]);
}
refine[i] = ((rawImport / (SAMPLE_RATE/DISPLAY_RATE)) * DISPLAY_MOD);//change to be percent based off of the average value of the song.
//find min, max, and average
rawImport =0.0;
}
printf("Frequency imported\n");//test
}
void playDisplay(double refine[], int songLength){
int i = 0, j = 0;
double t = clock()/((double)CLOCKS_PER_SEC);
while(i<songLength){
if(t <= clock()/((double)CLOCKS_PER_SEC) - 1/DISPLAY_RATE){
for(j=0;j<refine[i];j++){
printf("*");
}
printf("\n");
i++;
t = clock()/((double)CLOCKS_PER_SEC);
}
}
printf("end");//test
}
double minFreq(double array[]){
double min;
int i;
for(i=0;array[i]!= '\0';i++){
if(min > array[i]) min = array[i];
}
return min;
}
double maxFreq(double array[]){
double max;
int i;
for(i=0;array[i]!= '\0';i++){
if(max < array[i]) max = array[i];
}
return max;
}
double avgFreq(double array[]){
double avg;
int i;
for(i=0;array[i]!= '\0';i++){
avg+= array[i];
}
avg = avg/i;
return avg;
}
void refineArray(double array[], length){//FINISH_ME
double min = minFreq(array);
double max = maxFreq(array);
double difference = max - min;
int i;
for(i=0;i<length;i++){
array[i]-=min;
}
}