Downsample PCM audio from 44100 to 16000

开篇

一上来就是 降采 ,看起来高大上,每次遇到问题大概就是翻东找西的,靠谱的少,心塞,先解释几个概念,然后再入正题。

概念

1)采样位数——可以理解数字音频设备处理声音的解析度,即对声音的辨析度。就像表示颜色的位数一样(8位表示256种颜色,16位表示65536种颜色),有8位,16位,24位等。这个数值越大,解析度就越高,录制和回放的声音就越真实。
2)采样频率——就是对声音信息1秒钟采样多少次,以记录成数字信息。如CD音频是44.1KHz采样率,它对声音以每秒44100次的频率来记录信息。原则上采样率越高,声音的质量越好。
3)比特率——表示单位时间(1秒)内传送的比特数bps(bit per second,位/秒)的速度。作为一种数字音乐压缩效率的参考性指标,通常使用kbps(通俗地讲就是每秒钟1024比特)作为单位。

方案

1、取关键帧

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.
不逐句翻译,大概意思就是不需要精确降采,志勇第五第六第七帧,写到文件里就好啦。看起来,挺像那么回事的,而且提问者回复说非常好用,尴尬的问题出现了,我是在安卓设备录音时,通过AudioRecord读取的byte数组,暴漏的api没办法获取每一帧的大小,想了半天没搞定。

2、过滤数据

How to convert between (most) audio formats in .NET

// Just about worst resampling algorithm possible:
private float[] ResampleNaive(float[] inBuffer, int inputSampleRate, int outputSampleRate)
{
    var outBuffer = new List();
    double ratio = (double) inputSampleRate / outputSampleRate;
    int outSample = 0;
    while (true)
    {
        int inBufferIndex = (int)(outSample++ * ratio);
        if (inBufferIndex < read)
            writer.WriteSample(inBuffer[inBufferIndex]);
        else
            break;    
    } 
    return outBuffer.ToArray();    
}

当然用的时候是翻译成了Java代码,简单粗暴,关键是好用。

3、现成的库

现在大概是成了固定思维,遇到问题先找个现成的库,没有再造车,
关键是没找到好用的。

你可能感兴趣的:(Downsample PCM audio from 44100 to 16000)