用程序生成的wave进行音频点击

时间:2014-05-25 19:48:53

标签: vb.net audio

最新更新:wave文件构造不正确!谢谢你的夹克!

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

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

该程序的目标是播放莫尔斯代码,以便用户可以学习它。必须有良好的声音。在发送莫尔斯电码的收音机上,音调/嘟嘟声的开始和结束处有一个5毫秒的斜坡,以消除接收无线电中的刺耳声和咔嗒声。我希望这个程序的音频模仿,所以我添加了一个' ramp'在正弦波的产生。大多数听众会听到400到1000赫兹之间的莫尔斯音。

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

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

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

以下是生成wave的代码:

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

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

建议会有所帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

我看了一眼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