title
Products            Buy            Support Forum            Professional            About            Codec Central
 

[CONTRIBUTION] My CoreConverter Helper Class to use CoreConverter in VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pitoloko
    • Mar 2011
    • 38

    [CONTRIBUTION] My CoreConverter Helper Class to use CoreConverter in VB.NET

    I want to share this Generic Class I've did to convert some files in my PC using CoreConverter executable.

    I preffer to use the executable instead dmcScriptingLib because @Spoon saids me that the dll has no way to check for a conversion progress, but in a GUI application this is very important to manage.

    My class has two basic events, one for capture the percent done and the other to return error/standard messages.

    Also I've added support to use some DSP Effects (they are DSP Effects which do not need to parse special parametters to it, I can't manage for example the "move file" plugin because needs to input a location, but if you want predefine the location then you can manage it)

    By the moment I've only integrated the necessary parametters to convert files using mP3 (Lame) (cbr/abr but not vbr), Wav uncompressed, and WindowsMediaAudio 9.2, you can easy extend/improve this Class by adding more specific codec parametters.

    This Class is Free to use, free to share.

    Sure this could help much .NET developer people,
    and maybe authors or moderators would add this sample to the developer help section.

    Thanks for read!

    Code:
        *Region " CoreConverter Helper "
         
         
         
        ' [ CoreConverter Helper ]
        '
        ' // By Elektro H@cker
        '
        '
        ' Instructions:
        '
        ' 1. Add the "CoreConverter.exe" into the project,
        '    together with the dbPoweramp Effects and Codec folders.
        '
        ' Examples :
        '
        ' -------------------
        ' CONVERT FILE TO MP3
        ' -------------------
        ' CoreConverter.Convert_To_MP3("C:\Input.wav", "C:\Output.mp3", _
        '                              CoreConverter.Lame_Bitrate.kbps_320, _
        '                              CoreConverter.Lame_Bitrate_Mode.cbr, _
        '                              CoreConverter.Lame_Profile.SLOW, _
        '                              CoreConverter.Lame_Quality.Q0_Maximum, _
        '                              CoreConverter.Lame_Khz.Same_As_Source, _
        '                              CoreConverter.Lame_Channels.auto, _
        '                              { _
        '                                CoreConverter.DSP_Effects.Delete_Output_File_on_Error, _
        '                                CoreConverter.DSP_Effects.Recycle_Source_File_After_Conversion _
        '                              }, _
        '                              False, _
        '                              CoreConverter.Priority.normal)
        '
        ' -------------------
        ' CONVERT FILE TO WAV
        ' -------------------
        ' CoreConverter.Convert_To_WAV_Uncompressed("C:\Input.mp3", "C:\Output.wav", _
        '                                           CoreConverter.WAV_Uncompressed_Bitrate.Same_As_Source, _
        '                                           CoreConverter.WAV_Uncompressed_Khz.Same_As_Source, _
        '                                           CoreConverter.WAV_Uncompressed_Channels.Same_As_Source, , False)
        '
        ' -------------------
        ' CONVERT FILE TO WMA
        ' -------------------
        ' CoreConverter.Convert_To_WMA("C:\Input.mp3", "C:\Output.wma", _
        '                              CoreConverter.WMA_9_2_BitRates.Kbps_128, _
        '                              CoreConverter.WMA_9_2_Khz.Khz_44100, _
        '                              CoreConverter.WMA_9_2_Channels.stereo, , False)
        '
        ' ------
        ' EVENTS
        ' ------
        ' Public WithEvents Converter As New CoreConverter()
        '
        ' Sub Converter_Progress(Progress As Integer, e As EventArgs) Handles Converter.PercentDone
        '     ProgressBar1.Maximum = 59
        '     ProgressBar1.Step = 1
        '     ProgressBar1.PerformStep()
        ' End Sub
        '
        ' Sub Converter_Message(Message As String, e As EventArgs) Handles Converter.Exited
        '     ProgressBar1.Value = 0
        '     MessageBox.Show(Message)
        ' End Sub
         
         
         
        Public Class CoreConverter : Implements IDisposable
         
        *Region " Variables "
         
           ' <summary>
           ' Gets or sets CoreConverter.exe executable path.
           ' </summary>
           Public Shared CoreConverter_Location As String = ".\CoreConverter.exe"
         
           ' Stores the CoreConverter process progress
           Private Shared CurrentProgress As Integer = 0
         
           ' Stores the CoreConverter process StandarOutput
           Private Shared StandardOutput As String = String.Empty
         
           ' Stores the CoreConverter process ErrorOutput
           Private Shared ErrorOutput As String = String.Empty
         
           ' Stores the next output character
           Private Shared OutputCharacter As Char = Nothing
         
           ' Stores the DSP Effects formatted string
           Private Shared Effects As String = String.Empty
         
        *End Region
         
        *Region " Events "
         
           ' <summary>
           ' Event raised when conversion 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 CoreConverter 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 " Process Info "
         
           ' CoreConverter Process Information.
           Private Shared CoreConverter As New Process() With { _
               .StartInfo = New ProcessStartInfo With { _
               .CreateNoWindow = True, _
               .UseShellExecute = False, _
               .RedirectStandardError = True, _
               .RedirectStandardOutput = True, _
               .StandardErrorEncoding = System.Text.Encoding.Unicode, _
               .StandardOutputEncoding = System.Text.Encoding.Unicode}}
         
        *End Region
         
        *Region " CoreConverter Enumerations "
         
           ' Priority level of CoreConverter.exe
           Enum Priority
               idle
               low
               normal
               high
           End Enum
         
           ' DSP Effects
           Public Enum DSP_Effects
               Delete_Output_File_on_Error ' Delete failed conversion (not deletes source file).
               Delete_Source_File_After_Conversion ' Delete source file after conversion.
               Recycle_Source_File_After_Conversion ' Send source file to recycle bin after conversion.
               Karaoke_Remove_Voice ' Remove voice from file.
               Karaoke_Remove_Instrument ' Remove instruments from file.
               Reverse ' Reverse complete audio file.
               Write_Silence ' Write silence at start of file.
           End Enum
         
        *End Region
         
        *Region " Codec Enumerations "
         
        *Region " MP3 Lame "
         
           Enum Lame_Bitrate
               kbps_8 = 8
               kbps_16 = 16
               kbps_24 = 24
               kbps_32 = 32
               kbps_40 = 40
               kbps_48 = 48
               kbps_56 = 56
               kbps_64 = 64
               kbps_80 = 80
               kbps_96 = 96
               kbps_112 = 112
               kbps_128 = 128
               kbps_144 = 144
               kbps_160 = 160
               kbps_192 = 192
               kbps_224 = 224
               kbps_256 = 256
               kbps_320 = 320
           End Enum
         
           Enum Lame_Bitrate_Mode
               cbr
               abr
           End Enum
         
           Enum Lame_Profile
               NORMAL
               FAST
               SLOW
           End Enum
         
           Enum Lame_Quality
               Q0_Maximum = 0
               Q1 = 1
               Q2 = 2
               Q3 = 3
               Q4 = 4
               Q5 = 5
               Q6 = 6
               Q7 = 7
               Q8 = 8
               Q9_Minimum = 9
           End Enum
         
           Enum Lame_Khz
               Same_As_Source
               khz_8000 = 8000
               khz_11025 = 11025
               khz_12000 = 12000
               khz_16000 = 16000
               khz_22050 = 22050
               khz_24000 = 24000
               khz_32000 = 32000
               khz_44100 = 44100
               khz_48000 = 48000
           End Enum
         
           Enum Lame_Channels
               auto
               mono
               stereo
               joint_stereo
               forced_joint_stereo
               forced_stereo
               dual_channels
           End Enum
         
         
        *End Region
         
        *Region " WAV Uncompressed "
         
           Enum WAV_Uncompressed_Bitrate
               Same_As_Source
               bits_8 = 8
               bits_16 = 16
               bits_24 = 24
               bits_32 = 32
           End Enum
         
           Enum WAV_Uncompressed_Khz
               Same_As_Source
               khz_8000 = 8000
               khz_11025 = 11025
               khz_12000 = 12000
               khz_16000 = 16000
               khz_22050 = 22050
               khz_24000 = 24000
               khz_32000 = 32000
               khz_44100 = 44100
               khz_48000 = 48000
               khz_96000 = 96000
               khz_192000 = 192000
           End Enum
         
           Enum WAV_Uncompressed_Channels
               Same_As_Source
               Channels_1_Mono = 1
               Channels_2_Stereo = 2
               Channels_3 = 3
               Channels_4_Quadraphonic = 4
               Channels_5_Surround = 5
               Channels_6_Surround_DVD = 6
               Channels_7 = 7
               Channels_8_Theater = 8
           End Enum
         
        *End Region
         
        *Region " WMA 9.2 "
         
           Enum WMA_9_2_BitRates
               Kbps_12 = 12
               Kbps_16 = 16
               Kbps_20 = 20
               Kbps_22 = 22
               Kbps_24 = 24
               Kbps_32 = 32
               Kbps_40 = 40
               Kbps_48 = 48
               Kbps_64 = 64
               Kbps_80 = 80
               Kbps_96 = 96
               Kbps_128 = 128
               Kbps_160 = 160
               Kbps_192 = 192
               Kbps_256 = 256
               Kbps_320 = 320
           End Enum
         
           Enum WMA_9_2_Khz
               Khz_8000 = 8
               Khz_16000 = 16
               Khz_22050 = 22
               Khz_32000 = 32
               Khz_44100 = 44
               Khz_48000 = 48
           End Enum
         
           Enum WMA_9_2_Channels
               mono
               stereo
           End Enum
         
        *End Region
         
        *End Region
         
        *Region " Codec Procedures "
         
        *Region " MP3 Lame "
         
           ' <summary>
           ' Converts a file to MP3 using Lame codec.
           ' </summary>
           Public Shared Sub Convert_To_MP3(ByVal In_File As String, _
                                    ByVal Out_File As String, _
                                    ByVal Bitrate As Lame_Bitrate, _
                                    ByVal Bitrate_Mode As Lame_Bitrate_Mode, _
                                    ByVal Encoding_Profile As Lame_Profile, _
                                    ByVal Quality As Lame_Quality, _
                                    ByVal Khz As Lame_Khz, _
                                    ByVal Channels As Lame_Channels, _
                                    Optional ByVal DSP_Effects() As DSP_Effects = Nothing, _
                                    Optional ByVal Update_Tag As Boolean = True, _
                                    Optional ByVal Priority As Priority = Priority.normal, _
                                    Optional ByVal Processor As Short = 1)
         
               Get_Effects(DSP_Effects)
         
               Set_Main_Parametters("mp3 (Lame)", In_File, Out_File, If(Not Update_Tag, "-noidtag", ""), Effects, Priority.ToString, Processor.ToString)
         
               CoreConverter.StartInfo.Arguments &= _
               String.Format("-b {0} --{1} -encoding=""{2}"" -freq=""{3}"" -channels=""{4}"" --noreplaygain --extracli=""-q {5}""", _
                             CInt(Bitrate), _
                             Bitrate_Mode.ToString, _
                             Encoding_Profile.ToString, _
                             If(Khz = Lame_Khz.Same_As_Source, "", CInt(Khz)), _
                             If(Channels = Lame_Channels.auto, "", Channels), _
                             CInt(Quality))
         
               Run_CoreConverter()
         
           End Sub
         
        *End Region
         
        *Region " WAV Uncompressed "
         
           ' <summary>
           ' Converts a file to WAV
           ' </summary>
           Public Shared Sub Convert_To_WAV_Uncompressed(ByVal In_File As String, _
                                        ByVal Out_File As String, _
                                        ByVal Bitrate As WAV_Uncompressed_Bitrate, _
                                        ByVal Khz As WAV_Uncompressed_Khz, _
                                        ByVal Channels As WAV_Uncompressed_Channels, _
                                        Optional ByVal DSP_Effects() As DSP_Effects = Nothing, _
                                        Optional ByVal Update_Tag As Boolean = True, _
                                        Optional ByVal Priority As Priority = Priority.normal, _
                                        Optional ByVal Processor As Short = 1)
         
               Get_Effects(DSP_Effects)
         
               Set_Main_Parametters("Wave", In_File, Out_File, If(Not Update_Tag, "-noidtag", ""), Effects, Priority.ToString, Processor.ToString)
         
               CoreConverter.StartInfo.Arguments &= _
               String.Format("-compression=""PCM"" -bits=""{0}"" -freq=""{1}"" -channels=""{2}""", _
                             If(Bitrate = WAV_Uncompressed_Bitrate.Same_As_Source, "", CInt(Bitrate)), _
                             If(Khz = WAV_Uncompressed_Khz.Same_As_Source, "", CInt(Khz)), _
                             If(Channels = WAV_Uncompressed_Channels.Same_As_Source, "", CInt(Channels)))
         
               Run_CoreConverter()
         
           End Sub
         
        *End Region
         
        *Region " WMA 9.2 "
         
           ' <summary>
           ' Converts a file to WMA 9.2
           ' </summary>
           Public Shared Sub Convert_To_WMA(ByVal In_File As String, _
                                        ByVal Out_File As String, _
                                        ByVal Bitrate As WMA_9_2_BitRates, _
                                        ByVal Khz As WMA_9_2_Khz, _
                                        ByVal Channels As WMA_9_2_Channels, _
                                        Optional ByVal DSP_Effects() As DSP_Effects = Nothing, _
                                        Optional ByVal Update_Tag As Boolean = True, _
                                        Optional ByVal Priority As Priority = Priority.normal, _
                                        Optional ByVal Processor As Short = 1)
         
               Get_Effects(DSP_Effects)
         
               Set_Main_Parametters("Windows Media Audio 10", In_File, Out_File, If(Not Update_Tag, "-noidtag", ""), Effects, Priority.ToString, Processor.ToString)
         
               CoreConverter.StartInfo.Arguments &= _
               String.Format("-codec=""Windows Media Audio 9.2"" -settings=""{0} kbps, {1} kHz, {2} CBR""",
                             CInt(Bitrate), _
                             CInt(Khz), _
                             Channels.ToString)
         
               Run_CoreConverter()
         
           End Sub
         
        *End Region
         
        *End Region
         
        *Region " Run Converter Procedure "
         
           Private Shared Sub Run_CoreConverter()
         
               CoreConverter.StartInfo.FileName = CoreConverter_Location
               CoreConverter.Start()
         
               While Not CoreConverter.HasExited
         
                   OutputCharacter = ChrW(CoreConverter.StandardOutput.Read)
         
                   If OutputCharacter = "*" Then
                       CurrentProgress += 1 ' Maximum value is 59, so a ProgressBar Maximum property value would be 59.
                       RaiseEvent PercentDone(CurrentProgress, Nothing)
                   End If
         
                   If CurrentProgress = 59 Then
                       ' I store the last line(s) 'cause it has interesting information:
                       ' Example message: Conversion completed in 30 seconds x44 realtime encoding
                       StandardOutput = CoreConverter.StandardOutput.ReadToEnd.Trim
                   End If
         
               End While
         
               ' Stores the Error Message (If any)
               ErrorOutput = CoreConverter.StandardError.ReadToEnd
         
               Select Case CoreConverter.ExitCode
         
                   Case 0 : RaiseEvent Exited(StandardOutput, Nothing) ' Return StandardOutput
                   Case Else : RaiseEvent Exited(ErrorOutput, Nothing) ' Return ErrordOutput
         
               End Select
         
               CurrentProgress = Nothing
               OutputCharacter = Nothing
               StandardOutput = Nothing
               ErrorOutput = Nothing
               Effects = Nothing
               CoreConverter.Close()
         
           End Sub
         
        *End Region
         
        *Region " Miscellaneous functions "
         
           ' <summary>
           ' Checks if CoreConverter process is avaliable.
           ' </summary>
           Public Shared Function Is_Avaliable() As Boolean
               Return IO.File.Exists(CoreConverter_Location)
           End Function
         
           ' Set the constant parametters of CoreConverter process
           Private Shared Sub Set_Main_Parametters(ByVal Codec_Name As String, _
                                                   ByVal In_File As String, _
                                                   ByVal Out_File As String, _
                                                   ByVal Update_Tag As String, _
                                                   ByVal Effects As String, _
                                                   ByVal Priority As String, _
                                                   ByVal Processor As String)
         
               CoreConverter.StartInfo.Arguments = _
               String.Format("-infile=""{0}"" -outfile=""{1}"" -convert_to=""{2}"" {3} {4} -priority=""{5}"" -processor=""{6}"" ", _
                             In_File, Out_File, Codec_Name, Update_Tag, Effects, Priority, Processor)
         
           End Sub
         
           ' Returns all joined DSP Effects formatted string
           Private Shared Function Get_Effects(ByVal DSP_Effects() As DSP_Effects) As String
         
               If DSP_Effects Is Nothing Then Return Nothing
         
               For Effect As Integer = 0 To DSP_Effects.Length - 1
                   Effects &= String.Format(" -dspeffect{0}={1}", _
                                            Effect + 1, _
                                            Format_DSP_Effect(DSP_Effects(Effect).ToString))
               Next Effect
         
               Return Effects
         
           End Function
         
           ' Returns a DSP Effect formatted string
           Private Shared Function Format_DSP_Effect(ByVal Effect As String)
         
               Select Case Effect
                   Case "Reverse" : Return """Reverse"""
                   Case "Delete_Output_File_on_Error" : Return """Delete Destination File on Error="""
                   Case "Recycle_Source_File_After_Conversion" : Return """Delete Source File=-recycle"""
                   Case "Delete_Source_File_After_Conversion" : Return """Delete Source File="""
                   Case "Karaoke_Remove_Voice" : Return """Karaoke (Voice_ Instrument Removal)="""
                   Case "Karaoke_Remove_Instrument" : Return """Karaoke (Voice_ Instrument Removal)=-i"""
                   Case "Write_Silence" : Return """Write Silence=-lengthms={qt}2000{qt}""" ' 2 seconds
                   Case Else : Return String.Empty
               End Select
         
           End Function
         
        *End Region
         
        *Region " Dispose Objects "
         
           Public Sub Dispose() Implements IDisposable.Dispose
               ' CoreConverter_Location = Nothing ' Do not change if want to preserve a custom location.
               OutputCharacter = Nothing
               StandardOutput = Nothing
               ErrorOutput = Nothing
               CurrentProgress = Nothing
               Effects = Nothing
               CoreConverter.Close()
               GC.SuppressFinalize(Me)
           End Sub
         
        *End Region
         
        End Class
         
        *End Region
Working...

]]>