C# 如何制作粉红噪音发生器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/616897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:24:38  来源:igfitidea点击:

How can I make a Pink Noise generator?

c#.netaudionoise

提问by Erik Forbes

((Answer selected - see Edit 5 below.))

((选择答案 - 请参阅下面的编辑 5。))

I need to write a simple pink-noise generator in C#. The problem is, I've never done any audio work before, so I don't know how to interact with the sound card, etc. I do know that I want to stay away from using DirectX, mostly because I don't want to download a massive SDK just for this tiny project.

我需要用 C# 编写一个简单的粉红噪声生成器。问题是,我以前从未做过任何音频工作,所以我不知道如何与声卡等交互。我知道我想远离使用 DirectX,主要是因为我不想为这个小项目下载一个庞大的 SDK。

So I have two problems:

所以我有两个问题:

  1. How do I generate Pink Noise?
  2. How do I stream it to the sound card?
  1. 如何产生粉红噪声?
  2. 我如何将其流式传输到声卡?

Edit: I reallywant to make a pink noise generator... I'm aware there are other ways to solve the root problem. =)

编辑:我真的很想制作一个粉红噪音发生器...我知道还有其他方法可以解决根本问题。=)

Edit 2: Our firewall blocks streaming audio and video - otherwise I'd just go to www.simplynoise.comas suggested in the comments. :(

