5,427,303 members and growing! (19,456 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Multimedia     Intermediate

VB.NET LameMP3 Shell

By Qualtar

How to use lame.exe, a thread, and get a progress update while encoding a file.
VB, Windows, .NET 1.0, .NET 1.1, .NETVisual Studio, VS.NET2002, VS.NET2003, Dev

Posted: 22 Sep 2004
Updated: 22 Sep 2004
Views: 47,515
Bookmarked: 20 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 3.68 Rating: 3.41 out of 5
1 vote, 8.3%
1
2 votes, 16.7%
2
0 votes, 0.0%
3
4 votes, 33.3%
4
5 votes, 41.7%
5

Sample Image - LameShell.gif

Introduction

The idea for LameShell came from my wish to be able to resample MP3 files in VB.NET. The goal was: "Whatever valid file goes in, out comes a 32kbit encoded MP3". I started looking around and I really didn’t like the solutions I found. You either had to deal with Interop or unmanaged code, it would cost, the interfaces where out of date or they where just too much. So I created this wrapper that allows me to have all sorts of status updates while Lame encodes the MP3. All you have to do in the application is supply the source file, the destination file and options you need.

Background

While there are some .NET wrappers around the LAME encoder lame_enc.dll, I decided to go a different route – a simple wrapper around lame.exe. There are so many options in there you could expose easily if you wanted to, or just directly type into your source if you have a simple goal – like I had. You can find out more about LAME here and you will need lame.exe to use this class. (lame.exe is not included with the source code)

Using the code

Using the code is very simple; you supply the filenames and options. Additionally, you can supply the path to lame.exe if it isn't in your application directory.

Dim WithEvents _lameShell As New LameShell
[...]
_lameShell.InFile = Application.StartupPath & "\test.mp3"
_lameShell.OutFile = Application.StartupPath & "\testOut.mp3"
_lameShell.Options = "-b 32"
_lameShell.Start()

Add some event handlers:

Private Sub _lameShell_Done() Handles _lameShell.Done
    lblFeedback.Text = "Done"
End Sub

Private Sub _lameShell_Canceled() Handles _lameShell.Canceled
    lblFeedback.Text = "Canceled/Error"
    pBar.Value = 0
End Sub

Private Sub _lameShell_Progress(ByRef Progress As LameProgress) _
                                      Handles _lameShell.Progress

    If pBar.Maximum <> Progress.FrameMax Then
        pBar.Value = 0
        pBar.Maximum = Progress.FrameMax
    Else
        pBar.Value = Progress.FrameCurrent
    End If
    lblFeedback.Text = Progress.PercentDone & "%" & " ETA:" & Progress.ETA

End Sub

That's all there is to it.

How it Works

Essentially, you create a new System.Diagnostics.Process with the needed ProcessStartInfo.

Private _startInfo As New ProcessStartInfo
[...]
'need this stuff to hide the window and redirect the output

_startInfo.FileName = "lame.exe"
_startInfo.UseShellExecute = False
_startInfo.RedirectStandardOutput = True
_startInfo.RedirectStandardError = True
_startInfo.CreateNoWindow = True

And you create a Reader in a different thread that keeps reading the output of lame.exe.

Private _lameThread As System.Threading.Thread
[...]
_lameThread = New System.Threading.Thread(AddressOf LameReader)
_lameThread.IsBackground = True
_lameThread.Name = "LameReader"
_lameThread.Start()
[...]

Private Sub LameReader()
    Dim oneLine As String
    _lameProcess.Start()
    
    oneLine = _lameProcess.StandardError.ReadLine()
    While Not oneLine Is Nothing
        '[...] Analyze Line

        
        'this call is blocking... thats why we are using a thread

        oneLine = _lameProcess.StandardError.ReadLine()
    End While
End Sub

Points of Interest

One thing I haven’t figured out is why the thread that is launched to read the info from lame.exe does not throw a Threading.ThreadAbortException when the application exits. According to MSDN, it should when IsBackround = true. For now, you are safe if you make sure to call LameShell.Cancel() if your application is closing.

Make sure not to depend on the Closing or Closed event of a form (like in the example) if you use Application.Exit anywhere. Those won’t fire if you use Application.Exit. That's why I would have liked the Threading.ThreadAbortException to fire to clean up on all circumstances.

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

Qualtar



Location: Germany Germany

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 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionC# version?memberitjavelin9:59 16 Nov '07  
GeneralAnd Tag informations?memberGTClove9:11 3 Aug '07  
GeneralVB6... is it possible?memberThe Rockas15:53 1 Dec '06  
GeneralRe: VB6... is it possible?memberVorTechS22:16 7 Jan '07  
GeneralHelp launching various instances at a timemembermikeperaltag20:02 13 Nov '06  
GeneralAdvice...membersnort3:06 4 Jul '06  
GeneralRe: Advice...membervnesteem19:30 9 Jul '06  
GeneralAny suggestion on how to record wav / mp3 files ?memberParik Advani4:00 12 Jun '06  
GeneralRe: Any suggestion on how to record wav / mp3 files ?memberScRaT [ESI]4:59 16 Aug '06  
GeneralLame issuemembereharris11:43 16 Dec '05  
GeneralVB2005memberMikeBro11:38 14 Nov '05  
GeneralRe: VB2005memberBrett Clark7:44 18 Jul '06  
GeneralDoesn't workmemberxANA99314:30 5 Apr '05  
GeneralBugfix: LamePath not workingmemberBrian Low17:13 28 Feb '05  
GeneralRe: Bugfix: LamePath not workingmembermp4city2:34 21 Dec '05  

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

PermaLink | Privacy | Terms of Use
Last Updated: 22 Sep 2004
Editor: Smitha Vijayan
Copyright 2004 by Qualtar
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project