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.

209 lines
7.5KB

  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. For file data read completely into local memory, the \e doNormalize
  26. flag can be used to normalize all values with respect to the maximum
  27. absolute value of the data.
  28. If the file data format is fixed point, the flag \e doInt2FloatScaling
  29. can be used to control whether the values are scaled with respect to
  30. the corresponding fixed-point maximum. For example, if reading 16-bit
  31. signed integers, the input values will be scaled by 1 / 32768.0. This
  32. scaling will not happen for floating-point file data formats.
  33. When the file end is reached, subsequent calls to the tick()
  34. functions return zeros and isFinished() returns \e true.
  35. See the FileRead class for a description of the supported audio
  36. file formats.
  37. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  38. */
  39. /***************************************************/
  40. class FileWvIn : public WvIn
  41. {
  42. public:
  43. //! Default constructor.
  44. FileWvIn( unsigned long chunkThreshold = 1000000, unsigned long chunkSize = 1024 );
  45. //! Overloaded constructor for file input.
  46. /*!
  47. An StkError will be thrown if the file is not found, its format is
  48. unknown, or a read error occurs.
  49. */
  50. FileWvIn( std::string fileName, bool raw = false, bool doNormalize = true,
  51. unsigned long chunkThreshold = 1000000, unsigned long chunkSize = 1024,
  52. bool doInt2FloatScaling = true );
  53. //! Class destructor.
  54. ~FileWvIn( void );
  55. //! Open the specified file and load its data.
  56. /*!
  57. Data from a previously opened file will be overwritten by this
  58. function. An StkError will be thrown if the file is not found,
  59. its format is unknown, or a read error occurs. If the file length
  60. is less than the chunkThreshold limit and \e doNormalize is true,
  61. the file data will be normalized with respect to the maximum absolute
  62. value of the data. If the \e doInt2FloatScaling flag is true and the
  63. input data is fixed-point, a scaling will be applied with respect to
  64. the fixed-point limits.
  65. */
  66. virtual void openFile( std::string fileName, bool raw = false, bool doNormalize = true, bool doInt2FloatScaling = true );
  67. //! Close a file if one is open.
  68. virtual void closeFile( void );
  69. //! Clear outputs and reset time (file) pointer to zero.
  70. virtual void reset( void );
  71. //! Normalize data to a maximum of +-1.0.
  72. /*!
  73. This function has no effect when data is incrementally loaded
  74. from disk.
  75. */
  76. virtual void normalize( void );
  77. //! Normalize data to a maximum of \e +-peak.
  78. /*!
  79. This function has no effect when data is incrementally loaded
  80. from disk.
  81. */
  82. virtual void normalize( StkFloat peak );
  83. //! Return the file size in sample frames.
  84. virtual unsigned long getSize( void ) const { return fileSize_; };
  85. //! Return the input file sample rate in Hz (not the data read rate).
  86. /*!
  87. WAV, SND, and AIF formatted files specify a sample rate in
  88. their headers. STK RAW files have a sample rate of 22050 Hz
  89. by definition. MAT-files are assumed to have a rate of 44100 Hz.
  90. */
  91. virtual StkFloat getFileRate( void ) const { return data_.dataRate(); };
  92. //! Query whether a file is open.
  93. bool isOpen( void ) { return file_.isOpen(); };
  94. //! Query whether reading is complete.
  95. bool isFinished( void ) const { return finished_; };
  96. //! Set the data read rate in samples. The rate can be negative.
  97. /*!
  98. If the rate value is negative, the data is read in reverse order.
  99. */
  100. virtual void setRate( StkFloat rate );
  101. //! Increment the read pointer by \e time samples.
  102. /*!
  103. Note that this function will not modify the interpolation flag status.
  104. */
  105. virtual void addTime( StkFloat time );
  106. //! Turn linear interpolation on/off.
  107. /*!
  108. Interpolation is automatically off when the read rate is
  109. an integer value. If interpolation is turned off for a
  110. fractional rate, the time index is truncated to an integer
  111. value.
  112. */
  113. void setInterpolate( bool doInterpolate ) { interpolate_ = doInterpolate; };
  114. //! Return the specified channel value of the last computed frame.
  115. /*!
  116. If no file is loaded, the returned value is 0.0. The \c
  117. channel argument must be less than the number of output channels,
  118. which can be determined with the channelsOut() function (the first
  119. channel is specified by 0). However, range checking is only
  120. performed if _STK_DEBUG_ is defined during compilation, in which
  121. case an out-of-range value will trigger an StkError exception. \sa
  122. lastFrame()
  123. */
  124. StkFloat lastOut( unsigned int channel = 0 );
  125. //! Compute a sample frame and return the specified \c channel value.
  126. /*!
  127. For multi-channel files, use the lastFrame() function to get
  128. all values from the computed frame. If no file data is loaded,
  129. the returned value is 0.0. The \c channel argument must be less
  130. than the number of channels in the file data (the first channel is
  131. specified by 0). However, range checking is only performed if
  132. _STK_DEBUG_ is defined during compilation, in which case an
  133. out-of-range value will trigger an StkError exception.
  134. */
  135. virtual StkFloat tick( unsigned int channel = 0 );
  136. //! Fill the StkFrames object with computed sample frames, starting at the specified channel and return the same reference.
  137. /*!
  138. The \c channel argument plus the number of input channels must
  139. be less than the number of channels in the StkFrames argument (the
  140. first channel is specified by 0). However, range checking is only
  141. performed if _STK_DEBUG_ is defined during compilation, in which
  142. case an out-of-range value will trigger an StkError exception.
  143. */
  144. virtual StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  145. protected:
  146. void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
  147. FileRead file_;
  148. bool finished_;
  149. bool interpolate_;
  150. bool int2floatscaling_;
  151. bool chunking_;
  152. StkFloat time_;
  153. StkFloat rate_;
  154. unsigned long fileSize_;
  155. unsigned long chunkThreshold_;
  156. unsigned long chunkSize_;
  157. long chunkPointer_;
  158. };
  159. inline StkFloat FileWvIn :: lastOut( unsigned int channel )
  160. {
  161. #if defined(_STK_DEBUG_)
  162. if ( channel >= data_.channels() ) {
  163. oStream_ << "FileWvIn::lastOut(): channel argument and soundfile data are incompatible!";
  164. handleError( StkError::FUNCTION_ARGUMENT );
  165. }
  166. #endif
  167. if ( finished_ ) return 0.0;
  168. return lastFrame_[channel];
  169. }
  170. } // stk namespace
  171. #endif