-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound.java
More file actions
104 lines (89 loc) · 2.42 KB
/
Sound.java
File metadata and controls
104 lines (89 loc) · 2.42 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
import java.awt.Color;
import java.time.*;
import java.text.*;
import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.file.*;
import sound.*;
import image.*;
import java.util.Arrays;
class Sound {
//fields
private final int[] samples;
// constructor method
Sound(int[] samples){
this.samples = samples;
}
// instance methods
// getNumSamples
public int getNumSamples(){
return this.samples.length;
}
// getSample
public int getSample(int index){
return this.samples[index];
}
// getSamples
public int[] getSamples(){
return this.samples;
}
// toString
public String toString(){
int len = this.samples.length;
String samplesRef = this.samples.toString();
String address = samplesRef.substring(samplesRef.indexOf("@"));
return "Sound[samples=[" + len + "]" + address + "]";
}
// static methods
// crop
public static Sound crop(Sound original, int start, int end){
int size = (end - start);
int[] newArray = new int[size];
for(int i = 0; i < size; i+=1){
newArray[i] = original.getSample(start + i);
}
Sound newSound = new Sound(newArray);
return newSound;
}
// mix
public static Sound mix(Sound background, Sound foreground, int index){
int[] backgroundArray = background.getSamples();
int[] foregroundArray = foreground.getSamples();
int size = background.getNumSamples();
int foregroundSize = foreground.getNumSamples();
int[] mixedArray = new int[size];
for(int i = 0; i < size; i +=1){
if (i < index || i >= (index + foregroundSize)) {
mixedArray[i] = backgroundArray[i];
}
else {
mixedArray[i] = backgroundArray[i] + foregroundArray[i-index];
}
}
Sound mixedSound = new Sound(mixedArray);
return mixedSound;
}
// silence
public static Sound silence(int numSamples){
int size = numSamples;
int[] newArray = new int[size];
for(int i = 0; i < size; i +=1){
newArray[i] = 0;
}
Sound newSound = new Sound(newArray);
return newSound;
}
// scaleVolume
public static Sound scaleVolume(Sound original, double ratio){
Sound copy = new Sound(original.getSamples());
int[] samples = copy.getSamples();
int size = copy.getNumSamples();
int[] newArray = new int[size];
for(int i = 0; i < size; i +=1){
newArray[i] = (int)(samples[i]*ratio);
}
Sound newSound = new Sound(newArray);
return newSound;
}
}