Segmentar archivo WAV en VB6

votos
0

Necesito ayuda para solucionar el código de Visual Basic 6 adjunto, que se supone que debe tener un archivo de audio, que se dividió en 5 partes iguales.

Esta es la forma en que este código debería funcionar:

La primera parte se inicia desde el principio del archivo Track.wav. La segunda parte se inicia desde donde la primera parte terminó dividida. La tercera parte se inicia desde donde la segunda parte dividida terminó. La cuarta parte se inicia desde donde la tercera parte dividida terminó. La quinta parte se inicia desde donde la cuarta parte dividida terminó.

Esencialmente cada una de las partes del archivo es una continuación de la parte anterior de dividir archivos. Después de la separación tengo 1.wav, 2.wav, 3.wav. 4.wav y 5.wav todos derivados de un archivo Track.wav. El código que ya está fijado divide el archivo en cinco partes iguales, pero el problema es que todas las partes de audio son los mismos que la primera parte en lugar de una continuación.

Necesito ayuda para conseguir esto fijada al trabajo como debe ser en VB6 (no .NET). Estaría agradecido por su ayuda con esto.

Dim Wavlength As Integer

Private Sub Command1_Click()

On Error Resume Next

DoFirstWav

Me.SetFocus

End Sub

Private Sub DoFirstWav()

On Error Resume Next

Dim ByteData() As Byte
Dim FirstWav As Integer

FirstWav = Wavlength / 5

ByteData = ReadFile(App.Path & \Track.wav, 1, FirstWav & 0000)
Call WriteFile(App.Path & \Segments\1.wav, ByteData)

DoSecondWav

End Sub

Private Sub DoSecondWav()

On Error Resume Next

Dim ByteData() As Byte
Dim SecondWav As Integer

SecondWav = Wavlength / 5

ByteData = ReadFile(App.Path & \Track.wav, 1, SecondWav & 0000)
Call WriteFile(App.Path & \Segments\2.wav, ByteData)

DoThirdWav

End Sub

Private Sub DoThirdWav()

On Error Resume Next

Dim ByteData() As Byte
Dim ThirdWav As Integer

ThirdWav = Wavlength / 5

ByteData = ReadFile(App.Path & \Track.wav, 1, ThirdWav & 0000)
Call WriteFile(App.Path & \Segments\3.wav, ByteData)

DoFourthWav

End Sub

Private Sub DoFourthWav()

On Error Resume Next

Dim ByteData() As Byte
Dim FourthWav As Integer

FourthWav = Wavlength / 5

ByteData = ReadFile(App.Path & \Track.wav, 1, FourthWav & 0000)
Call WriteFile(App.Path & \Segments\4.wav, ByteData)

DoFifthWav

End Sub

Private Sub DoFifthWav()

On Error Resume Next

Dim ByteData() As Byte
Dim FifthWav As Integer

FifthWav = Wavlength / 5

ByteData = ReadFile(App.Path & \Track.wav, 1, FifthWav & 0000)
Call WriteFile(App.Path & \Segments\5.wav, ByteData)

MsgBox Wav Split Successfully, vbInformation

End

End Sub

Private Function ReadFile(ByVal strFileName As String, Optional ByVal lngStartPos As Long = 1, Optional ByVal lngFileSize As Long = -1) As Byte()

On Error Resume Next

Dim FilNum As Integer

FilNum = FreeFile

Open strFileName For Binary As #FilNum
    If lngFileSize = -1 Then
        ReDim ReadFile(LOF(FilNum) - lngStartPos)
        Else
        ReDim ReadFile(lngFileSize - 1)
    End If
    Get #FilNum, lngStartPos, ReadFile
Close #FilNum

End Function

Private Function WriteFile(ByVal strFileName As String, ByteData() As Byte, Optional ByVal lngStartPos As Long = -1, Optional ByVal OverWrite As Boolean = True)

On Error Resume Next

Dim FilNum As Integer

FilNum = FreeFile

If OverWrite = True And Dir(strFileName) <>  Then
    Kill strFileName
End If

Open strFileName For Binary As #FilNum
    If lngStartPos = -1 Then
        Put #FilNum, LOF(FilNum) + 1, ByteData
        Else
        Put #FilNum, l, ByteData
    End If
