如何在vb.net上提取声音的振动数据,如wav,mp3等。 [英] How do I extract a vibration data for sound like wav, mp3 etc on vb.net.?
本文介绍了如何在vb.net上提取声音的振动数据,如wav,mp3等。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以整数或任何可理解的形式提取声音文件的振动数据,如:0.0111,1.00 ...并通过输入提取的值来发出声音。
我尝试了什么:
我还没有尝试任何东西,因为我没有知道从哪里开始。(Noob)
I want to extract a vibration data of sound files in the form of an integer or any Understandable form like: 0.0111,1.00... and also make sound by entering the value that was extract.
What I have tried:
I haven't try any thing yet because I don't know where to start.(Noob)
推荐答案
要处理声音文件,必须先将它们转换为原始(未压缩)格式。对于Windows,这通常是WAV格式(请参阅 WAV - Wikipedia [ ^ ])。这个步骤相当简单,因为有很多程序可以做到这一点(可执行文件以及可以在自己的程序中实现的免费资源)。
一旦你有了WAV文件可以将样本加载到内存中并开始处理样本。这通常需要良好的数学知识(例如关于快速傅里叶变换 - 维基百科 [ ^ ]和奈奎斯特 - 香农采样定理 - 维基百科 [ ^ ]),即使没有基于数学公式实现代码但使用一些现有的免费代码。
请注意大多数代码音频处理(或更一般:信号处理)用C / C ++编写,用于MATLAB等数学软件,或LabVIEW等设计软件。您可能会发现一些C#源代码,但在VB.NET中查找示例代码的机会相当小。
在知道哪些方法时检测和隔离周期性振动数据应该没问题必须使用。但这是我所不知道的。
总的来说,这是一项繁重的任务,需要学习音频/信号处理和相关数学的基础知识。
To process sound files they must be first converted to a raw (uncompressed) format. With Windows this is usually the WAV format (see WAV - Wikipedia[^]). This step is rather simple because there are a many programs that can do that (executables and also free sources that can be implemented in own programs).
Once you have a WAV file you can load the samples into memory and start processing the samples. This usually requires good mathematical knowledge (e.g. about the Fast Fourier transform - Wikipedia[^] and the Nyquist–Shannon sampling theorem - Wikipedia[^] ), even when not implementing the code based on mathematical formulas but using some existing free code.
Note that most code for audio processing (or more general: signal processing) is written in C/C++, for mathematical software like MATLAB, or design software like LabVIEW. You might find some C# sources but chances to find example code in VB.NET are rather small.
Detecting and isolating periodic vibration data should be no problem when knowing which methods must be used. But this is beyond my knowledge.
Overall it is an extensive task that requires learning the basics of audio/signal processing and the related mathematics.
我终于找到了解决方案,它被称为样本而不是频率(很抱歉)。以下是读取/提取和创建wav文件的函数,适用于需要的人。
参考页: Microsoft WAVE声音文件格式 [ ^ ]
电话示例:
I Finally Found The Solution, It is known as "Sample" not a "Frequency"(Sorry for that). Here is the Function to read/Extract and create wav file, For those who need.
Reference Page: Microsoft WAVE soundfile format[^]
Call Example:
'This will save as txt
Getwav("Test.wav","Sample.txt")
'and
MsgBox("SampleRate:" & Getwav("Test.wav",Nothing,24))
代码:
Code:
'GetSample
Public Shared Function Getwav(Pt As String, Optional Opt As String = Nothing, Optional File_Offset As Integer = 44)
'Checking File
If File.Exists(Pt) = False Then
Return Nothing
End If
Try
'Reading File
Dim Byt() As Byte = File.ReadAllBytes(Pt)
Dim Mem As New MemoryStream(Byt)
Dim bin As New BinaryReader(Mem)
Dim Sample As New List(Of Integer)
Dim ChunkID As String = Nothing
bin.BaseStream.Position = 4
Dim ChunkSize As Integer = bin.ReadInt32
Dim Format As String = Nothing
Dim Subchunk1ID As String = Nothing
bin.BaseStream.Position = 16
Dim Subchunk1Size As Integer = bin.ReadInt32
Dim AudioFormat As Integer = bin.ReadInt16
Dim NumChannels As Integer = bin.ReadInt16
Dim SampleRate As Integer = bin.ReadInt32
Dim ByteRate As Integer = bin.ReadInt32
Dim BlockAlign As Integer = bin.ReadInt16
Dim BitsPerSample As Integer = bin.ReadInt16
Dim Subchunk2ID As String = Nothing
bin.BaseStream.Position = 40
Dim Subchunk2Size As Integer = bin.ReadInt32
bin.BaseStream.Position = 0
For i = 1 To 4
ChunkID &= bin.ReadChar
Next
bin.BaseStream.Position = 8
For i = 1 To 4
Format &= bin.ReadChar
Next
bin.BaseStream.Position = 12
For i = 1 To 4
Subchunk1ID &= bin.ReadChar
Next
bin.BaseStream.Position = 36
For i = 1 To 4
Subchunk2ID &= bin.ReadChar
Next
If File_Offset = 44 Then
bin.BaseStream.Position = 44
If Opt = Nothing = False Then
If File.Exists(Opt) Then
File.Delete(Opt)
End If
Dim wrt As New StreamWriter(Opt)
For i = 1 To Subchunk2Size / 2
wrt.WriteLine(bin.ReadInt16)
Next
wrt.Close()
Else
For i = 1 To Subchunk2Size / 2
Sample.Add(bin.ReadInt16)
Next
Return Sample
End If
End If
If File_Offset = 0 Then
Return ChunkID
End If
If File_Offset = 4 Then
Return ChunkSize
End If
If File_Offset = 8 Then
Return Format
End If
If File_Offset = 12 Then
Return Subchunk1ID
End If
If File_Offset = 16 Then
Return Subchunk1Size
End If
If File_Offset = 20 Then
Return AudioFormat
End If
If File_Offset = 22 Then
Return NumChannels
End If
If File_Offset = 24 Then
Return SampleRate
End If
If File_Offset = 28 Then
Return ByteRate
End If
If File_Offset = 32 Then
Return BlockAlign
End If
If File_Offset = 34 Then
Return BitsPerSample
End If
If File_Offset = 36 Then
Return Subchunk2ID
End If
If File_Offset = 40 Then
Return Subchunk2Size
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Return Nothing
End Function
从样本制作wav文件。
调用示例:
for making wav file from sample.
Call example:
mkwav("Sample.txt", "Sample.wav")
代码:
Code:
'Make wav
Public Shared Sub mkwav(pt As String, OPt As String,
Optional NumChannels As Integer = 1,
Optional SampleRate As Integer = 11025,
Optional BitsPerSample As Integer = 8)
'Checking Files
If File.Exists(pt) = False Then
Exit Sub
End If
'Creating Wav File
Dim Fdata As New List(Of Byte)
Try
Dim ChunkID As Integer = 1179011410
Dim ChunkSize As Integer = 0
Dim Format As Integer = 1163280727
Dim Subchunk1ID As Integer = 544501094
Dim Subchunk1Size As Integer = 16
Dim AudioFormat As Integer = 1
Dim ByteRate As Integer = 0
Dim BlockAlign As Integer = 0
Dim Subchunk2ID As Integer = 1635017060
Dim Subchunk2Size As Integer = 0
Dim NumSamples As Integer = 0
'Reading Sample
Dim Data As String = RNW.RED_DATA(pt)
Dim Ray() As String = Data.Split(Environment.NewLine.ToCharArray,
StringSplitOptions.RemoveEmptyEntries)
'Calculating Sizes
NumSamples = (Ray.Count * 2) + 1
Subchunk2Size = (NumSamples * NumChannels * BitsPerSample) / 8
ChunkSize = 4 + (8 + Subchunk1Size) + (8 + Subchunk2Size)
ByteRate = (SampleRate * NumChannels * BitsPerSample) / 8
BlockAlign = (NumChannels * BitsPerSample) / 8
'Adding to Fdata
Dim Count As Integer = 0
For Each B In BitConverter.GetBytes(ChunkID)
Fdata.Add(B)
Next
For Each B In BitConverter.GetBytes(ChunkSize)
Fdata.Add(B)
Next
For Each B In BitConverter.GetBytes(Format)
Fdata.Add(B)
Next
For Each B In BitConverter.GetBytes(Subchunk1ID)
Fdata.Add(B)
Next
For Each B In BitConverter.GetBytes(Subchunk1Size)
Fdata.Add(B)
Next
For Each B In BitConverter.GetBytes(AudioFormat)
If Count = 2 Then
Exit For
End If
Fdata.Add(B)
Count += 1
Next
Count = 0
For Each B In BitConverter.GetBytes(NumChannels)
If Count = 2 Then
Exit For
End If
Fdata.Add(B)
Count += 1
Next
For Each B In BitConverter.GetBytes(SampleRate)
Fdata.Add(B)
Next
For Each B In BitConverter.GetBytes(ByteRate)
Fdata.Add(B)
Next
Count = 0
For Each B In BitConverter.GetBytes(BlockAlign)
If Count = 2 Then
Exit For
End If
Fdata.Add(B)
Count += 1
Next
Count = 0
For Each B In BitConverter.GetBytes(BitsPerSample)
If Count = 2 Then
Exit For
End If
Fdata.Add(B)
Count += 1
Next
For Each B In BitConverter.GetBytes(Subchunk2ID)
Fdata.Add(B)
Next
For Each B In BitConverter.GetBytes(Subchunk2Size)
Fdata.Add(B)
Next
For Each S In Ray
If S = Nothing = False Then
Count = 0
For Each B In BitConverter.GetBytes(CInt(S))
If Count = 2 Then
Exit For
End If
Fdata.Add(B)
Count += 1
Next
End If
Next
Dim wrt As New FileStream(OPt, FileMode.Create)
For Each B In Fdata
wrt.WriteByte(B)
Next
wrt.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
这篇关于如何在vb.net上提取声音的振动数据,如wav,mp3等。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文