-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioCollage.java
More file actions
85 lines (79 loc) · 2.95 KB
/
Copy pathAudioCollage.java
File metadata and controls
85 lines (79 loc) · 2.95 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class AudioCollage {
// Returns a new array that rescales a[] by a multiplicative factor of alpha.
public static double[] amplify(double[] a, double alpha) {
for (int i = 0; i < a.length; i++) {
a[i] = a[i] * alpha;
}
return a;
}
// Returns a new array that is the reverse of a[].
public static double[] reverse(double[] a) {
double[] x = new double[a.length];
for (int i = 0; i < a.length; i++) {
x[i] = a[a.length - 1 - i];
}
return x;
}
// Returns a new array that is the concatenation of a[] and b[].
public static double[] merge(double[] a, double[] b) {
int length_a = a.length;
int length_b = b.length;
int l = length_a + length_b;
double[] x = new double[l];
int pos = 0;
for (int i = 0; i < a.length; i++) {
x[pos++] = a[i];
}
for (int i = 0; i < b.length; i++) {
x[pos++] = b[i];
}
return x;
}
// 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 length_a = a.length;
int length_b = b.length;
int l = Math.max(length_a, length_b);
double[] aa = new double[l];
double[] bb = new double[l];
double[] x = new double[l];
for (int i = 0; i < length_a; i++) aa[i] = a[i];
for (int i = 0; i < length_b; i++) bb[i] = b[i];
for (int i = 0; i < l; i++) {
x[i] = aa[i] + bb[i];
}
return x;
}
// Returns a new array that changes the speed by the given factor.
public static double[] changeSpeed(double[] a, double alpha) {
int samples_len = (int) Math.floor(a.length / alpha);
double[] x = new double[samples_len];
for (int i = 0; i < x.length; i++) {
int f = (int) Math.floor(i * alpha);
x[i] = a[f];
}
return x;
}
// Creates an audio collage and plays it on standard audio.
public static void main(String[] args) {
String[] wav_files = {
"chimes.wav", "harp.wav", "scratch.wav", "silence.wav", "singer.wav"
};
double[] f1 = StdAudio.read(wav_files[0]);
double[] f2 = StdAudio.read(wav_files[1]);
double[] f3 = StdAudio.read(wav_files[2]);
double[] f4 = StdAudio.read(wav_files[3]);
double[] f5 = StdAudio.read(wav_files[4]);
StdAudio.play(amplify(f1, 0.7));
StdAudio.play(reverse(f5));
StdAudio.play(mix(f1, f3));
StdAudio.play(merge(f5, f4));
StdAudio.play(changeSpeed(f2, 1.25));
}
}