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.

97 lines
2.9KB

  1. #ifndef STK_RTWVOUT_H
  2. #define STK_RTWVOUT_H
  3. #include "WvOut.h"
  4. #include "RtAudio.h"
  5. #include "Mutex.h"
  6. namespace stk {
  7. /***************************************************/
  8. /*! \class RtWvOut
  9. \brief STK realtime audio (blocking) output class.
  10. This class provides a simplified interface to RtAudio for realtime
  11. audio output. It is a subclass of WvOut. This class makes use of
  12. RtAudio's callback functionality by creating a large ring-buffer
  13. into which data is written. This class should not be used when
  14. low-latency is desired.
  15. RtWvOut supports multi-channel data in interleaved format. It is
  16. important to distinguish the tick() method that outputs a single
  17. sample to all channels in a sample frame from the overloaded one
  18. that takes a reference to an StkFrames object for multi-channel
  19. and/or multi-frame data.
  20. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  21. */
  22. /***************************************************/
  23. class RtWvOut : public WvOut
  24. {
  25. public:
  26. //! Default constructor.
  27. /*!
  28. The default \e device argument value (zero) will select the
  29. default output device on your system. The first device enumerated
  30. by the underlying audio API is specified with a value of one. The
  31. default buffer size of RT_BUFFER_SIZE is defined in Stk.h. An
  32. StkError will be thrown if an error occurs duing instantiation.
  33. */
  34. RtWvOut( unsigned int nChannels = 1, StkFloat sampleRate = Stk::sampleRate(),
  35. int device = 0, int bufferFrames = RT_BUFFER_SIZE, int nBuffers = 20 );
  36. //! Class destructor.
  37. ~RtWvOut();
  38. //! Start the audio output stream.
  39. /*!
  40. The stream is started automatically, if necessary, when a
  41. tick() method is called.
  42. */
  43. void start( void );
  44. //! Stop the audio output stream.
  45. /*!
  46. It may be necessary to use this method to avoid undesireable
  47. audio buffer cycling if you wish to temporarily stop audio output.
  48. */
  49. void stop( void );
  50. //! Output a single sample to all channels in a sample frame.
  51. /*!
  52. If the device is "stopped", it is "started".
  53. */
  54. void tick( const StkFloat sample );
  55. //! Output the StkFrames data.
  56. /*!
  57. If the device is "stopped", it is "started". The number of
  58. channels in the StkFrames argument must equal the number of
  59. channels specified during instantiation. However, this is only
  60. checked if _STK_DEBUG_ is defined during compilation, in which
  61. case an incompatibility will trigger an StkError exception.
  62. */
  63. void tick( const StkFrames& frames );
  64. // This function is not intended for general use but must be
  65. // public for access from the audio callback function.
  66. int readBuffer( void *buffer, unsigned int frameCount );
  67. protected:
  68. RtAudio dac_;
  69. Mutex mutex_;
  70. bool stopped_;
  71. unsigned int readIndex_;
  72. unsigned int writeIndex_;
  73. long framesFilled_;
  74. unsigned int status_; // running = 0, emptying buffer = 1, finished = 2
  75. };
  76. } // stk namespace
  77. #endif