我有一个很大的wav文件,我想进入较小的块。我还有一个.cue文件,它具有帧速率长度,较小的块应该是。我想出了如何分割wav,但所有的wav文件都是相同的声音。似乎每当我创建一个新的wav时,大的wav文件从头开始并使新波的长度正确但声音相同。
我想我需要一种方法来读取特定帧的wav,然后写入文件,然后继续读取并写入另一个文件等...
我已经在这几个小时了,似乎无法弄明白。任何帮助将不胜感激。这是我的代码,所有注释的东西都是我错误的代码,我一直在尝试。
int count2 = 0;
int totalFramesRead = 0;
//cap contains the how many wav's are to be made
//counter contains the vector position.
String wavFile1 = "C:\\Users\\DC3\\Desktop\\wav&text\\testwav.wav";
//String wavFile2 = "C:\\Users\\DC3\\Desktop\\waver\\Battlefield.wav";
while(count2 != counter){
try {
AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
int bytesPerFrame = clip1.getFormat().getFrameSize();
//System.out.println(bytesPerFrame);
// int numBytes = safeLongToInt(clip1.getFrameLength()) * bytesPerFrame;
// byte[] audioBytes = new byte[numBytes];
// int numBytesRead = 0;
// int numFramesRead = 0;
// // Try to read numBytes bytes from the file.
// while ((numBytesRead =
// clip1.read(audioBytes)) != -1) {
// // Calculate the number of frames actually read.
// clip1.read(audioBytes)
// numFramesRead = numBytesRead / bytesPerFrame;
// totalFramesRead += numFramesRead;
// System.out.println(totalFramesRead);
// }
long lengthofclip = Integer.parseInt(time.get(count2))- silence;
globallength = clip1.getFrameLength();
AudioInputStream appendedFiles = new AudioInputStream(clip1, clip1.getFormat(), lengthofclip);
//long test = (appendedFiles.getFrameLength() *24 *2)/8;
//int aaaaa = safeLongToInt(test);
//appendedFiles.mark(aaaaa);
AudioSystem.write(appendedFiles,
AudioFileFormat.Type.WAVE,
new File("C:\\Users\\DC3\\Desktop\\wav&text\\" + name.get(count2)));
count2++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static int safeLongToInt(long l) {
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw new IllegalArgumentException
(l + " cannot be cast to int without changing its value.");
}
return (int) l;
}
答案 0 :(得分:2)
乍一看只是一个想法,但我假设这条线路出了问题:
AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
把它放在你的while循环之外,这样就不会在每个循环中重新创建它。像这样:
//...
String wavFile1 = "C:\\Users\\DC3\\Desktop\\wav&text\\testwav.wav";
AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
int bytesPerFrame = clip1.getFormat().getFrameSize();
while(count2 != counter){
try {
//...
这也假设你的算法是正确的,我不会浪费时间思考,因为你没有问这个问题:-D