Click here to Skip to main content
6,596,602 members and growing! (19,733 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.1VS.NET2003, Dev
Posted:22 Sep 2004
Views:62,275
Bookmarked:29 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 3.68 Rating: 3.41 out of 5
1 vote, 8.3%
1
2 votes, 16.7%
2

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


Member

Location: Germany Germany

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralFor Visual Basic 2008 Pinmembertechnobase10:10 18 Oct '09  
GeneralYou legend! No hassle mp3 convertor! Pinmemberseanclancy12:39 14 Jun '09  
QuestionMP3 to wav conversion? Pinmemberrspercy607:48 8 Jun '09  
GeneralSinple and useful. ¡Thank you very much! PinmemberJesusAbizanda23:13 8 Jan '09  
QuestionGetting Started PinmemberPaul Rothenheber11:48 24 Oct '08  
QuestionC# version? Pinmemberitjavelin9:59 16 Nov '07  
GeneralAnd Tag informations? PinmemberGTClove9:11 3 Aug '07  
GeneralVB6... is it possible? PinmemberThe Rockas15:53 1 Dec '06  
GeneralRe: VB6... is it possible? PinmemberVorTechS22:16 7 Jan '07  
GeneralHelp launching various instances at a time Pinmembermikeperaltag20:02 13 Nov '06  
GeneralAdvice... Pinmembersnort3:06 4 Jul '06  
GeneralRe: Advice... Pinmembervnesteem19:30 9 Jul '06  
GeneralAny suggestion on how to record wav / mp3 files ? PinmemberParik Advani4:00 12 Jun '06  
GeneralRe: Any suggestion on how to record wav / mp3 files ? PinmemberScRaT [ESI]4:59 16 Aug '06  
QuestionRe: Any suggestion on how to record wav / mp3 files ? PinmemberMember 43963863:34 1 Sep '08  
GeneralLame issue Pinmembereharris11:43 16 Dec '05  
GeneralVB2005 PinmemberMikeBro11:38 14 Nov '05  
GeneralRe: VB2005 PinmemberBrett Clark7:44 18 Jul '06  
GeneralDoesn't work PinmemberxANA99314:30 5 Apr '05  
GeneralBugfix: LamePath not working PinmemberBrian Low17:13 28 Feb '05  
GeneralRe: Bugfix: LamePath not working Pinmembermp4city2: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-2009
Web21 | Advertise on the Code Project