首页 > 代码库 > 如何把两个声音合在一起
如何把两个声音合在一起
如 1.wav和2.wav,最后合并为3.wav.
---frmMain ‘这些都不用注释吧,一看就明白。 Private Sub cmdBrowse_Click(Index As Integer) ‘控件数组的妙用,绝吧!:-) With TheComDlg If Index < 2 Then .DialogTitle = "打开 WAVE 文件" .ShowOpen Else .DialogTitle = "选择要保存输出的 WAVE 文件名" .ShowSave End If If .FileName <> "" Then txtWaveFile(Index).Text = .FileName .FileName = "" End If End With End Sub Private Sub cmdExit_Click() Unload frmMain End Sub Private Sub cmdMix_Click() If txtWaveFile(0).Text = "" Then MsgBox "请选择第一个要混合的 WAVE 音频文件!", , "错误" Exit Sub End If If txtWaveFile(1).Text = "" Then MsgBox "请选择第二个要混合的 WAVE 音频文件!", , "错误" Exit Sub End If If txtWaveFile(2).Text = "" Then MsgBox "请选择要保存混合后的 WAVE 音频的文件!", , "错误" Exit Sub End If If MixWaveFile(txtWaveFile(0).Text, txtWaveFile(1).Text, txtWaveFile(2).Text) Then MsgBox "混合 WAVE 音频文件成功!", , "提示" End If TheProgBar.Value = 0 End Sub Private Sub cmdUnite_Click() If txtWaveFile(0).Text = "" Then MsgBox "请选择第一个要合并的 WAVE 音频文件!", , "错误" Exit Sub End If If txtWaveFile(1).Text = "" Then MsgBox "请选择第二个要合并的 WAVE 音频文件!", , "错误" Exit Sub End If If txtWaveFile(2).Text = "" Then MsgBox "请选择要保存合并后的 WAVE 音频的文件!", , "错误" Exit Sub End If If UniteWaveFile(txtWaveFile(0).Text, txtWaveFile(1).Text, txtWaveFile(2).Text) Then MsgBox "合并 WAVE 音频文件成功!", , "提示" End If TheProgBar.Value = 0 End Sub Private Sub Form_Unload(Cancel As Integer) If MsgBox("您真的要退出这个程序吗?", vbOKCancel, "确认退出") = vbCancel Then Cancel = -1 End If End Sub |
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 |
| #6 得分:0 回复于: 2004-12-06 10:54:36 ‘---BAS---------------- Option Explicit Private Type WAVEFILEHDR ‘WAVE 文件头的定义 strFileTag As String * 4 ‘“RIFF”标志 lngFileLen As Long ‘文件长度,不包括“RIFF”标志和它本身的长度 strFileType As String * 4 ‘“WAVE”标志 End Type Private Type DATABLOCKHDR ‘数据块头的定义 strBlockTag As String * 4 ‘数据块名 lngBlockLen As Long ‘数据块长度,不包括数据块名和它本身的长度 End Type Public Type PCMWAVEFORMAT ‘PCM 格式定义 wFormatTag As Integer ‘格式标志,PCM 为 1 nChannels As Integer ‘通道数 nSamplesPerSec As Long ‘采样率 nAvgBytesPerSec As Long ‘数据率 nBlockAlign As Integer ‘块对齐大小,即波形数据的最小单位 wBitsPerSample As Integer ‘采样大小 End Type ‘缓冲区最大长度 Public Const MAXBUFFERLEN As Long = 1024 ‘新建一个 FileSystemObject 对象,以方便文件的操作 Public MyFSO As New FileSystemObject ‘WAVE 文件合并函数 ‘File1 为第一个要合并的文件 ‘File2 为第二个要合并的文件 ‘OutFile 为合并输出的文件 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 "文件: “ " & FileName(I) & " ”不存在!请重新输入文件名或选择一个文件。", , "错误" 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 "文件: “ " & FileName(I) & " ”不是 WAVE 音频文件!", , "错误" Close UniteWaveFile = False Exit Function End If If FileHdr(I).lngFileLen <> LOF(I + 1) - 8 Then MsgBox "文件: “ " & FileName(I) & " ”已损坏!", , "错误" Close UniteWaveFile = False Exit Function End If Do Until EOF(I + 1) ‘循环直到读出“格式”数据块和“波形数据”数据块头为止 Get #(I + 1), , BlockHdr(I) With BlockHdr(I) If LOF(I + 1) - Loc(I + 1) < .lngBlockLen Then MsgBox "文件: “ " & FileName(I) & " ”已损坏!", , "错误" 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 "文件: “ " & FileName(I) & " ”不是 PCM 格式!" _ & vbCrLf & "不能进行合并!", , "错误" 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 "两个 WAVE 文件的通道数不同,不能进行合并!", , "错误" Close UniteWaveFile = False Exit Function End If If WaveFmt(0).wBitsPerSample <> WaveFmt(1).wBitsPerSample Then MsgBox "两个 WAVE 文件的采样大小不同,不能进行合并!", , "错误" Close UniteWaveFile = False Exit Function End If If WaveFmt(0).nSamplesPerSec <> WaveFmt(1).nSamplesPerSec Then MsgBox "两个 WAVE 文件的采样率不同,不能进行合并!", , "错误" 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 "打开文件:“ " & OutFile & " ”时错误,可能文件已被其它程序打开。" & _ vbCrLf & vbCrLf & "请关闭打开此文件的程序或关闭浏览器对此文件的预览。", , "错误" UniteWaveFile = False End If End Function |
如何把两个声音合在一起
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。