File: DirectivityVisualizer.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 (294 lines) | stat: -rw-r--r-- 10,699 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
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
/*
 ==============================================================================
 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/>.
 ==============================================================================
 */

#pragma once

#include <JuceHeader.h>

//==============================================================================
/*
*/

class DirectivityVisualizer : public juce::Component
{
    struct WeightsAndColour
    {
        float* weights;
        juce::Colour colour;
    };

    const float deg2rad = juce::MathConstants<float>::pi / 180.0f;
    const int degStep = 1;
    const int nLookUpSamples = 361;
    const int maxdB = 90;
    const float power = 3.0f;
    const int dBstep = 10;
    //#define scale 4
    const float scale = std::sqrt (4 * juce::MathConstants<float>::pi) * decodeCorrection (7);

public:
    DirectivityVisualizer()
    {
        // 0th
        lookUpTables.add (new juce::dsp::LookupTableTransform<float> (
            [this] (float phi) { return scale * (0.25f / (float) juce::MathConstants<float>::pi); },
            -juce::MathConstants<float>::pi,
            juce::MathConstants<float>::pi,
            nLookUpSamples));

        // 1st
        lookUpTables.add (new juce::dsp::LookupTableTransform<float> (
            [this] (float phi)
            { return scale * (0.75f / juce::MathConstants<float>::pi) * std::cos (phi); },
            -juce::MathConstants<float>::pi,
            juce::MathConstants<float>::pi,
            nLookUpSamples));

        // 2nd
        lookUpTables.add (new juce::dsp::LookupTableTransform<float> (
            [this] (float phi)
            {
                return scale * 2.0f * (5.0f / 16.0f / juce::MathConstants<float>::pi)
                       * (3 * std::cos (phi) * std::cos (phi) - 1.0f);
            },
            -juce::MathConstants<float>::pi,
            juce::MathConstants<float>::pi,
            nLookUpSamples));

        // 3rd
        lookUpTables.add (new juce::dsp::LookupTableTransform<float> (
            [this] (float phi)
            {
                return scale * (7.0f / juce::MathConstants<float>::pi) / 8.0f
                       * (5 * pow (std::cos (phi), 3) - 3.0f * std::cos (phi));
            },
            -juce::MathConstants<float>::pi,
            juce::MathConstants<float>::pi,
            nLookUpSamples));

        // 4th
        lookUpTables.add (new juce::dsp::LookupTableTransform<float> (
            [this] (float phi)
            {
                return scale * 9.0f / 32.0f / juce::MathConstants<float>::pi
                       * (35 * pow (std::cos (phi), 4) - 30 * pow (std::cos (phi), 2) + 3.0f);
            },
            -juce::MathConstants<float>::pi,
            juce::MathConstants<float>::pi,
            nLookUpSamples));

        // 5th
        lookUpTables.add (new juce::dsp::LookupTableTransform<float> (
            [this] (float phi)
            {
                return scale * 8.0f / 256.0f * 11.0f / juce::MathConstants<float>::pi
                       * (63 * pow (std::cos (phi), 5) - 70 * pow (std::cos (phi), 3)
                          + 15.0f * std::cos (phi));
            },
            -juce::MathConstants<float>::pi,
            juce::MathConstants<float>::pi,
            nLookUpSamples));

        // 6th
        lookUpTables.add (new juce::dsp::LookupTableTransform<float> (
            [this] (float phi)
            {
                return scale * 16.0f / 1024.0f * 13.0f / juce::MathConstants<float>::pi
                       * (231 * pow (std::cos (phi), 6) - 315 * pow (std::cos (phi), 4)
                          + 105 * pow (std::cos (phi), 2) - 5.0f);
            },
            -juce::MathConstants<float>::pi,
            juce::MathConstants<float>::pi,
            nLookUpSamples));

        // 7th
        lookUpTables.add (new juce::dsp::LookupTableTransform<float> (
            [this] (float phi)
            {
                return scale * 16.0f / 1024.0f * 15.0f / juce::MathConstants<float>::pi
                       * (429 * pow (std::cos (phi), 7) - 693 * pow (std::cos (phi), 5)
                          + 315 * pow (std::cos (phi), 3) - 35 * std::cos (phi));
            },
            -juce::MathConstants<float>::pi,
            juce::MathConstants<float>::pi,
            nLookUpSamples));

        for (int phi = -180; phi <= 180; phi += degStep)
        {
            pointsOnCircle.add (juce::Point<float> (cos (deg2rad * phi), sin (deg2rad * phi)));
        }

        juce::Path circle;
        circle.addEllipse (-1.0f, -1.0f, 2.0f, 2.0f);
        juce::Path line;
        line.startNewSubPath (0.0f, -1.0f);
        line.lineTo (0.0f, 1.0f);

        grid.clear();
        for (int dB = 0; dB < maxdB; dB += dBstep)
            grid.addPath (circle, juce::AffineTransform().scaled (dBToRadius (-dB)));

        subGrid.clear();
        for (int dB = dBstep / 2; dB < maxdB; dB += dBstep)
            subGrid.addPath (circle, juce::AffineTransform().scaled (dBToRadius (-dB)));

        subGrid.addPath (line);
        subGrid.addPath (line,
                         juce::AffineTransform().rotation (0.25f * juce::MathConstants<float>::pi));
        subGrid.addPath (line,
                         juce::AffineTransform().rotation (0.5f * juce::MathConstants<float>::pi));
        subGrid.addPath (line,
                         juce::AffineTransform().rotation (0.75f * juce::MathConstants<float>::pi));
    }

