views:

2510

answers:

9

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

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.

So I have two problems:

  1. How do I generate Pink Noise?
  2. How do I stream it to the sound card?

Edit: I really want 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.com as suggested in the comments. :(

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...

Edit 4: ... I'm surprised so many people have jumped in to very explicitly not answer 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...

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. =)

+2  A: 

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

Rich
Music distracts me - pink noise works perfectly, except I can't use a loop for the reasons I added in edit 3.
Erik Forbes
A: 

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

Dashogun
Music is too distracting.
Erik Forbes
sorry, i still can't believe you want to generate noise to listen to. Or rather not listen to. I believe that noise cancelling headphones don't require music to be playing to operate
Harry
Well, with high-quality noise canceling headphones costing between $250 and $400, and with the fact that they cancel *noise*, not conversation, I figured a home-brew solution would be more appropriate.
Erik Forbes
Appropriate for my budget, that is.
Erik Forbes
A: 

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....)

Michael Todd
+1  A: 

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

Bob King
+5  A: 

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

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.

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

switchmode
Ok, I can generate white noise - how do I perform the filtering?
Erik Forbes
Added Edit 3 to answer your last question.
Erik Forbes
+1  A: 

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

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

amdfan
+4  A: 

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

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.

Paul
I can't use a loop for the reasons I added in edit 3
Erik Forbes
I'm going to accept this as my answer, because it contains the formulas needed to convert white noise into pink noise - thanks for the reference. =)
Erik Forbes
+1  A: 

You could just buy her album. Although it is surprising how often she is on the radio. I do admit to liking some of her song none the less.

Harry
+1  A: 

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:

    /// <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.

Trap