Click here to Skip to main content
Click here to Skip to main content

C# MP3 Compressor

, 13 Jul 2004
Rate this:
Please Sign up or sign in to vote.
A C# MP3 compressor using "lame_enc.dll" from the LAME project.

Sample image

Introduction

The first question to my previous article (C Sharp Ripper) was about how to rip directly to MP3 format. Then I looked for some MP3 compressor done for .NET framework and I couldn't find the exact solution, so I decided to write one. The solution was to use the LAME MP3 encoder, specifically to wrap in C# the functions of lame_enc.dll. See this website for technical and copyright information regarding the LAME project.

In this work, there is code from the article: A low level audio player in C# by Ianier Munoz.

Background

While it is true that the LAME encoder is written in C and all source code is available, one could think of compiling these source files in managed C++ to obtain a managed compressor. However, an easier way to achieve that goal is to use the LAME compressor through the functions exposed by the lame_enc.dll. This DLL exports a few functions and it is relatively easy to use.

One problem to mention about the use of the DLL is that in the current version, it is not possible to compress two streams at the same time. The compressor I describe in this article (Mp3Writer) is a class derived from AudioWriter, which is a binary writer that can be used as a base of writers for different audio streams (different audio formatters, compressors, etc.). AudioWriter receives audio data in PCM format, then the data is compressed and written to the resulting stream, or just written without modifications using a specific format (WAV format, for instance, as in the WaveWriter class).

Using the code

AudioWriter and other base and tools classes are in a library named yeti.mmedia. All other classes related to the MP3 compressor are in a library yeti.mp3. Within this library, I included all the translation of structures, constants, and functions needed to use lame_enc.dll. The following code shows the simplest way to use the MP3 compressor with the default configuration:

...
using WaveLib;
using Yeti.MMedia; 
using Yeti.MMedia.Mp3;
..
WaveStream InStr = new WaveStream("SomeFile.wav");
try
{
  Mp3Writer writer = new Mp3Writer(new FileStream("SomeFile.mp3", 
                                      FileMode.Create), InStr.Format);
  try
  {
    byte[] buff = new byte[writer.OptimalBufferSize];
    int read = 0;
    while ( (read = InStr.Read(buff, 0, buff.Length)) > 0 )
    {
      writer.Write(buff, 0, read);
    }
  }
  finally
  {
    writer.Close();
  }
}
finally
{
  InStr.Close();
}

First, a WaveStream is created to read the uncompressed audio data from a WAV file, then a Mp3Writer is created. The Mp3Writer constructor is called passing as the first parameter the stream where to write the resulting MP3 stream (in this case, a FileStream), and as second parameter the WaveFormat that specifies the format of the input data. The input stream is read until the end and all the data is written to the writer, which converts it to MP3 format and saves the result to the output stream.

There are two other overloads of the Mp3Writer constructor: one that accepts a BE_CONFIG instance, which is a structure translated from LAME that describes how the stream must be compressed (bit rate, compression method, quality, etc.). The other overload accepts a Mp3WriterConfig instance, which wraps BE_CONFIG and it is serializable, so it can be used to persist the compressor configuration using serialization. Another important class is EditMp3Writer, which is a custom control that can be included in a form or dialog to configure the parameters of the compressor, as shown in the sample code.

Another example of using the writer could be an improved version of the ripper described in my article C Sharp Ripper to rip directly to MP3 format. In the file Form1.cs, inside the handler of the "Save as.." button, there is the line:

m_Writer = new WaveWriter(WaveFile, Format);

which may be changed to:

m_Writer = new Mp3Writer(WaveFile, Format);

The rest of the code remains without change and, of course, if you need more control on the compressor parameters then you should add extra code to use BE_CONFIG and/or Mp3WriterConfig.

Conclusion

As shown here, the use of this compressor is relatively easy. When more control about the compression process is required, then a deeper knowledge about the meaning and use of all fields of each LAME structure is mandatory. It is also important to note that if you use this code in any product, you should observe the LAME product licensing details.

Any use of this product does not convey a license under the relevant intellectual property of Thomson and/or Fraunhofer Gesellschaft nor imply any right to use this product in any finished end user or ready-to-use final product. An independent license for such use is required. For details, please visit http://www.mp3licensing.com.

You can find a first version of a Widows Media Audio (WMA) compressor in my article, Windows Media Audio Compressor. A better compressor and a translation of the Windows Media Format SDK could be found in my article, C Sharp Windows Media Format SDK translation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Idael Cardoso
Web Developer
France France
No Biography provided

Comments and Discussions

 
QuestionSignature in MP3 file using C# Pinmemberhamroush28-Aug-13 23:53 
QuestionError in DLL : URGENT HELP PinmemberJay Kotekar10-Aug-13 6:11 
GeneralMy vote of 2 PinmemberJay Kotekar23-Jul-13 23:58 
Questionwrite MP3 on the fly Pinmemberyoke21-Dec-12 13:25 
QuestionBWF Wavs Pinmemberrimsky82a21-Dec-12 7:53 
AnswerRe: BWF Wavs PinmemberBeercolhol24-Jul-14 1:54 
QuestionHi I got an error when my format.nSamplePerSec is 8000 Pinmemberulysses7898023-Aug-12 23:13 
AnswerRe: Hi I got an error when my format.nSamplePerSec is 8000 PinmemberBorsukWro15-Dec-12 2:40 
QuestionException PinmemberAzargoth7-Mar-12 23:28 
I'm receiving this error:
 