Close #FilNum

End Function

Private Sub Form_Load()

On Error Resume Next

Dim MyInt As Integer
Dim MyByte As Byte
Dim MyStr As String * 4
Dim MyLong As Long
Dim SampleRate, BytesPerSample, FileSize As Long

Open App.Path & \Track.wav For Binary Access Read Lock Read As #1
    Get #1, , MyStr:    Debug.Print Riff = ; MyStr
    Get #1, , MyLong:   Debug.Print File size = ; MyLong
    FileSize = MyLong
    Get #1, , MyStr:    Debug.Print Wave = ; MyStr
    Get #1, , MyStr:    Debug.Print Format = ; MyStr
    Get #1, , MyLong:   Debug.Print Any = ; MyLong
    Get #1, , MyInt:    Debug.Print formatTag = ; MyInt
    Get #1, , MyInt:    Debug.Print Channels = ; MyInt
    Get #1, , MyLong:   Debug.Print Samples per Sec = ; MyLong
    SampleRate = MyLong
    Get #1, , MyInt:    Debug.Print Bytes per Sec = ; MyInt
    Get #1, , MyInt:    Debug.Print BlockAlign = ; MyInt
    Get #1, , MyInt:    Debug.Print Bytes per Sample = ; MyInt
    BytesPerSample = MyInt
Close #1

Wavlength = FileSize / (SampleRate * BytesPerSample)

End Sub
Publicado el 14/02/2020 a las 00:01
fuente por usuario
En otros idiomas...                            


2 respuestas

votos
1

Esta pregunta es un poco complicado, sobre todo si cada parte tiene que ser reproducibles. La razón es que cada archivo se crea debe tener un registro de encabezado válido. Para complicar aún más, aparece un registro de encabezado puede ser 44 bytes, 46 bytes, o incluso otros tamaños.

He trabajado algo de código básico basado en su post que parece una labor para el archivo WAV que probé:

Option Explicit

Private Const HEADER_SIZE As Long = 46
Private Const CHUNK_COUNT As Long = 5

Private HeaderData(HEADER_SIZE) As Byte
Private ChunkSize As Long

Private Sub Form_Load()
   Dim MyInt As Integer
   Dim MyByte As Byte
   Dim MyStr As String * 4
   Dim MyLong As Long
   Dim FileSize As Long

   Open App.Path & "\Track.wav" For Binary Access Read Lock Read As #1
   Get #1, , MyStr:    Debug.Print "Riff = "; MyStr
   Get #1, , MyLong:   Debug.Print "File size = "; MyLong
   Get #1, , MyStr:    Debug.Print "Wave = "; MyStr
   Get #1, , MyStr:    Debug.Print "Format = "; MyStr
   Get #1, , MyLong:   Debug.Print "Any = "; MyLong
   Get #1, , MyInt:    Debug.Print "formatTag = "; MyInt
   Get #1, , MyInt:    Debug.Print "Channels = "; MyInt
   Get #1, , MyLong:   Debug.Print "Samples per Sec = "; MyLong
   Get #1, , MyInt:    Debug.Print "Bytes per Sec = "; MyInt
   Get #1, , MyInt:    Debug.Print "BlockAlign = "; MyInt
   Get #1, , MyInt:    Debug.Print "Bytes per Sample = "; MyInt
   Get #1, , MyInt:    Debug.Print "Something = "; MyInt  'for my wave file, I needed 2 extra bytes
   Get #1, , MyStr:    Debug.Print "SubchunkID = "; MyStr
   Get #1, , FileSize: Debug.Print "SubchunkSize = "; FileSize
   Get #1, 1, HeaderData  'the size changes depending upon the file
   Close #1

   ChunkSize = CLng(FileSize / CHUNK_COUNT)  'you might loose some data here
End Sub

