如何重新采样wav文件

时间:2011-11-03 13:13:05

标签: c# audio wav pcm

目前我正在录制具有以下规格的音频信号:

  • 频道:1
  • SamplesPerSecond:8000
  • BitsPerSample:16

如何将此.wav文件转换为例如以下规格(首选纯c#):

  • 频道:1
  • SamplesPerSecond:22050
  • BitsPerSample:16

4 个答案:

答案 0 :(得分:4)

重新采样音频的Windows API(其中一个)是Audio Resampler DSP。这个转换类非常简单,可以设置输入和输出类型,然后推送输入数据并提取输出。

您可能另外处理的另一项任务是从文件读取并写入新文件(尽管您未在原始描述中指定它是否实际需要)。

您可能还想使用第三方库,例如NAudio

另见:

答案 1 :(得分:3)

尝试Naudio - 它是一个免费的+开源.NET库,提供了一些功能,包括重新采样AFAIK的能力。

As requested sample source for resampling

答案 2 :(得分:3)

AS3重新取样功能。您可以轻松更改以将此代码转换为C#:

    private function resampling(fromSampleRate:int, toSampleRate:int, quality:int = 10):void
    {
        var samples:Vector.<Number> = new Vector.<Number>;

        var srcLength:uint = this._samples.length;
        var destLength:uint = this._samples.length*toSampleRate/fromSampleRate;
        var dx:Number = srcLength/destLength;

        // fmax : nyqist half of destination sampleRate
        // fmax / fsr = 0.5;
        var fmaxDivSR:Number = 0.5;
        var r_g:Number = 2 * fmaxDivSR;

        // Quality is half the window width
        var wndWidth2:int = quality;
        var wndWidth:int = quality*2;

        var x:Number = 0;
        var i:uint, j:uint;
        var r_y:Number;
        var tau:int;
        var r_w:Number;
        var r_a:Number;
        var r_snc:Number;
        for (i=0;i<destLength;++i)
        {
            r_y = 0.0;
            for (tau=-wndWidth2;tau < wndWidth2;++tau)
            {
                // input sample index
                j = (int)(x+tau);

                // Hann Window. Scale and calculate sinc
                r_w = 0.5 - 0.5 * Math.cos(2*Math.PI*(0.5 + (j-x)/wndWidth));
                r_a = 2*Math.PI*(j-x)*fmaxDivSR;
                r_snc = 1.0;
                if (r_a != 0)
                    r_snc = Math.sin(r_a)/r_a;

                if ((j >= 0) && (j < srcLength))
                {
                    r_y += r_g * r_w * r_snc * this._samples[j];
                }
            }
            samples[i] = r_y;
            x += dx;
        }

        this._samples = samples.concat();
        samples.length = 0;
    }

答案 3 :(得分:0)

C# resample audio from 8khz to 44.1/48khz

尝试以下代码
static void Resample(string fileName)
{
    IntPtr formatNew = AudioCompressionManager.GetPcmFormat(2, 16, 44100);
    WaveReader wr = new WaveReader(File.OpenRead(fileName));
    IntPtr format = wr.ReadFormat();
    byte[] data = wr.ReadData();
    wr.Close();
    //PCM 8000 Hz -> PCM 44100
    byte[] dataNew = AudioCompressionManager.Resample(format, data, formatNew);
    WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"),
        AudioCompressionManager.FormatBytes(formatNew));
    ww.WriteData(dataNew);
    ww.Close();
}