Plik Wav rozłam w VB6

głosy
0

Potrzebuję pomocy, aby naprawić załączonego kodu VB6, który ma wziąć plik audio i podzielić ją na 5 równych części.

Jest to sposób ten kod powinien działać:

Pierwsza część rozpoczyna się od początku pliku Track.wav. Druga część zaczyna się od gdzie zakończył pierwszą część podzielonego. Trzecia część rozpoczyna się od gdzie zakończył drugą część podzielonego. Czwarta część zaczyna się od gdzie zakończyła się trzecia część podzielonego. Piąta część zaczyna się od gdzie zakończyła czwartą część podzielonego.

Zasadniczo każda z części plików jest kontynuacją poprzedniej części podzielić plik. Po rozpadzie mam 1.wav, 2.wav, 3.wav. 4.wav i 5.wav wszystko pochodzi z pliku Track.wav. Kod już dołączony plik dzieli się na pięć równych części, ale problemem jest to, że wszystkie części audio są takie same jak w pierwszej części, a nie kontynuacji.

Potrzebuję pomocy, aby to naprawić do pracy tak jak powinien w VB6 (nie .NET). Byłbym wdzięczny za pomoc z tym.

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
Utwórz 13/02/2020 o 22:01
źródło użytkownik
W innych językach...                            


2 odpowiedzi

głosy
1

To pytanie jest bardzo zaangażowany, szczególnie jeśli każda część musi być odtwarzane. Powodem jest to, że każdy plik utworzyć musi mieć ważny rekord nagłówka. Komplikować go dalej, wydaje się zapis nagłówka może być 44 bajtów 46 bajtów, a nawet innych rozmiarach.

Pracowałem na niektóre podstawowe kodu na podstawie postu, który wydaje się działać dla pliku wav I badanego:

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

I wyeliminować wiele zduplikowanych kodu poprzez wdrożenie Forpętlę. W tej pętli obliczyć pozycję początek czytać, a także przekazać rekord nagłówka do zapisu.

Ponownie podkreślam, że jest to bardzo proste i nie będzie działać dla wszystkich plików wav. Można ręcznie ustawić HEADER_SIZE jeśli to nie działa dla pliku.

Prawdopodobne potrzeby rekord nagłówka zostać zmienione w celu odzwierciedlenia prawidłowy rozmiar nowego pliku, zamiast przy użyciu nagłówka z oryginalnego pliku.

To powinno Ci zacząć.

Odpowiedział 16/02/2020 o 22:25
źródło użytkownik

głosy
0

Jest to kod roboczego. Jestem pewien, że ktoś może potrzebować czegoś takiego w przyszłości si myślałem, że to tutaj.


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
Odpowiedział 17/02/2020 o 23:41
źródło użytkownik

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