-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioCollage.java
More file actions
146 lines (109 loc) · 4.6 KB
/
Copy pathAudioCollage.java
File metadata and controls
146 lines (109 loc) · 4.6 KB
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
public class AudioCollage {
// Returns a new array that rescales a[] by a multiplicative factor of alpha.
public static double[] amplify(double[] a, double alpha) {
//StdAudio.read("C:\\Users\\hemal\\IdeaProjects\\AudioCollage\\src\\beatbox.wav");
double c[] = new double[a.length];
for (int i = 0; i < a.length; i++) {
c[i] = a[i] * alpha;
//StdAudio.play(c);
}
//System.out.println("returning array");
return c;
}
// Returns a new array that is the reverse of a[].
public static double[] reverse(double[] a) {
double[] reverseArray = new double[a.length];
int j = a.length;
for (int i = 0; i < a.length; i++) {
reverseArray[j - 1] = a[i];
j--;
}
//StdAudio.play(reverseArray);
return reverseArray;
}
// Returns a new array that is the concatenation of a[] and b[].
public static double[] merge(double[] a, double[] b) {
double[] mergedArray = new double[a.length + b.length];
for (int i = 0; i < a.length; i++) {
mergedArray[i] = a[i];
}
//System.out.println("i = " + i);
for (int j = 0; j < b.length; j++) {
mergedArray[j + a.length] = b[j];
}
return mergedArray;
}
// Returns a new array that is the sum of a[] and b[],
// padding the shorter arrays with trailing 0s if necessary.
public static double[] mix(double[] a, double[] b) {
int mArrayValue;
double[] mixedArray;
if (a.length > b.length) {
mArrayValue = a.length;
mixedArray = new double[a.length];
for (int i = 0; i < b.length; i++) {
mixedArray[i] = a[i] + b[i];
}
for (int j = 0; j < (a.length - b.length); j++) {
mixedArray[j + b.length] = a[j + b.length];
}
} else {
mArrayValue = b.length;
mixedArray = new double[b.length];
for (int i = 0; i < a.length; i++) {
mixedArray[i] = a[i] + b[i];
}
for (int j = 0; j < (b.length - a.length); j++) {
mixedArray[j + a.length] = b[j + a.length];
}
}
return mixedArray;
}
// Returns a new array that changes the speed by the given factor.
public static double[] changeSpeed(double[] a, double alpha) {
int length = (int)(a.length * alpha);
double[] spedUp = new double[length];
for (int i = 0; i < length; i++) {
double x = (i / alpha);
spedUp[i] = a[(int)x];
}
return spedUp;
}
// Creates an audio collage and plays it on standard audio.
// See below for the requirements.
public static void main (String[]args) {
int SAMPLE_RATE = StdAudio.SAMPLE_RATE;
String wavFile1 = "C:\\Users\\hemal\\IdeaProjects\\AudioCollage\\src\\beatbox.wav";
String wavFile2 = "C:\\Users\\hemal\\IdeaProjects\\AudioCollage\\src\\chimes.wav";
String wavFile3 = "C:\\Users\\hemal\\IdeaProjects\\AudioCollage\\src\\cow.wav";
String wavFile4 = "C:\\Users\\hemal\\IdeaProjects\\AudioCollage\\src\\harp.wav";
String wavFile5 = "C:\\Users\\hemal\\IdeaProjects\\AudioCollage\\src\\piano.wav";
double alpha = 0.5;
//double values1[] = new double[SAMPLE_RATE + 1];
double values1[];
double values2[];
double values3[];
double values4[];
double values5[];
//System.out.println("SAMPLE_RATE = " + SAMPLE_RATE);
values1 = StdAudio.read(wavFile1);
values2 = StdAudio.read(wavFile2);
values3 = StdAudio.read(wavFile3);
values4 = StdAudio.read(wavFile4);
values5 = StdAudio.read(wavFile5);
/*for (int i = 0; i <= SAMPLE_RATE; i++) {
//StdAudio.play(0.5 * Math.sin(2*Math.PI * freq * i / StdAudio.SAMPLE_RATE));
//System.out.println("i = " + i);
values = StdAudio.read(wavFile1);
}*/
//System.out.println("calling play");
//StdAudio.play(amplify(values, alpha));
//StdAudio.play(reverse(values1));
StdAudio.play(changeSpeed(values5, alpha));
StdAudio.play(mix(values2, values3));
StdAudio.play(amplify(values1, alpha));
StdAudio.play(reverse(values2));
StdAudio.play(merge(values1, values4));
StdAudio.play(changeSpeed(values3, alpha));
}
}