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