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