2012-07-11 31 views
1

我有一個很大的wav文件,我希望進入更小的塊。我還有一個.cue文件,它具有幀速率的長度,在這個幀文件中小塊應該是。我想出瞭如何分割wav,但所有制作的wav文件都是相同的聲音。似乎每次我創建一個新的wav時,大的wav文件就從一開始就開始,並且使新的波形長度正確但聲音相同。在Java中將Big 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; 
} 

回答

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