How can I resample wav file

后端 未结 4 414
青春惊慌失措
青春惊慌失措 2021-01-03 17:24

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

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

How can I

相关标签:
4条回答
  • 2021-01-03 17:25

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

    As requested sample source for resampling

    0 讨论(0)
  • 2021-01-03 17:27

    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();
    }
    
    0 讨论(0)
  • 2021-01-03 17:31

    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;
        }
    
    0 讨论(0)
  • 2021-01-03 17:47

    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:

    • C# resample audio from 8khz to 44.1/48khz
    • Audio DSP in C#
    0 讨论(0)
提交回复
热议问题