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