FFT在java音频记录中的应用
我在这个网站上看到过类似的问题,但我的问题有点不同。我用来捕获音频的代码是。我想简单地获取捕获的音频,并对其应用一个256点的FFT 我意识到这FFT在java音频记录中的应用,java,audio,signal-processing,fft,Java,Audio,Signal Processing,Fft,我在这个网站上看到过类似的问题,但我的问题有点不同。我用来捕获音频的代码是。我想简单地获取捕获的音频,并对其应用一个256点的FFT 我意识到这line count=line.read(buffer,0,buffer.length)将音频分解为“块” 也可以找到我正在使用的FFT 我的问题是: 我想知道是否有一种方法可以将FFT应用于整个音频记录,而不仅仅是缓冲量 我看到FFT的代码需要一个实部和虚部,如何从音频文件的代码中得到实部和虚部 所有的javax.sound.sampled包所做的就是
line count=line.read(buffer,0,buffer.length)代码>将音频分解为“块”
也可以找到我正在使用的FFT
我的问题是:
我想知道是否有一种方法可以将FFT应用于整个音频记录,而不仅仅是缓冲量
我看到FFT的代码需要一个实部和虚部,如何从音频文件的代码中得到实部和虚部
所有的javax.sound.sampled
包所做的就是从文件中读取原始字节并将它们写入输出。因此,有一个“中间”步骤,你必须做这是转换自己的样本
以下显示了如何对PCM执行此操作(带注释),摘自我的代码示例:
publicstaticfloat[]解包(
字节[]字节,
长[]转移,
浮动[]个样本,
int bvalid,
音频格式
) {
如果(fmt.getEncoding()!=AudioFormat.Encoding.PCM_)
&&fmt.getEncoding()!=AudioFormat.Encoding.PCM\u UNSIGNED){
返回样品;
}
final int-bitsPerSample=fmt.getSampleSizeInBits();
final int bytesPerSample=bitsPerSample/8;
final int normalBytes=normalBytesFromBits(bitsPerSample);
/*
*这不是最简单的方法,但效率更高。
*否则,必须有4种不同的方法来进行
*endianness/signedness的每个组合或一次完成所有操作
*循环并检查每个示例的格式。
*
*辅助数组(传输)允许分割逻辑
*但不要太重复。
*
*这里有两个将字节转换为原始长样本的循环。
*Java中的整型原语在使用时会得到符号扩展
*升级为更大的类型,以便&0xffL掩码保持其完整性。
*
*/
if(fmt.isBigEndian()){
对于(int i=0,k=0,b;i
这段代码假定float[]
并且您的FFT需要一个double[]
但这是一个相当简单的更改。transfer
和samples
是长度等于bytes的数组。length*normalBytes
和bvalid
是read
的返回值。我的代码示例假定音频输入流,但相同的转换应该适用于TargetDataLine。我不确定你可以复制粘贴它,但这只是一个例子
关于你的两个问题:
您可以对整个记录进行很长的FFT,也可以对每个缓冲区的FFT求平均值
链接到的FFT就地计算。因此实部是音频样本,虚部是长度等于实部的空数组(用零填充)
但是当FFT完成时,仍然需要做一些事情,我看不到链接类在做:
- 转换为极坐标
- 通常丢弃负频率(整个频谱的上半部分是下半部分的镜像)
- 通过将结果大小除以变换的长度,可以潜在地缩放结果大小(真实部分)
编辑,相关:
你不能“简单地”做到这一点。你可以通过将字节转换为音频样本来复杂地做到这一点(可以手动完成).你看了很多吗?我的一位讲师建议我这样做,所以我没有考虑按照你建议的方式做。有没有更简单的方法来做,因为我真的需要256点的FFT。不过我肯定会阅读你建议的方法。我说的基本上是用Java做这件事的唯一方法。你可以Java声音的做法是读取原始字节并将其写入输出。完全可以“截取”流,你自己转换它们,然后用它们做任何你想做的事情。这就是我转换它们的方式吗?我没有看到任何转换发生在那里。非常感谢你的回答,它非常彻底!正是我想要的。我只是想知道是否有什么材料可以让我读一下,找出如何转换为极坐标不客气。我想,对于极坐标,(方程式8-6)。只要做一个计算方程式的小方法。没问题。祝你好运。:)
public static float[] unpack(
byte[] bytes,
long[] transfer,
float[] samples,
int bvalid,
AudioFormat fmt
) {
if(fmt.getEncoding() != AudioFormat.Encoding.PCM_SIGNED
&& fmt.getEncoding() != AudioFormat.Encoding.PCM_UNSIGNED) {
return samples;
}
final int bitsPerSample = fmt.getSampleSizeInBits();
final int bytesPerSample = bitsPerSample / 8;
final int normalBytes = normalBytesFromBits(bitsPerSample);
/*
* not the most DRY way to do this but it's a bit more efficient.
* otherwise there would either have to be 4 separate methods for
* each combination of endianness/signedness or do it all in one
* loop and check the format for each sample.
*
* a helper array (transfer) allows the logic to be split up
* but without being too repetetive.
*
* here there are two loops converting bytes to raw long samples.
* integral primitives in Java get sign extended when they are
* promoted to a larger type so the & 0xffL mask keeps them intact.
*
*/
if(fmt.isBigEndian()) {
for(int i = 0, k = 0, b; i < bvalid; i += normalBytes, k++) {
transfer[k] = 0L;
int least = i + normalBytes - 1;
for(b = 0; b < normalBytes; b++) {
transfer[k] |= (bytes[least - b] & 0xffL) << (8 * b);
}
}
} else {
for(int i = 0, k = 0, b; i < bvalid; i += normalBytes, k++) {
transfer[k] = 0L;
for(b = 0; b < normalBytes; b++) {
transfer[k] |= (bytes[i + b] & 0xffL) << (8 * b);
}
}
}
final long fullScale = (long)Math.pow(2.0, bitsPerSample - 1);
/*
* the OR is not quite enough to convert,
* the signage needs to be corrected.
*
*/
if(fmt.getEncoding() == AudioFormat.Encoding.PCM_SIGNED) {
/*
* if the samples were signed, they must be
* extended to the 64-bit long.
*
* so first check if the sign bit was set
* and if so, extend it.
*
* as an example, imagining these were 4-bit samples originally
* and the destination is 8-bit, a mask can be constructed
* with -1 (all bits 1) and a left shift:
*
* 11111111
* << (4 - 1)
* ===========
* 11111000
*
* (except the destination is 64-bit and the original
* bit depth from the file could be anything.)
*
* then supposing we have a hypothetical sample -5
* that ought to be negative, an AND can be used to check it:
*
* 00001011
* & 11111000
* ==========
* 00001000
*
* and an OR can be used to extend it:
*
* 00001011
* | 11111000
* ==========
* 11111011
*
*/
final long signMask = -1L << bitsPerSample - 1L;
for(int i = 0; i < transfer.length; i++) {
if((transfer[i] & signMask) != 0L) {
transfer[i] |= signMask;
}
}
} else {
/*
* unsigned samples are easier since they
* will be read correctly in to the long.
*
* so just sign them:
* subtract 2^(bits - 1) so the center is 0.
*
*/
for(int i = 0; i < transfer.length; i++) {
transfer[i] -= fullScale;
}
}
/* finally normalize to range of -1.0f to 1.0f */
for(int i = 0; i < transfer.length; i++) {
samples[i] = (float)transfer[i] / (float)fullScale;
}
return samples;
}
public static int normalBytesFromBits(int bitsPerSample) {
/*
* some formats allow for bit depths in non-multiples of 8.
* they will, however, typically pad so the samples are stored
* that way. AIFF is one of these formats.
*
* so the expression:
*
* bitsPerSample + 7 >> 3
*
* computes a division of 8 rounding up (for positive numbers).
*
* this is basically equivalent to:
*
* (int)Math.ceil(bitsPerSample / 8.0)
*
*/
return bitsPerSample + 7 >> 3;
}