Convertir archivos VOX a MP3

Acá les dejo unos códigos para convertir archivos VOX (normalmente creados por centrales telefónicas) a MP3.

Como el proyecto en el cual lo cree es en C#, vamos a crear una Class Library (dll) en Visual Basic, esta DLL lo que hace es convertir el archivo VOX a WAV para luego poder convertirlo a MP3

Namespace ConvertVOXToWAV
    Public Module moduleVOX2WAV

        Dim WaveHeader() As Int32 = New Int32() {&H46464952, &HFFFFFFDB, &H45564157, &H20746D66, 16, &H10001, 6000, 12000, &H100002, &H61746164, -1}
        Dim IndexShift() As Int16 = New Int16() {-1, -1, -1, -1, 2, 4, 6, 8}
        Dim StepSize() As Int16 = New Int16() {16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794}
        Dim Nibble2Bit(,) As Int16 = New Int16(,) {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}
        Dim SignTable() As Int16 = New Int16() {1, -1}
        Dim StepSizeIndex As Int16 = 0
        Dim Signal As Int16 = -2
        Dim AvgBuffer() As Int16 = New Int16() {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim AvgIndex As Int16 = 0
        Dim Total As Int32 = 0
        Dim Length As Int32 = 0
        Dim _Offset As Int32
        Dim nibble As Int16
        Dim ReadBuffer(4096) As Byte
        Dim WriteBuffer(8192) As Int16

        Public Sub Convert(ByVal srcVOXFile As String, ByVal destWAVFile As String)
            Dim strm As New IO.FileStream(srcVOXFile, IO.FileMode.Open, IO.FileAccess.Read)
            FileOpen(2, destWAVFile, OpenMode.Binary, OpenAccess.Write, OpenShare.LockWrite)
            FilePut(2, WaveHeader)
            Length = 4096
            While Length = 4096
                Length = strm.Read(ReadBuffer, 0, 4096)
                For moduleVOX2WAV._Offset = 0 To Length - 1
                    nibble = 0
                    Signal += Decode(ReadBuffer(_Offset) \ 16)
                    If (Signal > 2047 Or Signal < -2047) Then Truncate()                     WriteBuffer(2 * _Offset) = Signal                     nibble = 1                     Signal += Decode(ReadBuffer(_Offset) Mod 16)                     If (Signal > 2047 Or Signal < -2047) Then Truncate()
                    WriteBuffer(2 * _Offset + 1) = Signal
                Next
                FilePut(2, WriteBuffer)
                Total += Length
            End While
            strm.Close() : strm = Nothing
            Total = 4 * Total
            Length = Total + 36
            FilePut(2, Length, 4 + 1)
            FilePut(2, Total, 40 + 1)
            FileClose(2)
        End Sub

        Public Function Decode(ByVal i As Byte) As Int16
            Dim diff As Int16
            Dim value As Int32
            value = 2 * Total + 2 * _Offset + nibble
            diff = SignTable(Nibble2Bit(i, 0)) * (StepSize(StepSizeIndex) * Nibble2Bit(i, 1) + (StepSize(StepSizeIndex) / 2) * Nibble2Bit(i, 2) + (StepSize(StepSizeIndex) / 4) * Nibble2Bit(i, 3) + (StepSize(StepSizeIndex) / 8))
            StepSizeIndex = StepSizeIndex + IndexShift( i Mod 8 )
            If StepSizeIndex < 0 Then StepSizeIndex = 0             If StepSizeIndex > 48 Then
                StepSizeIndex = 48
            End If
            Return diff
        End Function

        Public Sub Truncate()
            Dim value As Int32
            value = 2 * Total + 2 * _Offset + nibble
            If Signal > 2047 Then Signal = 2047
            If Signal < -2047 Then Signal = -2047
        End Sub

    End Module

End Namespace

Una vez creada compilamos y tenemos nuestra dll lista para ser utilizada en cualquier proyecto. Ahora vamos a crear el código para convertir a MP3. Para poder realizar esto necesitamos descargar una aplicación esta es LAME la cual nos va a permitir convertir el Archivo WAV creado anteriormente a MP3

Esta función recibe el archivo a convertir, la ruta del mismo, y si va a esperar hasta que termine la conversión para seguir o lo hace en modo background.

 public static void mciConvertWavMP3(string fileName,string pworkinDir, bool waitFlag)
        {  //maxLen is in ms (1000 = 1 second)
            string outfile = "-h --scale 30 \"" + pworkingDir + fileName + "\" \"" + pworkingDir + fileName.Replace(".wav", ".mp3") + "\"";
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
            psi.FileName = "F:\\Lame\\lame.exe\\";
            psi.Arguments = outfile;
            //psi.WorkingDirectory=pworkingDir;
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
            System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
            if (waitFlag)
            {
                p.WaitForExit();  // wait for exit of called application
            }
        }

Ahora llamamos a la funciones correspondientes para la conversion.

        private static string pworkingDir = @"F:\Proyectos\Audios\";

        static void Main(string[] args)
        {
            string voxFile = pworkingDir + "11314801.vox";
            string wavFile = pworkingDir + "11314801.wav";

            //Convertimos el audio VOX a WAV
            moduleVOX2WAV.Convert(voxFile, wavFile);

            //Convertimos el audio WAV a MP3
            mciConvertWavMP3("11314801.wav",pworkingDir, false);

        }

Una vez terminado podemos eliminar el archivo WAV para que nos quede solo el archivo MP3. Espero les sirva.

Saludos.

Both comments and pings are currently closed.

One Response to “Convertir archivos VOX a MP3”

  1. Mario dice:

    Estimado, tu codigo me a ayudado muchisimo, pero tengo un inconveniente. En la primera linea; cito
    “Dim WaveHeader() As Int32 = New Int32() {&H46464952, &HFFFFFFDB, &H45564157, &H20746D66, 16, &H10001, 6000, 12000, &H100002, &H61746164, -1}”
    Tengo un error en la primera ‘declaracion’ {&amp…}. Me marca en el simbolo antes del ‘amp’. Me dice que ‘se esperaba una expresion’.
    Ojala me puedas ayudar. De verdad el codigo me saco un enredo y un gran peso de encima.
    Desde ya muy agradecido por tus gestiones y tu codigo.
    Saludos!!

Subscribe to RSS Feed Seguinos en Twitter