25 #include "SoxWindows.H" 28 #define OVERLAP_DEFAULT 0.5 29 #define WINDOWSIZE_DEFAULT 2048 32 #define OVERLAPADD_CHCNT_ERROR OVERLAPADD_ERROR_OFFSET-1 33 #define OVERLAPADD_FILESIZE_MISMATCH_ERROR OVERLAPADD_ERROR_OFFSET-2 34 #define OVERLAPADD_FACTOR_TOO_LARGE_ERROR OVERLAPADD_ERROR_OFFSET-3 45 errors[
OVERLAPADD_CHCNT_ERROR]=string(
"OverlapAdd: The requested channel is larger then the number available in the input audio file. If in the last window, this error will not be thrown. ");
68 cerr<<
"OverlapAdd::init The specified factor "<<factor<<
" is >= 1. This is not allowed : ERROR : exiting."<<endl;
79 Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic>
data;
128 uint N=(
uint)floor((
float)windowSize*overlapFactor);
135 uint windowCnt=(int)(ceil((
float)((float)sampleCount/(
float)windowSize/(1.-overlapFactor))));
136 cout<<
"sampleCount="<<sampleCount<<endl;
137 cout<<
"windowCnt="<<windowCnt<<
'\t'<<15.-windowCnt<<endl;
138 cout<<
"windowSize="<<windowSize<<endl;
140 if (data.cols()!=windowSize || data.rows()!=windowCnt)
141 data.resize(windowSize, windowCnt);
142 data=Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic>::Zero(windowSize, windowCnt);
145 Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> audioData;
146 for (
int i=0; i<windowCnt; i++) {
148 (i==0) ? toRead=windowSize : toRead=M;
149 if (i==windowCnt-1) toRead+=
N;
150 if (toRead>sampleCount+N)
151 toRead=sampleCount+
N;
154 int cnt=sox.
read(audioData, toRead);
157 return SoxDebug().evaluateError(cnt);
158 if (audioData.rows()!=toRead) {
159 ret=windowCnt-toRead;
164 audioData=Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic>::Zero(M, whichCh+1);
166 sampleCount-=audioData.rows();
168 data.block(0, i, audioData.rows(), 1)=audioData.block(0, whichCh, audioData.rows(), 1);
170 data.block(N, i, audioData.rows(), 1)=audioData.block(0, whichCh, audioData.rows(), 1);
172 data.block(0, i+1, N, 1)=data.block(windowSize-N, i, N, 1);
183 cout<<
"OverlapAdd::unloadData"<<endl;
187 uint windowSize=data.rows();
188 uint N=(
uint)floor((
float)windowSize*overlapFactor);
194 uint windowCnt=data.cols();
200 Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> audioData(M,1);
202 Eigen::Array<TYPE, Eigen::Dynamic, Eigen::Dynamic> wndFront, wndBack, wndData=Eigen::Array<TYPE, 1, Eigen::Dynamic>::LinSpaced(2*N,0.,M_PI-M_PI/(2*N)).sin().square().transpose();
203 wndFront=wndData.block(0, 0, N, 1);
204 wndBack=wndData.block(N, 0, N, 1);
206 for (
int i=0; i<windowCnt; i++) {
208 (i==0) ? toWrite=N: toWrite=M;
212 wndData=data.block(0, i, N, 1);
214 wndData+=data.block(0, i, N, 1).array()*wndFront;
217 audioData.block(0, 0, N, 1)=wndData;
218 if (windowSize-2*N > 0)
219 audioData.block(N, 0, M-N, 1)=data.block(N, i, M-N, 1);
221 cout<<audioData.transpose()<<endl;
223 wndData=data.block(windowSize-N, i, N, 1).array()*wndBack;
233 int cnt=sox.
write(audioData);
235 if (cnt!=audioData.rows())
236 return SoxDebug().evaluateError(cnt);
238 if (i==windowCnt-1) {
239 audioData=data.block(windowSize-N, i, N, 1);
240 cout<<audioData.transpose()<<endl;
241 cnt=sox.
write(audioData);
242 if (cnt!=audioData.rows())
243 return SoxDebug().evaluateError(cnt);
255 return overlapFactor;
275 cout<<data.transpose()<<endl;
283 return data.cwiseAbs().maxCoeff();
286 #endif // OVERLAPADD_H_ #define OVERLAPADD_FILESIZE_MISMATCH_ERROR
Error when the number of audio samples required can't be read from the input audio file...
#define OVERLAPADD_FACTOR_TOO_LARGE_ERROR
Error when the overlap factor is too large.
int loadData(Sox< float > &sox, uint windowSize, uint sampleCount, int whichCh=0)
float overlapFactor
Overlap factor, 0.5 for half.
#define OVERLAPADD_CHCNT_ERROR
Error when the specified channel is larger then the number of channels in the input audio file...
std::map< int, std::string > errors
This will contain a map between error numbers and descriptive std::strings for the errors...
#define NO_ERROR
There is no error.
int unloadData(Sox< float > &sox)
Eigen::Matrix< TYPE, Eigen::Dynamic, Eigen::Dynamic > data
virtual ~OverlapAdd()
Destructor.
#define WINDOWSIZE_DEFAULT
OverlapAdd(void)
Empty constructor defaults to OVERLAP_DEFAULT.
#define SOX_EOF_OR_ERROR
Sox returned from a read or write either at the end of the file or in error.
virtual int write(const vector< vector< FP_TYPE_ > > &audioData)
int read(Eigen::DenseBase< Derived > &audioData, int count=0)