0

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

4 Answers4

4

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:

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • the demo @http://stackoverflow.com/questions/3194184/c-sharp-resample-audio-from-8khz-to-44-1-48khz/4653746#4653746 looks really promising! –  Nov 03 '11 at 13:55
  • too funny ... Alvas.Audio is working with MsgBox ... fail on a web-page (even with their own example @http://alvas.net/alvas.audio,tips.aspx#tip24) –  Nov 04 '11 at 06:53
  • ha ... even more funny ... typeInitializer of Alvas.Audio is using a MsgBox ... so ... nope - but thanks! –  Nov 04 '11 at 07:00
  • This is why third party libraries are to be used with care :) – Roman R. Nov 04 '11 at 07:11
3

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;
    }
Sasha
  • 51
  • 3
3

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

As requested sample source for resampling

Yahia
  • 69,653
  • 9
  • 115
  • 144
-1

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();
}
user2217261
  • 455
  • 7
  • 18