【发布时间】:2010-06-30 09:13:51
【问题描述】:
首先我正在做一个小项目来查看一些声音的频谱。
我用麦克风完成了这个工作: alt text http://img25.imageshack.us/img25/4271/spectrumanalyzerfourier.png
上面的图片只是我通过麦克风说话和大喊几秒钟。这对我来说看起来不错。
但是当我尝试读取 MP3 文件并制作它的频谱图时,它看起来有点不同。我尝试了 Aphex Twin - Windowlicker,您通常应该在频谱图中看到一张脸,或者至少可以看到一些更暗的颜色。但它看起来不太好: alt text http://img10.imageshack.us/img10/3475/aphextwinhmm.png
这是我对麦克风所做的:
byte tempBuffer[] = new byte[10000];
ByteArrayOutputStream out = new ByteArrayOutputStream();
counter = 20;
// Microphone
while (counter != 0) {
int count = line.read(tempBuffer, 0, tempBuffer.length);
if (count > 0) {
out.write(tempBuffer, 0, count);
}
counter--;
}
out.close();
// FFT code below ...
byte audio[] = out.toByteArray();
// ...
这就是我使用 MP3 的方式:
我用同样的代码进行了转换和可视化,只是音频捕捉部分不同(我只是在绘图方法中调整了高度,看看有没有不同但没有):
byte tempBuffer[] = new byte[10000];
ByteArrayOutputStream out = new ByteArrayOutputStream();
FileInputStream input = null;
File mp3 = new File("Aphex Twin - Widowlicker.mp3");
input = new FileInputStream(mp3);
int len;
while((len = input.read(tempBuffer)) > 0) {
out.write(tempBuffer, 0, len);
}
out.close();
input.close();
// FFT code below ...
byte audio[] = out.toByteArray();
// ...
如果有人能指出我对 MP3 文件做错了什么,那就太好了。
这些是我的设置:
- 采样率:44100
- 每个样本位:8
- 频道:1(单声道)
- 签名:真
- big endian: true (我在 Java 中使用 AudioFormat)
- 要读取音频的tempBuffer:10000(byte tempBuffer[] = new byte[10000];)
- 对于 FFT,我将音频分成 4096 个块(必须是 2 的幂)
顺便问一下:这些设置可以吗?或者我应该使用 16bps 或立体声还是 10000 用于缓冲区太多或 4096 到小/大?
提前致谢
【问题讨论】:
标签: java audio mp3 microphone