2016-11-25 160 views
0

我想從.wav音頻文件繪製波形圖。我在這個網站發現,提取一個.wav的字節的函數:繪製音頻波形圖Java

ByteArrayOutputStream out = new ByteArrayOutputStream(); 
BufferedInputStream in = null; 
try { 
    in = new BufferedInputStream(new FileInputStream(args[0])); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

int read; 
byte[] buff = new byte[1024]; 
while ((read = in.read(buff)) > 0) 
{ 
    out.write(buff, 0, read); 
} 
out.flush(); 

byte[] audioBytes = out.toByteArray(); 
for (int i=0; i<audioBytes.length; i++) { 
    System.out.println(audioBytes[i]); 
} 

然後我用我在控制檯中發現的點(的System.out ...)繪製的「Microsoft Excel中」我的音頻波形和risult是:

waveform on Excel 但我的.wav文件的這種波形是從波形有很大不同的是地塊(即)開源「普瑞特」:

waveform on Praat 我哪裏錯了?不是我必須拿的文件的字節數?

回答

2

在陣「結果」有,你會發現點

public double[] extract(File inputFile) { 
     AudioInputStream in = null; 
     try { 
      in = AudioSystem.getAudioInputStream(inputFile); 
     } catch (Exception e) { 
      System.out.println("Cannot read audio file"); 
      return new double[0]; 
     } 
     AudioFormat format = in.getFormat(); 
     byte[] audioBytes = readBytes(in); 

     int[] result = null; 
     if (format.getSampleSizeInBits() == 16) { 
      int samplesLength = audioBytes.length/2; 
      result = new int[samplesLength]; 
      if (format.isBigEndian()) { 
       for (int i = 0; i < samplesLength; ++i) { 
        byte MSB = audioBytes[i * 2]; 
        byte LSB = audioBytes[i * 2 + 1]; 
        result[i] = MSB << 8 | (255 & LSB); 
       } 
      } else { 
       for (int i = 0; i < samplesLength; i += 2) { 
        byte LSB = audioBytes[i * 2]; 
        byte MSB = audioBytes[i * 2 + 1]; 
        result[i/2] = MSB << 8 | (255 & LSB); 
       } 
      } 
     } else { 
      int samplesLength = audioBytes.length; 
      result = new int[samplesLength]; 
      if (format.getEncoding().toString().startsWith("PCM_SIGN")) { 
       for (int i = 0; i < samplesLength; ++i) { 
        result[i] = audioBytes[i]; 
       } 
      } else { 
       for (int i = 0; i < samplesLength; ++i) { 
        result[i] = audioBytes[i] - 128; 
       } 
      } 
     } 

     return result; 
    } 
0

看來你假設文件中的每個字節都代表了下一個時間點波形的幅度。這(一般來說)不是這種情況。除了文件以標題開始的事實之外,每個樣本由多個通道組成,並且在每個通道內,樣本可能會佔用較少的空間(例如,4位或更多(例如16位))空間,而不僅僅是一個字節。例如這樣的解釋:http://www.topherlee.com/software/pcm-tut-wavformat.html

+0

哦,對了所以,對你來說,什麼點我應該採取有該圖表中! Praat的形象? –