在 windows 中生成声音频率 - VB.Net
Posted
技术标签:
【中文标题】在 windows 中生成声音频率 - VB.Net【英文标题】:Generate sound frequency in windows - VB.Net 【发布时间】:2020-05-03 17:13:23 【问题描述】:我需要在 windows 中生成方波声音。我正在使用此代码:
System.console.beep(500,500)
但效果不是很好:播放声音需要一点时间。 我有一个 2.7 GHZ 的 CPU,所以我不认为我的电脑很慢。
有人知道在 windows 中播放频率的库或其他代码吗?
【问题讨论】:
How to generate sound according to frequency 我想这就是你要找的。span> 很遗憾没有,我想在没有 System.console.beep() 的情况下继续播放声音频率 【参考方案1】:进口:
Imports System.IO
Imports System.Media
呼叫:
FrequencyBeep(500, 500, 10000)
子:
Public Shared Sub FrequencyBeep(ByVal Amplitude As Integer, ByVal Frequency As Integer, ByVal Duration As Integer)
Dim A As Double = ((Amplitude * (System.Math.Pow(2, 15))) / 1000) - 1
Dim DeltaFT As Double = 2 * Math.PI * Frequency / 44100.0
Dim Samples As Integer = 441 * Duration / 10
Dim Bytes As Integer = Samples * 4
Dim Hdr As Integer() = 1179011410, 36 + Bytes, 1163280727, 544501094, 16, 131073, 44100, 176400, 1048580, 1635017060, Bytes
Using MS As MemoryStream = New MemoryStream(44 + Bytes)
Using BW As BinaryWriter = New BinaryWriter(MS)
For I As Integer = 0 To Hdr.Length - 1
BW.Write(Hdr(I))
Next
For T As Integer = 0 To Samples - 1
Dim Sample As Short = System.Convert.ToInt16(A * Math.Sin(DeltaFT * T))
BW.Write(Sample)
BW.Write(Sample)
Next
BW.Flush()
MS.Seek(0, SeekOrigin.Begin)
Using SP As SoundPlayer = New SoundPlayer(MS)
SP.PlaySync()
End Using
End Using
End Using
End Sub
【讨论】:
这段代码有问题:SoundPlayer is not defined docs.microsoft.com/en-gb/dotnet/api/… 你导入 System.Media 了吗? 哦,是的,我忘了导入 System.Media,现在它可以工作了。非常感谢!以上是关于在 windows 中生成声音频率 - VB.Net的主要内容,如果未能解决你的问题,请参考以下文章