【发布时间】:2014-05-20 13:55:07
【问题描述】:
我有一个读取样本数组的声音类。它使用 WavIO 类来执行此操作。如果WavIO.read() 返回null,我希望它什么也不做。我该怎么办?不确定它是 set 还是 get 方法或两者兼而有之。另外,我遇到了这两个错误:
symptom: java.lang.ArrayIndexOutOfBoundsException: 7
at Sound.shorten(Sound.java:69)
symptom: java.lang.ArrayIndexOutOfBoundsException: 3
at Sound.lengthen(Sound.java:51)
public class Sound {
private String fileName;
private double [] samples;
public Sound(){
this.samples = new double[0];
}
public Sound(Sound pSound){
double[] temp;
temp = new double[pSound.samples.length];
for(int i = 0; i < temp.length; i++){
temp[i] = pSound.samples[i];
}
this.samples = temp;
}
public double[] get(){
return samples;
}
public void increaseVol(double percent){
// percent = percent/100.0;
for(int i = 0; i<samples.length;i++){
samples[i] = samples[i] * (1.0 + percent);
}
}
public void reduceVol(double percent){
//percent = percent/100.0;
for(int i = 0; i<samples.length;i++){
samples[i] = samples[i] * (1.0 - percent);
}
}
public void lengthen(){
double[]t = new double[samples.length];
for(int i = 0;i<samples.length;i++){
t[i] = samples[i];
}
samples = new double[t.length*2];
for(int i = 0; i < samples.length;i++){
samples[(2*i)] = t[i];
samples[(2*i)+1] = t[i];
}
}
public void shorten(){
double[]t = new double[samples.length];
for(int i = 0; i < samples.length;i++){
t[i] = samples[i];
}
samples = new double[t.length/2];
if(samples.length % 2 == 0){
for(int i = 0; i <samples.length;i++){
samples[i] = t[2*i];
}
}
else{
for(int i = 0; i <samples.length+1;i++){
samples[i] = t[(2*i)+1];
}
}
}
public void reverse(){
for(int i = 0; i<samples.length; i++){
samples[i] = samples[(samples.length)-1-i];
}
}
public void set(double[] mySamples){
if (mySamples == null) {
throw new IllegalArgumentException("null");
} else {
samples = new double[mySamples.length];
for (int i = 0; i < mySamples.length; i++) {
samples[i] = mySamples[i];
}
}
}
public void wavRead(String fileName){
this.samples = WavIO.read(fileName);
}
public void wavSave(String fileName){
WavIO.write(fileName,samples);
}
}
【问题讨论】:
-
请用 cmets 标记这两行
标签: java arrays exception methods wav