In addition to NAudio, I have another open source .NET audio related project NLayer which is a fully managed MP3 decoder. This was something I ported to C# from a Java project called JavaLayer many years ago, and has since been improved with some excellent contributions from Andrew Ward.

The reason the code never was made part of NAudio is that the original JavaLayer code was licensed under LGPL which is not compatible with the NAudio license. Also, for performance reasons, NAudio’s Mp3FileReader class uses the Fraunhoeffer ACM MP3 decoder that comes with most versions of Windows.

However, there are many situations in which you might want a fully managed MP3 decoder. One example is that you are running on a Windows Azure cloud service, or a Windows Phone, which means the ACM codec is not available, and neither is the DMO MP3 decoder which NAudio also offers as an alternative.

The way you use NLayer is very simple. You need to install the NLayer.NAudioSupport NuGet package which references both the NLayer and NAudio NuGet packages. Then, when you construct your Mp3FileReader, you can pass in a NLayer.NAudioSupport.Mp3FrameDecompressor as an alternative frame decoder.

Here’s a simple example where we decode an MP3 File and convert it to a WAV file:

var fileName = @"C:\Users\markh\Desktop\input.mp3";
var builder = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf));
using (var reader = new Mp3FileReader(fileName, builder))

And that’s all there is to it. Sadly this is a decoder only and can’t encode MP3s.

I haven’t made NLayer into a .NET Core project yet, but it would be a good candidate as it doesn’t use any features that couldn’t be cross-platform. Maybe when the dust settles a bit on the .NET Core tooling, I’ll see if I can update the project and NuGet package which would provide us with a nice managed MP3 decoder that would run cross-platform.

Want to get up to speed with the the fundamentals principles of digital audio and how to got about writing audio applications with NAudio? Be sure to check out my Pluralsight courses, Digital Audio Fundamentals, and Audio Programming with NAudio.
Vote on HN
comments powered by Disqus