Saturday, 11 May 2013

Algoritma Menulis File Wave

Posted by Anonymous On 01:16 | No comments
Algoritma Menulis File Wave
Sub Write2Wave(OrigFile As String, cInFile As String, cOutFile
    As String, cModeOps As String)
    Dim fh As FileHeader
    Dim wf As WaveFormat
    Dim ch As ChunkHeader
    Dim WaveInfo As WaveFileFormat
    Dim FileIO As Integer
    Dim Buffer() As Byte
    Dim FileSize As Long
    Dim ByteData As Long   
    WaveInfo = GetFileWaveInfo(OrigFile)
    FileIO = FreeFile
    Open cInFile For Binary As #FileIO
    ByteData = LOF(FileIO)
    ReDim Buffer(0 To LOF(FileIO) - 1)
    Get #FileIO, , Buffer()
    Close #FileIO
   
    FileIO = FreeFile
    Open cOutFile For Binary Access Write As #FileIO
 
    With fh
      .lRiff = &H46464952    ' <RIFF> chunk tag
      .lFileSize = 0         ' Will be calculate Later
      .lWave = &H45564157    ' <WAVE> chunk tag
      .lFormat = &H20746D66  ' <fmt > chunk tag
      .lFormatLength = Len(wf)
    End With
 
    Put #FileIO, , fh
   
    If cModeOps = "bfEncode" Then
       wf.wFormatTag = 88 'Calculate all chunk format value
    ElseIf cModeOps = "bfDecode" Then
       wf.wFormatTag = 1
    End If
   
    With wf
      .nChannels = WaveInfo.NumChannels
      .nSamplesPerSec = WaveInfo.SampleRate
      .wBitsPerSample = WaveInfo.BitsPerSample
      .nBlockAlign = WaveInfo.BlockAlign
      .nAvgBytesPerSec = WaveInfo.ByteRate
    End With
   
    Put #FileIO, , wf
    ch.lType = &H61746164
    Put #FileIO, , ch ' Save <data> chunk tag
    Put #FileIO, , Buffer()
   
    ' Save File Size.
    FileSize = Len(fh) + Len(wf) + Len(ch) + ByteData
    Put #FileIO, 5, FileSize
 
    'Write again size of header chunk data
    ch.lLen = ByteData
    Put #FileIO, Len(fh) + Len(wf) + 1, ch
    Close #FileIO   
End Sub

0 comments:

Post a Comment

Blogroll

About