none
Is there a white noise generator and pink noise generator for C#? RRS feed

  • Question

  • Is there a white noise generator and pink noise generator for C#?

    bhs67


    • Edited by bhs67 Sunday, October 25, 2020 2:15 PM
    Sunday, October 25, 2020 1:40 PM

All replies

  • When posting a question related to some terminology that is not well known you should really explain what you're talking about otherwise we are going to have a hard time helping you. Googling for white noise and pink noise seems to refer to sound frequencies. There is audio support in C# but no "noise" generator that I'm aware of.

    So the next thing to do is google for libraries in C# for the noise. A quick google for "white pink noise c#" reveals some others having a similar question. From there you might find what you're looking for. One of the results was noise generators in Github that you might find useful.


    Michael Taylor http://www.michaeltaylorp3.net

    Sunday, October 25, 2020 3:29 PM
    Moderator
  • I have known the phrase "white noise" and "pink noise" for more than 30 years.  I had assumed that is a standard item in C#.  Based on the response, that is not the case.

    BTW I had looked at a few websites - saw white noise for images, and other variations, with extensive code examples.  I'm just looking for a simple solution.

    For many years we used a random generator to create white noise (full spectrum) - this works:

        Random rnRandom = new Random();
        int iMin = -32767;
        int iMax = 32767;
        return rnRandom.Next(iMin, iMax);

      Pink noise is a little more challenging as it attenuates higher frequencies.


    bhs67

    Sunday, October 25, 2020 8:17 PM
  • Hi bhs67,

    Thank you for posting here.

    You can take a look at this link, although it is a bit old, I think it might help.

    DSP generation of Pink (1/f) Noise

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 26, 2020 8:27 AM
  • If you need a sound file with random values, then maybe change the known sample: https://docs.microsoft.com/en-us/archive/blogs/dawate/intro-to-audio-programming-part-3-synthesizing-simple-wave-audio-using-c

    Copy the classes and code and perform these adjustments:

    • Put a convenient file name to filePath variable.
    • Replace the loop with this one:

    var rnd = new Random( );

    for( uint i = 0; i < numSamples - 1; i++ )

    {

           short v = unchecked((short)rnd.Next( short.MinValue, short.MaxValue + 1 ));

           for( int channel = 0; channel < format.wChannels; channel++ )

           {

                  data.shortArray[i + channel] = v;

           }

    }

    • The sample also starts playing the file in a Windows Form application. If you write a Console application, then replace player.Play() with player.PlaySync(), or play the generated file using some player.
    • The sample generates a 1-second sound; to increase the duration to 5 seconds, add ‘*5’ here:

        uint numSamples = format.dwSamplesPerSec * format.wChannels * 5;


    Monday, October 26, 2020 10:12 AM
  • Timon Yang & Viorel, thanks for the response.  I've been to those websites.  I don't see a solution. 

    Perhaps a signal needs to be converted to a spectrum analyzer values, add the pink noise, then convert back to .wav values?


    bhs67

    Monday, October 26, 2020 1:25 PM
  • It's not hard to generate the numeric values; Martin Gardner showed how to do that for pink noise in Scientific American.  That's only half of the problem, however.  What do you want to DO with this noise?  Do you want to send it to the speakers?  Sending a buffer to the speakers is a separate problem from generating the signal.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, October 27, 2020 4:33 AM
  • Martin Gardner!  Wow, that is a long time ago!

    I am generating the sounds of a flute and a trumpet.

    It is easy to generate sound with a root note (such as 440 Hz) and harmonics (880 Hz, 1320 Hz, etc. - all at different amplitudes). 

    What is missing is the “breathiness” which appears as broad-spectrum noise.  Perhaps pink noise cutoff variations could add “breathiness”.


    bhs67

    Tuesday, October 27, 2020 1:25 PM
  • Hi bhs67,

    Please check if this helps:

    class SignalGenerator

    C# NAudio: How to access the samples provided by SignalGenerator in order to save them to WAVE format?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 29, 2020 9:36 AM
  • Timon, I have attempted to implement those StackOverflow contents.  Visual Studio treats:

         public WAVEManager(string inputFileName, string outputFileName)
        {

        }

     and

        var pinkNoiseGenerator = new SignalGenerator();

    as CS0246 Could not be found errors => WAVEManager --- SignalGenerator().

    What am I missing?  I do have "using System.Media.  Is there another "using System.x"?


    bhs67

    Friday, October 30, 2020 2:25 PM
  • Hi bhs67,

    Have you installed the nuget package of NAudio?

    SignalGenerator is a class in NAudio.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 2, 2020 9:15 AM
  • Nope.  Have had higher priorities.  Thanks for clarifying.

    bhs67

    Monday, November 2, 2020 12:40 PM