-
daisy8675 daisy8675
-
-
This edition rating:
-
more medals
-
-
|
#6 Score: 0 replies.: 2004-12-06 10:54:36 '---BAS---------------- Option Explicit
Private Type wavefilehdr ' WAVE file header definition Strfiletag as String * 4 ' "RIFF" flag Lngfilelen as Long ' file length, excluding ' RIFF ' flag and its own length Strfiletype as String * 4 ' WAVE ' logo End Type
Private Type datablockhdr ' data block definition Strblocktag as String * 4 ' data block name Lngblocklen as Long ' data block length, excluding data block name and its own length End Type
Public Type pcmwaveformat ' PCM format definition wFormatTag as Integer ' format flag, PCM 1 Nchannels as Integer ' channel number Nsamplespersec as Long ' sample rate Navgbytespersec as Long ' data rate nBlockAlign as Integer ' block alignment size, which is the smallest unit of waveform data wBitsPerSample as Integer ' sample size End Type
' Maximum buffer length Public Const Maxbufferlen as Long = 1024
' Create a new FileSystemObject object to facilitate the operation of the file Public Myfso as New FileSystemObject
' WAVE file Merge function ' File1 is the first file to be merged ' File2 for the second file to be merged ' OutFile files for merge output Public Function Unitewavefile (File1 As String, File2 as String, OutFile as String) as Boolean Dim FileName (1) as String Dim FILEHDR (1) as Wavefilehdr Dim BLOCKHDR (1) as Datablockhdr Dim wavefmt (1) as Pcmwaveformat Dim Readedformat (1) as Boolean Dim Buffer () as Byte, I as Long FileName (0) = File1 FileName (1) = File2 For I = 0 to 1 If not Myfso.fileexists (FileName (I)) Then MsgBox "File:" "& FileName (I) &" "does not exist! Please re-enter the file name or select a file. ",," error " Unitewavefile = False Exit Function End If Open FileName (i) for Binary Access Read as # (i + 1) Get # (i + 1), FILEHDR (i) If Filehdr (i). Strfiletag <> "RIFF" Or filehdr (i). Strfiletype <> "WAVE" Then MsgBox "File:" "& FileName (I) &" "is not a WAVE audio file! ",," error " Close Unitewavefile = False Exit Function End If If Filehdr (i). Lngfilelen <> LOF (i + 1)-8 Then MsgBox "File:" "& FileName (I) &" "is corrupted! ",," error " Close Unitewavefile = False Exit Function End If Do Until EOF (I + 1) ' Loop until you read the "format" data block and the "waveform data" data size Get # (i + 1), BLOCKHDR (i) With BLOCKHDR (I) If LOF (i + 1)-Loc (i + 1) <. Lngblocklen Then MsgBox "File:" "& FileName (I) &" "is corrupted! ",," error " Close Unitewavefile = False Exit Function End If If. Strblocktag = "FMT" and not Readedformat (I) Then Get # (i + 1), wavefmt (i) . Lngblocklen =. lngBlockLen-16 Readedformat (I) = True If wavefmt (I). wFormatTag <> 1 Then MsgBox "File:" "& FileName (I) &" "is not a PCM format! " _ & VbCrLf & "cannot be merged! ",," error " Close Unitewavefile = False Exit Function End If End If If. Strblocktag = "Data" and Readedformat (I) Then Exit do End If Seek # (i + 1), Seek (i + 1) +. Lngblocklen End with Loop Next If wavefmt (0). Nchannels <> wavefmt (1). Nchannels Then MsgBox "Two WAVE files are different in number of channels and cannot be merged! ",," error " Close Unitewavefile = False Exit Function End If If wavefmt (0). wBitsPerSample <> wavefmt (1). wBitsPerSample Then MsgBox "Two WAVE files are sampled in different sizes and cannot be merged! ",," error " Close Unitewavefile = False Exit Function End If If wavefmt (0). Nsamplespersec <> wavefmt (1). Nsamplespersec Then MsgBox "Two WAVE files have a different sampling rate and cannot be merged! ",," error " Close Unitewavefile = False Exit Function End If On Error GoTo ErrorLine If myfso.fileexists (OutFile) Then Kill OutFile End If Open OutFile for Binary Access Write as #3 Put #3, "RIFF" Put #3, 0& Put #3, "WAVE" Put #3, "FMT" Put #3, 16& Put #3, wavefmt (0) Put #3, "data" Put #3, BLOCKHDR (0). Lngblocklen + BLOCKHDR (1). Lngblocklen FrmMain.TheProgBar.Min = 0 FrmMain.TheProgBar.Value = 0 FrmMain.TheProgBar.Max = BLOCKHDR (0). lngblocklen \ Maxbufferlen + BLOCKHDR (1). lngblocklen \ Maxbufferlen + 2 For I = 0 to 1 ReDim Buffer (MAXBUFFERLEN-1) With BLOCKHDR (I) While. Lngblocklen > 0 If. Lngblocklen > Maxbufferlen Then . Lngblocklen =. Lngblocklen-maxbufferlen Else ReDim Buffer (. lngBlockLen-1) . Lngblocklen = 0 End If Get # (I + 1), Buffer Put #3, Buffer With Frmmain.theprogbar If. Value <. Max Then . Value =. Value + 1 End If End with Wend End with Next Put #3, 5, LOF (3)-8 Close Unitewavefile = True Exit Function ErrorLine: If err.number > 0 Then Close MsgBox "Open File:" "& OutFile &" "Error, the file may have been opened by another program. "& _ VbCrLf & vbCrLf & Close the program to open this file or close the browser's preview of this file. ",," error " Unitewavefile = False End If End Function |