You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.3KB

  1. #ifndef STK_NOISE_H
  2. #define STK_NOISE_H
  3. #include "Generator.h"
  4. #include <stdlib.h>
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class Noise
  8. \brief STK noise generator.
  9. Generic random number generation using the
  10. C rand() function. The quality of the rand()
  11. function varies from one OS to another.
  12. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  13. */
  14. /***************************************************/
  15. class Noise : public Generator
  16. {
  17. public:
  18. //! Default constructor that can also take a specific seed value.
  19. /*!
  20. If the seed value is zero (the default value), the random number generator is
  21. seeded with the system time.
  22. */
  23. Noise( unsigned int seed = 0 );
  24. //! Seed the random number generator with a specific seed value.
  25. /*!
  26. If no seed is provided or the seed value is zero, the random
  27. number generator is seeded with the current system time.
  28. */
  29. void setSeed( unsigned int seed = 0 );
  30. //! Return the last computed output value.
  31. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  32. //! Compute and return one output sample.
  33. StkFloat tick( void );
  34. //! Fill a channel of the StkFrames object with computed outputs.
  35. /*!
  36. The \c channel argument must be less than the number of
  37. channels in the StkFrames argument (the first channel is specified
  38. by 0). However, range checking is only performed if _STK_DEBUG_
  39. is defined during compilation, in which case an out-of-range value
  40. will trigger an StkError exception.
  41. */
  42. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  43. protected:
  44. };
  45. inline StkFloat Noise :: tick( void )
  46. {
  47. return lastFrame_[0] = (StkFloat) ( 2.0 * rand() / (RAND_MAX + 1.0) - 1.0 );
  48. }
  49. inline StkFrames& Noise :: tick( StkFrames& frames, unsigned int channel )
  50. {
  51. #if defined(_STK_DEBUG_)
  52. if ( channel >= frames.channels() ) {
  53. oStream_ << "Noise::tick(): channel and StkFrames arguments are incompatible!";
  54. handleError( StkError::FUNCTION_ARGUMENT );
  55. }
  56. #endif
  57. StkFloat *samples = &frames[channel];
  58. unsigned int hop = frames.channels();
  59. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  60. *samples = (StkFloat) ( 2.0 * rand() / (RAND_MAX + 1.0) - 1.0 );
  61. lastFrame_[0] = *(samples-hop);
  62. return frames;
  63. }
  64. } // stk namespace
  65. #endif