编辑 2:我们的防火墙阻止流式音频和视频 - 否则我会按照评论中的建议访问www.simplynoise.com。:(

Edit 3: I've got the generation of white-noise down, as well as sending output to the sound card - now all I need to know is how to turn the white-noise into pink noise. Oh - and I don't want to loop a wav file because every application I've tried to use for looping ends up with a tiny little break in between loops, which is jarring enough to have prompted me in this direction in the first place...

编辑 3:我已经减少了白噪声的产生,并将输出发送到声卡 - 现在我需要知道的是如何将白噪声变成粉红噪声。哦 - 我不想循环一个 wav 文件,因为我试图用于循环的每个应用程序最终都会在循环之间有一个小小的中断,这足以让我首先想到这个方向...

Edit 4: ... I'm surprised so many people have jumped in to very explicitly notanswer a question. I probably would have gotten a better response if I lied about why I need pink noise... This question is more about how to generate and stream data to the sound card than it is about what sort of headphones I should be using. To that end I've edited out the background details - you can read about it in the edits...

编辑 4:...我很惊讶有这么多人非常明确地回答问题。如果我对我为什么需要粉红噪音撒谎,我可能会得到更好的回答……这个问题更多地是关于如何生成数据并将数据传输到声卡,而不是我应该使用什么样的耳机。为此,我已经编辑掉了背景细节——你可以在编辑中阅读它......

Edit 5: I've selected Paul's answer below because the link he provided gave me the formula to convert white noise (which is easily generated via the random number generator) into pink noise. In addition to this, I used Ianier Munoz's CodeProject entry "Programming Audio Effects in C#"to learn how to generate, modify, and output sound data to the sound card. Thank you guys for your help. =)

编辑 5:我在下面选择了 Paul 的答案,因为他提供的链接给了我将白噪声(很容易通过随机数生成器生成)转换为粉红噪声的公式。除此之外,我还使用了Ianier Munoz 的 CodeProject 条目“Programming Audio Effects in C#”来学习如何生成、修改和输出声音数据到声卡。谢谢你们的帮助。=)

采纳答案by Paul

Maybe you can convert the C/C++ code here to C#:

也许您可以将这里的 C/C++ 代码转换为 C#:

http://www.firstpr.com.au/dsp/pink-noise/

http://www.firstpr.com.au/dsp/pink-noise/

The easiest way to get sound to the sound card is to generate a wav (spit out some hardcoded headers and then sample data). Then you can play the .wav file.

获得声音到声卡的最简单方法是生成一个 wav(吐出一些硬编码的标头,然后采样数据)。然后你就可以播放 .wav 文件了。

回答by Rich

Not really an answer to your question, but can't you just listen to some music, ideally with some noise cancelling headphones?

不是你问题的真正答案,但你不能只听一些音乐,最好带一些降噪耳机吗?

回答by Dashogun

I can't speak about C#, but you might be better off with some good noise canceling headphones and your favorite mp3's.

我不能谈论 C#,但你可能会更好地使用一些好的降噪耳机和你最喜欢的 mp3。

回答by Michael Todd

As a quick and dirty way to do it, how about just looping a pink noise wav in your audio player? (Yes, I know part of the fun is to make it yourself....)

作为一种快速而肮脏的方法,在您的音频播放器中循环播放粉红噪声 wav 怎么样?(是的,我知道部分乐趣是自己制作......)

回答by Bob King

What about an .mp3 sample of Pink Noise on repeat?

重复播放粉红噪声的 .mp3 样本怎么样?

回答by switchmode

Pink noise is just white noise put through a -3dB/octave LPF. You can generate white noise using rand() (or any function that generates uniformly random numbers).

粉红噪声只是通过 -3dB/octave LPF 的白噪声。您可以使用 rand() (或任何生成均匀随机数的函数)生成白噪声。

Streaming stuff to the soundcard is reasonably trivial, as long as you have Google handy. If you choose to avoid DirectX, consider using PortAudio or ASIO for interfacing with the soundcard... although I think you're gonna have to use C++ or C.

只要你有谷歌,将内容流式传输到声卡是相当简单的。如果您选择避免使用 DirectX,请考虑使用 PortAudio 或 ASIO 与声卡接口...尽管我认为您将不得不使用 C++ 或 C。

Other than that, why waste CPU time generating it? Loop a damn WAV file!

除此之外,为什么要浪费 CPU 时间来生成它?循环播放该死的 WAV 文件!

回答by ine

You could use Audacityto generate as much pink noise as you want, and then repeat it.

您可以使用Audacity生成任意数量的粉红噪声,然后重复此操作。

Or you could dig into the source code and see how Audacity does the pink noise generation.

或者您可以深入研究源代码,看看 Audacity 如何生成粉红噪声。

回答by Trap

Here's is an example of what the playback thread looks like. I'm using DirectSound to create a SecondaryBuffer where the samples are written. As you can see it's pretty straightforward:

下面是播放线程的示例。我正在使用 DirectSound 创建一个 SecondaryBuffer,在其中写入样本。如您所见,它非常简单:

    /// <summary>
    /// Thread in charge of feeding the playback buffer.
    /// </summary>
    private void playbackThreadFn()
    {
        // Begin playing the sound buffer.
        m_playbackBuffer.Play( 0, BufferPlayFlags.Looping );

        // Change playing state.
        IsPlaying = true;

        // Playback loop.
        while( IsPlaying )
        {
            // Suspend thread until the playback cursor steps into a trap...
            m_trapEvent.WaitOne();

            // ...read audio from the input stream... (In this case from your pink noise buffer)
            Input.Collect( m_target, m_target.Length );

            // ...calculate the next writing position...
            var writePosition = m_traps[ ((1 & m_pullCounter++) != 0) ? 0 : 1 ].Offset;

            // ...and copy audio to the device buffer.
            m_playbackBuffer.Write( writePosition, m_deviceBuffer, LockFlag.None );
        }

        // Stop playback.
        m_playbackBuffer.Stop();
    }

If you need more details on how it works I'll be glad to help.

如果您需要有关其工作原理的更多详细信息,我将很乐意为您提供帮助。

回答by The Son

bit late to this i realise, but anyone coming across it for answers should know that pink noise is white noise with -3dB/octave, not -6 as stated above, which is actually brown noise.

我意识到这一点有点晚,但是任何遇到它寻求答案的人都应该知道粉红噪声是具有 -3dB/octave 的白噪声,而不是如上所述的 -6,实际上是棕噪声。

回答by Eirik Birkeland

If you're on Linux, you can use SOX (you may have it already, try the playcommand).

如果您使用的是 Linux,则可以使用 SOX(您可能已经拥有它,请尝试使用该play命令)。

play -t sl - synth 3 pinknoise band -n 1200 200 tremolo .1 40 < /dev/zero

play -t sl - synth 3 pinknoise band -n 1200 200 tremolo .1 40 < /dev/zero