    ~DirectivityVisualizer() {}

    float dBToRadius (float dB)
    {
        if (dB > 0.0f)
            dB = 0.0f;
        //float radius = 1.0f - pow(abs(dB) / maxdB, power);
        float radius = (exp (power * dB / maxdB) - exp (-power)) / (1.0f - exp (-power));
        if (radius < 0.0f)
            radius = 0.0f;
        return radius;
    }

    void paint (juce::Graphics& g) override
    {
        juce::Rectangle<int> bounds = getLocalBounds();
        const int scale = plotArea.getWidth() / 2;
        //const int height = bounds.getHeight();

        int centreX = bounds.getCentreX();
        int centreY = bounds.getCentreY();

        juce::Path path;
        path = grid;
        path.applyTransform (transform);
        g.setColour (juce::Colours::skyblue.withMultipliedAlpha (0.1f));
        g.fillPath (path);
        g.setColour (juce::Colours::white);
        g.strokePath (path, juce::PathStrokeType (1.0f));

        path = subGrid;
        path.applyTransform (transform);
        g.setColour (juce::Colours::skyblue.withMultipliedAlpha (0.3f));
        g.strokePath (path, juce::PathStrokeType (0.5f));

        g.setColour (juce::Colours::white);

        auto currentFont =
            juce::FontOptions (getLookAndFeel().getTypefaceForFont (juce::FontOptions (12.0f, 2)))
                .withHeight (12.0f);
        g.setFont (currentFont);
        g.drawText ("0 dB",
                    centreX - 10,
                    centreY + scale * dBToRadius (0.0f) - 12,
                    20,
                    12,
                    juce::Justification::centred);
        g.drawText ("-10",
                    centreX - 10,
                    centreY + scale * dBToRadius (-10.0f),
                    20,
                    12,
                    juce::Justification::centred);
        g.drawText ("-20",
                    centreX - 10,
                    centreY + scale * dBToRadius (-20.0f),
                    20,
                    12,
                    juce::Justification::centred);

        int size = elements.size();
        for (int i = 0; i < size; ++i)
        {
            WeightsAndColour& handle (elements.getReference (i));
            g.setColour (handle.colour);

            path.clear();

            int idx = 0;
            for (int phi = -180; phi <= 180; phi += degStep)
            {
                float gain = 0.0f;
                float phiInRad = (float) phi * deg2rad;
                for (int o = 0; o < 8; ++o)
                {
                    gain += handle.weights[o] * lookUpTables[o]->processSample (phiInRad);
                }
                juce::Point<float> point =
                    dBToRadius (juce::Decibels::gainToDecibels (std::abs (gain), -1.0f * maxdB))
                    * pointsOnCircle[idx];
                if (phi == -180)
                    path.startNewSubPath (point);
                else
                    path.lineTo (point);
                ++idx;
            }

            path.closeSubPath();
            path.applyTransform (transform);
            g.strokePath (path, juce::PathStrokeType (2.0f));
        }
    }

    void resized() override
    {
        juce::Rectangle<int> bounds = getLocalBounds();
        juce::Point<int> centre = bounds.getCentre();

        bounds.reduce (10, 10);

        if (bounds.getWidth() > bounds.getHeight())
            bounds.setWidth (bounds.getHeight());
        else
            bounds.setHeight (bounds.getWidth());
        bounds.setCentre (centre);

        transform = juce::AffineTransform::fromTargetPoints ((float) centre.x,
                                                             (float) centre.y,
                                                             (float) centre.x,
                                                             bounds.getY(),
                                                             bounds.getX(),
                                                             centre.y);

        plotArea = bounds;
    }

    void addElement (float* weights, juce::Colour colour) { elements.add ({ weights, colour }); }

private:
    juce::OwnedArray<juce::dsp::LookupTableTransform<float>> lookUpTables;
    juce::Path grid;
    juce::Path subGrid;
    juce::AffineTransform transform;
    juce::Rectangle<int> plotArea;

    int maxOrder;

    juce::Array<WeightsAndColour> elements;

    juce::Array<juce::Point<float>> pointsOnCircle;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DirectivityVisualizer)
};