2016-11-25 12 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]); 
} 

は、それから私は、「Microsoft Excelの」で私のオーディオ波形をプロットするために、私は、コンソールで見つかったポイント(のSystem.outを...)を使用します

waveform on Excel しかし、私の.wavファイルのこの波形は、そのプロット(IE)のオープンソース「Praat」波形から多くの異なるです::とrisultがある

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

ファイル内の各バイトが次の時点での波の振幅を表すと仮定しているようです。これは(一般的に言えば)大文字小文字の区別はありませファイルがヘッダで始まるという事実とは別に、各サンプルは多数のチャネルから構成され、各チャネル内でサンプルは1バイトよりも少ない(例えば4ビット以上(例えば16ビット))スペースを占める可能性があります。 たとえばこの説明:http://www.topherlee.com/software/pcm-tut-wavformat.html

+0

右ああそう、あなたのために、どのようなポイント私はのようにそのチャートを持って取る必要があります。。!プラートのイメージ? –

関連する問題