1

我有一个大的 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;
}
4

1 回答 1

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

于 2012-08-18T17:44:40.790 回答