Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

FileWvIn Class Reference

STK audio file input class. More...

#include <FileWvIn.h>

List of all members.

Public Slots

void go ()
void stop ()
void join ()

Public Member Functions

 FileWvIn (unsigned long chunkThreshold=1000000, unsigned long chunkSize=7000)
 Default constructor.
 FileWvIn (std::string fileName, bool raw=false, bool doNormalize=true, unsigned long chunkThreshold=1000000, unsigned long chunkSize=7000)
 Overloaded constructor for file input.
 FileWvIn (bool loop, std::string fileName, bool raw=false, bool doNormalize=true, unsigned long chunkThreshold=1000000, unsigned long chunkSize=7000)
virtual ~FileWvIn ()
 Class destructor.
void openFile (std::string fileName, bool raw=false, bool doNormalize=true)
 Open the specified file and load its data.
void closeFile (void)
 Close a file if one is open.
void reset (void)
 Clear outputs and reset time (file) pointer to zero.
void normalize (void)
 Normalize data to a maximum of +-1.0.
void normalize (StkFloat peak)
 Normalize data to a maximum of +-peak.
unsigned long getSize (void) const
 Return the file size in sample frames.
StkFloat getFileRate (void) const
 Return the input file sample rate in Hz (not the data read rate).
bool isFinished (void) const
 Query whether reading is complete.
void setRate (StkFloat rate)
 Set the data read rate in samples. The rate can be negative.
virtual void addTime (StkFloat time)
 Increment the read pointer by time samples.
void setInterpolate (bool doInterpolate)
 Turn linear interpolation on/off.
StkFloat lastOut (void)
void loadFrames (void)
virtual void run ()
void setLoop (bool l)
void setGain (double g)

Public Attributes

bool _loop
bool file_loop
double _gain
bool data1old
bool data2old
QMutex mutex
StkFrames data1
StkFrames data2
int ptr

Protected Member Functions

virtual void computeFrame (void)

Protected Attributes

FileRead file_
bool finished_
bool interpolate_
bool normalizing_
bool chunking_
StkFloat time_
StkFloat rate_
unsigned long chunkThreshold_
unsigned long chunkSize_
long chunkPointer_


Detailed Description

STK audio file input class.

This class inherits from WvIn. It provides a "tick-level" interface to the FileRead class. It also provides variable-rate "playback" functionality. Audio file support is provided by the FileRead class. Linear interpolation is used for fractional "read rates".

FileWvIn supports multi-channel data. It is important to distinguish the tick() methods, which return samples produced by averaging across sample frames, from the tickFrame() methods, which return references to multi-channel sample frames.

FileWvIn will either load the entire content of an audio file into local memory or incrementally read file data from disk in chunks. This behavior is controlled by the optional constructor arguments chunkThreshold and chunkSize. File sizes greater than chunkThreshold (in sample frames) will be read incrementally in chunks of chunkSize each (also in sample frames).

When the file end is reached, subsequent calls to the tick() functions return zero-valued data and isFinished() returns true.

See the FileRead class for a description of the supported audio file formats.

by Perry R. Cook and Gary P. Scavone, 1995 - 2005.

Definition at line 42 of file FileWvIn.h.


Constructor & Destructor Documentation

FileWvIn::FileWvIn unsigned long  chunkThreshold = 1000000,
unsigned long  chunkSize = 7000
 

Default constructor.

Definition at line 38 of file FileWvIn.cpp.

00038                                                                         :
00039 finished_ (true),
00040 interpolate_ (false),
00041 time_ (0.0),
00042 chunkThreshold_ (chunkThreshold),
00043 chunkSize_ (chunkSize)
00044 {
00045 }

FileWvIn::FileWvIn std::string  fileName,
bool  raw = false,
bool  doNormalize = true,
unsigned long  chunkThreshold = 1000000,
unsigned long  chunkSize = 7000
 

Overloaded constructor for file input.

An StkError will be thrown if the file is not found, its format is unknown, or a read error occurs.

Definition at line 47 of file FileWvIn.cpp.

References _gain, _loop, file_loop, and openFile().

00048                                                                           :
00049 finished_ (true),
00050 interpolate_ (false),
00051 time_ (0.0),
00052 chunkThreshold_ (chunkThreshold),
00053 chunkSize_ (chunkSize)
00054 {
00055         openFile (fileName, raw, doNormalize);
00056         _loop = false;
00057 file_loop = false;
00058         _gain = 1.0;
00059 }

Here is the call graph for this function:

FileWvIn::FileWvIn bool  loop,
std::string  fileName,
bool  raw = false,
bool  doNormalize = true,
unsigned long  chunkThreshold = 1000000,
unsigned long  chunkSize = 7000
 

FileWvIn::~FileWvIn  )  [virtual]
 

Class destructor.

Definition at line 61 of file FileWvIn.cpp.

References closeFile().

00062 {
00063         this->closeFile ();
00064 }

Here is the call graph for this function:


Member Function Documentation

void FileWvIn::addTime StkFloat  time  )  [virtual]
 

Increment the read pointer by time samples.

Note that this function will not modify the interpolation flag status.

Definition at line 183 of file FileWvIn.cpp.

References file_, finished_, and time_.

00184 {
00185         // Add an absolute time in samples 
00186         time_ += time;
00187 
00188         if (time_ < 0.0)
00189                 time_ = 0.0;
00190         if (time_ > file_.fileSize () - 1.0)
00191         {
00192                 time_ = file_.fileSize () - 1.0;
00193                 for (unsigned int i = 0; i < lastOutputs_.size (); i++)
00194                         lastOutputs_[i] = 0.0;
00195                 finished_ = true;
00196         }
00197 }

void FileWvIn::closeFile void   ) 
 

Close a file if one is open.

Definition at line 67 of file FileWvIn.cpp.

References file_, and finished_.

Referenced by openFile(), and ~FileWvIn().

00068 {
00069         if (file_.isOpen ())
00070                 file_.close ();
00071         finished_ = true;
00072 }

void FileWvIn::computeFrame void   )  [protected, virtual]
 

Definition at line 247 of file FileWvIn.cpp.

References _gain, chunkSize_, data1, data1old, data2, data2old, finished_, mutex, and ptr.

00248 {
00249         QMutexLocker locker (&mutex);
00250         if (finished_)
00251                 return;
00252 
00253 
00255 //cout << ptr << endl;  
00256         if (ptr < chunkSize_)
00257         {
00258                 if ((ptr == 0) && (data1old == true))
00259                         cout << "behind  1" << endl;
00260                 for (unsigned int i = 0; i < lastOutputs_.size (); i++)
00261                         lastOutputs_[i] = data1 ((size_t) (double) ptr, i);
00262                 data1old = true;
00263         }
00264         else
00265         {
00266                 if ((ptr == chunkSize_) && (data2old == true))
00267                         cout << "behind  2" << endl;
00268                 for (unsigned int i = 0; i < lastOutputs_.size (); i++)
00269                         lastOutputs_[i] =
00270                                 data2 ((size_t) (double) (ptr - chunkSize_),
00271                                        i);
00272                 data2old = true;
00273         }
00275         ptr++;
00276         int mod = 2 * chunkSize_;
00277         ptr = ptr % mod;
00278                 for (unsigned int i = 0; i < lastOutputs_.size (); i++)
00279                         lastOutputs_[i] *= _gain;
00280 /*      
00281  double tmp1 = sin(6.28*0.001*(double)time_);
00282                 for (unsigned int i = 0; i < lastOutputs_.size (); i++)
00283                         lastOutputs_[i] =
00284                                 tmp1;
00285         */
00286         }

StkFloat FileWvIn::getFileRate void   )  const [inline]
 

Return the input file sample rate in Hz (not the data read rate).

WAV, SND, and AIF formatted files specify a sample rate in their headers. STK RAW files have a sample rate of 22050 Hz by definition. MAT-files are assumed to have a rate of 44100 Hz.

Definition at line 104 of file FileWvIn.h.

00104 { return data_.dataRate(); };

unsigned long FileWvIn::getSize void   )  const [inline]
 

Return the file size in sample frames.

Definition at line 96 of file FileWvIn.h.

00096 { return data_.frames(); };

void FileWvIn::go  )  [slot]
 

Definition at line 305 of file FileWvIn.cpp.

Referenced by Jukebox::ambiOn(), Jukebox::maskOn(), and Jukebox::talkOn().

00307 {
00308   //cout << "Starting loop" << endl;
00309         start ();
00310 }

bool FileWvIn::isFinished void   )  const [inline]
 

Query whether reading is complete.

Definition at line 107 of file FileWvIn.h.

References finished_.

00107 { return finished_; };

void FileWvIn::join  )  [slot]
 

Definition at line 320 of file FileWvIn.cpp.

00322 {
00323         wait ();
00324 }

StkFloat FileWvIn::lastOut void   ) 
 

Definition at line 200 of file FileWvIn.cpp.

References finished_.

00201 {
00202         if (finished_)
00203                 return 0.0;
00204         return WvIn::lastOut ();
00205 }

void FileWvIn::loadFrames void   ) 
 

Definition at line 208 of file FileWvIn.cpp.

References chunkPointer_, chunkSize_, data1, data1old, data2, data2old, file_, file_loop, finished_, mutex, normalizing_, and ptr.

Referenced by run().

00209 {
00210         QMutexLocker locker (&mutex);
00211         if (finished_)
00212                 return;
00213         if (chunkPointer_ > (file_.fileSize ()-1))
00214                 if (file_loop)  
00215                 {
00216                         chunkPointer_ = 0;
00217                         //cout << "repeat" << endl;
00218                 }
00219                         else 
00220                         {
00221                                 finished_ = true;
00222                cout << "finish" << endl;
00223                         }
00224         if (ptr < chunkSize_)
00225         {
00226                 if (data2old)
00227                 {
00228                         file_.read (data2, chunkPointer_, normalizing_);
00229                         data2old = false;
00230                         chunkPointer_ += chunkSize_;
00231 //               cout << "loaded data2" << endl;
00232                 }
00233         }
00234         else
00235         {
00236                 if (data1old)
00237                 {
00238                         file_.read (data1, chunkPointer_, normalizing_);
00239                         data1old = false;
00240                         chunkPointer_ += chunkSize_;
00241 //               cout << "loaded data1" << endl;
00242                 }
00243         }
00244 }

void FileWvIn::normalize StkFloat  peak  ) 
 

Normalize data to a maximum of +-peak.

This function has no effect when data is incrementally loaded from disk.

Definition at line 142 of file FileWvIn.cpp.

References chunking_.

00143 {
00144         // When chunking, the "normalization" scaling is performed by FileRead.
00145         if (chunking_)
00146                 return;
00147 
00148         size_t i;
00149         StkFloat max = 0.0;
00150 
00151         for (i = 0; i < data_.size (); i++)
00152         {
00153                 if (fabs (data_[i]) > max)
00154                         max = (StkFloat) fabs ((double) data_[i]);
00155         }
00156 
00157         if (max > 0.0)
00158         {
00159                 max = 1.0 / max;
00160                 max *= peak;
00161                 for (i = 0; i < data_.size (); i++)
00162                         data_[i] *= max;
00163         }
00164 }

void FileWvIn::normalize void   ) 
 

Normalize data to a maximum of +-1.0.

This function has no effect when data is incrementally loaded from disk.

Definition at line 135 of file FileWvIn.cpp.

Referenced by openFile().

00136 {
00137         this->normalize (1.0);
00138 }

void FileWvIn::openFile std::string  fileName,
bool  raw = false,
bool  doNormalize = true
 

Open the specified file and load its data.

Data from a previously opened file will be overwritten by this function. An StkError will be thrown if the file is not found, its format is unknown, or a read error occurs. If the file data is to be loaded incrementally from disk and normalization is specified, a scaling will be applied with respect to fixed-point limits. If the data format is floating-point, no scaling is performed.

Definition at line 75 of file FileWvIn.cpp.

References chunking_, chunkPointer_, chunkSize_, closeFile(), data1, data2, file_, normalize(), normalizing_, reset(), and setRate().

Referenced by FileWvIn().

00076 {
00077         // Call close() in case another file is already open.
00078         this->closeFile ();
00079 
00080         // Attempt to open the file ... an error might be thrown here.
00081         file_.open (fileName, raw);
00082 
00083         // Determine whether chunking or not.
00084         if (true)               // always
00085                 //( file_.fileSize() > chunkThreshold_ ) 
00086         {
00087                 chunking_ = true;
00088                 chunkPointer_ = 0;
00089                 data_.resize (chunkSize_, file_.channels ());
00090                 data1.resize (chunkSize_, file_.channels ());
00091                 data2.resize (chunkSize_, file_.channels ());
00092                 if (doNormalize)
00093                         normalizing_ = true;
00094                 else
00095                         normalizing_ = false;
00096         }
00097         else
00098         {
00099                 chunking_ = false;
00100                 data_.resize ((size_t) file_.fileSize (), file_.channels ());
00101         }
00102 
00103         // Load all or part of the data.
00104         file_.read (data_, 0, doNormalize);
00105 
00106         // Resize our lastOutputs container.
00107         lastOutputs_.resize (1, file_.channels ());
00108 
00109         // Set default rate based on file sampling rate.
00110         this->setRate (data_.dataRate () / Stk::sampleRate ());
00111 
00112         if (doNormalize & !chunking_)
00113                 this->normalize ();
00114 
00115         this->reset ();
00116 }

Here is the call graph for this function:

void FileWvIn::reset void   ) 
 

Clear outputs and reset time (file) pointer to zero.

Definition at line 119 of file FileWvIn.cpp.

References chunkPointer_, data1, data1old, data2, data2old, file_, finished_, mutex, normalizing_, and ptr.

Referenced by openFile().

00120 {
00121         QMutexLocker locker (&mutex);
00122 //      time_ = (StkFloat) 0.0;
00123         for (unsigned int i = 0; i < lastOutputs_.size (); i++)
00124                 lastOutputs_[i] = 0.0;
00125         finished_ = false;
00126         ptr = 0;                // cc
00127         file_.read (data1, chunkPointer_, normalizing_);
00128         file_.read (data2, chunkPointer_, normalizing_);
00129         data1old = false;
00130         data2old = true;        // to trigger next read
00131 //      cout << "init loaded data1" << endl;
00132 }

void FileWvIn::run  )  [virtual]
 

Definition at line 291 of file FileWvIn.cpp.

References _loop, loadFrames(), and set_fifo_priority().

00293 {
00294         _loop = true;
00295 
00296         set_fifo_priority (true);
00297         while (_loop)
00298         {
00299                 loadFrames ();
00300                 msleep (10);
00301         }
00302 }

Here is the call graph for this function:

void FileWvIn::setGain double  g  )  [inline]
 

Definition at line 139 of file FileWvIn.h.

References _gain.

Referenced by Jukebox::openFiles().

00139 {_gain = g;};

void FileWvIn::setInterpolate bool  doInterpolate  )  [inline]
 

Turn linear interpolation on/off.

Interpolation is automatically off when the read rate is an integer value. If interpolation is turned off for a fractional rate, the time index is truncated to an integer value.

Definition at line 128 of file FileWvIn.h.

References interpolate_.

00128 { interpolate_ = doInterpolate; };

void FileWvIn::setLoop bool  l  )  [inline]
 

Definition at line 138 of file FileWvIn.h.

References file_loop.

Referenced by Jukebox::openFiles().

00138 {file_loop = l;};

void FileWvIn::setRate StkFloat  rate  ) 
 

Set the data read rate in samples. The rate can be negative.

If the rate value is negative, the data is read in reverse order.

Definition at line 167 of file FileWvIn.cpp.

References file_, interpolate_, rate_, and time_.

Referenced by openFile().

00168 {
00169         rate_ = rate;
00170 
00171         // If negative rate and at beginning of sound, move pointer to end
00172         // of sound.
00173         if ((rate_ < 0) && (time_ == 0.0))
00174                 time_ = file_.fileSize () - 1.0;
00175 
00176         if (fmod (rate_, 1.0) != 0.0)
00177                 interpolate_ = true;
00178         else
00179                 interpolate_ = false;
00180 }

void FileWvIn::stop  )  [slot]
 

Definition at line 313 of file FileWvIn.cpp.

References _loop.

Referenced by Jukebox::stopPlay().

00315 {
00316         _loop = false;
00317 }


Member Data Documentation

double FileWvIn::_gain
 

Definition at line 137 of file FileWvIn.h.

Referenced by computeFrame(), FileWvIn(), and setGain().

bool FileWvIn::_loop
 

Definition at line 135 of file FileWvIn.h.

Referenced by FileWvIn(), run(), and stop().

bool FileWvIn::chunking_ [protected]
 

Definition at line 160 of file FileWvIn.h.

Referenced by normalize(), and openFile().

long FileWvIn::chunkPointer_ [protected]
 

Definition at line 165 of file FileWvIn.h.

Referenced by loadFrames(), openFile(), and reset().

unsigned long FileWvIn::chunkSize_ [protected]
 

Definition at line 164 of file FileWvIn.h.

Referenced by computeFrame(), loadFrames(), and openFile().

unsigned long FileWvIn::chunkThreshold_ [protected]
 

Definition at line 163 of file FileWvIn.h.

StkFrames FileWvIn::data1
 

Definition at line 143 of file FileWvIn.h.

Referenced by computeFrame(), loadFrames(), openFile(), and reset().

bool FileWvIn::data1old
 

Definition at line 139 of file FileWvIn.h.

Referenced by computeFrame(), loadFrames(), and reset().

StkFrames FileWvIn::data2
 

Definition at line 144 of file FileWvIn.h.

Referenced by computeFrame(), loadFrames(), openFile(), and reset().

bool FileWvIn::data2old
 

Definition at line 141 of file FileWvIn.h.

Referenced by computeFrame(), loadFrames(), and reset().

FileRead FileWvIn::file_ [protected]
 

Definition at line 156 of file FileWvIn.h.

Referenced by addTime(), closeFile(), loadFrames(), openFile(), reset(), and setRate().

bool FileWvIn::file_loop
 

Definition at line 136 of file FileWvIn.h.

Referenced by FileWvIn(), loadFrames(), and setLoop().

bool FileWvIn::finished_ [protected]
 

Definition at line 157 of file FileWvIn.h.

Referenced by addTime(), closeFile(), computeFrame(), isFinished(), lastOut(), loadFrames(), and reset().

bool FileWvIn::interpolate_ [protected]
 

Definition at line 158 of file FileWvIn.h.

Referenced by setInterpolate(), and setRate().

QMutex FileWvIn::mutex
 

Definition at line 142 of file FileWvIn.h.

Referenced by computeFrame(), loadFrames(), and reset().

bool FileWvIn::normalizing_ [protected]
 

Definition at line 159 of file FileWvIn.h.

Referenced by loadFrames(), openFile(), and reset().

int FileWvIn::ptr
 

Definition at line 146 of file FileWvIn.h.

Referenced by computeFrame(), loadFrames(), and reset().

StkFloat FileWvIn::rate_ [protected]
 

Definition at line 162 of file FileWvIn.h.

Referenced by setRate().

StkFloat FileWvIn::time_ [protected]
 

Definition at line 161 of file FileWvIn.h.

Referenced by addTime(), and setRate().


The documentation for this class was generated from the following files:
Generated on Thu Aug 3 16:14:52 2006 by  doxygen 1.4.4