1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/**********************************************************************
Audacity: A Digital Audio Editor
SpectrumTransformer.cpp
Edward Hui
**********************************************************************/
#include "SpectrumTransformer.h"
#include <algorithm>
#include "FFT.h"
SpectrumTransformer::SpectrumTransformer( bool needsOutput,
eWindowFunctions inWindowType,
eWindowFunctions outWindowType,
size_t windowSize, unsigned stepsPerWindow,
bool leadingPadding, bool trailingPadding )
: mWindowSize{ windowSize }
, mSpectrumSize{ 1 + mWindowSize / 2 }
, mStepsPerWindow{ stepsPerWindow }
, mStepSize{ mWindowSize / mStepsPerWindow }
, mLeadingPadding{ leadingPadding }
, mTrailingPadding{ trailingPadding }
, hFFT{ GetFFT(mWindowSize) }
, mFFTBuffer( mWindowSize )
, mInWaveBuffer( mWindowSize )
, mOutOverlapBuffer( mWindowSize )
, mNeedsOutput{ needsOutput }
{
// Check preconditions
// Powers of 2 only!
wxASSERT(mWindowSize > 0 &&
0 == (mWindowSize & (mWindowSize - 1)));
wxASSERT(mWindowSize % mStepsPerWindow == 0);
wxASSERT(!(inWindowType == eWinFuncRectangular && outWindowType == eWinFuncRectangular));
// To do: check that inWindowType, outWindowType, and mStepsPerWindow
// are compatible for correct overlap-add reconstruction.
// Create windows as needed
if (inWindowType != eWinFuncRectangular) {
mInWindow.resize(mWindowSize);
std::fill(mInWindow.begin(), mInWindow.end(), 1.0f);
NewWindowFunc(inWindowType, mWindowSize, false, mInWindow.data());
}
if (outWindowType != eWinFuncRectangular) {
mOutWindow.resize(mWindowSize);
std::fill(mOutWindow.begin(), mOutWindow.end(), 1.0f);
NewWindowFunc(outWindowType, mWindowSize, false, mOutWindow.data());
}
// Must scale one or the other window so overlap-add
// comes out right
double denom = 0;
for (size_t ii = 0; ii < mWindowSize; ii += mStepSize) {
denom +=
(mInWindow.empty() ? 1.0 : mInWindow[ii])
*
(mOutWindow.empty() ? 1.0 : mOutWindow[ii]);
}
// It is ASSUMED that you have chosen window types and
// steps per window, so that this sum denom would be the
// same, starting the march anywhere from 0 to mStepSize - 1.
// Else, your overlap-add won't be right, and the transformer
// might not be an identity even when you do nothing to the
// spectra.
float *pWindow = 0;
if (!mInWindow.empty())
pWindow = mInWindow.data();
else if (!mOutWindow.empty())
pWindow = mOutWindow.data();
else
// Can only happen if both window types were rectangular
wxASSERT(false);
for (size_t ii = 0; ii < mWindowSize; ++ii)
*pWindow++ /= denom;
}
auto SpectrumTransformer::NewWindow(size_t windowSize)
-> std::unique_ptr<Window>
{
return std::make_unique<Window>(windowSize);
}
bool SpectrumTransformer::DoStart()
{
return true;
}
bool SpectrumTransformer::DoFinish()
{
return true;
}
bool SpectrumTransformer::Start(size_t queueLength)
{
// Prepare clean queue
ResizeQueue(queueLength);
for (auto &pWindow : mQueue)
pWindow->Zero();
// invoke derived method
if (!DoStart())
return false;
// Clean input and output buffers
{
float *pFill;
pFill = mInWaveBuffer.data();
std::fill(pFill, pFill + mWindowSize, 0.0f);
pFill = mOutOverlapBuffer.data();
std::fill(pFill, pFill + mWindowSize, 0.0f);
}
if (!mLeadingPadding)
{
// We do not want leading zero padded windows
mInWavePos = 0;
mOutStepCount = -static_cast<int>(queueLength - 1);
}
else
{
// So that the queue gets primed with some windows,
// zero-padded in front, the first having mStepSize
// samples of wave data:
mInWavePos = mWindowSize - mStepSize;
// This starts negative, to count up until the queue fills:
mOutStepCount = -static_cast<int>(queueLength - 1)
// ... and then must pass over the padded windows,
// before the first full window:
- static_cast<int>(mStepsPerWindow - 1);
}
mInSampleCount = 0;
return true;
}
bool SpectrumTransformer::ProcessSamples( const WindowProcessor &processor,
const float *buffer, size_t len )
{
if (buffer)
mInSampleCount += len;
bool success = true;
while (success && len &&
mOutStepCount * static_cast<int>(mStepSize) < mInSampleCount) {
auto avail = std::min(len, mWindowSize - mInWavePos);
if (buffer)
memmove(&mInWaveBuffer[mInWavePos], buffer, avail * sizeof(float));
else
memset(&mInWaveBuffer[mInWavePos], 0, avail * sizeof(float));
if (buffer)
buffer += avail;
len -= avail;
mInWavePos += avail;
if (mInWavePos == mWindowSize) {
FillFirstWindow();
// invoke derived method
if ( (success = processor(*this)), success )
OutputStep();
++mOutStepCount;
RotateWindows();
// Shift input.
memmove(mInWaveBuffer.data(), &mInWaveBuffer[mStepSize],
(mWindowSize - mStepSize) * sizeof(float));
mInWavePos -= mStepSize;
}
}
return success;
}
void SpectrumTransformer::ResizeQueue(size_t queueLength)
{
int oldLen = mQueue.size();
mQueue.resize(queueLength);
for (size_t ii = oldLen; ii < queueLength; ++ii)
// invoke derived method to get a queue element
// with appropriate extra fields
mQueue[ii] = NewWindow(mWindowSize);
}
void SpectrumTransformer::FillFirstWindow()
{
// Transform samples to frequency domain, windowed as needed
{
auto pFFTBuffer = mFFTBuffer.data(), pInWaveBuffer = mInWaveBuffer.data();
if (mInWindow.size() > 0) {
auto pInWindow = mInWindow.data();
for (size_t ii = 0; ii < mWindowSize; ++ii)
*pFFTBuffer++ = *pInWaveBuffer++ * *pInWindow++;
}
else
memmove(pFFTBuffer, pInWaveBuffer, mWindowSize * sizeof(float));
}
RealFFTf(mFFTBuffer.data(), hFFT.get());
auto &record = Nth(0);
// Store real and imaginary parts for later inverse FFT
{
float *pReal = &record.mRealFFTs[1];
float *pImag = &record.mImagFFTs[1];
int *pBitReversed = &hFFT->BitReversed[1];
const auto last = mSpectrumSize - 1;
for (size_t ii = 1; ii < last; ++ii) {
const int kk = *pBitReversed++;
*pReal++ = mFFTBuffer[kk];
*pImag++ = mFFTBuffer[kk + 1];
}
// DC and Fs/2 bins need to be handled specially
const float dc = mFFTBuffer[0];
record.mRealFFTs[0] = dc;
const float nyquist = mFFTBuffer[1];
record.mImagFFTs[0] = nyquist; // For Fs/2, not really imaginary
}
}
void SpectrumTransformer::RotateWindows()
{
std::rotate(mQueue.begin(), mQueue.end() - 1, mQueue.end());
}
bool SpectrumTransformer::Finish(const WindowProcessor &processor)
{
bool bLoopSuccess = true;
if (mTrailingPadding) {
// Keep flushing empty input buffers through the history
// windows until we've output exactly as many samples as
// were input.
// Well, not exactly, but not more than one step-size of extra samples
// at the end.
while (bLoopSuccess &&
mOutStepCount * static_cast<int>(mStepSize) < mInSampleCount)
bLoopSuccess = ProcessSamples(processor, nullptr, mStepSize);
}
if (bLoopSuccess)
// invoke derived method
bLoopSuccess = DoFinish();
return bLoopSuccess;
}
size_t SpectrumTransformer::CurrentQueueSize() const
{
auto allocSize = mQueue.size();
auto size = mOutStepCount + allocSize;
if (mLeadingPadding)
size += mStepsPerWindow - 1;
if (size < allocSize)
return size.as_size_t();
else
return allocSize;
}
// Formerly part of EffectNoiseReduction::Worker::ReduceNoise()
void SpectrumTransformer::OutputStep()
{
if (!mNeedsOutput)
return;
if (QueueIsFull()) {
const auto last = mSpectrumSize - 1;
Window &record = **mQueue.rbegin();
const float *pReal = &record.mRealFFTs[1];
const float *pImag = &record.mImagFFTs[1];
float *pBuffer = &mFFTBuffer[2];
auto nn = mSpectrumSize - 2;
for (; nn--;) {
*pBuffer++ = *pReal++;
*pBuffer++ = *pImag++;
}
mFFTBuffer[0] = record.mRealFFTs[0];
// The Fs/2 component is stored as the imaginary part of the DC component
mFFTBuffer[1] = record.mImagFFTs[0];
// Invert the FFT into the output buffer
InverseRealFFTf(mFFTBuffer.data(), hFFT.get());
// Overlap-add
if (mOutWindow.size() > 0) {
auto pOut = mOutOverlapBuffer.data();
auto pWindow = mOutWindow.data();
auto pBitReversed = &hFFT->BitReversed[0];
for (size_t jj = 0; jj < last; ++jj) {
auto kk = *pBitReversed++;
*pOut++ += mFFTBuffer[kk] * (*pWindow++);
*pOut++ += mFFTBuffer[kk + 1] * (*pWindow++);
}
}
else {
auto pOut = mOutOverlapBuffer.data();
auto pBitReversed = &hFFT->BitReversed[0];
for (size_t jj = 0; jj < last; ++jj) {
auto kk = *pBitReversed++;
*pOut++ += mFFTBuffer[kk];
*pOut++ += mFFTBuffer[kk + 1];
}
}
auto buffer = mOutOverlapBuffer.data();
if (mOutStepCount >= 0) {
// Output the first portion of the overlap buffer, they're done
DoOutput(buffer, mStepSize);
}
// Shift the remainder over.
memmove(buffer, buffer + mStepSize, sizeof(float)*(mWindowSize - mStepSize));
std::fill(buffer + mWindowSize - mStepSize, buffer + mWindowSize, 0.0f);
}
}
bool SpectrumTransformer::QueueIsFull() const
{
if (mLeadingPadding)
return (mOutStepCount >= -static_cast<int>(mStepsPerWindow - 1));
else
return (mOutStepCount >= 0);
}
SpectrumTransformer::~SpectrumTransformer() = default;
SpectrumTransformer::Window::~Window() = default;
|