Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
- <CodeSnippet Format="1.0.0">
- <Header>
- <SnippetTypes>
- <SnippetType>Module</SnippetType>
- </SnippetTypes>
- <Title>
- [mp3gain] Helper
- </Title>
- <Author>Elektro H@cker</Author>
- <Description>
- Ejemplos de uso de la aplicación mp3gain.
- </Description>
- <HelpUrl>
- </HelpUrl>
- <Shortcut>
- </Shortcut>
- </Header>
- <Snippet>
- <Declarations>
- <Literal Editable="true">
- <ID>aaaaaaaaa</ID>
- <ToolTip>sfsdf</ToolTip>
- <Default>
- </Default>
- <Function>sdfsdf</Function>
- </Literal>
- </Declarations>
- <Code Language="vb"><![CDATA[
- ' [ mp3gain Helper ]
- '
- ' // By Elektro H@cker
- '
- ' Instructions:
- ' 1. Add the "mp3gain.exe" into the project.
- #Region " mp3gain Helper "
- #Region " Usage Examples "
- ' Public Class Form1
- '
- ' Private WithEvents _mp3gain As New mp3gain _
- ' With {.mp3gain_location = "C:\windows\system32\mp3gain.exe",
- ' .CheckFileExist = True}
- '
- ' Private Sub Test() Handles MyBase.Shown
- '
- ' ' Checks if mp3gain executable is avaliable.
- ' MsgBox(_mp3gain.Is_Avaliable())
- '
- ' ' Checks if file contains APEv2 mp3gain tag
- ' MsgBox(_mp3gain.File_Has_MP3Gain_Tag("C:\File.mp3"))
- '
- ' ' Set the global volume Gain of file to "89" db (In a scale of "0-100"),
- ' ' and preserve the datetime of file.
- ' _mp3gain.Set_Gain("C:\File.mp3", 89, True)
- '
- ' ' Apply a volume change of +5 db,
- ' ' in the curent global volume gain of file.
- ' _mp3gain.Apply_Gain("C:\File.mp3", +5)
- '
- ' ' Apply a volume change of -5 db,
- ' ' in the curent global volume gain of file.
- ' _mp3gain.Apply_Gain("C:\File.mp3", -5)
- '
- ' ' Apply a volume change of +10 db,
- ' ' in the curent volume gain of the Left channel of an Stereo file.
- ' _mp3gain.Apply_Channel_Gain("C:\File.mp3", mp3gain.Channel.Left, +10)
- '
- ' ' Apply a volume change of -10 db,
- ' ' in the curent volume gain of the Right channel of an Stereo file.
- ' _mp3gain.Apply_Channel_Gain("C:\File.mp3", mp3gain.Channel.Right, -10)
- '
- ' ' Undos all volume gain changes made in file.
- ' _mp3gain.Undo_Gain("C:\File.mp3")
- '
- ' End Sub
- '
- ' ' mp3gain [Started]
- ' Private Sub mp3gain_Started(ByVal sender As Process, ByVal e As mp3gain.StartedEventArgs) _
- ' Handles _mp3gain.Started
- '
- ' ProgressBar1.Value = ProgressBar1.Minimum
- '
- ' Dim sb As New System.Text.StringBuilder
- '
- ' sb.AppendLine(String.Format("Started an ""{0}"" operation", e.Operation.ToString))
- ' sb.AppendLine(String.Format("Input file is: ""{0}""", e.File))
- ' sb.AppendLine(String.Format("mp3gain process PID is: ""{0}""", CStr(sender.Id)))
- '
- ' MessageBox.Show(sb.ToString, "mp3gain", MessageBoxButtons.OK, MessageBoxIcon.Information)
- '
- ' End Sub
- '
- ' ' mp3gain [Exited]
- ' Private Sub mp3gain_Exited(ByVal sender As Process, ByVal e As mp3gain.ExitedEventArgs) _
- ' Handles _mp3gain.Exited
- '
- ' Dim sb As New System.Text.StringBuilder
- '
- ' If e.Operation <> mp3gain.Operation.Check_Tag Then
- '
- ' sb.AppendLine(String.Format("Finished an ""{0}"" operation", e.Operation.ToString))
- ' sb.AppendLine(String.Format("Input file is: ""{0}""", e.File))
- ' sb.AppendLine(String.Format("mp3gain process PID is: {0}", CStr(sender.Id)))
- '
- ' If Not String.IsNullOrEmpty(e.InfoMessage) Then
- ' sb.AppendLine(String.Format("Operation Information: {0}", e.InfoMessage))
- ' End If
- '
- ' If Not String.IsNullOrEmpty(e.ErrorMessage) Then
- ' sb.AppendLine(String.Format("Error Information: {0}", e.ErrorMessage))
- ' End If
- '
- ' If e.db <> 0 Then
- ' sb.AppendLine(String.Format("Volume gain change: {0}", CStr(e.db)))
- ' End If
- '
- ' MessageBox.Show(sb.ToString, "mp3gain", MessageBoxButtons.OK, MessageBoxIcon.Information)
- '
- ' End If
- '
- ' End Sub
- '
- ' ' mp3gain [Progress]
- ' Sub mp3gain_Progress(sender As Process, e As mp3gain.ProgressEventArgs) _
- ' Handles _mp3gain.Progress
- '
- ' ProgressBar1.Value = e.Percent
- '
- ' End Sub
- '
- ' End Class
- #End Region
- Public Class mp3gain : Implements IDisposable
- #Region " CommandLine parametter legend "
- ' /c - Ignore clipping warning when applying gain.
- ' /d - Set global gain.
- ' /e - Skip Album analysis, even if multiple files listed.
- ' /g - apply gain
- ' /p - Preserve original file timestamp.
- ' /r - apply Track gain automatically (all files set to equal loudness)
- ' /t - Writes modified data to temp file, then deletes original instead of modifying bytes in original file.
- ' /u - Undo changes made (based on stored APEv2 mp3gain tag info).
- ' /s c - Check stored APEv2 mp3gain tag info.
- #End Region
- #Region " Variables, Properties, Enumerations "
- ''' <summary>
- ''' Gets or sets the mp3gain.exe executable path.
- ''' </summary>
- Public Property mp3gain_location As String = ".\mp3gain.exe"
- ''' <summary>
- ''' Indicates if should check that the file exist before realize an operation.
- ''' If True, an exception would be launched if file does not exist.
- ''' </summary>
- Public Property CheckFileExist As Boolean = False
- ''' <summary>
- ''' Sets a Flag to indicate if file has APEv2 mp3gain tag or not.
- ''' </summary>
- Private HasTag As Boolean = False
- ''' <summary>
- ''' Stores the StandardOutput.
- ''' </summary>
- Private Output As String() = Nothing
- ''' <summary>
- ''' Stores an information message of the realized operation (if any).
- ''' </summary>
- Private InfoMessage As String = String.Empty
- ''' <summary>
- ''' Stores an error message of the realized operation (if any).
- ''' </summary>
- Private ErrorMessage As String = String.Empty
- ''' <summary>
- ''' Stores the volume gain level change applied to file (if any).
- ''' </summary>
- Private db As Integer = 0
- ''' <summary>
- ''' Gets some information about the file.
- ''' </summary>
- Private db_RegEx As New System.Text.RegularExpressions.Regex("Applying.+change of (.*) to",
- System.Text.RegularExpressions.RegexOptions.None)
- ''' <summary>
- ''' Process to realize an operation,
- ''' for files that already contains APEv2 mp3gain tag.
- ''' Also is used to realize a single TagCheck operation.
- ''' </summary>
- Private Process_For_Tag As Process =
- New Process With {.StartInfo =
- New ProcessStartInfo With {
- .CreateNoWindow = True,
- .UseShellExecute = False,
- .RedirectStandardError = False,
- .RedirectStandardOutput = True
- }
- }
- ''' <summary>
- ''' Process to realize an operation,
- ''' for files that does not contains mp3gain Tag.
- ''' </summary>
- Private Process_For_NonTag As Process =
- New Process With {.StartInfo =
- New ProcessStartInfo With {
- .CreateNoWindow = True,
- .UseShellExecute = False,
- .RedirectStandardError = True,
- .RedirectStandardOutput = True
- }
- }
- ''' <summary>
- ''' Stores the StartedEventArgs Arguments.
- ''' </summary>
- Private StartedArgs As New StartedEventArgs
- ''' <summary>
- ''' Stores the ExitedEventArgs Arguments.
- ''' </summary>
- Private ExitedArgs As New ExitedEventArgs
- ''' <summary>
- ''' Stores the ProgressEventArgs Arguments.
- ''' </summary>
- Private ProgressArgs As New ProgressEventArgs
- ''' <summary>
- ''' File Stereo Channel.
- ''' </summary>
- Public Enum Channel As Short
- Left = 0 ' /l 0
- Right = 1 ' /l 1
- End Enum
- ''' <summary>
- ''' MP3Gain Type Of Operation.
- ''' </summary>
- Public Enum Operation
- Check_Tag = 0
- Apply_Gain = 1
- Apply_Channel_Gain = 2
- Set_Gain = 3
- Undo_Gain = 4
- End Enum
- #End Region
- #Region " Events "
- ''' <summary>
- ''' Event raised when the process has started.
- ''' </summary>
- Public Event Started As EventHandler(Of StartedEventArgs)
- Public Class StartedEventArgs : Inherits EventArgs
- ''' <summary>
- ''' Gets the file that was passed as argument to the process.
- ''' </summary>
- Public Property File As String
- ''' <summary>
- ''' Gets the type of operation to realize.
- ''' </summary>
- Public Property Operation As Operation
- End Class
- ''' <summary>
- ''' Event raised when the process has exited.
- ''' </summary>
- Public Event Exited As EventHandler(Of ExitedEventArgs)
- Public Class ExitedEventArgs : Inherits EventArgs
- ''' <summary>
- ''' Gets the file that was passed as argument to the process.
- ''' </summary>
- Public Property File As String
- ''' <summary>
- ''' Gets the type of operation to realize.
- ''' </summary>
- Public Property Operation As Operation
- ''' <summary>
- ''' Gets the information message of the realized operation (if any).
- ''' </summary>
- Public Property InfoMessage As String
- ''' <summary>
- ''' Gets the error message of the realized operation (if any).
- ''' </summary>
- Public Property ErrorMessage As String
- ''' <summary>
- ''' Gets the volume gain level change applied to file (if any).
- ''' </summary>
- Public Property db As Integer
- End Class
- ''' <summary>
- ''' Event raised when the process progress changes.
- ''' </summary>
- Public Event Progress As EventHandler(Of ProgressEventArgs)
- Public Class ProgressEventArgs : Inherits EventArgs
- ''' <summary>
- ''' Gets the process operation percent done.
- ''' </summary>
- Public Property Percent As Integer
- End Class
- #End Region
- #Region " MP3Gain Procedures "
- ''' <summary>
- ''' Checks if mp3gain.exe process is avaliable.
- ''' </summary>
- Public Function Is_Avaliable() As Boolean
- Return IO.File.Exists(Me.mp3gain_location)
- End Function
- ''' <summary>
- ''' Checks if APEv2 mp3gain tag exists in file.
- ''' </summary>
- Public Function File_Has_MP3Gain_Tag(ByVal MP3_File As String) As Boolean
- DisposedCheck()
- Run_MP3Gain(MP3_File,
- Operation.Check_Tag,
- String.Format("/s c ""{0}""", MP3_File),
- True)
- Return HasTag
- End Function
- ''' <summary>
- ''' Set the global volume gain of file.
- ''' </summary>
- Public Sub Set_Gain(ByVal MP3_File As String,
- ByVal Gain As Integer,
- Optional ByVal Preserve_Datestamp As Boolean = True)
- DisposedCheck()
- File_Has_MP3Gain_Tag(MP3_File)
- Run_MP3Gain(MP3_File,
- Operation.Set_Gain,
- String.Format("/c /e /r /t {1} /d {2} ""{0}""",
- MP3_File,
- If(Preserve_Datestamp, "/p", ""),
- If(Gain < 0, Gain + 89.0, Gain - 89.0)),
- False)
- End Sub
- ''' <summary>
- ''' Apply a volume gain change to file.
- ''' </summary>
- Public Sub Apply_Gain(ByVal MP3_File As String,
- ByVal Gain As Integer,
- Optional ByVal Preserve_Datestamp As Boolean = True)
- DisposedCheck()
- File_Has_MP3Gain_Tag(MP3_File)
- Run_MP3Gain(MP3_File,
- Operation.Apply_Gain,
- String.Format("/c /e /r /t {1} /g {2} ""{0}""",
- MP3_File,
- If(Preserve_Datestamp, "/p", ""),
- Gain),
- False)
- End Sub
- ''' <summary>
- ''' Apply a volume gain change to file only in left or right channel.
- ''' Only works for Stereo MP3 files.
- ''' </summary>
- Public Sub Apply_Channel_Gain(ByVal MP3_File As String,
- ByVal Channel As Channel,
- ByVal Gain As Integer,
- Optional ByVal Preserve_Datestamp As Boolean = True)
- DisposedCheck()
- File_Has_MP3Gain_Tag(MP3_File)
- Run_MP3Gain(MP3_File,
- Operation.Apply_Channel_Gain,
- String.Format("/c /e /r {1} /l {2} {3} ""{0}""",
- MP3_File,
- If(Preserve_Datestamp, "/p", ""),
- If(Channel = Channel.Left, 0, 1),
- Gain),
- False)
- End Sub
- ''' <summary>
- ''' Undos all mp3gain volume changes made in a file,
- ''' based on stored APEv2 mp3gain tag info.
- ''' </summary>
- Public Sub Undo_Gain(ByVal MP3_File As String,
- Optional ByVal Preserve_Datestamp As Boolean = True)
- DisposedCheck()
- File_Has_MP3Gain_Tag(MP3_File)
- Run_MP3Gain(MP3_File,
- Operation.Undo_Gain,
- String.Format("/c /t {1} /u ""{0}""",
- MP3_File,
- If(Preserve_Datestamp, "/p", "")),
- False)
- End Sub
- #End Region
- #Region " Run Procedures "
- ''' <summary>
- ''' Run MP3Gain process.
- ''' </summary>
- Private Sub Run_MP3Gain(ByVal MP3_File As String,
- ByVal operation As Operation,
- ByVal Parametters As String,
- ByVal IsCheckTagOperation As Boolean)
- If Me.CheckFileExist Then
- FileExist(MP3_File)
- End If
- With Process_For_Tag.StartInfo
- .FileName = Me.mp3gain_location
- .Arguments = Parametters
- End With
- With Process_For_NonTag.StartInfo
- .FileName = Me.mp3gain_location
- .Arguments = Parametters
- End With
- ' Reset Variables before relaize the operation.
- InfoMessage = Nothing
- ErrorMessage = Nothing
- db = 0
- ' Check if file has APEv2 mp3gain tag or not,
- ' before doing any other operation.
- If IsCheckTagOperation Then
- Run_MP3Gain_For_Tag(Process_For_Tag, MP3_File, operation.Check_Tag, True)
- Exit Sub ' If only would to check the tag then exit from this sub.
- Else ' Else, continue with the operation (Modify volume gain)...
- Select Case HasTag
- Case True
- Run_MP3Gain_For_Tag(Process_For_Tag, MP3_File, operation, False)
- Case False
- Run_MP3Gain_For_NonTag(Process_For_NonTag, MP3_File, operation)
- End Select ' HasTag
- End If ' IsCheckTagOperation
- End Sub
- ''' <summary>
- ''' Runs mp3gain for files that already contains APEv2 mp3gain tag.
- ''' </summary>
- Private Sub Run_MP3Gain_For_Tag(ByVal p As Process,
- ByVal MP3_File As String,
- ByVal operation As Operation,
- ByVal IsTagCheckOperation As Boolean)
- p.Start()
- RaiseEvent_Started(p, MP3_File, operation)
- p.WaitForExit()
- If IsTagCheckOperation Then
- HasTag = CBool(p.StandardOutput.ReadToEnd.Trim.Split(Environment.NewLine).Count - 1)
- End If
- ProgressArgs.Percent = 100
- RaiseEvent Progress(p, ProgressArgs)
- SetMessages(p.StandardOutput.ReadToEnd())
- RaiseEvent_Exited(p,
- MP3_File,
- operation,
- If(IsTagCheckOperation, "File Has Tag?: " & CStr(HasTag), InfoMessage),
- ErrorMessage,
- db)
- ' p.Close()
- End Sub
- ''' <summary>
- ''' Runs mp3gain for files that doesn't contains APEv2 mp3gain tag.
- ''' </summary>
- Private Sub Run_MP3Gain_For_NonTag(ByVal p As Process,
- ByVal MP3_File As String,
- ByVal operation As Operation)
- p.Start()
- RaiseEvent_Started(p, MP3_File, operation)
- Do Until p.HasExited
- Try
- ProgressArgs.Percent = CInt(p.StandardError.ReadLine.Split("%").First.Trim)
- If ProgressArgs.Percent < 101 Then
- RaiseEvent Progress(p, ProgressArgs)
- End If
- Catch
- End Try
- Loop
- ProgressArgs.Percent = 100
- RaiseEvent Progress(p, ProgressArgs)
- SetMessages(p.StandardOutput.ReadToEnd())
- RaiseEvent_Exited(p,
- MP3_File,
- operation,
- InfoMessage,
- ErrorMessage,
- db)
- ' p.Close()
- End Sub
- #End Region
- #Region " Miscellaneous Procedures "
- ''' <summary>
- ''' Checks if a file exists.
- ''' </summary>
- Private Sub FileExist(ByVal File As String)
- If Not IO.File.Exists(File) Then
- Throw New Exception(String.Format("File doesn't exist: ""{0}""", File))
- ' MessageBox.Show(String.Format("File doesn't exist: ""{0}""", File), "mp3gain", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End If
- End Sub
- ''' <summary>
- ''' Raises the Event Started
- ''' </summary>
- Private Sub RaiseEvent_Started(ByVal p As Process,
- ByVal file As String,
- ByVal operation As Operation)
- With StartedArgs
- .File = file
- .Operation = operation
- End With
- RaiseEvent Started(p, StartedArgs)
- End Sub
- ''' <summary>
- ''' Raises the Event Exited
- ''' </summary>
- Private Sub RaiseEvent_Exited(ByVal p As Process,
- ByVal file As String,
- ByVal operation As Operation,
- ByVal InfoMessage As String,
- ByVal ErrorMessage As String,
- ByVal db As Integer)
- With ExitedArgs
- .File = file
- .Operation = operation
- .InfoMessage = InfoMessage
- .ErrorMessage = ErrorMessage
- .db = db
- End With
- RaiseEvent Exited(p, ExitedArgs)
- End Sub
- ''' <summary>
- ''' Sets the InfoMessage, ErrorMessage and db variables.
- ''' </summary>
- Private Sub SetMessages(ByVal StandardOutput As String)
- Output = StandardOutput.
- Split(Environment.NewLine).
- Select(Function(line) line.Replace(Environment.NewLine, "").Trim).
- Where(Function(null) Not String.IsNullOrEmpty(null)).ToArray
- For Each line In Output
- Select Case True
- Case line.StartsWith("No changes")
- InfoMessage = "No volume gain changes are necessary."
- Case line.StartsWith("Applying")
- db = db_RegEx.Match(line).Groups(1).Value
- If String.IsNullOrEmpty(InfoMessage) Then
- InfoMessage = line
- End If
- Case line.StartsWith("Can't")
- ErrorMessage = line
- End Select
- Next line
- End Sub
- #End Region
- #Region " IDisposable "
- ''' <summary>
- ''' To detect redundant calls when disposing.
- ''' </summary>
- Private IsDisposed As Boolean = False
- ''' <summary>
- ''' Prevents calls to methods after disposing.
- ''' </summary>
- Private Sub DisposedCheck()
- If Me.IsDisposed Then
- Throw New ObjectDisposedException(Me.GetType().FullName)
- End If
- End Sub
- ''' <summary>
- ''' Disposes the objects generated by this instance.
- ''' </summary>
- Public Sub Dispose() Implements IDisposable.Dispose
- Dispose(True)
- GC.SuppressFinalize(Me)
- End Sub
- ' IDisposable
- Protected Overridable Sub Dispose(IsDisposing As Boolean)
- If Not Me.IsDisposed Then
- If IsDisposing Then
- Process_For_Tag.Dispose()
- Process_For_NonTag.Dispose()
- End If
- End If
- Me.IsDisposed = True
- End Sub
- #End Region
- End Class
- #End Region
- ]]></Code>
- </Snippet>
- </CodeSnippet>
- </CodeSnippets>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement