File: VisualizerComponent.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 (337 lines) | stat: -rw-r--r-- 13,110 bytes parent folder | download | duplicates (2)
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
337
/*
 ==============================================================================
 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 "../../resources/heatmap.h"
#include "../../resources/viridis_cropped.h"
#include "../JuceLibraryCode/JuceHeader.h"

//==============================================================================
/*
*/
class VisualizerComponent : public juce::Component, public juce::OpenGLRenderer, private juce::Timer
{
public:
    VisualizerComponent()
    {
        // In your constructor, you should add any child components, and
        // initialise any special settings that your component needs.

        openGLContext.setComponentPaintingEnabled (true);
        openGLContext.setContinuousRepainting (false);
        openGLContext.setRenderer (this);
        openGLContext.attachTo (*this);

        addAndMakeVisible (&hammerAitovGrid);

        visualizedRMS.resize (nSamplePoints);

        startTimer (20);
    }

    ~VisualizerComponent()
    {
        openGLContext.detach();
        openGLContext.setRenderer (nullptr);
    }

    void timerCallback() override { openGLContext.triggerRepaint(); }

    void setRmsDataPtr (float* rmsPtr) { pRMS = rmsPtr; }

    void newOpenGLContextCreated() override { createShaders(); }

    void setPeakLevel (const float newPeakLevel) { peakLevel = newPeakLevel; }

    void setDynamicRange (const float newDynamicRange) { dynamicRange = newDynamicRange; }

    void setHoldMax (const bool newHoldMax) { holdMax = newHoldMax; }

    void renderOpenGL() override
    {
        using namespace juce;
        using namespace juce::gl;

        jassert (juce::OpenGLHelpers::isContextActive());

        juce::OpenGLHelpers::clear (juce::Colour (0xFF2D2D2D));

        const float desktopScale = (float) openGLContext.getRenderingScale();
        glViewport (-5,
                    -5,
                    juce::roundToInt (desktopScale * getWidth() + 10),
                    juce::roundToInt (desktopScale * getHeight() + 10));

        glEnable (GL_DEPTH_TEST);
        glDepthFunc (GL_LESS);
        glEnable (GL_BLEND);
        glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        openGLContext.extensions.glActiveTexture (GL_TEXTURE0);
        glEnable (GL_TEXTURE_2D);

        texture.bind();
        glTexParameteri (GL_TEXTURE_2D,
                         GL_TEXTURE_MIN_FILTER,
                         GL_LINEAR); // linear interpolation when too small
        glTexParameteri (GL_TEXTURE_2D,
                         GL_TEXTURE_MAG_FILTER,
                         GL_LINEAR); // linear interpolation when too bi

        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClear (GL_COLOR_BUFFER_BIT);

        shader->use();

        if (firstRun)
        {
            juce::PixelARGB colormapData[512];
            for (int i = 0; i < 256; ++i)
            {
                const float alpha = juce::jlimit (0.0f, 1.0f, (float) i / 50.0f);
                colormapData[i] = juce::Colour::fromFloatRGBA (viridis_cropped[i][0],
                                                               viridis_cropped[i][1],
                                                               viridis_cropped[i][2],
                                                               alpha)
                                      .getPixelARGB();
                colormapData[256 + i] = juce::Colour::fromFloatRGBA (heatmap[i][0],
                                                                     heatmap[i][1],
                                                                     heatmap[i][2],
                                                                     heatmap[i][3])
                                            .getPixelARGB();
            }
            texture.loadARGB (colormapData, 256, 2);

            firstRun = false;
            openGLContext.extensions.glGenBuffers (1, &vertexBuffer);
            openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
            openGLContext.extensions.glBufferData (GL_ARRAY_BUFFER,
                                                   sizeof (hammerAitovSampleVertices),
                                                   hammerAitovSampleVertices,
                                                   GL_STATIC_DRAW);

            openGLContext.extensions.glGenBuffers (1, &indexBuffer);
            openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
            openGLContext.extensions.glBufferData (GL_ELEMENT_ARRAY_BUFFER,
                                                   sizeof (hammerAitovSampleIndices),
                                                   hammerAitovSampleIndices,
                                                   GL_STATIC_DRAW);
        }

        static GLfloat g_colorMap_data[nSamplePoints];
        for (int i = 0; i < nSamplePoints; i++)
        {
            if (holdMax)
                visualizedRMS[i] = std::max (pRMS[i], visualizedRMS[i]);
            else
                visualizedRMS[i] = pRMS[i];

            const float val =
                (juce::Decibels::gainToDecibels (visualizedRMS[i]) - peakLevel) / dynamicRange
                + 1.0f;
            g_colorMap_data[i] = juce::jlimit (0.0f, 1.0f, val);
        }

        GLuint colorBuffer;
        openGLContext.extensions.glGenBuffers (1, &colorBuffer);
        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, colorBuffer);
        openGLContext.extensions.glBufferData (GL_ARRAY_BUFFER,
                                               sizeof (g_colorMap_data),
                                               g_colorMap_data,
                                               GL_STATIC_DRAW);

        if (colormapChooser != nullptr)
            colormapChooser->set (usePerceptualColormap ? 0.0f : 1.0f);

        GLint attributeID;
        GLint programID = shader->getProgramID();

        // 1st attribute buffer : vertices
        attributeID = openGLContext.extensions.glGetAttribLocation (programID, "position");
        openGLContext.extensions.glEnableVertexAttribArray (attributeID);
        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
        openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

        openGLContext.extensions.glVertexAttribPointer (
            attributeID, // attribute 0. No particular reason for 0, but must match the layout in the shader.
            2, // size
            GL_FLOAT, // type
            GL_FALSE, // normalized?
            0, // stride
            (void*) 0 // array buffer offset
        );

        // 2nd attribute buffer : colors
        attributeID = openGLContext.extensions.glGetAttribLocation (programID, "colormapDepthIn");

        openGLContext.extensions.glEnableVertexAttribArray (attributeID);
        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, colorBuffer);

        openGLContext.extensions.glVertexAttribPointer (
            attributeID, // attribute. No particular reason for 1, but must match the layout in the shader.
            1, // size
            GL_FLOAT, // type
            GL_FALSE, // normalized?
            0, // stride
            (void*) 0 // array buffer offset
        );

        //glDrawElements (GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);  // Draw triangles!
        //glDrawArrays(GL_TRIANGLES, 0, 3);

        //        glPointSize(20.0);
        //        glDrawElements (GL_POINTS, 100, GL_UNSIGNED_INT, 0);  // Draw triangles!
        glDrawElements (GL_TRIANGLES, // mode
                        sizeof (hammerAitovSampleIndices), // count
                        GL_UNSIGNED_INT, // type
                        (void*) 0 // element array buffer offset
        );

        openGLContext.extensions.glDisableVertexAttribArray (0);
        openGLContext.extensions.glDisableVertexAttribArray (1);

        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, 0);
        openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);

        openGLContext.extensions.glDeleteBuffers (1, &colorBuffer);
    }
    void openGLContextClosing() override
    {
        openGLContext.extensions.glDeleteBuffers (1, &vertexBuffer);
        openGLContext.extensions.glDeleteBuffers (1, &indexBuffer);
        texture.release();
    }

    void paint (juce::Graphics& g) override {}

    void resized() override
    {
        // This method is where you should set the bounds of any child
        // components that your component contains..
        hammerAitovGrid.setBounds (getLocalBounds());
    }

    void createShaders()
    {
        //        vertexShader =
        //        "attribute vec3 position;\n"
        //        "void main()\n"
        //        "{\n"
        //        "   gl_Position.xyz = position;\n"
        //        "   gl_Position.w = 1.0;\n"
        //        "}";
        //

        //        fragmentShader =
        //        "void main()\n"
        //        "{\n"
        //        "      gl_FragColor = vec4(1,0,0,0.5);\n"
        //        "}";

        vertexShader = "attribute vec2 position;\n"
                       "attribute float colormapDepthIn;\n"
                       "uniform float colormapChooser;\n"
                       "varying float colormapChooserOut;\n"
                       "varying float colormapDepthOut;\n"
                       "void main()\n"
                       "{\n"
                       "   gl_Position.xy = position;\n"
                       "   gl_Position.z = 0.0;\n"
                       "   gl_Position.w = 1.0;\n"
                       "   colormapDepthOut = colormapDepthIn;\n"
                       "   colormapChooserOut = colormapChooser;\n"
                       "}";

        fragmentShader =
            "varying float colormapDepthOut;\n"
            "varying float colormapChooserOut;\n"
            "uniform sampler2D tex0;\n"
            "void main()\n"
            "{\n"
            //"      gl_FragColor = fragmentColor;\n"
            "      gl_FragColor = texture2D(tex0, vec2(colormapDepthOut, colormapChooserOut));\n"
            "}";

        std::unique_ptr<juce::OpenGLShaderProgram> newShader (
            new juce::OpenGLShaderProgram (openGLContext));
        juce::String statusText;

        if (newShader->addVertexShader (
                juce::OpenGLHelpers::translateVertexShaderToV3 (vertexShader))
            && newShader->addFragmentShader (
                juce::OpenGLHelpers::translateFragmentShaderToV3 (fragmentShader))
            && newShader->link())
        {
            shader = std::move (newShader);
            shader->use();

            colormapChooser.reset (createUniform (openGLContext, *shader, "colormapChooser"));
            statusText =
                "GLSL: v" + juce::String (juce::OpenGLShaderProgram::getLanguageVersion(), 2);
        }
        else
        {
            statusText = newShader->getLastError();
        }
    }

    void setColormap (bool shouldUsePerceptualColormap)
    {
        usePerceptualColormap = shouldUsePerceptualColormap;
    }

private:
    HammerAitovGrid hammerAitovGrid;
    GLuint vertexBuffer, indexBuffer;
    const char* vertexShader;
    const char* fragmentShader;
    std::unique_ptr<juce::OpenGLShaderProgram> shader;
    std::unique_ptr<juce::OpenGLShaderProgram::Uniform> colormapChooser;
    bool usePerceptualColormap = true;

    std::vector<float> visualizedRMS;

    static juce::OpenGLShaderProgram::Uniform*
        createUniform (juce::OpenGLContext& openGLContext,
                       juce::OpenGLShaderProgram& shaderProgram,
                       const char* uniformName)
    {
        if (openGLContext.extensions.glGetUniformLocation (shaderProgram.getProgramID(),
                                                           uniformName)
            < 0)
            return nullptr; // Return if error
        return new juce::OpenGLShaderProgram::Uniform (shaderProgram, uniformName);
    }

    float peakLevel = 0.0f; // dB
    float dynamicRange = 35.0f; // dB

    juce::OpenGLTexture texture;

    bool firstRun = true;
    bool holdMax = false;

    float* pRMS;

    juce::OpenGLContext openGLContext;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VisualizerComponent)
};