简体   繁体   English

Java - 下采样wav音频文件

[英]Java - downsampling wav audio file

Hi I need to downsample a wav audio file's sample rate from 44.1kHz to 8kHz. 嗨我需要将wav音频文件的采样率从44.1kHz下采样到8kHz。 I have to do all the work manually with a byte array...it's for academic purposes. 我必须使用字节数组手动完成所有工作......这是出于学术目的。

I am currently using 2 classes, Sink and Source, to pop and push arrays of bytes. 我目前正在使用2个类,Sink和Source来弹出和推送字节数组。 Everything goes well until I reach the part where I need to downsample the data chunk using a linear interpolation. 一切顺利,直到我到达需要使用线性插值对数据块进行下采样的部分。

Since I'm downsampling from 44100 to 8000 Hz, how do I interpolate a byte array containing something like 128 000 000 bytes? 由于我从44100下采样到8000 Hz,如何插入包含128 000 000字节的字节数组? Right now I'm popping 5, 6 or 7 bytes depending on i%2 == 0, i%2 == 1 and i%80 == 0 and push the average of these 5, 6 or 7 bytes into the new file. 现在我正在弹出5,6或7个字节,具体取决于i%2 == 0,i%2 == 1和i%80 == 0并将这些5,6或7个字节的平均值推入新文件。

The result is indeed a smaller audio file than the original but it cannot be played on windows media player (says there is an error while reading the file) and there is a lot of noise although I can hear the right track behind the noise. 结果确实是一个比原始音频文件更小的音频文件,但它不能在Windows媒体播放器上播放(说在阅读文件时出错)并且虽然我能听到噪音背后的正确轨道,但仍有很多噪音。

So, to sum things up, I need help concerning the linear interpolation part. 因此,总而言之,我需要有关线性插值部分的帮助。 Thanks in advance. 提前致谢。

I think you shouldn't use the average of those samples as that would be a median filter, not exactly downsampling. 我认为您不应该使用这些样本的平均值,因为这将是中值滤波器,而不是精确的下采样。 Just use every 5th/6th/7th sample and write that to the new file. 只需使用每个第5/6 / 7个样本并将其写入新文件。

That will probably have some aliasing artifacts but might overall be recognizable. 这可能会有一些混叠伪像,但可能总体上是可识别的。

Another, more complex solution but probably one with better results, quality-wise, would be to first convert your samples into a frequency distribution using a FFT or DFT and then convert it back with the appropriate sample rate. 另一个更复杂的解决方案,但可能是质量更好的结果,首先是使用FFT或DFT将样本转换为频率分布,然后使用适当的采样率将其转换回来。 It's been a while since I have done such a thing but it's definitely doable. 已经有一段时间了,因为我做了这样的事情,但它绝对可行。 You may need to fiddle around a bit to get it working properly, though. 不过,您可能需要摆弄一下才能使其正常工作。

Also when not taking a FT of the complete array but rather in segments you have the problem of the segment boundaries being 0. A few years ago when I played with those things I didn't come up with a viable solution to this (since it generates artifacts as well) but there probably is one if you read the right books :-) 此外,当没有采用整个阵列的FT而是分段时你会遇到段边界为0的问题。几年前,当我玩这些东西时,我没有想出一个可行的解决方案(因为它也可以生成工件)但如果你读了正确的书,可能还有一本:-)

As for WMP complaining about the file: You did modify the header you write accordingly, right? 至于WMP抱怨文件:你确实修改了你写的标题,对吧?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM