将WAV文件中的一部分截取出来保存为一个WAV文件
各位大侠,谁有将WAV文件中的一部分截取出来保存为一个WAV文件的代码。
[解决办法]
这里有你所需要的东西:
http://blog.csdn.net/bluesoal/archive/2006/07/17/932395.aspx
[解决办法]
'我以前研究Wav文件格式写的生成声音文件的代码,声音是随机产生的。
Option Explicit
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