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

FileWvIn.cpp

Go to the documentation of this file.
00001 /***************************************************/
00031 /***************************************************/
00032 
00033 #include "../include/FileWvIn.h"
00034 #include <cmath>
00035 #include <iostream>
00036 using namespace std;
00037 
00038 FileWvIn::FileWvIn (unsigned long chunkThreshold, unsigned long chunkSize):
00039 finished_ (true),
00040 interpolate_ (false),
00041 time_ (0.0),
00042 chunkThreshold_ (chunkThreshold),
00043 chunkSize_ (chunkSize)
00044 {
00045 }
00046 
00047 FileWvIn::FileWvIn (std::string fileName, bool raw, bool doNormalize,
00048                     unsigned long chunkThreshold, unsigned long chunkSize):
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 }
00060 
00061 FileWvIn::~FileWvIn ()
00062 {
00063         this->closeFile ();
00064 }
00065 
00066 void
00067 FileWvIn::closeFile (void)
00068 {
00069         if (file_.isOpen ())
00070                 file_.close ();
00071         finished_ = true;
00072 }
00073 
00074 void
00075 FileWvIn::openFile (std::string fileName, bool raw, bool doNormalize)
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 }
00117 
00118 void
00119 FileWvIn::reset (void)
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 }
00133 
00134 void
00135 FileWvIn::normalize (void)
00136 {
00137         this->normalize (1.0);
00138 }
00139 
00140 // Normalize all channels equally by the greatest magnitude in all of the data.
00141 void
00142 FileWvIn::normalize (StkFloat peak)
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 }
00165 
00166 void
00167 FileWvIn::setRate (StkFloat rate)
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 }
00181 
00182 void
00183 FileWvIn::addTime (StkFloat 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 }
00198 
00199 StkFloat
00200 FileWvIn::lastOut (void) 
00201 {
00202         if (finished_)
00203                 return 0.0;
00204         return WvIn::lastOut ();
00205 }
00206 
00207 void
00208 FileWvIn::loadFrames (void)
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 }
00245 
00246 void
00247 FileWvIn::computeFrame (void)
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         }
00287 
00288 extern int set_fifo_priority (bool half);
00289 
00290 void
00291 FileWvIn::run ()
00292 /* start thread */
00293 {
00294         _loop = true;
00295 
00296         set_fifo_priority (true);
00297         while (_loop)
00298         {
00299                 loadFrames ();
00300                 msleep (10);
00301         }
00302 }
00303 
00304 void
00305 FileWvIn::go ()
00306 /* cause run loop to start */
00307 {
00308   //cout << "Starting loop" << endl;
00309         start ();
00310 }
00311 
00312 void
00313 FileWvIn::stop ()
00314 /* cause run loop to finish */
00315 {
00316         _loop = false;
00317 }
00318 
00319 void
00320 FileWvIn::join ()
00321 /* wait for thread to exit */
00322 {
00323         wait ();
00324 }

Generated on Thu Aug 3 16:14:47 2006 by  doxygen 1.4.4