| 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 end of a file is reached, subsequent calls to the tick() |
| 24 |
functions return zero-valued data. |
| 25 |
|
| 26 |
See the FileRead class for a description of the supported audio |
| 27 |
file formats. |
| 28 |
|
| 29 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 30 |
*/ |
| 31 |
/***************************************************/ |
| 32 |
|
| 33 |
#include "FileWvIn.h" |
| 34 |
#include <cmath> |
| 35 |
|
| 36 |
using namespace Nyq; |
| 37 |
|
| 38 |
FileWvIn :: FileWvIn( unsigned long chunkThreshold, unsigned long chunkSize ) |
| 39 |
: finished_(true), interpolate_(false), time_(0.0), |
| 40 |
chunkThreshold_(chunkThreshold), chunkSize_(chunkSize) |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
FileWvIn :: FileWvIn( std::string fileName, bool raw, bool doNormalize, |
| 45 |
unsigned long chunkThreshold, unsigned long chunkSize ) |
| 46 |
: finished_(true), interpolate_(false), time_(0.0), |
| 47 |
chunkThreshold_(chunkThreshold), chunkSize_(chunkSize) |
| 48 |
{ |
| 49 |
openFile( fileName, raw, doNormalize ); |
| 50 |
} |
| 51 |
|
| 52 |
FileWvIn :: ~FileWvIn() |
| 53 |
{ |
| 54 |
this->closeFile(); |
| 55 |
} |
| 56 |
|
| 57 |
void FileWvIn :: closeFile( void ) |
| 58 |
{ |
| 59 |
if ( file_.isOpen() ) file_.close(); |
| 60 |
finished_ = true; |
| 61 |
} |
| 62 |
|
| 63 |
void FileWvIn :: openFile( std::string fileName, bool raw, bool doNormalize ) |
| 64 |
{ |
| 65 |
// Call close() in case another file is already open. |
| 66 |
this->closeFile(); |
| 67 |
|
| 68 |
// Attempt to open the file ... an error might be thrown here. |
| 69 |
file_.open( fileName, raw ); |
| 70 |
|
| 71 |
// Determine whether chunking or not. |
| 72 |
if ( file_.fileSize() > chunkThreshold_ ) { |
| 73 |
chunking_ = true; |
| 74 |
chunkPointer_ = 0; |
| 75 |
data_.resize( chunkSize_, file_.channels() ); |
| 76 |
if ( doNormalize ) normalizing_ = true; |
| 77 |
else normalizing_ = false; |
| 78 |
} |
| 79 |
else { |
| 80 |
chunking_ = false; |
| 81 |
data_.resize( (size_t) file_.fileSize(), file_.channels() ); |
| 82 |
} |
| 83 |
|
| 84 |
// Load all or part of the data. |
| 85 |
file_.read( data_, 0, doNormalize ); |
| 86 |
|
| 87 |
// Resize our lastOutputs container. |
| 88 |
lastOutputs_.resize( 1, file_.channels() ); |
| 89 |
|
| 90 |
// Set default rate based on file sampling rate. |
| 91 |
this->setRate( data_.dataRate() / Stk::sampleRate() ); |
| 92 |
|
| 93 |
if ( doNormalize & !chunking_ ) this->normalize(); |
| 94 |
|
| 95 |
this->reset(); |
| 96 |
} |
| 97 |
|
| 98 |
void FileWvIn :: reset(void) |
| 99 |
{ |
| 100 |
time_ = (StkFloat) 0.0; |
| 101 |
for ( unsigned int i=0; i<lastOutputs_.size(); i++ ) |
| 102 |
lastOutputs_[i] = 0.0; |
| 103 |
finished_ = false; |
| 104 |
} |
| 105 |
|
| 106 |
void FileWvIn :: normalize(void) |
| 107 |
{ |
| 108 |
this->normalize( 1.0 ); |
| 109 |
} |
| 110 |
|
| 111 |
// Normalize all channels equally by the greatest magnitude in all of the data. |
| 112 |
void FileWvIn :: normalize( StkFloat peak ) |
| 113 |
{ |
| 114 |
// When chunking, the "normalization" scaling is performed by FileRead. |
| 115 |
if ( chunking_ ) return; |
| 116 |
|
| 117 |
size_t i; |
| 118 |
StkFloat max = 0.0; |
| 119 |
|
| 120 |
for ( i=0; i<data_.size(); i++ ) { |
| 121 |
if ( fabs( data_[i] ) > max ) |
| 122 |
max = (StkFloat) fabs((double) data_[i]); |
| 123 |
} |
| 124 |
|
| 125 |
if (max > 0.0) { |
| 126 |
max = 1.0 / max; |
| 127 |
max *= peak; |
| 128 |
for ( i=0; i<data_.size(); i++ ) |
| 129 |
data_[i] *= max; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void FileWvIn :: setRate( StkFloat rate ) |
| 134 |
{ |
| 135 |
rate_ = rate; |
| 136 |
|
| 137 |
// If negative rate and at beginning of sound, move pointer to end |
| 138 |
// of sound. |
| 139 |
if ( (rate_ < 0) && (time_ == 0.0) ) time_ = file_.fileSize() - 1.0; |
| 140 |
|
| 141 |
if ( fmod( rate_, 1.0 ) != 0.0 ) interpolate_ = true; |
| 142 |
else interpolate_ = false; |
| 143 |
} |
| 144 |
|
| 145 |
void FileWvIn :: addTime( StkFloat time ) |
| 146 |
{ |
| 147 |
// Add an absolute time in samples |
| 148 |
time_ += time; |
| 149 |
|
| 150 |
if ( time_ < 0.0 ) time_ = 0.0; |
| 151 |
if ( time_ > file_.fileSize() - 1.0 ) { |
| 152 |
time_ = file_.fileSize() - 1.0; |
| 153 |
for ( unsigned int i=0; i<lastOutputs_.size(); i++ ) |
| 154 |
lastOutputs_[i] = 0.0; |
| 155 |
finished_ = true; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
StkFloat FileWvIn :: lastOut( void ) const |
| 160 |
{ |
| 161 |
if ( finished_ ) return 0.0; |
| 162 |
return WvIn :: lastOut(); |
| 163 |
} |
| 164 |
|
| 165 |
void FileWvIn :: computeFrame( void ) |
| 166 |
{ |
| 167 |
if ( finished_ ) return; |
| 168 |
|
| 169 |
if ( time_ < 0.0 || time_ > (StkFloat) ( file_.fileSize() - 1.0 ) ) { |
| 170 |
for ( unsigned int i=0; i<lastOutputs_.size(); i++ ) |
| 171 |
lastOutputs_[i] = 0.0; |
| 172 |
finished_ = true; |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
StkFloat tyme = time_; |
| 177 |
if ( chunking_ ) { |
| 178 |
|
| 179 |
// Check the time address vs. our current buffer limits. |
| 180 |
if ( ( time_ < (StkFloat) chunkPointer_ ) || |
| 181 |
( time_ > (StkFloat) ( chunkPointer_ + chunkSize_ - 1 ) ) ) { |
| 182 |
|
| 183 |
while ( time_ < (StkFloat) chunkPointer_ ) { // negative rate |
| 184 |
chunkPointer_ -= chunkSize_ - 1; // overlap chunks by one frame |
| 185 |
if ( chunkPointer_ < 0 ) chunkPointer_ = 0; |
| 186 |
} |
| 187 |
while ( time_ > (StkFloat) ( chunkPointer_ + chunkSize_ - 1 ) ) { // positive rate |
| 188 |
chunkPointer_ += chunkSize_ - 1; // overlap chunks by one frame |
| 189 |
if ( chunkPointer_ + chunkSize_ > file_.fileSize() ) // at end of file |
| 190 |
chunkPointer_ = file_.fileSize() - chunkSize_; |
| 191 |
} |
| 192 |
|
| 193 |
// Load more data. |
| 194 |
file_.read( data_, chunkPointer_, normalizing_ ); |
| 195 |
} |
| 196 |
|
| 197 |
// Adjust index for the current buffer. |
| 198 |
tyme -= chunkPointer_; |
| 199 |
} |
| 200 |
|
| 201 |
if ( interpolate_ ) { |
| 202 |
for ( unsigned int i=0; i<lastOutputs_.size(); i++ ) |
| 203 |
lastOutputs_[i] = data_.interpolate( tyme, i ); |
| 204 |
} |
| 205 |
else { |
| 206 |
for ( unsigned int i=0; i<lastOutputs_.size(); i++ ) |
| 207 |
lastOutputs_[i] = data_( (size_t) tyme, i ); |
| 208 |
} |
| 209 |
|
| 210 |
// Increment time, which can be negative. |
| 211 |
time_ += rate_; |
| 212 |
} |
| 213 |
|