gpt4 book ai didi

vb.net - 带有程序生成波形的音频点击

转载 作者:行者123 更新时间:2023-12-03 02:11:07 25 4
gpt4 key购买 nike

最新更新:波形文件构造不正确!谢谢夹克!

更新:现在正确构建了波形文件,并且在使用 playSync() 时仍然会发生点击。将波形加载到波形编辑器中,它们看起来和听起来都很好。

我的程序中的音频有问题。播放结束时有一个咔哒声。我在 VS 2013 中使用 vb.net。

该程序的目标是播放莫尔斯电码,以便用户学习。好声音是必须的。在传输莫尔斯电码的 radio 上,音/哔声的开始和结束有一个 5 毫秒的斜坡,以消除接收 radio 中刺耳的爆裂声和咔嗒声。我希望这个程序的音频能够模拟它,所以我在正弦波的生成中添加了一个“斜坡”。大多数听众会听到 400 到 1000 Hz 之间的莫尔斯音。

我相信代码中应该有足够的注释来理解发生了什么。我不是程序员。

波形作为内存流生成并通过 player = system.media.player 传递给 system.media.player,然后使用 player.stream = ditstream 设置流(ditstream 是波形格式音频),然后使用 player.playsync 播放()。

我在生成的波的末尾添加了零,它有点帮助。

这是生成波形的代码:

Function createWave(ByRef genStream As MemoryStream, ByVal frequency As UInt16, ByVal msDuration As Integer, _
Optional msRamp As Integer = 5, Optional ByVal volume As UInt16 = 16383) ' 16383
'set variables
Dim writer As New BinaryWriter(genStream)
Dim TAU As Double = 2 * Math.PI
Dim formatChunkSize As Integer = 16
Dim headerSize As Integer = 8
Dim formatType As Short = 1
Dim tracks As Short = 1
Dim samplesPerSecond As Integer = 44100
Dim bitsPerSample As Short = 16
Dim frameSize As Short = CShort(tracks * ((bitsPerSample + 7) \ 8))
Dim bytesPerSecond As Integer = samplesPerSecond * frameSize
Dim waveSize As Integer = 4
Dim samples As Integer = CInt(Math.Truncate(CType(samplesPerSecond, [Decimal]) * msDuration \ 1000)) 'removed /1000 from both
Dim rampSamples As Integer = CInt(Math.Truncate(CType(samplesPerSecond, [Decimal]) * msRamp \ 1000)) 'number of samples for ramp
Dim fullSamples As Integer = samples - (rampSamples * 2) 'number of samples at full amplitude
Dim dataChunkSize As Integer = samples * frameSize
Dim fileSize As Integer = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize
' var encoding = new System.Text.UTF8Encoding();
writer.Write(&H46464952) ' = encoding.GetBytes("RIFF")
writer.Write(fileSize)
writer.Write(&H45564157) ' = encoding.GetBytes("WAVE")
writer.Write(&H20746D66) ' = encoding.GetBytes("fmt ")
writer.Write(formatChunkSize)
writer.Write(formatType)
writer.Write(tracks)
writer.Write(samplesPerSecond)
writer.Write(bytesPerSecond)
writer.Write(frameSize)
writer.Write(bitsPerSample)
writer.Write(&H61746164) ' = encoding.GetBytes("data")
writer.Write(dataChunkSize)
Dim theta As Double = frequency * TAU / CDbl(samplesPerSecond - 1)
' 'volume' is UInt16 with range 0 thru Uint16.MaxValue ( = 65 535)
' we need 'amp' to have the range of 0 thru Int16.MaxValue ( = 32 767)
Dim amp As Double = volume >> 2 ' so we simply set amp = volume / 2
Dim rampAmp As Double = 0

'create amplification ramp of wave for number of ramp samples (duration of msRamp)
For [step] As Integer = 0 To rampSamples - 1
rampAmp = [step] / rampSamples
Dim s As Short = CShort(Math.Truncate((amp) * Math.Sin(theta * CDbl([step]))))
s = s * rampAmp
writer.Write(s)
'Debug.Print("Step :" & [step] & " Ramp at beginning: " & rampAmp & " S Value :" & s)
Next [step]

amp = volume >> 2
' create regular amplitude wave for full duration minus ending ramp
For [step] As Integer = rampSamples To fullSamples - 1
Dim s As Short = CShort(Math.Truncate(amp * Math.Sin(theta * CDbl([step]))))
writer.Write(s)
'Debug.Print("Step: " & [step] & " Full Amp sample : " & s)
Next [step]

更新:以下这部分不再是问题!
==================================================== =================================
下面的这个例程会导致正弦波失真!

        'create ending ramp amplfication from full volume to 0
For [step] As Integer = (rampSamples + fullSamples) To (((2 * rampSamples) + fullSamples - 1)) 'removed -1 for testing (((2 * rampSamples) + fullSamples -1))
rampAmp = CDbl((rampSamples + fullSamples + rampSamples - [step]) / rampSamples)
Dim s As Short = CShort(Math.Truncate((amp) * Math.Sin(theta * CDbl([step]))))
s = s * rampAmp
'debug statement
'Debug.Print("Step: " & [step] & " RampAmp at ending : " & rampAmp & " S value : " & s)
Debug.Print("Step : " & [step] & " Value : " & s & vbCrLf)
writer.Write(s)
If [step] = (((2 * rampSamples) + fullSamples) - 1) Then Debug.Print("End [STEP] = " & [step] & vbCrLf)
Next [step]

==================================================== =================================

这没关系....我认为....
 'add extra zero at end for good measure
'Dim z As Short = 0
'writer.Write(z)
'add some extra 0's to clean up any noise! Cheap and dirty fix!
For [x] = 0 To 200
writer.Write(0)
Next
Debug.Print("generateWave stream length: " & genStream.Length)


Return genStream

以下是播放代码示例:
         Sub playDit()

ditStream.Seek(0, SeekOrigin.Begin)
player.Stream = ditStream
player.PlaySync()

End Sub

这是Git的链接:
Morse Git Code

我不是程序员!只是想学习!
任何帮助删除音频点击和弹出将不胜感激!

建议会有所帮助。谢谢!

最佳答案

我查看了 dahwave.wav,并对其进行了背靠背分析。首先是波形的两个副本连接在一起时会出现故障。在第 5 个周期也有一个毛刺。据推测,这是您从斜坡变为满量程波形的地方。

其次,只要波形中有尖角,就必须有可能引入咔嗒声或爆破声。由于您的坡道是一条直线,因此您在坡道的起点和终点都有一个拐角。通常,人们会为窗口使用更平滑的函数。在这种情况下,一个实用的窗口是余弦平方或汉恩窗口。

要实现这一点,您只需一个循环:

For [step] As Integer = 0 To samples-1
a = 0.5 * (1 - Math.Cos(2*Math.PI*i/CDbl(samples));
Dim s As Short = CShort(Math.Truncate(amp * a * Math.Sin(theta * CDbl([step]))))
writer.Write(s)
Next [step]

enter image description here

关于vb.net - 带有程序生成波形的音频点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com