【问题标题】:Play sound in .NET using generated waveform data使用生成的波形数据在 .NET 中播放声音
【发布时间】:2010-11-08 05:43:36
【问题描述】:

如何根据 .NET 程序从用户输入和数学函数生成的波形数据播放声音?

“波形数据”是指固定间隔时间序列(可能为 44.1 kHz)中的 SPL(声压级)值。我认为这需要某种流式缓冲区安排。

请注意,这必须是实时/实时的,因此仅创建一个 .wav 文件然后播放是不够的。 VB.NET 是首选,但 C# 也可以接受。

澄清一下:我正在寻找的是一个简单的工作代码示例。

【问题讨论】:

标签: c# .net vb.net audio waveform


【解决方案1】:

您可以使用NAudio 执行此操作。您创建一个派生自 WaveStream 的流,并在其重写的 Read 方法中返回您可以即时生成的样本。您可以控制声卡使用的缓冲区大小,从而控制延迟。

【讨论】:

  • 我终于有机会尝试 NAudio,它非常棒。谢谢指点。
【解决方案2】:

How to play from an array of doubles

    PlayerEx pl = new PlayerEx();

    private static void PlayArray(PlayerEx pl)
    {
        double fs = 8000; // sample freq
        double freq = 1000; // desired tone
        short[] mySound = new short[4000];
        for (int i = 0; i < 4000; i++)
        {
            double t = (double)i / fs; // current time
            mySound[i] = (short)(Math.Cos(t * freq) * (short.MaxValue));
        }
        IntPtr format = AudioCompressionManager.GetPcmFormat(1, 16, (int)fs);
        pl.OpenPlayer(format);
        byte[] mySoundByte = new byte[mySound.Length * 2];
        Buffer.BlockCopy(mySound, 0, mySoundByte, 0, mySoundByte.Length);
        pl.AddData(mySoundByte);
        pl.StartPlay();
    }

【讨论】:

    【解决方案3】:

    查看this thread,了解如何使用任意数据加载DirectSound 缓冲区并进行播放。

    每条评论:是的,我知道。您需要将 C++ 翻译成 C# 或 VB.NET。但是,概念才是最重要的。您创建一个辅助 DirectSound 缓冲区,然后使用它流式传输到您的主缓冲区并播放。

    【讨论】:

    • 那是不是 VB.net 或C#。事实上,它看起来一点也不像 .Net。
    【解决方案4】:

    IrrKlangBASS.net(在“其他 API”下)、NAudioCLAMG3D 以及其他可以做到这一点的人。

    【讨论】:

      【解决方案5】:

      我认为您需要为此使用 DirectSound (DirectX API)。它适用于您可以用生成的数据填充的缓冲区。

      也许像thishere 在后面的机器上)这样的东西会有所帮助

      【讨论】:

        【解决方案6】:

        我有这段代码,但你必须有代码才能在内存中生成你的 wav 文件。

        Option Strict Off
        Option Explicit On
        Imports Microsoft.DirectX.DirectSound
        Imports Microsoft.DirectX
        Imports System.Threading
        
        Public Class Form1
        Const SRATE As Integer = 44100
        Const FREQ As Integer = 440
        Const DUR As Integer = 1
        
        Private dsDesc As BufferDescription
        Private wvFormat As WaveFormat
        Private DS As Device
        
        Dim SecondaryBuffer As Microsoft.DirectX.DirectSound.SecondaryBuffer
        Dim BufferDescription As Microsoft.DirectX.DirectSound.BufferDescription
        Dim DXFormat As Microsoft.DirectX.DirectSound.WaveFormat
        Dim sbuf(DUR * SRATE) As Short
        
        
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Show()
            DS = New Microsoft.DirectX.DirectSound.Device
            DS.SetCooperativeLevel(Me, CooperativeLevel.Normal)
            wvFormat.FormatTag = WaveFormatTag.Pcm
            wvFormat.Channels = 1
            wvFormat.SamplesPerSecond = SRATE
            wvFormat.BitsPerSample = 16
            wvFormat.AverageBytesPerSecond = 2 * SRATE
            wvFormat.BlockAlign = 2
            dsDesc = New BufferDescription(wvFormat)
            dsDesc.BufferBytes = 2 * DUR * SRATE
            dsDesc.Flags = 0
            Dim buff1 = PlayWave(400)
            Dim buff2 = PlayWave(600)
            buff1 = PlayWave(400)
            buff1.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default)
            Thread.Sleep(1000)
            buff1 = PlayWave(600)
            buff1.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default)
            ' End
        End Sub
        Function PlayWave(FREQ As Integer) As SecondaryBuffer
            ' create a buffer
            Dim dsBuffer As SecondaryBuffer
            dsBuffer = New SecondaryBuffer(dsDesc, DS)
            Dim sbuf(DUR * SRATE) As Short
            ' create tone                
            For i As Integer = 0 To DUR * SRATE
                sbuf(i) = CShort(10000 * Math.Sin(2 * Math.PI * FREQ * i / SRATE))
            Next
            ' copy to buffer
            dsBuffer.Write(0, sbuf, LockFlag.EntireBuffer)
            Return dsBuffer
        End Function
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 2014-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-04
          相关资源
          最近更新 更多