![]() |
VOOZH | about |
dotnet add package NAudio.Lame.CrossPlatform --version 2.2.1
NuGet\Install-Package NAudio.Lame.CrossPlatform -Version 2.2.1
<PackageReference Include="NAudio.Lame.CrossPlatform" Version="2.2.1" />
<PackageVersion Include="NAudio.Lame.CrossPlatform" Version="2.2.1" />Directory.Packages.props
<PackageReference Include="NAudio.Lame.CrossPlatform" />Project file
paket add NAudio.Lame.CrossPlatform --version 2.2.1
#r "nuget: NAudio.Lame.CrossPlatform, 2.2.1"
#:package NAudio.Lame.CrossPlatform@2.2.1
#addin nuget:?package=NAudio.Lame.CrossPlatform&version=2.2.1Install as a Cake Addin
#tool nuget:?package=NAudio.Lame.CrossPlatform&version=2.2.1Install as a Cake Tool
Wrapper for libmp3lame to add MP3 encoding support to NAudio 2.x on Windows and Linux.
IMPORTANT: This is the cross-platform version of the awesome NAudio.Lame by Corey Murtagh.
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.
On Linux, you need to install libmp3lame.so via the package manager of the OS.
Examples:
apt-get install lameapk add lameThe LameDLLWrap project is the interface to the native DLL.
Please note that native library loading will fail on Windows 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.
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.
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;
using NLayer.NAudioSupport; // Required for reading MP3 on Linux
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)
{
var builder = new Mp3FileReaderBase.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf)); // Required on Linux
using (var reader = new Mp3FileReaderBase(mp3FileName, builder))
using (var writer = new WaveFileWriter(waveFileName, reader.WaveFormat))
reader.CopyTo(writer);
}
}
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.
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 to the limit is to write the ID3 tags directly if they are too large for LAME to handle. Probably a good idea to keep the size reasonable.
using NAudio.Wave;
using NAudio.Lame;
using System;
class Program
{
static void Main(string[] args)
{
ID3TagData tag = new ID3TagData
{
Title = "A Test File",
Artist = "Microsoft",
Album = "Windows 7",
Year = "2009",
Comment = "Test only.",
Genre = LameMP3FileWriter.Genres[1],
Subtitle = "From the Calligraphy theme"
};
using (var reader = new AudioFileReader(@"test.wav"))
using (var writer = new LameMP3FileWriter(@"test.mp3", reader.WaveFormat, 128, tag))
{
reader.CopyTo(writer);
}
}
}
As of v1.0.4 there is now an event (MP3FileWriter.OnProgress)that you can use to get progress information during the encoding process. After each call to the LAME encoder the number of bytes in and out are sent to whatever event handler you've attached. At the end of the process when the encoder is closing it will send the final numbers along with a flag to indicate that the encoding is complete.
Since blocks are encoded very frequently I've added a very simple rate limiter that will skip progress notifications if one has happened within a certain time, except for the final progress event which is always raised. By default the progress time limit is 100ms, so you should get no more than 10 progress updates per second. You can raise or lower this by changing the MP3FileWriter.MinProgressTime property. Timing is approximate as it uses DateTime to store the last progress timestamp. Resolution may vary, but expect 15ms to be a fairly common minimum. Setting MinProgressTime to 0 will disable the delay and send you updates for every encoder call.
using NAudio.Wave;
using NAudio.Lame;
using System;
class Program
{
// For calculation of progress percentage, total bytes to be input
static long input_length = 0;
static void Main(string[] args)
{
using (var reader = new NAudio.Wave.AudioFileReader(@"C:\Temp\TestWave.wav"))
using (var writer = new NAudio.Lame.LameMP3FileWriter(@"C:\Temp\Encoded.mp3", reader.WaveFormat, NAudio.Lame.LAMEPreset.V3))
{
writer.MinProgressTime = 250;
input_length = reader.Length;
writer.OnProgress += writer_OnProgress;
reader.CopyTo(writer);
}
}
static void writer_OnProgress(object writer, long inputBytes, long outputBytes, bool finished)
{
string msg = string.Format("Progress: {0:0.0}%, Output: {1:#,0} bytes, Ratio: 1:{2:0.0}",
(inputBytes * 100.0) / input_length,
outputBytes,
((double)inputBytes) / Math.Max(1, outputBytes));
Console.Write("\r{0," + (Console.BufferWidth - 1).ToString() + "}\r{1}", "", msg);
if (finished)
Console.WriteLine();
}
}
From v1.1.1 there is a new LameConfig class which has a variety of settings that will alter the encoder's operation. This allows you to set the Copyright flag on encoded frames, etc.
While there are many more settings available I don't have a clear picture of who wants what. If you're desperate for the quantization or filtering settings let me know.
Release to NuGet 19-March-2024
Changes:
Released to NuGet 30-May-2023
Changes:
Released to NuGet 18-Jan-2022
Changes:
Released to NuGet 10-Feb-2021
Changes:
Released to NuGet 6-Sep-2020
Changes:
Released to NuGet 29-Jun-2020
Changes:
OutputSampleRate to LameConfig.Released to NuGet 22-Jul-2020
Changes:
VBR to LameConfig to control VBR mode.VBRMode enumeration, mirroring from LameDLLWrap.V0-V9 VBR is automatically enabled.Released to NuGet 14-Jul-2020
Changes:
Added LameDLL.LoadNativeDLL(...) method to load correct version of the DLL to memory.
Note: This is very Windows-specific. There is a guard against attempting to load on non-windows OS.
Call LoadNativeDLL() when wrapper assembly loaded to load Native DLL from default paths.
Releasted to NuGet 25-May-2020
Changes:
MPEGMode configuration being unavailable outside library (#37).Released to NuGet 6-Apr-2020.
New Features:
LameConfig class to allow more control over encoder initialization.LameConfig has a short list of settings for the encoder. More may be added in future.
Released to NuGet 25-Dec-2019.
New Features:
Replaced static constructor initialization with ModuleInit.Fody module initializer to properly initialize the Resource Assembly Loader.
Rebuilt as .NET Standard 2.0 to attempt to make this fully compatible with .NET Core on Windows.
Made Resource Assembly Loader (Loader) public to allow manual initialization on .NET Core as static constructors don't cut it anymore.
Initial attempt to add .NET Standard support using multi-targetted compilation.
Since working on the Unicode stuff I decided I should add some tests. While working on the initial tests themselves I found a couple of bugs and some ideas for new features.
Released to NuGet 29-Jan-2019.
New Features:
bool returns in DLL wrapper to capture actual return value to improve availability of error information.ID3TagData.UserDefinedTags and replaced with Dictionary<>-backed ID3TagData.UserDefinedText property instead.LameDLLWrap libraries during build, making debugging and testing simpler than just using Release build everywhere.Bugs Squashed:
In response to PR #23 I got to looking through the LAME source a lot more. Took a little more work than I'd like, but Unicode is now partially supported.
While I was at it I fixed a couple of other things.
Released to NuGet 27-Jan-2019.
New Features:
Bugs Squashed:
IMp3FrameDecompressor implementation for pluggable MP3 decoding| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 2 NuGet packages that depend on NAudio.Lame.CrossPlatform:
| Package | Downloads |
|---|---|
|
LlmTornado.Contrib
Provides extra functionality to LlmTornado. |
|
|
Eyu.Audio
Package Description |
Showing the top 1 popular GitHub repositories that depend on NAudio.Lame.CrossPlatform:
| Repository | Stars |
|---|---|
|
lofcz/LLMTornado
The .NET library to build AI agents with 30+ built-in connectors.
|
v2.2.1 Add Linux support, updated to NAudio 2.2.1.
v2.1.0 Improve ID3v2 handling of unicode strings, updated to NAudio v2.1.
v2.0.1 Fix exception when NAudio.Lame loaded as memory assembly.
v2.0.0 Binding to NAudio 2.0.0, version alignment with NAudio. No feature updates.
v1.1.6 Added more VBR encoding options and some basic tests for them.
v1.1.5 Added OutputSampleRate to LameConfig.
v1.1.4 Fixed VBR presets and added VBR mode to configuration.
v1.1.3 Added a new NativeDLL loader method that can be called with search paths.
v1.1.2 Fixed some issues with sample rate, configuration, short ID3 tags, etc. Refer to project page for full information.
v1.1.1 Updated all projects to NAudio v1.10.0, added LameConfig, reorganised writer construction, updated .NET Core tests to dotnet 3.1.
v1.1.0 Changed all projects to .NET STandard 2.0, rebuilt testing using .NET Core, fixed loader initialization problems.