Skip to content

File Convolver.h

File List > Amplitude > DSP > Convolver.h

Go to the documentation of this file

// Copyright (c) 2021-present Sparky Studios. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Based on the code from https://github.com/HiFi-LoFi/FFTConvolver
// Copyright (c) 2017 HiFi-LoFi, MIT License

#pragma once

#ifndef _AM_DSP_CONVOLVER_H
#define _AM_DSP_CONVOLVER_H

#include <SparkyStudios/Audio/Amplitude/Core/Common.h>

#include <SparkyStudios/Audio/Amplitude/DSP/FFT.h>

#include <vector>

namespace SparkyStudios::Audio::Amplitude
{
    class Convolver
    {
    public:
        Convolver();

        virtual ~Convolver();

        Convolver(const Convolver& other) = delete;

        Convolver& operator=(const Convolver& other) = delete;

        bool Init(AmSize blockSize, const AmAudioSample* ir, AmSize irLen);

        void Process(const AmAudioSample* input, AmAudioSample* output, AmSize len);

        void Reset();

        [[nodiscard]] AmSize GetSegmentSize() const;

        [[nodiscard]] AmSize GetSegmentCount() const;

    private:
        AmSize _blockSize;
        AmSize _segSize;
        AmSize _segCount;
        AmSize _fftComplexSize;
        std::vector<SplitComplex*> _segments;
        std::vector<SplitComplex*> _segmentsIR;
        AmAlignedReal32Buffer _fftBuffer;
        FFT _fft;
        SplitComplex _preMultiplied;
        SplitComplex _conv;
        AmAlignedReal32Buffer _overlap;
        AmSize _current;
        AmAlignedReal32Buffer _inputBuffer;
        AmSize _inputBufferFill;
    };
} // namespace SparkyStudios::Audio::Amplitude

#endif // _AM_DSP_CONVOLVER_H