简体   繁体   中英

C# library for audio resampling that has the same abilities as FFmpeg

I have to use a pure C# solution for resampling audio, which can produce me the exact same results as FFmpeg's audio sampling can.

FFmpeg first builds some kind of polyphase filter bank, and then uses that for the sampling process (sorry for the vague phrasing, but I'm not too familiar with this topic). According to this brief documentation , the initialization can be customized this way:

AVResampleContext* av_resample_init(
    int     out_rate,
    int     in_rate,
    int     filter_length,
    int     log2_phase_count,
    int     linear,
    double  cutoff   
    )

The parameters are:

  • out_rate : output sample rate
  • in_rate : input sample rate
  • filter_length : length of each FIR filter in the filterbank relative to the cutoff freq
  • log2_phase_count : log2 of the number of entries in the polyphase filterbank
  • linear : if 1 then the used FIR filter will be linearly interpolated between the 2 closest, if 0 the closest will be used
  • cutoff : cutoff frequency, 1.0 corresponds to half the output sampling rate

I'd need to use a C# library that is configurable in the same depth. I've been trying to use NAudio (more specifically, its WaveFormatConversionStream class), but there, I could only set the input and output sample rates, so I didn't get the expected results.

So, is there a C# lib that could resample with the same settings as FFmpeg can? Or one that has almost all of these settings or similar ones? Note: I need a C# solution, not a wrapper!

In addition to WaveFormatConversionStream (which uses ACM codecs), NAudio includes another resampler that can be accessed as a DirectX Media Object (DMO), or (in the latest prerelease of NAudio 1.7) as a Media Foundation Transform. These can be used in Windows Vista and above. Sadly I think they are not available in XP (but has been a while since I tried).

The DMO version found is in the Resampler class (there is also a ResamplerDmoStream ), and the Media Foundation Version is in MediaFoundationResampler . They actually both create the same underlying object, but on the MFT version I have added a property called ResamplerQuality which allows you to choose anywhere between 1 (linear interpolation) and 60 (max quality). In this article I include a spectogram of a resampled sine wave sweep and you will see that the quality is very good.

You could easily make the same change to the Resampler class if you want to go the DMO route, since it has access to IWMResamplerProps , which allows you to set the half filter length (which is the same value between 1 and 60).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM