Fix for FileWvIn and FileLoop classes so that files are closed unless chunking.

This commit is contained in:
Gary Scavone
2016-02-03 16:37:51 -05:00
parent f0f6668f25
commit 83b75ed339
4 changed files with 33 additions and 28 deletions

View File

@@ -95,7 +95,7 @@ class FileLoop : protected FileWvIn
corresponds to file cycles per second. The frequency can be
negative, in which case the loop is read in reverse order.
*/
void setFrequency( StkFloat frequency ) { this->setRate( file_.fileSize() * frequency / Stk::sampleRate() ); };
void setFrequency( StkFloat frequency ) { this->setRate( fileSize_ * frequency / Stk::sampleRate() ); };
//! Increment the read pointer by \e time samples, modulo file size.
void addTime( StkFloat time );

View File

@@ -89,7 +89,7 @@ public:
virtual void normalize( StkFloat peak );
//! Return the file size in sample frames.
virtual unsigned long getSize( void ) const { return file_.fileSize(); };
virtual unsigned long getSize( void ) const { return fileSize_; };
//! Return the input file sample rate in Hz (not the data read rate).
/*!
@@ -171,6 +171,7 @@ protected:
bool chunking_;
StkFloat time_;
StkFloat rate_;
unsigned long fileSize_;
unsigned long chunkThreshold_;
unsigned long chunkSize_;
long chunkPointer_;

View File

@@ -77,6 +77,10 @@ void FileLoop :: openFile( std::string fileName, bool raw, bool doNormalize )
// Resize our lastOutputs container.
lastFrame_.resize( 1, file_.channels() );
// Close the file unless chunking
fileSize_ = file_.fileSize();
if ( !chunking_ ) file_.close();
// Set default rate based on file sampling rate.
this->setRate( data_.dataRate() / Stk::sampleRate() );
@@ -98,29 +102,27 @@ void FileLoop :: addTime( StkFloat time )
// Add an absolute time in samples.
time_ += time;
StkFloat fileSize = file_.fileSize();
while ( time_ < 0.0 )
time_ += fileSize;
while ( time_ >= fileSize )
time_ -= fileSize;
time_ += fileSize_;
while ( time_ >= fileSize_ )
time_ -= fileSize_;
}
void FileLoop :: addPhase( StkFloat angle )
{
// Add a time in cycles (one cycle = fileSize).
StkFloat fileSize = file_.fileSize();
time_ += fileSize * angle;
time_ += fileSize_ * angle;
while ( time_ < 0.0 )
time_ += fileSize;
while ( time_ >= fileSize )
time_ -= fileSize;
time_ += fileSize_;
while ( time_ >= fileSize_ )
time_ -= fileSize_;
}
void FileLoop :: addPhaseOffset( StkFloat angle )
{
// Add a phase offset in cycles, where 1.0 = fileSize.
phaseOffset_ = file_.fileSize() * angle;
phaseOffset_ = fileSize_ * angle;
}
StkFloat FileLoop :: tick( unsigned int channel )
@@ -134,20 +136,18 @@ StkFloat FileLoop :: tick( unsigned int channel )
// Check limits of time address ... if necessary, recalculate modulo
// fileSize.
StkFloat fileSize = file_.fileSize();
while ( time_ < 0.0 )
time_ += fileSize;
while ( time_ >= fileSize )
time_ -= fileSize;
time_ += fileSize_;
while ( time_ >= fileSize_ )
time_ -= fileSize_;
StkFloat tyme = time_;
if ( phaseOffset_ ) {
tyme += phaseOffset_;
while ( tyme < 0.0 )
tyme += fileSize;
while ( tyme >= fileSize )
tyme -= fileSize;
tyme += fileSize_;
while ( tyme >= fileSize_ )
tyme -= fileSize_;
}
if ( chunking_ ) {
@@ -162,8 +162,8 @@ StkFloat FileLoop :: tick( unsigned int channel )
}
while ( time_ > (StkFloat) ( chunkPointer_ + chunkSize_ - 1 ) ) { // positive rate
chunkPointer_ += chunkSize_ - 1; // overlap chunks by one frame
if ( chunkPointer_ + chunkSize_ > file_.fileSize() ) { // at end of file
chunkPointer_ = file_.fileSize() - chunkSize_ + 1; // leave extra frame at end of buffer
if ( chunkPointer_ + chunkSize_ > fileSize_ ) { // at end of file
chunkPointer_ = fileSize_ - chunkSize_ + 1; // leave extra frame at end of buffer
// Now fill extra frame with first frame data.
for ( unsigned int j=0; j<firstFrame_.channels(); j++ )
data_( data_.frames() - 1, j ) = firstFrame_[j];

View File

@@ -98,6 +98,10 @@ void FileWvIn :: openFile( std::string fileName, bool raw, bool doNormalize )
// Resize our lastFrame container.
lastFrame_.resize( 1, file_.channels() );
// Close the file unless chunking
fileSize_ = file_.fileSize();
if ( !chunking_ ) file_.close();
// Set default rate based on file sampling rate.
this->setRate( data_.dataRate() / Stk::sampleRate() );
@@ -146,7 +150,7 @@ void FileWvIn :: setRate( StkFloat rate )
// If negative rate and at beginning of sound, move pointer to end
// of sound.
if ( (rate_ < 0) && (time_ == 0.0) ) time_ = file_.fileSize() - 1.0;
if ( (rate_ < 0) && (time_ == 0.0) ) time_ = fileSize_ - 1.0;
if ( fmod( rate_, 1.0 ) != 0.0 ) interpolate_ = true;
else interpolate_ = false;
@@ -158,8 +162,8 @@ void FileWvIn :: addTime( StkFloat time )
time_ += time;
if ( time_ < 0.0 ) time_ = 0.0;
if ( time_ > file_.fileSize() - 1.0 ) {
time_ = file_.fileSize() - 1.0;
if ( time_ > fileSize_ - 1.0 ) {
time_ = fileSize_ - 1.0;
for ( unsigned int i=0; i<lastFrame_.size(); i++ ) lastFrame_[i] = 0.0;
finished_ = true;
}
@@ -176,7 +180,7 @@ StkFloat FileWvIn :: tick( unsigned int channel )
if ( finished_ ) return 0.0;
if ( time_ < 0.0 || time_ > (StkFloat) ( file_.fileSize() - 1.0 ) ) {
if ( time_ < 0.0 || time_ > (StkFloat) ( fileSize_ - 1.0 ) ) {
for ( unsigned int i=0; i<lastFrame_.size(); i++ ) lastFrame_[i] = 0.0;
finished_ = true;
return 0.0;
@@ -195,8 +199,8 @@ StkFloat FileWvIn :: tick( unsigned int channel )
}
while ( time_ > (StkFloat) ( chunkPointer_ + chunkSize_ - 1 ) ) { // positive rate
chunkPointer_ += chunkSize_ - 1; // overlap chunks by one frame
if ( chunkPointer_ + chunkSize_ > file_.fileSize() ) // at end of file
chunkPointer_ = file_.fileSize() - chunkSize_;
if ( chunkPointer_ + chunkSize_ > fileSize_ ) // at end of file
chunkPointer_ = fileSize_ - chunkSize_;
}
// Load more data.