绘制音频波形图Java
问题描述:
我想从.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 我哪里错了?不是我必须拿的文件的字节数?
答
在阵“结果”有,你会发现点
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;
}
答
看来你假设文件中的每个字节都代表了下一个时间点波形的幅度。这(一般来说)不是这种情况。除了文件以标题开始的事实之外,每个样本由多个通道组成,并且在每个通道内,样本可能会占用较少的空间(例如,4位或更多(例如16位))空间,而不仅仅是一个字节。例如这样的解释:http://www.topherlee.com/software/pcm-tut-wavformat.html
哦,对了所以,对你来说,什么点我应该采取有该图表中! Praat的形象? –