用VB怎么把两个wav音频文件合并成一个文件?
用VB怎么把两个wav音频文件合并成一个文件?比如文件c1.wav,c2.wav,合并后的c3.wav是c2追加到c1的后面,而不是c1,c2波形的的线性叠加(也就是所谓的混合音)。望高手们不吝赐教。 --------------------编程问答-------------------- 方法一,用设备录制,可以用MCI或DirectShow方法二,自己分析出数据区然后运算,然后再生成文件头,不过需要了解WAV文件格式
注意,采样频率不同需要对不同的采样频率进行合成的处理 --------------------编程问答-------------------- 不好意思,可能是我没说清楚,我的意思是用VB代码实现此功能,不是用工具合成,还有就是1楼的方法二在不同位速或者不同采样率的情况下,音频文件声音会失真。各位有没有更好的方法呢? --------------------编程问答-------------------- ding --------------------编程问答--------------------
Option Explicit--------------------编程问答-------------------- c1.wav,c2.wav采样频率不同、位速不同,早会出问题的。必须事先进行处理。 --------------------编程问答-------------------- 如楼上几位所说的,WAV文件合并要考虑采样频率、位速等。曾经分析过WAV文件的格式,但没有分析完全。下面的代码是生成一个声音文件的例子,可以帮助大家了解一下WAV文件的格式。如果哪位有完整分析的,希望能共享一下:
'分割\合并音频文件
'-- 合并文件
'-- 将之前分割出来的 1.mp3 和 2.mp3 合并为 music_new.mp3
Private Sub cmdAddFile_Click()
Dim bytData() As Byte
bytData = ReadFile(App.Path & "\1.mp3")
Call WriteFile(App.Path & "\music_new.mp3", bytData)
bytData = ReadFile(App.Path & "\2.mp3")
Call WriteFile(App.Path & "\music_new.mp3", bytData, , False)
End Sub
'-- 分割文件
'-- 示例将当前目录的 music.mp3 切割为 1.mp3(30000字节) 和 2.mp3(剩余部分)
Private Sub cmdCut_Click()
Dim bytData() As Byte
bytData = ReadFile(App.Path & "\01.mp3", 1, 100000)
Call WriteFile(App.Path & "\1.rmvb", bytData)
bytData = ReadFile(App.Path & "\01.mp3", 100001)
Call WriteFile(App.Path & "\2.rmvb", bytData)
End Sub
Private Function ReadFile(ByVal strFileName As String, Optional ByVal lngStartPos As Long = 1, Optional ByVal lngFileSize As Variant = -1) As Byte()
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, bytData() As Byte, Optional ByVal lngStartPos As Long = -1, Optional ByVal OverWrite As Boolean = True)
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, bytData
Else
Put #FilNum, lngStartPos, bytData
End If
Close #FilNum
End Function
Option Explicit--------------------编程问答-------------------- 学习。。。 --------------------编程问答-------------------- ding --------------------编程问答--------------------
Private Type RIFFHEADER
GroupID As String * 4
Length As Long
RiffType As String * 4
End Type
Private Type FMT
ChunkID As String * 4
ChunkSize As Long
wFormatTag As Integer
wChannels As Integer
dwSamplesPerSec As Long
dwAvgBytesPerSec As Long
wBlockAlign As Integer
wBitsPerSample As Integer
End Type
Private Type DATACHUNK
ChunkID As String * 4
ChunkSize As Long
End Type
Private Declare Function sndPlaySound Lib "winmm.dll" Alias _
"sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Const SND_LOOP = &H8
Const SND_NOSTOP = &H10
Private Sub Command1_Click()
Dim b() As Byte
Dim fS As Long
fS = 20000 '声音数据长度
Dim u As Long
u = fS - 44
ReDim b(1 To u)
Dim i As Integer
Dim d As Byte
'产生一些随机数据作为声音数据
For i = 1 To u
Randomize
d = CByte(Rnd() * 255)
b(i) = d
DoEvents
Next i
makeWavFile fS, b() '写入文件
MsgBox "OK,Wave file done!"
End Sub
Private Sub makeWavFile(ByVal fSize As Long, wavData() As Byte)
Dim myRiffHeader As RIFFHEADER
Dim myFMT As FMT
Dim myDataChunck As DATACHUNK
If fSize < 44 Then Exit Sub
'写Riff头
With myRiffHeader
.GroupID = "RIFF"
.Length = fSize - 8 ' Riff外的其它内容的长度
.RiffType = "WAVE"
End With
'format chunk 格式块
With myFMT
.ChunkID = "fmt "
.ChunkSize = 16 '未压缩的文件格式
.wFormatTag = 1 '未压缩
.wChannels = 1 '通道数据,此处用音声道
.dwSamplesPerSec = &H2B11 '每秒钟取样数,有三种11025,22050和44100
.dwAvgBytesPerSec = .dwSamplesPerSec * .wChannels
.wBlockAlign = .wChannels * 1 '声道数乘以取样宽(字节数),此处假设为1
.wBitsPerSample = 8 '每个样本的位数,此处假设为8位,即一个字节
End With
'数据块
With myDataChunck
.ChunkID = "data"
.ChunkSize = fSize - Len(myRiffHeader) - Len(myFMT) - Len(myDataChunck)
' .ChunkSize = fSize - 44
End With
Open App.Path & "/test.wav" For Binary As #1
Put #1, , myRiffHeader
Put #1, , myFMT
Put #1, , myDataChunck
Put #1, , wavData
Close #1
End Sub
Private Sub Command2_Click()
'播放声音
Dim Fn As String
Fn = App.Path & "\test.wav"
If Dir(Fn) <> "" Then
Dim sFlags As Long
sFlags = SND_ASYNC Or SND_NODEFAULT
sndPlaySound Fn, sFlags
End If
End Sub
好复杂
菜鸟看热闹 高手看门道 --------------------编程问答-------------------- 读出第一个和第二个文件内容,合在一起,再保存 --------------------编程问答-------------------- c1.wav,c2.wav采样频率一样就好办
--------------------编程问答-------------------- 学习学习,文件结构最复杂了 --------------------编程问答-------------------- 先把采样频率变得一样(就低不就高),再合并。 --------------------编程问答--------------------
你真的读懂了格式?读懂了你就不这样说了
补充:VB , 多媒体