Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将大Wav文件提取为较小的块_Java_Wav - Fatal编程技术网

在Java中将大Wav文件提取为较小的块

在Java中将大Wav文件提取为较小的块,java,wav,Java,Wav,我有一个大的wav文件,我想把它分成更小的块。我还有一个.cue文件,它具有帧速率长度,较小的块应该在这个长度上。我想出了如何分割wav,但所有的wav文件都是相同的声音。似乎每次我创建一个新的wav时,大的wav文件都是从一开始就开始,使新的wav具有正确的长度,但声音相同 我想我需要一种方法来读取wav到一个特定的帧,然后写入一个文件,然后继续读取和写入另一个文件,等等 这件事我已经做了好几个小时了,似乎还没弄明白。任何帮助都将不胜感激。这是我的代码,所有注释的东西都是我一直在尝试的错误代码

我有一个大的wav文件,我想把它分成更小的块。我还有一个.cue文件,它具有帧速率长度,较小的块应该在这个长度上。我想出了如何分割wav,但所有的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;
}
int count2=0;
int totalFramesRead=0;
//cap包含要制作的wav数量
//计数器包含向量位置。
String wavFile1=“C:\\Users\\DC3\\Desktop\\wav&text\\testwav.wav”;
//String wavFile2=“C:\\Users\\DC3\\Desktop\\waver\\Battlefield.wav”;
while(count2!=计数器){
试一试{
AudioInputStream clip1=AudioSystem.getAudioInputStream(新文件(wavFile1));
int bytesPerFrame=clip1.getFormat().getFrameSize();
//System.out.println(bytesPerFrame);
//int numBytes=safeLongToInt(clip1.getFrameLength())*bytesPerFrame;
//字节[]音频字节=新字节[numBytes];
//int numBytesRead=0;
//int numFramesRead=0;
////尝试从文件中读取numBytes字节。
//而((numBytesRead=
//clip1.读取(音频字节))!=-1){
////计算实际读取的帧数。
//clip1.读取(音频字节)
//numFramesRead=numbytes read/bytesPerFrame;
//totalFramesRead+=numFramesRead;
//System.out.println(totalFramesRead);
//                     }
long lengthofclip=Integer.parseInt(time.get(count2))-静默;
globallength=clip1.getFrameLength();
AudioInputStream appendedFiles=新的AudioInputStream(clip1,clip1.getFormat(),lengthofclip);
//长测试=(appendedFiles.getFrameLength()*24*2)/8;
//int AAAA=安全点(测试);
//附加文件.mark(aaaaa);
AudioSystem.write(附加文件,
AudioFileFormat.Type.WAVE,
新文件(“C:\\Users\\DC3\\Desktop\\wav&text\\”+name.get(count2));
count2++;
}捕获(例外e){
e、 printStackTrace();
}
}
}
公共静态安全点(长l){
if(lInteger.MAX_值){
抛出新的IllegalArgumentException
(l+“不能在不改变其值的情况下转换为int。”);
}
返回(int)l;
}

乍一看只是一个想法,但我认为这一行会带来麻烦:

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