Private Sub Command1_Click()
   Dim i As Integer
   Dim ByteData() As Byte
   Dim StartPos As Long

   For i = 1 To CHUNK_COUNT
      StartPos = HEADER_SIZE + ((i - 1) * ChunkSize)
      ByteData = ReadFile(App.Path & "\Track.wav", StartPos, ChunkSize)
      Call WriteFile(App.Path & "\Segments\" & i & ".wav", HeaderData, ByteData)
   Next

   MsgBox "Wav Split Successfully", vbInformation
   End
End Sub

Private Function ReadFile(ByVal strFileName As String, ByVal lngStartPos As Long, ByVal lngFileSize As Long) As Byte()
   On Error Resume Next

   Dim FilNum As Integer

   FilNum = FreeFile

   ReDim ReadFile(lngFileSize - 1)

   Open strFileName For Binary As #FilNum

   Get #FilNum, lngStartPos, ReadFile

   Close #FilNum
End Function

Private Function WriteFile(ByVal strFileName As String, HeaderData() As Byte, ByteData() As Byte, Optional ByVal OverWrite As Boolean = True)
   On Error Resume Next

   Dim FilNum As Integer

   FilNum = FreeFile

   If OverWrite = True And Dir(strFileName) <> "" Then
       Kill strFileName
   End If

   Open strFileName For Binary As #FilNum

   Put #FilNum, LOF(FilNum) + 1, HeaderData
   Put #FilNum, HEADER_SIZE, ByteData

   Close #FilNum
End Function

He eliminado una gran cantidad de código duplicado mediante la implementación de un Forbucle. En ese bucle, calculo de la posición de inicio de la lectura, y también paso el registro de cabecera para la escritura.

Una vez más, hago hincapié en que esto es muy básico y no funcionará para todos los archivos wav. Se puede ajustar manualmente el HEADER_SIZE si no funciona para su archivo.

Probablemente las necesidades de registro de encabezado a ser modificados para reflejar el tamaño correcto del nuevo archivo, en lugar de utilizar el encabezado del archivo original.

Esto debería ayudarle a empezar.

Respondida el 17/02/2020 a las 00:25
fuente por usuario

votos
0

Este es el código de trabajo. Estoy seguro de que alguien puede necesitar algo como esto en el futuro, si pensé que había puesto aquí.


Dim Wavlength As Long
Dim PartLength As Integer
Dim WavHeader() As Byte

Private Sub Command1_Click()

On Error Resume Next

WavHeader = ReadFile(App.Path & "\Track.wav", 1, 320)

PartLength = Wavlength / 6 - 2

DoFirstWav

End Sub

Private Sub DoFirstWav()

On Error Resume Next

Dim ByteData() As Byte
Dim FirstWav As Integer

ByteData = ReadFile(App.Path & "\Track.wav", 1, PartLength & "0000")
Call WriteFile(App.Path & "\Segments\1.wav", ByteData)

DoSecondWav

End Sub

Private Sub DoSecondWav()

On Error Resume Next

Dim ByteData() As Byte
Dim ByteRead() As Byte
Dim SecondWav As Integer

SecondWav = PartLength

ByteRead = ReadFile(App.Path & "\Track.wav", SecondWav & "0000", PartLength & "0000")

ReDim ByteData(UBound(WavHeader) + UBound(ByteRead)) As Byte

For i = 0 To UBound(WavHeader)
    ByteData(i) = WavHeader(i)
Next i

For i = 0 To UBound(ByteRead)
    ByteData(UBound(WavHeader) + i) = ByteRead(i)
Next i

Call WriteFile(App.Path & "\Segments\2.wav", ByteData)

DoThirdWav

End Sub

Private Sub DoThirdWav()

On Error Resume Next

Dim ByteData() As Byte
Dim ByteRead() As Byte
Dim ThirdWav As Integer

ThirdWav = PartLength * 2 + 1

ByteRead = ReadFile(App.Path & "\Track.wav", ThirdWav & "0000", PartLength & "0000")

ReDim ByteData(UBound(WavHeader) + UBound(ByteRead)) As Byte

For i = 0 To UBound(WavHeader)
    ByteData(i) = WavHeader(i)
Next i

For i = 0 To UBound(ByteRead)
    ByteData(UBound(WavHeader) + i) = ByteRead(i)
Next i

Call WriteFile(App.Path & "\Segments\3.wav", ByteData)

DoFourthWav

End Sub

Private Sub DoFourthWav()

On Error Resume Next

Dim ByteData() As Byte
Dim ByteRead() As Byte
Dim FourthWav As Integer

FourthWav = PartLength * 3 + 1

ByteRead = ReadFile(App.Path & "\Track.wav", FourthWav & "0000", PartLength & "0000")

ReDim ByteData(UBound(WavHeader) + UBound(ByteRead)) As Byte

For i = 0 To UBound(WavHeader)
    ByteData(i) = WavHeader(i)
Next i

For i = 0 To UBound(ByteRead)
    ByteData(UBound(WavHeader) + i) = ByteRead(i)
Next i

Call WriteFile(App.Path & "\Segments\4.wav", ByteData)

DoFifthWav

End Sub

Private Sub DoFifthWav()

On Error Resume Next

Dim ByteData() As Byte
Dim ByteRead() As Byte
Dim FifthWav As Integer

FifthWav = PartLength * 4 + 1

ByteRead = ReadFile(App.Path & "\Track.wav", FifthWav & "0000", PartLength & "0000")

ReDim ByteData(UBound(WavHeader) + UBound(ByteRead)) As Byte

For i = 0 To UBound(WavHeader)
    ByteData(i) = WavHeader(i)
Next i

For i = 0 To UBound(ByteRead)
    ByteData(UBound(WavHeader) + i) = ByteRead(i)
Next i

Call WriteFile(App.Path & "\Segments\5.wav", ByteData)

End Sub

Private Function ReadFile(ByVal strFileName As String, Optional ByVal lngStartPos As Long = 1, Optional ByVal lngFileSize As Long = -1) As Byte()

On Error Resume Next

Dim FilNum As Integer

FilNum = FreeFile

Open strFileName For Binary As #FilNum
    If lngFileSize = -1 Then
        ReDim ReadFile(LOF(FilNum) - lngStartPos)
        Else
        ReDim ReadFile(lngFileSize - 1)
    End If
    Get #FilNum, lngStartPos, ReadFile
Close #FilNum

End Function

Private Function WriteFile(ByVal strFileName As String, ByteData() As Byte, Optional ByVal lngStartPos As Long = -1, Optional ByVal OverWrite As Boolean = True)

On Error Resume Next

Dim FilNum As Integer

FilNum = FreeFile

If OverWrite = True And Dir(strFileName) <> "" Then
    Kill strFileName
End If

Open strFileName For Binary As #FilNum
    If lngStartPos = -1 Then
        Put #FilNum, LOF(FilNum) + 1, ByteData
        Else
        Put #FilNum, l, ByteData
    End If
Close #FilNum

End Function

Private Sub Form_Load()

On Error Resume Next

Dim MyInt As Integer
Dim MyByte As Byte
Dim MyStr As String * 4
Dim MyLong As Long
Dim SampleRate, BytesPerSample, FileSize As Long

Open App.Path & "\Track.wav" For Binary Access Read Lock Read As #1
    Get #1, , MyStr:    Debug.Print "Riff = "; MyStr
    Get #1, , MyLong:   Debug.Print "File size = "; MyLong
    FileSize = MyLong
    Get #1, , MyStr:    Debug.Print "Wave = "; MyStr
    Get #1, , MyStr:    Debug.Print "Format = "; MyStr
    Get #1, , MyLong:   Debug.Print "Any = "; MyLong
    Get #1, , MyInt:    Debug.Print "formatTag = "; MyInt
    Get #1, , MyInt:    Debug.Print "Channels = "; MyInt
    Get #1, , MyLong:   Debug.Print "Samples per Sec = "; MyLong
    SampleRate = MyLong
    Get #1, , MyInt:    Debug.Print "Bytes per Sec = "; MyInt
    Get #1, , MyInt:    Debug.Print "BlockAlign = "; MyInt
    Get #1, , MyInt:    Debug.Print "Bytes per Sample = "; MyInt
    BytesPerSample = MyInt

Close #1

Wavlength = FileSize \ (SampleRate * BytesPerSample)

Debug.Print "Wavlength"; Wavlength

End Sub
Respondida el 18/02/2020 a las 01:41
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more