0
我的程序已完成90%的任務,但我不斷收到一個錯誤。它說:「如果WavIO.read()返回null,則不應當對當前樣本數組進行更改。」不知道要改變什麼。此外,進出口新的寫作的javadoc,什麼正確的方式來寫的構造函數/方法和一個javadoc的@params等。這裏是我的代碼:返回null什麼都不做
public class Sound
{
private String fileName;
private double [] samples;
/**
* initializes the empty Sound array.
*/
public Sound() {
this.samples = new double[0];
}
/**
* Makes copy sound object of given sound.
*/
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;
}
/**
* get the array of sound samples
* @return samples
*/
public double[] get() {
return samples;
}
/**
* increase volume of sound by given percent
* @param double how much to increase volume by
*/
public void increaseVol(double percent) {
// percent = percent/100.0;
for (int i = 0; i < samples.length; i++) {
samples[i] = samples[i] * (1.0 + percent);
}
}
/**
* reduce volume of sound by given percent
* @param double how much to reduce volume by
*/
public void reduceVol(double percent) {
//percent = percent/100.0;
for (int i = 0; i < samples.length; i++) {
samples[i] = samples[i] * (1.0 - percent);
}
}
/**
* lengthen sound duration by two
*/
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[i] = t[i/2];
}
}
/**
* shorten sound duration by half.
*/
public void shorten() {
double[]t = new double[samples.length];
if (samples.length % 2 == 0) {
t = new double[samples.length/2];
}
else {
t = new double[samples.length/2 + 1];
}
int j = 0;
for (int i = 0; i < t.length; i++) {
t[i] = samples[j];
j = j + 2;
}
samples = t;
}
/**
* reverse the sound
*/
public void reverse() {
for (int i = 0; i < samples.length/2; i++) {
double temp = samples[i];
samples[i] = samples[samples.length - 1 - i];
samples[samples.length - 1 - i] = temp;
}
}
/**
* set array of sound samples
*/
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];
}
}
}
/**
* read a Wav file of given file name
*/
public void wavRead(String fileName) {
this.samples = WavIO.read(fileName);
}
/**
* save sound sample giving it a name
*/
public void wavSave(String fileName) {
WavIO.write(fileName, samples);
}
}
謝謝這有助於很多! – user3511198