.NET MP3
Written by Mike James   
Wednesday, 23 February 2011
Article Index
.NET MP3
Running LAME

Running the LAME MP3 encoder under the control of .NET isn't difficult and it provides a really good example of controlling an external .EXE.

 

The idea of using runtime components predates COM, ActiveX and managed code. The concept of filters, standard executables that can be linked together by data pipes is an old one. It never really worked well because the degree of interaction and control one filter could exert over another was generally minimal.

However the idea of using some sort of computational engine and a front end is still attractive when the task in hand is fairly simple. If the computational engine is a .EXE application then it is easy to produce and can be easily tested using the command line.

The problems start when you try to automate the running of such an engine. You have to mimic a user pressing keys via facilities such as Shellm, SendKeys and AppActivate and so on.

These generally don’t work very well due to lack of control over how the application is run and the difficulty of dealing with any unexpected behaviour of the application that is being controlled.

For example, if the application crashes it doesn’t inform the front end that launched it. If you have tried to use any of these facilities you will know what the pitfalls are.

What is really needed is some way to take a .EXE application and run it in a much more controlled way – to put it in a box. From looking at the documentation you might conclude that .NET had little to offer that is new in this area.

It does have all of the commands familiar to VB programmers within the Visual Basic namespace - they just don’t do any better than their VB 6 equivalents. However the good news is that .NET does have the facilities to let you run and keep control of any application you care to work with – but it is in the Diagnostics namespace so you might well miss it.

LAME

As an example of how to “box” a standard .EXE or application engine the LAME MP3 encoder is ideal. It is an open source MP3 encoder in the form of a .EXE file that accepts command line parameters and will convert wav files to mp3. The only problem with it is its name – why do so many open source projects have embarrassingly silly names.

The main LAME web site is:

http://lame.sourceforge.net/

but it only supplies source code that has to be compiled. If you want a pre-compiled version then look at the links page at the LAME website or try:

http://www.rarewares.org/mp3.html

All you need is a copy of Lame.exe in the project directory but you will also find some limited documentation included with the distribution.

Create the project

Once you have the LAME encoder installed, start a new C# Windows project (you can achieve the same results using VB .NET or C++) and add three buttons – Browse, Encode and Exit -  a textbox and a multi-line textbox. Enable scroll bars on the multi-lne textbox.

You can create a more interesting user interface later.

The first command button, Browse, simply allows the user to browse to a suitable wav file to encode. This is straightforward C# and Framework code:

private void button1_Click(object sender,
 EventArgs e)
{
OpenFileDialog Dialog1 =
new OpenFileDialog();
Dialog1.InitialDirectory = "c:\\";
Dialog1.Filter =
"Wav files |*.wav|All files (*.*)|*.*";
Dialog1.FilterIndex = 1;
Dialog1.RestoreDirectory = false;
if (Dialog1.ShowDialog() ==
DialogResult.OK)
{
textBox1.Text=Dialog1.FileName;
};
}

Hopefully at this point the user has selected a suitable audio file that needs to be MP3 encoded. The full file name, including the path is stored in the textbox.

The command line

The user can change the selected file as many times as they want but sooner or later they are going to click the Encode Now button and this starts the conversion to MP3.

There are a lot of options that can be set on the LAME command line but the simplest is to use one of the “presets”:

--preset medium
  •  the resulting bitrate should be in the 150-180kbps range, according to music complexity and the loss of quality shouldn't be noticeable to most people.
--preset standard
  • the resulting bitrate should be in the 170-210kbps range, according to music complexity and again most people will not notice the compression
--preset extreme
  • if you have extremely good hearing and similar equipment, this preset will provide slightly higher quality than the "standard" mode. The resulting bitrate should be in the 200-240kbps range, according to music complexity.

For simplicity we will create a “medium” compression MP3 for which the command line is:

Lame –preset medium inputfile

If you build a program using these arguments you will discover that interpreting the output is a little complicated.

The reason is that LAME uses control characters to create a histogram display in the command console. Once you have seen how output from LAME is collected you can modify the program to display the histogram, or a high resolution version if you want to. For now the simplest solution is to turn off the histogram by adding the –nohist option.

So, the command line we want to use is:

Lame –preset medium --nhist inputfile


Last Updated ( Monday, 18 April 2011 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.