我想从.wav音频文件中绘制波形图。我在这个站点中找到了一个提取.wav字节的函数:
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 ...)中找到的点来绘制我的音频波形&#34; Microsoft Excel&#34;而且罪恶是:
waveform on Excel 但是我的.wav文件的这个波形与绘制(即)开源的波形有很大的不同&#34; Praat&#34; :
waveform on Praat 哪里错了?不是我必须采取的文件字节?
答案 0 :(得分: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;
}
答案 1 :(得分:0)
似乎您假设文件中的每个字节都代表下一个时间点的波幅。这(一般来说)并非如此。除了文件以标题开始的事实之外,每个样本由多个信道组成,并且在每个信道内,样本可能占用较少(例如,4比特或更多(例如16比特)空间而不仅仅是一个字节。 例如,请参阅此解释:http://www.topherlee.com/software/pcm-tut-wavformat.html。