Audio plugin host https://kx.studio/carla
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.

197 lines
6.7KB

  1. #ifndef STK_FILEWVIN_H
  2. #define STK_FILEWVIN_H
  3. #include "WvIn.h"
  4. #include "FileRead.h"
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class FileWvIn
  8. \brief STK audio file input class.
  9. This class inherits from WvIn. It provides a "tick-level"
  10. interface to the FileRead class. It also provides variable-rate
  11. playback functionality. Audio file support is provided by the
  12. FileRead class. Linear interpolation is used for fractional read
  13. rates.
  14. FileWvIn supports multi-channel data. It is important to
  15. distinguish the tick() method that computes a single frame (and
  16. returns only the specified sample of a multi-channel frame) from
  17. the overloaded one that takes an StkFrames object for
  18. multi-channel and/or multi-frame data.
  19. FileWvIn will either load the entire content of an audio file into
  20. local memory or incrementally read file data from disk in chunks.
  21. This behavior is controlled by the optional constructor arguments
  22. \e chunkThreshold and \e chunkSize. File sizes greater than \e
  23. chunkThreshold (in sample frames) will be read incrementally in
  24. chunks of \e chunkSize each (also in sample frames).
  25. When the file end is reached, subsequent calls to the tick()
  26. functions return zeros and isFinished() returns \e true.
  27. See the FileRead class for a description of the supported audio
  28. file formats.
  29. by Perry R. Cook and Gary P. Scavone, 1995-2011.
  30. */
  31. /***************************************************/
  32. class FileWvIn : public WvIn
  33. {
  34. public:
  35. //! Default constructor.
  36. FileWvIn( unsigned long chunkThreshold = 1000000, unsigned long chunkSize = 1024 );
  37. //! Overloaded constructor for file input.
  38. /*!
  39. An StkError will be thrown if the file is not found, its format is
  40. unknown, or a read error occurs.
  41. */
  42. FileWvIn( std::string fileName, bool raw = false, bool doNormalize = true,
  43. unsigned long chunkThreshold = 1000000, unsigned long chunkSize = 1024 );
  44. //! Class destructor.
  45. ~FileWvIn( void );
  46. //! Open the specified file and load its data.
  47. /*!
  48. Data from a previously opened file will be overwritten by this
  49. function. An StkError will be thrown if the file is not found,
  50. its format is unknown, or a read error occurs. If the file data
  51. is to be loaded incrementally from disk and normalization is
  52. specified, a scaling will be applied with respect to fixed-point
  53. limits. If the data format is floating-point, no scaling is
  54. performed.
  55. */
  56. virtual void openFile( std::string fileName, bool raw = false, bool doNormalize = true );
  57. //! Close a file if one is open.
  58. virtual void closeFile( void );
  59. //! Clear outputs and reset time (file) pointer to zero.
  60. virtual void reset( void );
  61. //! Normalize data to a maximum of +-1.0.
  62. /*!
  63. This function has no effect when data is incrementally loaded
  64. from disk.
  65. */
  66. virtual void normalize( void );
  67. //! Normalize data to a maximum of \e +-peak.
  68. /*!
  69. This function has no effect when data is incrementally loaded
  70. from disk.
  71. */
  72. virtual void normalize( StkFloat peak );
  73. //! Return the file size in sample frames.
  74. virtual unsigned long getSize( void ) const { return data_.frames(); };
  75. //! Return the input file sample rate in Hz (not the data read rate).
  76. /*!
  77. WAV, SND, and AIF formatted files specify a sample rate in
  78. their headers. STK RAW files have a sample rate of 22050 Hz
  79. by definition. MAT-files are assumed to have a rate of 44100 Hz.
  80. */
  81. virtual StkFloat getFileRate( void ) const { return data_.dataRate(); };
  82. //! Query whether a file is open.
  83. bool isOpen( void ) { return file_.isOpen(); };
  84. //! Query whether reading is complete.
  85. bool isFinished( void ) const { return finished_; };
  86. //! Set the data read rate in samples. The rate can be negative.
  87. /*!
  88. If the rate value is negative, the data is read in reverse order.
  89. */
  90. virtual void setRate( StkFloat rate );
  91. //! Increment the read pointer by \e time samples.
  92. /*!
  93. Note that this function will not modify the interpolation flag status.
  94. */
  95. virtual void addTime( StkFloat time );
  96. //! Turn linear interpolation on/off.
  97. /*!
  98. Interpolation is automatically off when the read rate is
  99. an integer value. If interpolation is turned off for a
  100. fractional rate, the time index is truncated to an integer
  101. value.
  102. */
  103. void setInterpolate( bool doInterpolate ) { interpolate_ = doInterpolate; };
  104. //! Return the specified channel value of the last computed frame.
  105. /*!
  106. If no file is loaded, the returned value is 0.0. The \c
  107. channel argument must be less than the number of output channels,
  108. which can be determined with the channelsOut() function (the first
  109. channel is specified by 0). However, range checking is only
  110. performed if _STK_DEBUG_ is defined during compilation, in which
  111. case an out-of-range value will trigger an StkError exception. \sa
  112. lastFrame()
  113. */
  114. StkFloat lastOut( unsigned int channel = 0 );
  115. //! Compute a sample frame and return the specified \c channel value.
  116. /*!
  117. For multi-channel files, use the lastFrame() function to get
  118. all values from the computed frame. If no file data is loaded,
  119. the returned value is 0.0. The \c channel argument must be less
  120. than the number of channels in the file data (the first channel is
  121. specified by 0). However, range checking is only performed if
  122. _STK_DEBUG_ is defined during compilation, in which case an
  123. out-of-range value will trigger an StkError exception.
  124. */
  125. virtual StkFloat tick( unsigned int channel = 0 );
  126. //! Fill the StkFrames argument with computed frames and return the same reference.
  127. /*!
  128. The number of channels in the StkFrames argument must equal
  129. the number of channels in the file data. However, this is only
  130. checked if _STK_DEBUG_ is defined during compilation, in which
  131. case an incompatibility will trigger an StkError exception. If no
  132. file data is loaded, the function does nothing (a warning will be
  133. issued if _STK_DEBUG_ is defined during compilation).
  134. */
  135. virtual StkFrames& tick( StkFrames& frames );
  136. protected:
  137. void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
  138. FileRead file_;
  139. bool finished_;
  140. bool interpolate_;
  141. bool normalizing_;
  142. bool chunking_;
  143. StkFloat time_;
  144. StkFloat rate_;
  145. unsigned long chunkThreshold_;
  146. unsigned long chunkSize_;
  147. long chunkPointer_;
  148. };
  149. inline StkFloat FileWvIn :: lastOut( unsigned int channel )
  150. {
  151. #if defined(_STK_DEBUG_)
  152. if ( channel >= data_.channels() ) {
  153. oStream_ << "FileWvIn::lastOut(): channel argument and soundfile data are incompatible!";
  154. handleError( StkError::FUNCTION_ARGUMENT );
  155. }
  156. #endif
  157. if ( finished_ ) return 0.0;
  158. return lastFrame_[channel];
  159. }
  160. } // stk namespace
  161. #endif