File: PluginProcessor.h

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (251 lines) | stat: -rw-r--r-- 8,460 bytes parent folder | download
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
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Author: Daniel Rudrich
 Copyright (c) 2017 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The IEM plug-in suite is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this software.  If not, see <https://www.gnu.org/licenses/>.
 ==============================================================================
 */

#define JUCE_USE_VDSP_FRAMEWORK 1

#pragma once

#include "../../resources/AudioProcessorBase.h"
#include "../../resources/ambisonicTools.h"
#include "../../resources/customComponents/FilterVisualizer.h"
#include "../../resources/efficientSHvanilla.h"
#include "../../resources/interpLagrangeWeights.h"
#include "../JuceLibraryCode/JuceHeader.h"
#include "reflections.h"

#ifdef JUCE_MAC
    #define VIMAGE_H // avoid namespace clashes
    #include <Accelerate/Accelerate.h>
#endif

#if JUCE_USE_SIMD
using IIRfloat = juce::dsp::SIMDRegister<float>;
static constexpr int IIRfloat_elements = juce::dsp::SIMDRegister<float>::size();
#else /* !JUCE_USE_SIMD */
using IIRfloat = float;
static constexpr int IIRfloat_elements = 1;
#endif /* JUCE_USE_SIMD */

#define ProcessorClass RoomEncoderAudioProcessor

const int mSig[] = { 1, -1 };
using namespace juce::dsp;

struct RoomParams
{
    bool validRoomData = false;
    bool validListenerData = false;
    bool validReflectionData = false;
    float roomX, roomY, roomZ;
    float listenerX, listenerY, listenerZ;
    float reflCoeff, lowShelfFreq, lowShelfGain, highShelfFreq, highShelfGain, numRefl;
    float wallAttenuationFront, wallAttenuationBack, wallAttenuationLeft, wallAttenuationRight,
        wallAttenuationCeiling, wallAttenuationFloor;
};

struct SharedParams
{
    SharedParams()
    {
        rooms.add (RoomParams());
        rooms.add (RoomParams());
        rooms.add (RoomParams());
        rooms.add (RoomParams());
    }
    juce::Array<RoomParams> rooms;
};

struct ReflectionProperty
{
    const int x; // coordinate in image source space
    const int y; // coordinate in image source space
    const int z; // coordinate in image source space
    const int order; // image source order
    const int xPlusReflections; // number of reflections at front wall
    const int xMinusReflections; // number of reflections at back wall
    const int yPlusReflections; // number of reflections at left wall
    const int yMinusReflections; // number of reflections at right wall
    const int zPlusReflections; // number of reflections at ceiling
    const int zMinusReflections; // number of reflections at floor
};

