Advertisement
CliveTooth

Untitled

Mar 13th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    
  3.      Generating random numbers using NAudio
  4.      ======================================
  5.  
  6.   I have written a C# class, SoundRandom, which may be used to generate small quantities of random numbers. It is simple to use...
  7.  
  8. 1) Create a new instance of the class:
  9.   SoundRandom sr = new SoundRandom();
  10.  
  11. 2) An unlimited sequence of random uints may then be collected by repeatedly
  12.    getting the RandomUint property:
  13.   sr.RandomUint
  14.  
  15. Such random uints, which are truly random, will be generated at the rate of about
  16. one thousand per second. These values may then be used, for example, to create
  17. seeds for some deterministic random number generator.
  18.  
  19. The program extracts the least significant bits from successive samples from
  20. a microphone socket. No microphone needs to be plugged into the socket.
  21. By default, the program sets the audio sample rate to 100,000 Hz, in stereo.
  22.  
  23. The program uses a traditional Von Neumann randomness extractor on the
  24. bit stream to create the random uints.
  25.  
  26. So, a simple demo program to create ten random uints and display them in
  27. hexadecimal might be as follows:
  28.  
  29.   SoundRandom sr = new SoundRandom();
  30.   for (int i = 0; i < 10; i++)
  31.   {
  32.     Tell(sr.RandomUint.ToString("x8"));
  33.   }
  34.  
  35. Producing the following output, perhaps:
  36.  
  37.   5b6525e3
  38.   083d5a7a
  39.   dddf47f2
  40.   0fee1071
  41.   1d8b4e65
  42.   83027594
  43.   58f18820
  44.   c0afaac9
  45.   92e61b46
  46.   0fb6b399
  47.  
  48.   */
  49.  
  50.   public class SoundRandom
  51.   {
  52.     private static readonly int           bitsPerUint = 32;
  53.     private EventHandler<WaveInEventArgs> bufferHandler = null;
  54.     private int                           channels = 0;
  55.     private ManualResetEvent              bufferReady = new ManualResetEvent(false);
  56.     private int                           nextI = 0;
  57.     private int                           randomBufferSize = 0;
  58.     private int                           randomCount = 0;
  59.     private uint[]                        randoms = null;
  60.     private uint                          randomUint = 0;
  61.     private int                           randomUintBitCount = 0;
  62.     private int                           sampleRate = 0;
  63.     private WaveInEvent                   waveInStream = null;
  64.  
  65.     public SoundRandom(
  66.       int randomBufferSize_param,
  67.       int sampleRate_param,
  68.       int channels_param
  69.     )
  70.     {
  71.       randomBufferSize = randomBufferSize_param;
  72.       sampleRate = sampleRate_param;
  73.       channels = channels_param;
  74.  
  75.       nextI = randomBufferSize;
  76.       randoms = new uint[randomBufferSize];
  77.     } // ctor int int int
  78.  
  79.     public SoundRandom() : this(1000, 100000, 2)
  80.     {
  81.       //
  82.     } // ctor
  83.  
  84.     public uint RandomUint
  85.     {
  86.       get
  87.       {
  88.         if (nextI >= randomBufferSize)
  89.         {
  90.           StartCapture();
  91.           bufferReady.WaitOne();
  92.           bufferReady.Reset();
  93.         }
  94.         return randoms[nextI++];
  95.       } // get
  96.  
  97.     } // RandomUint
  98.  
  99.     private void HandleNextBuffer(object sender, WaveInEventArgs e)
  100.     {
  101.       byte[] buffer = e.Buffer;
  102.       int    bytes = e.BytesRecorded;
  103.       int    count = bytes/4;
  104.  
  105.       for (int i = 0; i < count; i++)
  106.       {
  107.         int j = i*4;
  108.         uint b1 = (uint)(buffer[j] & 1);
  109.         uint b2 = (uint)(buffer[j+2] & 1);
  110.  
  111.         if (b1 != b2)
  112.         {
  113.           randomUint = (randomUint << 1) | b2;
  114.           ++randomUintBitCount;
  115.           if (randomUintBitCount == bitsPerUint)
  116.           {
  117.             randomUintBitCount = 0;
  118.             if (randomCount >= 0)
  119.             {
  120.               randoms[randomCount] = randomUint;
  121.             }
  122.             randomUint = 0;
  123.             ++randomCount;
  124.             if (randomCount == randomBufferSize)
  125.             {
  126.               waveInStream.DataAvailable -= bufferHandler;
  127.               waveInStream.StopRecording();
  128.               waveInStream.Dispose();
  129.               waveInStream = null;
  130.               bufferReady.Set();
  131.               return;
  132.             }
  133.           }
  134.         }
  135.       } // for i
  136.     } // HandleNextBuffer
  137.  
  138.     public void StartCapture()
  139.     {
  140.       bufferHandler = new EventHandler<WaveInEventArgs>(HandleNextBuffer);
  141.       nextI = 0;
  142.       randomCount = -1;
  143.       randomUint = 0;
  144.       randomUintBitCount = 0;
  145.  
  146.       waveInStream = new WaveInEvent();
  147.       waveInStream.WaveFormat = new WaveFormat(sampleRate, channels);
  148.       waveInStream.DataAvailable += bufferHandler;
  149.       waveInStream.StartRecording();
  150.     } // StartCapture
  151.  
  152.   } // SoundRandom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement