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.

166 lines
6.2KB

  1. #ifndef STK_FILELOOP_H
  2. #define STK_FILELOOP_H
  3. #include "FileWvIn.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class FileLoop
  7. \brief STK file looping / oscillator class.
  8. This class provides audio file looping functionality. Any audio
  9. file that can be loaded by FileRead can be looped using this
  10. class.
  11. FileLoop supports multi-channel data. It is important to
  12. distinguish the tick() method that computes a single frame (and
  13. returns only the specified sample of a multi-channel frame) from
  14. the overloaded one that takes an StkFrames object for
  15. multi-channel and/or multi-frame data.
  16. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  17. */
  18. /***************************************************/
  19. class FileLoop : protected FileWvIn
  20. {
  21. public:
  22. //! Default constructor.
  23. FileLoop( unsigned long chunkThreshold = 1000000, unsigned long chunkSize = 1024 );
  24. //! Class constructor that opens a specified file.
  25. FileLoop( std::string fileName, bool raw = false, bool doNormalize = true,
  26. unsigned long chunkThreshold = 1000000, unsigned long chunkSize = 1024,
  27. bool doInt2FloatScaling = true );
  28. //! Class destructor.
  29. ~FileLoop( void );
  30. //! Open the specified file and load its data.
  31. /*!
  32. Data from a previously opened file will be overwritten by this
  33. function. An StkError will be thrown if the file is not found,
  34. its format is unknown, or a read error occurs. If the file length
  35. is less than the chunkThreshold limit and \e doNormalize is true,
  36. the file data will be normalized with respect to the maximum absolute
  37. value of the data. If the \e doInt2FloatScaling flag is true and the
  38. input data is fixed-point, a scaling will be applied with respect to
  39. the fixed-point limits.
  40. */
  41. void openFile( std::string fileName, bool raw = false, bool doNormalize = true, bool doInt2FloatScaling = true );
  42. //! Close a file if one is open.
  43. void closeFile( void ) { FileWvIn::closeFile(); };
  44. //! Clear outputs and reset time (file) pointer to zero.
  45. void reset( void ) { FileWvIn::reset(); };
  46. //! Return the number of audio channels in the data or stream.
  47. unsigned int channelsOut( void ) const { return data_.channels(); };
  48. //! Normalize data to a maximum of +-1.0.
  49. /*!
  50. This function has no effect when data is incrementally loaded
  51. from disk.
  52. */
  53. void normalize( void ) { FileWvIn::normalize( 1.0 ); };
  54. //! Normalize data to a maximum of \e +-peak.
  55. /*!
  56. This function has no effect when data is incrementally loaded
  57. from disk.
  58. */
  59. void normalize( StkFloat peak ) { FileWvIn::normalize( peak ); };
  60. //! Return the file size in sample frames.
  61. //unsigned long getSize( void ) const { return data_.frames(); };
  62. unsigned long getSize( void ) const { return fileSize_; };
  63. //! Return the input file sample rate in Hz (not the data read rate).
  64. /*!
  65. WAV, SND, and AIF formatted files specify a sample rate in
  66. their headers. STK RAW files have a sample rate of 22050 Hz
  67. by definition. MAT-files are assumed to have a rate of 44100 Hz.
  68. */
  69. StkFloat getFileRate( void ) const { return data_.dataRate(); };
  70. //! Set the data read rate in samples. The rate can be negative.
  71. /*!
  72. If the rate value is negative, the data is read in reverse order.
  73. */
  74. void setRate( StkFloat rate );
  75. //! Set the data interpolation rate based on a looping frequency.
  76. /*!
  77. This function determines the interpolation rate based on the file
  78. size and the current Stk::sampleRate. The \e frequency value
  79. corresponds to file cycles per second. The frequency can be
  80. negative, in which case the loop is read in reverse order.
  81. */
  82. void setFrequency( StkFloat frequency ) { this->setRate( fileSize_ * frequency / Stk::sampleRate() ); };
  83. //! Increment the read pointer by \e time samples, modulo file size.
  84. void addTime( StkFloat time );
  85. //! Increment current read pointer by \e angle, relative to a looping frequency.
  86. /*!
  87. This function increments the read pointer based on the file
  88. size and the current Stk::sampleRate. The \e anAngle value
  89. is a multiple of file size.
  90. */
  91. void addPhase( StkFloat angle );
  92. //! Add a phase offset to the current read pointer.
  93. /*!
  94. This function determines a time offset based on the file
  95. size and the current Stk::sampleRate. The \e angle value
  96. is a multiple of file size.
  97. */
  98. void addPhaseOffset( StkFloat angle );
  99. //! Return the specified channel value of the last computed frame.
  100. /*!
  101. For multi-channel files, use the lastFrame() function to get
  102. all values from the last computed frame. If no file data is
  103. loaded, the returned value is 0.0. The \c channel argument must
  104. be less than the number of channels in the file data (the first
  105. channel is specified by 0). However, range checking is only
  106. performed if _STK_DEBUG_ is defined during compilation, in which
  107. case an out-of-range value will trigger an StkError exception.
  108. */
  109. StkFloat lastOut( unsigned int channel = 0 ) { return FileWvIn::lastOut( channel ); };
  110. //! Compute a sample frame and return the specified \c channel value.
  111. /*!
  112. For multi-channel files, use the lastFrame() function to get
  113. all values from the computed frame. If no file data is loaded,
  114. the returned value is 0.0. The \c channel argument must be less
  115. than the number of channels in the file data (the first channel is
  116. specified by 0). However, range checking is only performed if
  117. _STK_DEBUG_ is defined during compilation, in which case an
  118. out-of-range value will trigger an StkError exception.
  119. */
  120. StkFloat tick( unsigned int channel = 0 );
  121. //! Fill the StkFrames object with computed sample frames, starting at the specified channel and return the same reference.
  122. /*!
  123. The \c channel argument plus the number of output channels must
  124. be less than the number of channels in the StkFrames argument (the
  125. first channel is specified by 0). However, range checking is only
  126. performed if _STK_DEBUG_ is defined during compilation, in which
  127. case an out-of-range value will trigger an StkError exception.
  128. */
  129. virtual StkFrames& tick( StkFrames& frames,unsigned int channel = 0 );
  130. protected:
  131. StkFrames firstFrame_;
  132. StkFloat phaseOffset_;
  133. };
  134. } // stk namespace
  135. #endif