オーディオ ファイルからデータのバイトを抽出する際の次の 2 つの実装の違いは何ですか?
ファイルは.wav
ファイルであり、ヘッダーやその他のものを使用せずにデータのみを抽出したい。
実装 1:
public byte[] extractAudioFromFile(String filePath) {
try {
// Get an input stream on the byte array
// containing the data
File file = new File(filePath);
final AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(file);
byte[] buffer = new byte[4096];
int counter;
while ((counter = audioInputStream.read(buffer, 0, buffer.length)) != -1) {
if (counter > 0) {
byteOut.write(buffer, 0, counter);
}
}
audioInputStream.close();
byteOut.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}// end catch
return ((ByteArrayOutputStream) byteOut).toByteArray();
}
実装 2:
public byte[] readAudioFileData(String filePath) throws IOException,
UnsupportedAudioFileException {
final AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(new File(filePath));
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, byteOut);
audioInputStream.close();
byteOut.close();
return ((ByteArrayOutputStream) byteOut).toByteArray();
}
すべての実装は、異なるサイズのバイトを返します。最初のものbyte[]
は、2 番目の実装よりも短い長さで返されます。
ファイルのスペクトログラムを視覚化するために、データのバイトを抽出しようとしています。
説明をいただければ幸いです。
ありがとう、
サメール