我试图创建一个有趣的应用程序,它将获取声音文件并对其进行修改,并创建一个新的声音文件。
我要做的一件事就是让声音文件的长度增加两倍。所以我基本上创建了一个新的数组{2,2,3,3,7,7,8,8}而不是原来的{2,3,7,8}。我正在使用双打,这只是一个例子
我希望原始数组(样本)现在引用我刚刚创建的数组的开始(temp),所以当文件保存它时,现在保存临时数组。
我可以增加或减少音乐文件的音量没问题,并保存它。我省略了部分代码,因为它与此无关。
如果有人能够帮助我,我也想知道其背后的原因
public class Sound {
double[] samples;
//So we only have to declare it once. Reference to an array
public Sound() {
//This constructor should initialize the samples array to be empty
samples = new double[0];
//Initialize an array with nothing because we will be using that to reference the
//location of other arrays
}
public void wavRead(String fileName) {
samples = WavIO.read(fileName);
//Samples was an adress of an array we set to 0. Then we used WavIO to create an aray of doubles, now
//we tell samples to reference this new address over here. Samples has the addsss of the new array
}
public void wavSave(String fileName) {
WavIO.write(fileName, samples);
}
public void lengthen() {
double[] temp = new double[(samples.length *2)];
int t = 0;
for(int i = 0; i < samples.length; i++) {
//Set a variable to increase the temp array by
temp[t] = samples[i];
//Have position 0 of temp = position 0 of soundRaw
t++;
//Increase the position in the temp array by one
temp[t] = samples[i];
//Have position 1 of temp array = position 0 of soundRaw
}
samples[0] = temp;
//Here is where I try and have the samples array reference the start of another array. I tried multiple things, this is simply the last effort I tried
}
这是我用来测试代码的应用程序
公共类App {
public static void main(String[] args) {
Sound s = new Sound();
//We are now calling the other code
s.wavRead("bye8");
//If you want to mess with your own .wav file, replace this name
s.lengthen();
s.wavSave("bye8New");
}
}
答案 0 :(得分:0)
使用DoubleBuffer
,可以更轻松地完成您想要实现的目标。
要创建一个新的DoubleBuffer
,它是...... Double ...大小和重复,您将会这样做:
// Important!
origBuf.rewind();
final DoubleBuffer newBuf = DoubleBuffer.allocate(origBuf.remaining() * 2);
double value;
while (origBuf.hasRemaining()) {
value = origBuf.get();
newBuf.put(value).put(value);
}
然后newBuf.array()
将返回带有重复项的double[]
数组“。
另请注意,DoubleBuffer
与任何XBuffer
一样,允许您设置字节顺序。
答案 1 :(得分:0)
替换此代码行
samples[0] = temp;
只是
samples = temp;
就够了! :)
答案 2 :(得分:0)
只需使用samples=temp;
代替samples[0] = temp;
!