//==============================================================================
/**
*/
class RoomEncoderAudioProcessor
    : public AudioProcessorBase<IOTypes::Ambisonics<>, IOTypes::Ambisonics<>>,
      private juce::Timer
{
public:
    constexpr static int numberOfInputChannels = 64;
    constexpr static int numberOfOutputChannels = 64;
    //==============================================================================
    RoomEncoderAudioProcessor();
    ~RoomEncoderAudioProcessor();

    //==============================================================================

    void initializeReflectionList();

    //==============================================================================
    void prepareToPlay (double sampleRate, int samplesPerBlock) override;
    void releaseResources() override;

    void processBlock (juce::AudioSampleBuffer&, juce::MidiBuffer&) override;

    //==============================================================================
    juce::AudioProcessorEditor* createEditor() override;
    bool hasEditor() const override;

    //==============================================================================
    int getNumPrograms() override;
    int getCurrentProgram() override;
    void setCurrentProgram (int index) override;
    const juce::String getProgramName (int index) override;
    void changeProgramName (int index, const juce::String& newName) override;

    //==============================================================================
    void getStateInformation (juce::MemoryBlock& destData) override;
    void setStateInformation (const void* data, int sizeInBytes) override;

    void parameterChanged (const juce::String& parameterID, float newValue) override;

    //======= Parameters ===========================================================
    std::vector<std::unique_ptr<juce::RangedAudioParameter>> createParameterLayout();

    //==============================================================================
    double oldDelay[nImgSrc];
    float allGains[nImgSrc];

    //filter coefficients
    IIR::Coefficients<float>::Ptr lowShelfCoefficients;
    IIR::Coefficients<float>::Ptr highShelfCoefficients;

    bool userChangedFilterSettings = true;
    bool updateFv = false;

    void timerCallback() override;

    void updateFilterCoefficients (double sampleRate);
    void calculateImageSourcePositions (const float t, const float b, const float h);

    std::atomic<float>* numRefl;
    float mRadius[nImgSrc];

    void updateBuffers() override;

    juce::Atomic<bool> repaintPositionPlanes = true;

private:
    //==============================================================================
    inline void clear (juce::dsp::AudioBlock<IIRfloat>& ab);

    bool readingSharedParams = false;
    ;

    double phi;
    double theta;

    // Parameters
    std::atomic<float>* directivityOrderSetting;
    std::atomic<float>* inputIsSN3D;
    std::atomic<float>* orderSetting;
    std::atomic<float>* useSN3D;

    std::atomic<float>* roomX;
    std::atomic<float>* roomY;
    std::atomic<float>* roomZ;

    std::atomic<float>* sourceX;
    std::atomic<float>* sourceY;
    std::atomic<float>* sourceZ;

    std::atomic<float>* listenerX;
    std::atomic<float>* listenerY;
    std::atomic<float>* listenerZ;

    std::atomic<float>* reflCoeff;

    std::atomic<float>* lowShelfFreq;
    std::atomic<float>* lowShelfGain;
    std::atomic<float>* highShelfFreq;
    std::atomic<float>* highShelfGain;

    std::atomic<float>* syncChannel;
    std::atomic<float>* syncRoomSize;
    std::atomic<float>* syncReflection;
    std::atomic<float>* syncListener;

    std::atomic<float>* renderDirectPath;
    std::atomic<float>* directPathZeroDelay;
    std::atomic<float>* directPathUnityGain;

    std::atomic<float>* wallAttenuationFront;
    std::atomic<float>* wallAttenuationBack;
    std::atomic<float>* wallAttenuationLeft;
    std::atomic<float>* wallAttenuationRight;
    std::atomic<float>* wallAttenuationCeiling;
    std::atomic<float>* wallAttenuationFloor;

    std::atomic<float>* constantGainDistance;

    int _numRefl;

    juce::SharedResourcePointer<SharedParams> sharedParams;

    //SIMD IIR Filter

    juce::OwnedArray<juce::OwnedArray<IIR::Filter<IIRfloat>>> lowShelfArray;
    juce::OwnedArray<juce::OwnedArray<IIR::Filter<IIRfloat>>> highShelfArray;
    juce::HeapBlock<char> interleavedBlockData[16], zeroData; //todo: dynamically?
    juce::OwnedArray<juce::dsp::AudioBlock<IIRfloat>> interleavedData;
    juce::dsp::AudioBlock<float> zero;

    juce::Array<int> filterPoints { 1, 7, 25, 61, 113, 169, 213 };

    juce::Vector3D<float> sourcePos, listenerPos;

    float mx[nImgSrc];
    float my[nImgSrc];
    float mz[nImgSrc];
    float smx[nImgSrc];
    float smy[nImgSrc];
    float smz[nImgSrc];

    int bufferSize;
    int bufferReadIdx;

    int readOffset;

    float powReflCoeff[maxOrderImgSrc + 1];
    double dist2smpls;

    float SHcoeffsOld[nImgSrc][64];
    IIRfloat SHsampleOld[nImgSrc][16]; //TODO: can be smaller: (N+1)^2/IIRfloat_elements()

    juce::AudioBuffer<float> delayBuffer;
    juce::AudioBuffer<float> monoBuffer;

    juce::OwnedArray<ReflectionProperty> reflectionList;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RoomEncoderAudioProcessor)
};