NAudioLame

v2.1.1·Updated 2024-07-17

Wrapper for libmp3lame.dll to add MP3 encoding support to NAudio.

About this mod Author’s original text

NAudio.Lame

Note: This is not my library. This is an upload containing the binaries to allow mods to refer to it as a dependency.

This currently contains my fork:

Description

Wrapper for libmp3lame.dll to add MP3 encoding support to NAudio 2.0 on Windows.

IMPORTANT: Because this wraps Windows native DLLs it will not work on any other operating system.

It may function with Windows emulation layers but I have never tested this.

Includes both 32-bit and 64-bit versions of Windows native libmp3lame.dll (named libmp3lame.32.dll and libmp3lame.64.dll respectively), both of which will be copied to the output folder on build.

If you are compiling for a specific CPU target - x86 or x64 - then you only need to distribute the appropriate version.

The LameDLLWrap project is the interface to both 32-bit and 64-bit version of the native DLLs, and is compiled for both targets.

Both versions are compiled into resources in NAudio.Lame.dll.

At runtime the version for the current process bit width is loaded from resources, which then references the appropriate native library.

Please note that native library loading will fail if for any reason the application's binary path is not in the current search path.

This will happen for example in ASP.NET projects.

Usage

The LameMP3FileWriter class implements a Stream that encodes data written to it, writing the encoded MP3 data to either a file or a stream you provide.

Sample Code

Here is a very simple codec class to convert a WAV file to and from MP3:

using System.IO;

using NAudio.Wave;

using NAudio.Lame;

public static class Codec

{

// Convert WAV to MP3 using libmp3lame library

public static void WaveToMP3(string waveFileName, string mp3FileName, int bitRate = 128)

{

using (var reader = new AudioFileReader(waveFileName))

using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate))

reader.CopyTo(writer);

}

// Convert MP3 file to WAV using NAudio classes only

public static void MP3ToWave(string mp3FileName, string waveFileName)

{

using (var reader = new Mp3FileReader(mp3FileName))

using (var writer = new WaveFileWriter(waveFileName, reader.WaveFormat))

reader.CopyTo(writer);

}

}

Native DLL Loading

The new NAudio.Lame.LameDLL.LoadNativeDLL(...) method in v1.1.3 allows you to specify a set of root

folders to search for the native DLLs and load the correct version for the current architecture. This is

useful in ASP.NET and ASP.NET Core to resolve the native DLL in paths outside of the current directory

and the system PATH.

For ASP.NET (Framework version):

public static void LoadLameDLL()

{

LameDLL.LoadNativeDLL(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"));

}

For ASP.NET Core, from a PageModel or Controller:

public class IndexModel: PageModel

{

public IndexModel(IWebHostEnvironment hostEnvironment)

{

LameDLL.LoadNativeDLL(hostEnvironment.ContentRootPath);

}

}

When the native DLL is loaded this way the handle is preserved and repeated calls to the method will

return true without attempting to load the DLL again.

The method is called during LameDLLWrap assembly loading the first time a method that uses the

NAudio.Lame classes is called. On ASP.NET (Framework or Core) it is important to load the native DLL

before it is needed.

ID3 tag support

The LameMP3FileWriter class now accepts an ID3TagData parameter, allowing you to supply some information that will be set as the ID3 tag on the MP3 file.

The ID3TagData class is pretty simple right now, with only basic information support.

There are a lot of other bits of information that can potentially be stored in the ID3 tag, with all sorts of interesting ways of encoding the data.

I'll have to play with it some more.

And yes, you can add a cover image, in JPG, PNG or GIF format.

LAME can't directly support ID3v2 tags greater than 32KB in size due to internal buffer size constraints, it does allow you to write your own ID3 tags.

The solution

Install & use Install: BepInEx plugin

  1. The archive from playbuff is the mod itself — usually a BepInEx plugin (a .dll or a plugin folder once unzipped).
  2. Recommended: use a community mod manager (such as r2modman) — pick this game, import the mod, and the manager sets up BepInEx and its dependencies for you.
  3. Manual route: install the BepInEx loader in the game folder, then drop the unzipped .dll or plugin folder into BepInEx/plugins/.
  4. Launch the game — the BepInEx console logs every plugin it loads; seeing this mod in the log means it is installed.

Mods with listed dependencies need those installed too, or they will not load. Options are usually changed in-game or in the plugin's config file.

Dependencies

Install these first, or the mod will not work:

Bobbie-NAudio

File & checksum

Filename
NAudioLame-2.1.1.zip
File size
792.2 KiB