#Region " mp3gain Helper "
' [ mp3gain Helper ]
'
' // By Elektro H@cker
'
'
' Instructions:
'
' 1. Add the "mp3gain.exe" into the project.
'
'
' Examples :
'
' MsgBox(mp3gain.Is_Avaliable) ' Checks if mp3gain executable is avaliable.
'
' MsgBox(mp3gain.File_Has_MP3Gain_Tag("File.mp3")) ' Checks if file contains mp3gain APE tag
'
' mp3gain.Set_Gain("File.mp3", 95) ' Set the db Gain of file to 95 db (In a scale of "0/100" db)
' mp3gain.Set_Gain("File.mp3", 95, True) ' Set the db Gain of file to -95 db and preserve the datetime of file.
'
' mp3gain.Apply_Gain("File.mp3", +5) ' Apply a change of +5 db in the curent gain of file.
' mp3gain.Apply_Gain("File.mp3", -5) ' Apply a change of -5 db in the curent gain of file.
'
' mp3gain.Apply_Channel_Gain("File.mp3", mp3gain.Channels.Left, +10) ' Apply a change of +10 db in the curent Left channel gain of file.
' mp3gain.Apply_Channel_Gain("File.mp3", mp3gain.Channels.Right, -10) ' Apply a change of -10 db in the curent Right channel gain of file.
'
' mp3gain.Undo_Gain("File.mp3") ' Undo all MP3Gain db changes made in file.
'
'
' ------
' EVENTS
' ------
' Public WithEvents mp3gain As New mp3gain
'
' Sub mp3gain_Progress(Progress As Integer, e As EventArgs) Handles mp3gain.PercentDone
' ProgressBar1.Maximum = 100
' ProgressBar1.Value = Progress
' End Sub
'
' Sub mp3gain_Exited(Message As String, e As EventArgs) Handles mp3gain.Exited
' ProgressBar1.Value = 0
' MessageBox.Show(Message)
' End Sub
Public Class mp3gain
#Region " CommandLine parametter legend "
' MP3Gain 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 tag info).
' /s c - Check stored tag info.
#End Region
#Region " Variables "
' <summary>
' Gets or sets the mp3gain.exe executable path.
' </summary>
Public Shared mp3gain_Location As String = "c:\mp3gain.exe"
' Stores the MP3Gain process ErrorOutput.
Private Shared ErrorOutput As String = String.Empty
' Stores the MP3Gain process StandardOutput.
Private Shared StandardOutput As String = String.Empty ' Is not needed
' Sets a Flag to know if file has MP3Gain APE tag.
Private Shared HasTag As Boolean = False
#End Region
#Region " Enumerations "
Enum Channels As Short
Left = 0 ' /l 0
Right = 1 ' /l 1
End Enum
#End Region
#Region " Events "
' <summary>
' Event raised when process progress changes.
' </summary>
Public Shared Event PercentDone As EventHandler(Of PercentDoneEventArgs)
Public Class PercentDoneEventArgs : Inherits EventArgs
Public Property Progress As Integer
End Class
' <summary>
' Event raised when MP3Gain process has exited.
' </summary>
Public Shared Event Exited As EventHandler(Of ExitedEventArgs)
Public Class ExitedEventArgs : Inherits EventArgs
Public Property Message As String
End Class
#End Region
#Region " Processes Info "
Private Shared Process_TagCheck As New Process() With { _
.StartInfo = New ProcessStartInfo With { _
.CreateNoWindow = True, _
.UseShellExecute = False, _
.RedirectStandardError = False, _
.RedirectStandardOutput = True _
}}
Private Shared Process_For_Tag As New Process() With { _
.StartInfo = New ProcessStartInfo With { _
.CreateNoWindow = True, _
.UseShellExecute = False, _
.RedirectStandardError = False, _
.RedirectStandardOutput = True _
}}
Private Shared Process_For_NonTag As New Process() With { _
.StartInfo = New ProcessStartInfo With { _
.CreateNoWindow = True, _
.UseShellExecute = False, _
.RedirectStandardError = True, _
.RedirectStandardOutput = True _
}}
#End Region
#Region " Miscellaneous functions "
' <summary>
' Checks if mp3gain.exe process is avaliable.
' </summary>
Public Shared Function Is_Avaliable() As Boolean
Return IO.
File.
Exists(mp3gain_Location
) End Function
' Checks if a file exist.
Private Shared Sub CheckFileExists
(ByVal File As String)
' Throw New Exception("File doesn't exist: " & File)
MessageBox.
Show("File doesn't exist: " & File,
"MP3Gain", MessageBoxButtons.
OK, MessageBoxIcon.
Error) End If
End Sub
#End Region
#Region " Gain Procedures "
' <summary>
' Checks if mp3gain APE tag exists in file.
' </summary>
Public Shared Function File_Has_MP3Gain_Tag(ByVal MP3_File As String) As Boolean
CheckFileExists(MP3_File)
Process_TagCheck.StartInfo.FileName = mp3gain_Location
Process_TagCheck.StartInfo.Arguments = String.Format("/s c ""{0}""", MP3_File)
Process_TagCheck.Start()
Process_TagCheck.WaitForExit()
Return Process_TagCheck.StandardOutput.ReadToEnd.Trim.Split(Environment.NewLine).Count - 1
' Process_TagCheck.Close()
End Function
' <summary>
' Set global db Gain in file.
' </summary>
Public Shared Sub Set_Gain(ByVal MP3_File As String, _
ByVal Gain As Integer, _
Optional ByVal Preserve_Datestamp As Boolean = True)
Run_MP3Gain(MP3_File, 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)))
End Sub
' <summary>
' Apply db Gain change in file.
' </summary>
Public Shared Sub Apply_Gain(ByVal MP3_File As String, _
ByVal Gain As Integer, _
Optional ByVal Preserve_Datestamp As Boolean = True)
Run_MP3Gain(MP3_File, String.Format("/c /e /r /t {1} /g {2} ""{0}""", _
MP3_File, _
If(Preserve_Datestamp, "/p", ""), _
Gain))
End Sub
' <summary>
' Apply db Gain change of desired channel in file.
' Only works for Stereo MP3 files.
' </summary>
Public Shared Sub Apply_Channel_Gain(ByVal MP3_File As String, _
ByVal Channel As Channels, _
ByVal Gain As Integer, _
Optional ByVal Preserve_Datestamp As Boolean = True)
Run_MP3Gain(MP3_File, String.Format("/c /e /r /l {2} {3} ""{0}""", _
MP3_File, _
If(Preserve_Datestamp, "/p", ""), _
If(Channel = Channels.Left, 0, 1), _
Gain))
End Sub
' <summary>
' Undo all MP3Gain db changes made in file (based on stored tag info).
' </summary>
Public Shared Sub Undo_Gain(ByVal MP3_File As String, _
Optional ByVal Preserve_Datestamp As Boolean = True)
Run_MP3Gain(MP3_File, String.Format("/c /t {1} /u ""{0}""", _
MP3_File, _
If(Preserve_Datestamp, "/p", "")))
End Sub
#End Region
#Region " Run MP3Gain Procedures "
Private Shared Sub Run_MP3Gain(ByVal MP3_File As String, ByVal Parametters As String)
CheckFileExists(MP3_File)
HasTag = File_Has_MP3Gain_Tag(MP3_File)
Process_For_Tag.StartInfo.FileName = mp3gain_Location
Process_For_Tag.StartInfo.Arguments = Parametters
Process_For_NonTag.StartInfo.FileName = mp3gain_Location
Process_For_NonTag.StartInfo.Arguments = Parametters
If HasTag Then
Run_MP3Gain_For_Tag()
Else
Run_MP3Gain_For_NonTag()
End If
End Sub
Private Shared Sub Run_MP3Gain_For_Tag()
Process_For_Tag.Start()
Process_For_Tag.WaitForExit()
RaiseEvent Exited(Process_For_Tag.StandardOutput.ReadToEnd.Trim.Split(Environment.NewLine).LastOrDefault, Nothing)
StandardOutput = Nothing
' Process_For_Tag.Close()
End Sub
Private Shared Sub Run_MP3Gain_For_NonTag()
Process_For_NonTag.Start()
While Not Process_For_NonTag.HasExited
Try
ErrorOutput = Process_For_NonTag.StandardError.ReadLine.Trim.Split("%").First
If CInt(ErrorOutput) < 101 Then
RaiseEvent PercentDone(ErrorOutput, Nothing)
End If
Catch : End Try
End While
StandardOutput = Process_For_NonTag.StandardOutput.ReadToEnd.Trim.Split(Environment.NewLine).Last
RaiseEvent Exited(StandardOutput, Nothing)
ErrorOutput = Nothing
StandardOutput = Nothing
' Process_For_Tag.Close()
End Sub
#End Region
End Class
#End Region