| 1 |
/***************************************************/ |
| 2 |
/*! \class WvIn |
| 3 |
\brief STK audio input abstract base class. |
| 4 |
|
| 5 |
This class provides common functionality for a variety of audio |
| 6 |
data input subclasses. |
| 7 |
|
| 8 |
WvIn supports multi-channel data. It is important to distinguish |
| 9 |
the tick() methods, which return samples produced by averaging |
| 10 |
across sample frames, from the tickFrame() methods, which return |
| 11 |
references or pointers to multi-channel sample frames. |
| 12 |
|
| 13 |
Both interleaved and non-interleaved data is supported via the use |
| 14 |
of StkFrames objects. |
| 15 |
|
| 16 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 17 |
*/ |
| 18 |
/***************************************************/ |
| 19 |
|
| 20 |
#include "WvIn.h" |
| 21 |
#include <cmath> |
| 22 |
|
| 23 |
using namespace Nyq; |
| 24 |
|
| 25 |
WvIn :: WvIn() |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
WvIn :: ~WvIn() |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
StkFloat WvIn :: lastOut( void ) const |
| 34 |
{ |
| 35 |
if ( lastOutputs_.empty() ) return 0.0; |
| 36 |
|
| 37 |
if ( lastOutputs_.size() == 1 ) |
| 38 |
return lastOutputs_[0]; |
| 39 |
|
| 40 |
StkFloat output = 0.0; |
| 41 |
for ( unsigned int i=0; i<lastOutputs_.size(); i++ ) { |
| 42 |
output += lastOutputs_[i]; |
| 43 |
} |
| 44 |
return output / lastOutputs_.size(); |
| 45 |
} |
| 46 |
|
| 47 |
StkFloat WvIn :: tick( void ) |
| 48 |
{ |
| 49 |
computeFrame(); |
| 50 |
return lastOut(); |
| 51 |
} |
| 52 |
|
| 53 |
StkFrames& WvIn :: tick( StkFrames& frames, unsigned int channel ) |
| 54 |
{ |
| 55 |
if ( channel >= frames.channels() ) { |
| 56 |
errorString_ << "WvIn::tick(): channel and StkFrames arguments are incompatible!"; |
| 57 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( frames.channels() == 1 ) { |
| 61 |
for ( unsigned int i=0; i<frames.frames(); i++ ) |
| 62 |
frames[i] = tick(); |
| 63 |
} |
| 64 |
else if ( frames.interleaved() ) { |
| 65 |
unsigned int hop = frames.channels(); |
| 66 |
unsigned int index = channel; |
| 67 |
for ( unsigned int i=0; i<frames.frames(); i++ ) { |
| 68 |
frames[index] = tick(); |
| 69 |
index += hop; |
| 70 |
} |
| 71 |
} |
| 72 |
else { |
| 73 |
unsigned int iStart = channel * frames.frames(); |
| 74 |
for ( unsigned int i=0; i<frames.frames(); i++ ) |
| 75 |
frames[iStart++] = tick(); |
| 76 |
} |
| 77 |
|
| 78 |
return frames; |
| 79 |
} |
| 80 |
|
| 81 |
StkFrames& WvIn :: tickFrame( StkFrames& frames ) |
| 82 |
{ |
| 83 |
unsigned int nChannels = lastOutputs_.channels(); |
| 84 |
if ( nChannels == 0 ) { |
| 85 |
errorString_ << "WvIn::tickFrame(): no data has been loaded!"; |
| 86 |
handleError( StkError::WARNING ); |
| 87 |
return frames; |
| 88 |
} |
| 89 |
|
| 90 |
if ( nChannels != frames.channels() ) { |
| 91 |
errorString_ << "WvIn::tickFrame(): incompatible channel value in StkFrames argument!"; |
| 92 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 93 |
} |
| 94 |
|
| 95 |
unsigned int j; |
| 96 |
if ( nChannels == 1 || frames.interleaved() ) { |
| 97 |
unsigned int counter = 0; |
| 98 |
for ( unsigned int i=0; i<frames.frames(); i++ ) { |
| 99 |
this->computeFrame(); |
| 100 |
for ( j=0; j<nChannels; j++ ) |
| 101 |
frames[counter++] = lastOutputs_[j]; |
| 102 |
} |
| 103 |
} |
| 104 |
else { // non-interleaved data |
| 105 |
unsigned int hop = frames.frames(); |
| 106 |
unsigned int index; |
| 107 |
for ( unsigned int i=0; i<frames.frames(); i++ ) { |
| 108 |
this->computeFrame(); |
| 109 |
index = i; |
| 110 |
for ( j=0; j<nChannels; j++ ) { |
| 111 |
frames[index] = lastOutputs_[j]; |
| 112 |
index += hop; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return frames; |
| 118 |
} |