An exception has ocurred with the following message
 
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
 
I've copied "lame_enc.dll" to following folders: Windows root, Windows\System, Windows\System32, application working folder. My system is Windows 7 64 bit. Any ideas?
AnswerRe: Exception PinmemberMike Dorn22-Mar-12 14:02 
GeneralRe: Exception PinmemberEric R Cosner4-Apr-12 5:20 
GeneralRe: Exception PinmemberJay Kotekar11-Oct-13 19:11 
QuestionAny VB.Net Solution??? Pinmemberjpope6-Aug-11 13:09 
AnswerRe: Any VB.Net Solution??? PinmemberguideX2-Jan-12 9:16 
QuestionTrying to make this work in my C# web site PinmemberDan Hewins8-Jun-11 8:19 
QuestionConvertion PinmemberJohnnyOddSmile13-Apr-11 2:02 
GeneralEncoding multiple Mp3 at the same time. [Will this trick work?] Pinmemberhujanais8-Mar-11 4:17 
GeneralRe: Encoding multiple Mp3 at the same time. [Will this trick work?] PinmemberSike Mullivan24-Mar-11 8:28 
GeneralRe: Encoding multiple Mp3 at the same time. [Will this trick work?] Pinmemberhujanais4-Jul-11 16:59 
QuestionHow can Configure Track Name, Author and Other Meta Data PinmemberDileep.M13-Jun-10 20:53 
QuestionCan Mp3 be converted to Wav using this PinmemberAnoop Unnikrishnan2-Nov-09 22:06 
AnswerRe: Can Mp3 be converted to Wav using this Pinmemberscasbyte19-Dec-10 20:00 
GeneralRe: Can Mp3 be converted to Wav using this PinmemberVovikDRG`21-Nov-11 4:43 
QuestionDecode Pinmemberxgelectro13-May-09 8:31 
General-mp3input parameter Pinmemberzzachattack3-Feb-09 14:18 
Generalmp3 compressor Pinmemberliviae24-Dec-08 2:55 
GeneralInvalid Wave File or Format Pinmembercdebel18-Jan-08 15:05 
GeneralRe: Invalid Wave File or Format PinmemberJon8-May-08 23:25 
GeneralRe: Invalid Wave File or Format Pinmemberelectriac23-Dec-09 8:29 
Generalif do you have sample source with mfc, give me source please! PinmemberHoiN\ kyung-joon30-Oct-07 3:54 
GeneralCompiling using .NET 2005 PinmemberLife as a Coder5-Aug-07 23:19 
GeneralRe: Compiling using .NET 2005 PinmemberJuraj Borza26-Aug-07 20:05 
GeneralRe: Compiling using .NET 2005 Pinmembercdebel18-Jan-08 15:03 
GeneralLame_encDll.beInitStream failed with the error code 4294967295 when using VBR PinmemberFink Christoph13-Apr-07 10:28 
GeneralRe: Lame_encDll.beInitStream failed with the error code 4294967295 when using VBR Pinmembertrylon10-Sep-07 1:34 
GeneralRe: Lame_encDll.beInitStream failed with the error code 4294967295 when using VBR Pinmemberbkaratte17-Nov-10 4:27 
GeneralRe: Lame_encDll.beInitStream failed with the error code 4294967295 when using VBR Pinmembersiddhe12-Apr-13 13:00 
GeneralErrors after conversion to VS 2005 solution Pinmemberhswear321-Feb-07 15:53 
QuestionPermission Pinmemberacosano10-Jan-07 4:33 
GeneralWell done Pinmembermartin.susil14-Nov-06 10:09 
GeneralBE_CONFIG arguments Pinmemberdabneyr27-Jul-06 22:19 
GeneralGreat stuff! PinmemberIPC20006-Jun-06 3:39 
GeneralMP3 Reader! PinmemberMarc Schneider3-May-06 0:34 
GeneralProblems extending Structure in C# PinmemberMark Johnson13-Mar-06 22:23 
GeneralChange samplerate Pinmemberrobertpnl27-Feb-06 8:16 
AnswerRe: Change samplerate PinmemberIdael Cardoso10-Mar-06 6:52 
QuestionRe: Change samplerate Pinmembersee7a3-Feb-07 8:26 
GeneralC Sharp MP3 Compressor on pocket pc Pinmembermattmematty9-Oct-05 11:18 
GeneralRe: C Sharp MP3 Compressor on pocket pc PinmemberIdael Cardoso28-Nov-05 10:20 
GeneralResampling Pinmemberlos2k118-Aug-05 8:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 14 Jul 2004
Article Copyright 2004 by Idael Cardoso
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid