Reputation: 406
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:
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!
Upvotes: 3
Views: 1772
Reputation: 49482
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).
Upvotes: 2