14 #ifdef HAVE_SSE_INTRINSICS
15 #include <xmmintrin.h>
16 #elif defined(HAVE_NEON)
21 #include "alcomplex.h"
23 #include "alnumbers.h"
24 #include "alnumeric.h"
27 #include "core/ambidefs.h"
28 #include "core/bufferline.h"
29 #include "core/buffer_storage.h"
30 #include "core/context.h"
31 #include "core/devformat.h"
32 #include "core/device.h"
33 #include "core/effectslot.h"
34 #include "core/filters/splitter.h"
35 #include "core/fmt_traits.h"
36 #include "core/mixer.h"
37 #include "intrusive_ptr.h"
38 #include "polyphase_resampler.h"
44 /* Convolution reverb is implemented using a segmented overlap-add method. The
45 * impulse response is broken up into multiple segments of 128 samples, and
46 * each segment has an FFT applied with a 256-sample buffer (the latter half
47 * left silent) to get its frequency-domain response. The resulting response
48 * has its positive/non-mirrored frequencies saved (129 bins) in each segment.
50 * Input samples are similarly broken up into 128-sample segments, with an FFT
51 * applied to each new incoming segment to get its 129 bins. A history of FFT'd
52 * input segments is maintained, equal to the length of the impulse response.
54 * To apply the reverberation, each impulse response segment is convolved with
55 * its paired input segment (using complex multiplies, far cheaper than FIRs),
56 * accumulating into a 256-bin FFT buffer. The input history is then shifted to
57 * align with later impulse response segments for next time.
59 * An inverse FFT is then applied to the accumulated FFT buffer to get a 256-
60 * sample time-domain response for output, which is split in two halves. The
61 * first half is the 128-sample output, and the second half is a 128-sample
62 * (really, 127) delayed extension, which gets added to the output next time.
63 * Convolving two time-domain responses of lengths N and M results in a time-
64 * domain signal of length N+M-1, and this holds true regardless of the
65 * convolution being applied in the frequency domain, so these "overflow"
66 * samples need to be accounted for.
68 * To avoid a delay with gathering enough input samples to apply an FFT with,
69 * the first segment is applied directly in the time-domain as the samples come
70 * in. Once enough have been retrieved, the FFT is applied on the input and
71 * it's paired with the remaining (FFT'd) filter segments for processing.
75 void LoadSamples(float *RESTRICT dst
, const al::byte
*src
, const size_t srcstep
, FmtType srctype
,
76 const size_t samples
) noexcept
78 #define HANDLE_FMT(T) case T: al::LoadSampleArray<T>(dst, src, srcstep, samples); break
84 HANDLE_FMT(FmtDouble
);
92 inline auto& GetAmbiScales(AmbiScaling scaletype
) noexcept
96 case AmbiScaling::FuMa
: return AmbiScale::FromFuMa();
97 case AmbiScaling::SN3D
: return AmbiScale::FromSN3D();
98 case AmbiScaling::UHJ
: return AmbiScale::FromUHJ();
99 case AmbiScaling::N3D
: break;
101 return AmbiScale::FromN3D();
104 inline auto& GetAmbiLayout(AmbiLayout layouttype
) noexcept
106 if(layouttype
== AmbiLayout::FuMa
) return AmbiIndex::FromFuMa();
107 return AmbiIndex::FromACN();
110 inline auto& GetAmbi2DLayout(AmbiLayout layouttype
) noexcept
112 if(layouttype
== AmbiLayout::FuMa
) return AmbiIndex::FromFuMa2D();
113 return AmbiIndex::FromACN2D();
123 constexpr float Deg2Rad(float x
) noexcept
124 { return static_cast<float>(al::numbers::pi
/ 180.0 * x
); }
127 using complex_f
= std::complex<float>;
129 constexpr size_t ConvolveUpdateSize
{256};
130 constexpr size_t ConvolveUpdateSamples
{ConvolveUpdateSize
/ 2};
133 void apply_fir(al::span
<float> dst
, const float *RESTRICT src
, const float *RESTRICT filter
)
135 #ifdef HAVE_SSE_INTRINSICS
136 for(float &output
: dst
)
138 __m128 r4
{_mm_setzero_ps()};
139 for(size_t j
{0};j
< ConvolveUpdateSamples
;j
+=4)
141 const __m128 coeffs
{_mm_load_ps(&filter
[j
])};
142 const __m128 s
{_mm_loadu_ps(&src
[j
])};
144 r4
= _mm_add_ps(r4
, _mm_mul_ps(s
, coeffs
));
146 r4
= _mm_add_ps(r4
, _mm_shuffle_ps(r4
, r4
, _MM_SHUFFLE(0, 1, 2, 3)));
147 r4
= _mm_add_ps(r4
, _mm_movehl_ps(r4
, r4
));
148 output
= _mm_cvtss_f32(r4
);
153 #elif defined(HAVE_NEON)
155 for(float &output
: dst
)
157 float32x4_t r4
{vdupq_n_f32(0.0f
)};
158 for(size_t j
{0};j
< ConvolveUpdateSamples
;j
+=4)
159 r4
= vmlaq_f32(r4
, vld1q_f32(&src
[j
]), vld1q_f32(&filter
[j
]));
160 r4
= vaddq_f32(r4
, vrev64q_f32(r4
));
161 output
= vget_lane_f32(vadd_f32(vget_low_f32(r4
), vget_high_f32(r4
)), 0);
168 for(float &output
: dst
)
171 for(size_t j
{0};j
< ConvolveUpdateSamples
;++j
)
172 ret
+= src
[j
] * filter
[j
];
179 struct ConvolutionState final
: public EffectState
{
180 FmtChannels mChannels
{};
181 AmbiLayout mAmbiLayout
{};
182 AmbiScaling mAmbiScaling
{};
186 std::array
<float,ConvolveUpdateSamples
*2> mInput
{};
187 al::vector
<std::array
<float,ConvolveUpdateSamples
>,16> mFilter
;
188 al::vector
<std::array
<float,ConvolveUpdateSamples
*2>,16> mOutput
;
190 alignas(16) std::array
<complex_f
,ConvolveUpdateSize
> mFftBuffer
{};
192 size_t mCurrentSegment
{0};
193 size_t mNumConvolveSegs
{0};
196 alignas(16) FloatBufferLine mBuffer
{};
197 float mHfScale
{}, mLfScale
{};
198 BandSplitter mFilter
{};
199 float Current
[MAX_OUTPUT_CHANNELS
]{};
200 float Target
[MAX_OUTPUT_CHANNELS
]{};
202 using ChannelDataArray
= al::FlexArray
<ChannelData
>;
203 std::unique_ptr
<ChannelDataArray
> mChans
;
204 std::unique_ptr
<complex_f
[]> mComplexData
;
207 ConvolutionState() = default;
208 ~ConvolutionState() override
= default;
210 void NormalMix(const al::span
<FloatBufferLine
> samplesOut
, const size_t samplesToDo
);
211 void UpsampleMix(const al::span
<FloatBufferLine
> samplesOut
, const size_t samplesToDo
);
212 void (ConvolutionState::*mMix
)(const al::span
<FloatBufferLine
>,const size_t)
213 {&ConvolutionState::NormalMix
};
215 void deviceUpdate(const DeviceBase
*device
, const Buffer
&buffer
) override
;
216 void update(const ContextBase
*context
, const EffectSlot
*slot
, const EffectProps
*props
,
217 const EffectTarget target
) override
;
218 void process(const size_t samplesToDo
, const al::span
<const FloatBufferLine
> samplesIn
,
219 const al::span
<FloatBufferLine
> samplesOut
) override
;
221 DEF_NEWDEL(ConvolutionState
)
224 void ConvolutionState::NormalMix(const al::span
<FloatBufferLine
> samplesOut
,
225 const size_t samplesToDo
)
227 for(auto &chan
: *mChans
)
228 MixSamples({chan
.mBuffer
.data(), samplesToDo
}, samplesOut
, chan
.Current
, chan
.Target
,
232 void ConvolutionState::UpsampleMix(const al::span
<FloatBufferLine
> samplesOut
,
233 const size_t samplesToDo
)
235 for(auto &chan
: *mChans
)
237 const al::span
<float> src
{chan
.mBuffer
.data(), samplesToDo
};
238 chan
.mFilter
.processScale(src
, chan
.mHfScale
, chan
.mLfScale
);
239 MixSamples(src
, samplesOut
, chan
.Current
, chan
.Target
, samplesToDo
, 0);
244 void ConvolutionState::deviceUpdate(const DeviceBase
*device
, const Buffer
&buffer
)
246 using UhjDecoderType
= UhjDecoder
<512>;
247 static constexpr auto DecoderPadding
= UhjDecoderType::sInputPadding
;
249 constexpr uint MaxConvolveAmbiOrder
{1u};
253 decltype(mFilter
){}.swap(mFilter
);
254 decltype(mOutput
){}.swap(mOutput
);
255 mFftBuffer
.fill(complex_f
{});
258 mNumConvolveSegs
= 0;
261 mComplexData
= nullptr;
263 /* An empty buffer doesn't need a convolution filter. */
264 if(!buffer
.storage
|| buffer
.storage
->mSampleLen
< 1) return;
266 mChannels
= buffer
.storage
->mChannels
;
267 mAmbiLayout
= IsUHJ(mChannels
) ? AmbiLayout::FuMa
: buffer
.storage
->mAmbiLayout
;
268 mAmbiScaling
= IsUHJ(mChannels
) ? AmbiScaling::UHJ
: buffer
.storage
->mAmbiScaling
;
269 mAmbiOrder
= minu(buffer
.storage
->mAmbiOrder
, MaxConvolveAmbiOrder
);
271 constexpr size_t m
{ConvolveUpdateSize
/2 + 1};
272 const auto bytesPerSample
= BytesFromFmt(buffer
.storage
->mType
);
273 const auto realChannels
= buffer
.storage
->channelsFromFmt();
274 const auto numChannels
= (mChannels
== FmtUHJ2
) ? 3u : ChannelsFromFmt(mChannels
, mAmbiOrder
);
276 mChans
= ChannelDataArray::Create(numChannels
);
278 /* The impulse response needs to have the same sample rate as the input and
279 * output. The bsinc24 resampler is decent, but there is high-frequency
280 * attenuation that some people may be able to pick up on. Since this is
281 * called very infrequently, go ahead and use the polyphase resampler.
283 PPhaseResampler resampler
;
284 if(device
->Frequency
!= buffer
.storage
->mSampleRate
)
285 resampler
.init(buffer
.storage
->mSampleRate
, device
->Frequency
);
286 const auto resampledCount
= static_cast<uint
>(
287 (uint64_t{buffer
.storage
->mSampleLen
}*device
->Frequency
+(buffer
.storage
->mSampleRate
-1)) /
288 buffer
.storage
->mSampleRate
);
290 const BandSplitter splitter
{device
->mXOverFreq
/ static_cast<float>(device
->Frequency
)};
291 for(auto &e
: *mChans
)
292 e
.mFilter
= splitter
;
294 mFilter
.resize(numChannels
, {});
295 mOutput
.resize(numChannels
, {});
297 /* Calculate the number of segments needed to hold the impulse response and
298 * the input history (rounded up), and allocate them. Exclude one segment
299 * which gets applied as a time-domain FIR filter. Make sure at least one
300 * segment is allocated to simplify handling.
302 mNumConvolveSegs
= (resampledCount
+(ConvolveUpdateSamples
-1)) / ConvolveUpdateSamples
;
303 mNumConvolveSegs
= maxz(mNumConvolveSegs
, 2) - 1;
305 const size_t complex_length
{mNumConvolveSegs
* m
* (numChannels
+1)};
306 mComplexData
= std::make_unique
<complex_f
[]>(complex_length
);
307 std::fill_n(mComplexData
.get(), complex_length
, complex_f
{});
309 /* Load the samples from the buffer. */
310 const size_t srclinelength
{RoundUp(buffer
.storage
->mSampleLen
+DecoderPadding
, 16)};
311 auto srcsamples
= std::make_unique
<float[]>(srclinelength
* numChannels
);
312 std::fill_n(srcsamples
.get(), srclinelength
* numChannels
, 0.0f
);
313 for(size_t c
{0};c
< numChannels
&& c
< realChannels
;++c
)
314 LoadSamples(srcsamples
.get() + srclinelength
*c
, buffer
.samples
.data() + bytesPerSample
*c
,
315 realChannels
, buffer
.storage
->mType
, buffer
.storage
->mSampleLen
);
319 auto decoder
= std::make_unique
<UhjDecoderType
>();
320 std::array
<float*,4> samples
{};
321 for(size_t c
{0};c
< numChannels
;++c
)
322 samples
[c
] = srcsamples
.get() + srclinelength
*c
;
323 decoder
->decode({samples
.data(), numChannels
}, buffer
.storage
->mSampleLen
,
324 buffer
.storage
->mSampleLen
);
327 auto ressamples
= std::make_unique
<double[]>(buffer
.storage
->mSampleLen
+
328 (resampler
? resampledCount
: 0));
329 complex_f
*filteriter
= mComplexData
.get() + mNumConvolveSegs
*m
;
330 for(size_t c
{0};c
< numChannels
;++c
)
332 /* Resample to match the device. */
335 std::copy_n(srcsamples
.get() + srclinelength
*c
, buffer
.storage
->mSampleLen
,
336 ressamples
.get() + resampledCount
);
337 resampler
.process(buffer
.storage
->mSampleLen
, ressamples
.get()+resampledCount
,
338 resampledCount
, ressamples
.get());
341 std::copy_n(srcsamples
.get() + srclinelength
*c
, buffer
.storage
->mSampleLen
,
344 /* Store the first segment's samples in reverse in the time-domain, to
345 * apply as a FIR filter.
347 const size_t first_size
{minz(resampledCount
, ConvolveUpdateSamples
)};
348 std::transform(ressamples
.get(), ressamples
.get()+first_size
, mFilter
[c
].rbegin(),
349 [](const double d
) noexcept
-> float { return static_cast<float>(d
); });
351 auto fftbuffer
= std::vector
<std::complex<double>>(ConvolveUpdateSize
);
352 size_t done
{first_size
};
353 for(size_t s
{0};s
< mNumConvolveSegs
;++s
)
355 const size_t todo
{minz(resampledCount
-done
, ConvolveUpdateSamples
)};
357 auto iter
= std::copy_n(&ressamples
[done
], todo
, fftbuffer
.begin());
359 std::fill(iter
, fftbuffer
.end(), std::complex<double>{});
361 forward_fft(al::as_span(fftbuffer
));
362 filteriter
= std::copy_n(fftbuffer
.cbegin(), m
, filteriter
);
368 void ConvolutionState::update(const ContextBase
*context
, const EffectSlot
*slot
,
369 const EffectProps
* /*props*/, const EffectTarget target
)
371 /* NOTE: Stereo and Rear are slightly different from normal mixing (as
372 * defined in alu.cpp). These are 45 degrees from center, rather than the
373 * 30 degrees used there.
375 * TODO: LFE is not mixed to output. This will require each buffer channel
376 * to have its own output target since the main mixing buffer won't have an
377 * LFE channel (due to being B-Format).
379 static constexpr ChanMap MonoMap
[1]{
380 { FrontCenter
, 0.0f
, 0.0f
}
382 { FrontLeft
, Deg2Rad(-45.0f
), Deg2Rad(0.0f
) },
383 { FrontRight
, Deg2Rad( 45.0f
), Deg2Rad(0.0f
) }
385 { BackLeft
, Deg2Rad(-135.0f
), Deg2Rad(0.0f
) },
386 { BackRight
, Deg2Rad( 135.0f
), Deg2Rad(0.0f
) }
388 { FrontLeft
, Deg2Rad( -45.0f
), Deg2Rad(0.0f
) },
389 { FrontRight
, Deg2Rad( 45.0f
), Deg2Rad(0.0f
) },
390 { BackLeft
, Deg2Rad(-135.0f
), Deg2Rad(0.0f
) },
391 { BackRight
, Deg2Rad( 135.0f
), Deg2Rad(0.0f
) }
393 { FrontLeft
, Deg2Rad( -30.0f
), Deg2Rad(0.0f
) },
394 { FrontRight
, Deg2Rad( 30.0f
), Deg2Rad(0.0f
) },
395 { FrontCenter
, Deg2Rad( 0.0f
), Deg2Rad(0.0f
) },
397 { SideLeft
, Deg2Rad(-110.0f
), Deg2Rad(0.0f
) },
398 { SideRight
, Deg2Rad( 110.0f
), Deg2Rad(0.0f
) }
400 { FrontLeft
, Deg2Rad(-30.0f
), Deg2Rad(0.0f
) },
401 { FrontRight
, Deg2Rad( 30.0f
), Deg2Rad(0.0f
) },
402 { FrontCenter
, Deg2Rad( 0.0f
), Deg2Rad(0.0f
) },
404 { BackCenter
, Deg2Rad(180.0f
), Deg2Rad(0.0f
) },
405 { SideLeft
, Deg2Rad(-90.0f
), Deg2Rad(0.0f
) },
406 { SideRight
, Deg2Rad( 90.0f
), Deg2Rad(0.0f
) }
408 { FrontLeft
, Deg2Rad( -30.0f
), Deg2Rad(0.0f
) },
409 { FrontRight
, Deg2Rad( 30.0f
), Deg2Rad(0.0f
) },
410 { FrontCenter
, Deg2Rad( 0.0f
), Deg2Rad(0.0f
) },
412 { BackLeft
, Deg2Rad(-150.0f
), Deg2Rad(0.0f
) },
413 { BackRight
, Deg2Rad( 150.0f
), Deg2Rad(0.0f
) },
414 { SideLeft
, Deg2Rad( -90.0f
), Deg2Rad(0.0f
) },
415 { SideRight
, Deg2Rad( 90.0f
), Deg2Rad(0.0f
) }
418 if(mNumConvolveSegs
< 1) [[unlikely
]]
421 mMix
= &ConvolutionState::NormalMix
;
423 for(auto &chan
: *mChans
)
424 std::fill(std::begin(chan
.Target
), std::end(chan
.Target
), 0.0f
);
425 const float gain
{slot
->Gain
};
426 if(IsAmbisonic(mChannels
))
428 DeviceBase
*device
{context
->mDevice
};
429 if(mChannels
== FmtUHJ2
&& !device
->mUhjEncoder
)
431 mMix
= &ConvolutionState::UpsampleMix
;
432 (*mChans
)[0].mHfScale
= 1.0f
;
433 (*mChans
)[0].mLfScale
= DecoderBase::sWLFScale
;
434 (*mChans
)[1].mHfScale
= 1.0f
;
435 (*mChans
)[1].mLfScale
= DecoderBase::sXYLFScale
;
436 (*mChans
)[2].mHfScale
= 1.0f
;
437 (*mChans
)[2].mLfScale
= DecoderBase::sXYLFScale
;
439 else if(device
->mAmbiOrder
> mAmbiOrder
)
441 mMix
= &ConvolutionState::UpsampleMix
;
442 const auto scales
= AmbiScale::GetHFOrderScales(mAmbiOrder
, device
->mAmbiOrder
,
444 (*mChans
)[0].mHfScale
= scales
[0];
445 (*mChans
)[0].mLfScale
= 1.0f
;
446 for(size_t i
{1};i
< mChans
->size();++i
)
448 (*mChans
)[i
].mHfScale
= scales
[1];
449 (*mChans
)[i
].mLfScale
= 1.0f
;
452 mOutTarget
= target
.Main
->Buffer
;
454 auto&& scales
= GetAmbiScales(mAmbiScaling
);
455 const uint8_t *index_map
{Is2DAmbisonic(mChannels
) ?
456 GetAmbi2DLayout(mAmbiLayout
).data() :
457 GetAmbiLayout(mAmbiLayout
).data()};
459 std::array
<float,MaxAmbiChannels
> coeffs
{};
460 for(size_t c
{0u};c
< mChans
->size();++c
)
462 const size_t acn
{index_map
[c
]};
463 coeffs
[acn
] = scales
[acn
];
464 ComputePanGains(target
.Main
, coeffs
.data(), gain
, (*mChans
)[c
].Target
);
470 DeviceBase
*device
{context
->mDevice
};
471 al::span
<const ChanMap
> chanmap
{};
474 case FmtMono
: chanmap
= MonoMap
; break;
476 case FmtStereo
: chanmap
= StereoMap
; break;
477 case FmtRear
: chanmap
= RearMap
; break;
478 case FmtQuad
: chanmap
= QuadMap
; break;
479 case FmtX51
: chanmap
= X51Map
; break;
480 case FmtX61
: chanmap
= X61Map
; break;
481 case FmtX71
: chanmap
= X71Map
; break;
490 mOutTarget
= target
.Main
->Buffer
;
491 if(device
->mRenderMode
== RenderMode::Pairwise
)
493 auto ScaleAzimuthFront
= [](float azimuth
, float scale
) -> float
495 constexpr float half_pi
{al::numbers::pi_v
<float>*0.5f
};
496 const float abs_azi
{std::fabs(azimuth
)};
497 if(!(abs_azi
>= half_pi
))
498 return std::copysign(minf(abs_azi
*scale
, half_pi
), azimuth
);
502 for(size_t i
{0};i
< chanmap
.size();++i
)
504 if(chanmap
[i
].channel
== LFE
) continue;
505 const auto coeffs
= CalcAngleCoeffs(ScaleAzimuthFront(chanmap
[i
].angle
, 2.0f
),
506 chanmap
[i
].elevation
, 0.0f
);
507 ComputePanGains(target
.Main
, coeffs
.data(), gain
, (*mChans
)[i
].Target
);
510 else for(size_t i
{0};i
< chanmap
.size();++i
)
512 if(chanmap
[i
].channel
== LFE
) continue;
513 const auto coeffs
= CalcAngleCoeffs(chanmap
[i
].angle
, chanmap
[i
].elevation
, 0.0f
);
514 ComputePanGains(target
.Main
, coeffs
.data(), gain
, (*mChans
)[i
].Target
);
519 void ConvolutionState::process(const size_t samplesToDo
,
520 const al::span
<const FloatBufferLine
> samplesIn
, const al::span
<FloatBufferLine
> samplesOut
)
522 if(mNumConvolveSegs
< 1) [[unlikely
]]
525 constexpr size_t m
{ConvolveUpdateSize
/2 + 1};
526 size_t curseg
{mCurrentSegment
};
527 auto &chans
= *mChans
;
529 for(size_t base
{0u};base
< samplesToDo
;)
531 const size_t todo
{minz(ConvolveUpdateSamples
-mFifoPos
, samplesToDo
-base
)};
533 std::copy_n(samplesIn
[0].begin() + base
, todo
,
534 mInput
.begin()+ConvolveUpdateSamples
+mFifoPos
);
536 /* Apply the FIR for the newly retrieved input samples, and combine it
537 * with the inverse FFT'd output samples.
539 for(size_t c
{0};c
< chans
.size();++c
)
541 auto buf_iter
= chans
[c
].mBuffer
.begin() + base
;
542 apply_fir({buf_iter
, todo
}, mInput
.data()+1 + mFifoPos
, mFilter
[c
].data());
544 auto fifo_iter
= mOutput
[c
].begin() + mFifoPos
;
545 std::transform(fifo_iter
, fifo_iter
+todo
, buf_iter
, buf_iter
, std::plus
<>{});
551 /* Check whether the input buffer is filled with new samples. */
552 if(mFifoPos
< ConvolveUpdateSamples
) break;
555 /* Move the newest input to the front for the next iteration's history. */
556 std::copy(mInput
.cbegin()+ConvolveUpdateSamples
, mInput
.cend(), mInput
.begin());
558 /* Calculate the frequency domain response and add the relevant
559 * frequency bins to the FFT history.
561 auto fftiter
= std::copy_n(mInput
.cbegin(), ConvolveUpdateSamples
, mFftBuffer
.begin());
562 std::fill(fftiter
, mFftBuffer
.end(), complex_f
{});
563 forward_fft(al::as_span(mFftBuffer
));
565 std::copy_n(mFftBuffer
.cbegin(), m
, &mComplexData
[curseg
*m
]);
567 const complex_f
*RESTRICT filter
{mComplexData
.get() + mNumConvolveSegs
*m
};
568 for(size_t c
{0};c
< chans
.size();++c
)
570 std::fill_n(mFftBuffer
.begin(), m
, complex_f
{});
572 /* Convolve each input segment with its IR filter counterpart
575 const complex_f
*RESTRICT input
{&mComplexData
[curseg
*m
]};
576 for(size_t s
{curseg
};s
< mNumConvolveSegs
;++s
)
578 for(size_t i
{0};i
< m
;++i
,++input
,++filter
)
579 mFftBuffer
[i
] += *input
* *filter
;
581 input
= mComplexData
.get();
582 for(size_t s
{0};s
< curseg
;++s
)
584 for(size_t i
{0};i
< m
;++i
,++input
,++filter
)
585 mFftBuffer
[i
] += *input
* *filter
;
588 /* Reconstruct the mirrored/negative frequencies to do a proper
591 for(size_t i
{m
};i
< ConvolveUpdateSize
;++i
)
592 mFftBuffer
[i
] = std::conj(mFftBuffer
[ConvolveUpdateSize
-i
]);
594 /* Apply iFFT to get the 256 (really 255) samples for output. The
595 * 128 output samples are combined with the last output's 127
596 * second-half samples (and this output's second half is
597 * subsequently saved for next time).
599 inverse_fft(al::as_span(mFftBuffer
));
601 /* The iFFT'd response is scaled up by the number of bins, so apply
602 * the inverse to normalize the output.
604 for(size_t i
{0};i
< ConvolveUpdateSamples
;++i
)
606 (mFftBuffer
[i
].real()+mOutput
[c
][ConvolveUpdateSamples
+i
]) *
607 (1.0f
/float{ConvolveUpdateSize
});
608 for(size_t i
{0};i
< ConvolveUpdateSamples
;++i
)
609 mOutput
[c
][ConvolveUpdateSamples
+i
] = mFftBuffer
[ConvolveUpdateSamples
+i
].real();
612 /* Shift the input history. */
613 curseg
= curseg
? (curseg
-1) : (mNumConvolveSegs
-1);
615 mCurrentSegment
= curseg
;
617 /* Finally, mix to the output. */
618 (this->*mMix
)(samplesOut
, samplesToDo
);
622 struct ConvolutionStateFactory final
: public EffectStateFactory
{
623 al::intrusive_ptr
<EffectState
> create() override
624 { return al::intrusive_ptr
<EffectState
>{new ConvolutionState
{}}; }
629 EffectStateFactory
*ConvolutionStateFactory_getFactory()
631 static ConvolutionStateFactory ConvolutionFactory
{};
632 return &ConvolutionFactory
;