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.

127 lines
4.3KB

  1. #ifndef STK_RTWVIN_H
  2. #define STK_RTWVIN_H
  3. #include "WvIn.h"
  4. #include "RtAudio.h"
  5. #include "Mutex.h"
  6. namespace stk {
  7. /***************************************************/
  8. /*! \class RtWvIn
  9. \brief STK realtime audio (blocking) input class.
  10. This class provides a simplified interface to RtAudio for realtime
  11. audio input. It is a subclass of WvIn. This class makes use of
  12. RtAudio's callback functionality by creating a large ring-buffer
  13. from which data is read. This class should not be used when
  14. low-latency is desired.
  15. RtWvIn supports multi-channel data in both interleaved and
  16. non-interleaved formats. It is important to distinguish the
  17. tick() method that computes a single frame (and returns only the
  18. specified sample of a multi-channel frame) from the overloaded one
  19. that takes an StkFrames object for multi-channel and/or
  20. multi-frame data.
  21. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  22. */
  23. /***************************************************/
  24. class RtWvIn : public WvIn
  25. {
  26. public:
  27. //! Default constructor.
  28. /*!
  29. The default \e device argument value (zero) will select the
  30. default input device on your system. The first device enumerated
  31. by the underlying audio API is specified with a value of one. The
  32. default buffer size of RT_BUFFER_SIZE is defined in Stk.h. An
  33. StkError will be thrown if an error occurs duing instantiation.
  34. */
  35. RtWvIn( unsigned int nChannels = 1, StkFloat sampleRate = Stk::sampleRate(),
  36. int device = 0, int bufferFrames = RT_BUFFER_SIZE, int nBuffers = 20 );
  37. //! Class destructor.
  38. ~RtWvIn();
  39. //! Start the audio input stream.
  40. /*!
  41. The stream is started automatically, if necessary, when a
  42. tick() or tickFrame() method is called.
  43. */
  44. void start( void );
  45. //! Stop the audio input stream.
  46. /*!
  47. It may be necessary to use this method to avoid audio underflow
  48. problems if you wish to temporarily stop audio input.
  49. */
  50. void stop( void );
  51. //! Return the specified channel value of the last computed frame.
  52. /*!
  53. For multi-channel files, use the lastFrame() function to get
  54. all values from the last computed frame. If the device is
  55. stopped, the returned value is 0.0. The \c channel argument must
  56. be less than the number of channels in the audio stream (the first
  57. channel is specified by 0). However, range checking is only
  58. performed if _STK_DEBUG_ is defined during compilation, in which
  59. case an out-of-range value will trigger an StkError exception.
  60. */
  61. StkFloat lastOut( unsigned int channel = 0 );
  62. //! Compute a sample frame and return the specified \c channel value.
  63. /*!
  64. For multi-channel files, use the lastFrame() function to get
  65. all values from the computed frame. If the device is "stopped",
  66. it is "started". The \c channel argument must be less than the
  67. number of channels in the audio stream (the first channel is
  68. specified by 0). However, range checking is only performed if
  69. _STK_DEBUG_ is defined during compilation, in which case an
  70. out-of-range value will trigger an StkError exception.
  71. */
  72. StkFloat tick( unsigned int channel = 0 );
  73. //! Fill the StkFrames object with computed sample frames, starting at the specified channel and return the same reference.
  74. /*!
  75. If the device is "stopped", it is "started". The \c channel
  76. argument plus the number of input channels must be less than the
  77. number of channels in the StkFrames argument (the first channel is
  78. specified by 0). However, range checking is only performed if
  79. _STK_DEBUG_ is defined during compilation, in which case an
  80. out-of-range value will trigger an StkError exception.
  81. */
  82. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  83. // This function is not intended for general use but must be
  84. // public for access from the audio callback function.
  85. void fillBuffer( void *buffer, unsigned int nFrames );
  86. protected:
  87. RtAudio adc_;
  88. Mutex mutex_;
  89. bool stopped_;
  90. unsigned int readIndex_;
  91. unsigned int writeIndex_;
  92. unsigned int framesFilled_;
  93. };
  94. inline StkFloat RtWvIn :: lastOut( unsigned int channel )
  95. {
  96. #if defined(_STK_DEBUG_)
  97. if ( channel >= data_.channels() ) {
  98. oStream_ << "RtWvIn::lastOut(): channel argument and audio stream are incompatible!";
  99. handleError( StkError::FUNCTION_ARGUMENT );
  100. }
  101. #endif
  102. return lastFrame_[channel];
  103. }
  104. } // stk namespace
  105. #endif