How can I resample wav file

2019-02-11 13:53发布

Currently I'm recording an audio signal with following specs:

  • Channels: 1
  • SamplesPerSecond: 8000
  • BitsPerSample: 16

How can I convert this .wav-file to eg following specs (pure c# is preferred):

  • Channels: 1
  • SamplesPerSecond: 22050
  • BitsPerSample: 16

标签: c# audio wav pcm
4条回答
\"骚年 ilove
2楼-- · 2019-02-11 14:33

Windows API (one of) to resample audio is Audio Resampler DSP. This transform class is pretty straightforward to set up input and output types, then push input data and pull output.

Another task you would possible deal additionally with is reading from file and writing into a new file (you did not specify if it is actually needed in your original description though).

You might also want to use third party libraries like NAudio.

See also:

查看更多
Bombasti
3楼-- · 2019-02-11 14:35

Try code below from 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();
}
查看更多
4楼-- · 2019-02-11 14:36

AS3 function for resampling. You can easy change to convert this code to 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;
    }
查看更多
别忘想泡老子
5楼-- · 2019-02-11 14:45

try Naudio - it is a free + opensource .NET library offering several things including the ability to resample AFAIK.

As requested sample source for resampling

查看更多
登录 后发表回答