5,557,174 members and growing! (16,204 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate

C# MP3 Compressor

By Idael Cardoso

A C# MP3 compressor using "lame_enc.dll" from the LAME project.
C#Windows, .NET, .NET 1.0, .NET 1.1, NT4, Win2K, WinXP, Win2003VS.NET2003, Visual Studio, Dev

Posted: 19 Jan 2004
Updated: 13 Jul 2004
Views: 262,570
Bookmarked: 95 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
26 votes for this Article.
Popularity: 5.85 Rating: 4.13 out of 5
2 votes, 7.7%
1
0 votes, 0.0%
2
2 votes, 7.7%
3
5 votes, 19.2%
4
17 votes, 65.4%
5

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

About the Author

Idael Cardoso



Occupation: Web Developer
Location: France France

Other popular Audio and Video articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 83 (Total in Forum: 83) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionOutput Configuration? [modified]memberBerni8622:18 2 Aug '08  
GeneralInvalid Wave File or Formatmembercdebel16:05 18 Jan '08  
GeneralRe: Invalid Wave File or FormatmemberJon0:25 9 May '08  
Generalif do you have sample source with mfc, give me source please!memberHoiN\ kyung-joon4:54 30 Oct '07  
GeneralCompiling using .NET 2005memberLife as a Coder0:19 6 Aug '07  
GeneralRe: Compiling using .NET 2005memberJuraj Borza21:05 26 Aug '07  
GeneralRe: Compiling using .NET 2005membercdebel16:03 18 Jan '08  
GeneralLame_encDll.beInitStream failed with the error code 4294967295 when using VBRmemberFink Christoph11:28 13 Apr '07  
GeneralRe: Lame_encDll.beInitStream failed with the error code 4294967295 when using VBRmembertrylon2:34 10 Sep '07  
GeneralErrors after conversion to VS 2005 solutionmemberhswear316:53 21 Feb '07  
QuestionPermissionmemberacosano5:33 10 Jan '07  
GeneralWell donemembermartin.susil11:09 14 Nov '06  
GeneralBE_CONFIG argumentsmemberdabneyr23:19 27 Jul '06  
GeneralGreat stuff!memberIPC20004:39 6 Jun '06  
GeneralMP3 Reader!memberMarc Schneider1:34 3 May '06  
GeneralProblems extending Structure in C#memberMark Johnson23:23 13 Mar '06  
GeneralChange sampleratememberrobertpnl9:16 27 Feb '06  
AnswerRe: Change sampleratememberIdael Cardoso7:52 10 Mar '06  
QuestionRe: Change sampleratemembersee7a9:26 3 Feb '07  
GeneralC Sharp MP3 Compressor on pocket pcmembermattmematty12:18 9 Oct '05  
GeneralRe: C Sharp MP3 Compressor on pocket pcmemberIdael Cardoso11:20 28 Nov '05  
GeneralResamplingmemberlos2k19:19 18 Aug '05  
GeneralRe: ResamplingmemberIdael Cardoso1:05 4 Sep '05  
GeneralID3 tagsmemberEd Brey2:52 31 Jul '05  
GeneralRe: ID3 tagsmemberIdael Cardoso7:22 7 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Jul 2004
Editor: Smitha Vijayan
Copyright 2004 by Idael Cardoso
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project