Skip to content

Instantly share code, notes, and snippets.

@wtsnz
Created April 15, 2017 22:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wtsnz/a5ee2c0216b834df5aaace5e67716f0b to your computer and use it in GitHub Desktop.
Save wtsnz/a5ee2c0216b834df5aaace5e67716f0b to your computer and use it in GitHub Desktop.
A hacky version of the JUCE AudioVisualiserComponent that renders the graph OpenGL
/*
==============================================================================
OpenGLAudioVisualiserComponent.cpp
Created: 15 Apr 2017 8:00:56pm
Author: Will Townsend
==============================================================================
*/
#include "../JuceLibraryCode/JuceHeader.h"
#include "OpenGLAudioVisualiserComponent.h"
#include <OpenGL/gl.h>
struct OpenGLAudioVisualiserComponent::ChannelInfo
{
ChannelInfo (OpenGLAudioVisualiserComponent& o, int bufferSize)
: owner (o), nextSample (0), subSample (0)
{
setBufferSize (bufferSize);
clear();
}
void clear() noexcept
{
for (int i = 0; i < levels.size(); ++i)
levels.getReference(i) = Range<float>();
value = Range<float>();
subSample = 0;
}
void pushSamples (const float* inputSamples, const int num) noexcept
{
for (int i = 0; i < num; ++i)
pushSample (inputSamples[i]);
}
void pushSample (const float newSample) noexcept
{
if (--subSample <= 0)
{
nextSample %= levels.size();
levels.getReference (nextSample++) = value;
subSample = owner.getSamplesPerBlock();
value = Range<float> (newSample, newSample);
}
else
{
value = value.getUnionWith (newSample);
}
}
void setBufferSize (int newSize)
{
levels.removeRange (newSize, levels.size());
levels.insertMultiple (-1, Range<float>(), newSize - levels.size());
if (nextSample >= newSize)
nextSample = 0;
}
OpenGLAudioVisualiserComponent& owner;
Array<Range<float> > levels;
Range<float> value;
int nextSample, subSample;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChannelInfo)
};
//==============================================================================
OpenGLAudioVisualiserComponent::OpenGLAudioVisualiserComponent (const int initialNumChannels)
: numSamples (1024),
inputSamplesPerBlock (256),
backgroundColour (Colours::black),
waveformColour (Colours::white)
{
setOpaque (true);
setNumChannels (initialNumChannels);
setRepaintRate (60);
openGLContext.setComponentPaintingEnabled(true);
openGLContext.setRenderer(this);
// openGLContext.setContinuousRepainting(true);
openGLContext.attachTo(*this);
}
OpenGLAudioVisualiserComponent::~OpenGLAudioVisualiserComponent()
{
openGLContext.detach();
openGLContext.deactivateCurrentContext();
}
void OpenGLAudioVisualiserComponent::setNumChannels (const int numChannels)
{
channels.clear();
for (int i = 0; i < numChannels; ++i)
channels.add (new ChannelInfo (*this, numSamples));
}
void OpenGLAudioVisualiserComponent::setBufferSize (int newNumSamples)
{
numSamples = newNumSamples;
for (int i = 0; i < channels.size(); ++i)
channels.getUnchecked(i)->setBufferSize (newNumSamples);
}
void OpenGLAudioVisualiserComponent::clear()
{
for (int i = 0; i < channels.size(); ++i)
channels.getUnchecked(i)->clear();
}
void OpenGLAudioVisualiserComponent::pushBuffer (const float** d, int numChannels, int num)
{
numChannels = jmin (numChannels, channels.size());
for (int i = 0; i < numChannels; ++i)
channels.getUnchecked(i)->pushSamples (d[i], num);
}
void OpenGLAudioVisualiserComponent::pushBuffer (const AudioSampleBuffer& buffer)
{
pushBuffer (buffer.getArrayOfReadPointers(),
buffer.getNumChannels(),
buffer.getNumSamples());
}
void OpenGLAudioVisualiserComponent::pushBuffer (const AudioSourceChannelInfo& buffer)
{
const int numChannels = jmin (buffer.buffer->getNumChannels(), channels.size());
for (int i = 0; i < numChannels; ++i)
channels.getUnchecked(i)->pushSamples (buffer.buffer->getReadPointer (i, buffer.startSample),
buffer.numSamples);
}
void OpenGLAudioVisualiserComponent::pushSample (const float* d, int numChannels)
{
numChannels = jmin (numChannels, channels.size());
for (int i = 0; i < numChannels; ++i)
channels.getUnchecked(i)->pushSample (d[i]);
}
void OpenGLAudioVisualiserComponent::setSamplesPerBlock (int newSamplesPerPixel) noexcept
{
inputSamplesPerBlock = newSamplesPerPixel;
}
void OpenGLAudioVisualiserComponent::setRepaintRate (int frequencyInHz)
{
startTimerHz (frequencyInHz);
}
void OpenGLAudioVisualiserComponent::timerCallback()
{
repaint();
}
void OpenGLAudioVisualiserComponent::setColours (Colour bk, Colour fg) noexcept
{
backgroundColour = bk;
waveformColour = fg;
renderOpenGL();
}
void OpenGLAudioVisualiserComponent::paint (Graphics& g)
{
// All Happens in OpenGL
}
void OpenGLAudioVisualiserComponent::newOpenGLContextCreated() { }
void OpenGLAudioVisualiserComponent::openGLContextClosing() {
openGLContext.deactivateCurrentContext();
openGLContext.detach();
}
void OpenGLAudioVisualiserComponent::renderOpenGL()
{
const float desktopScale = (float) openGLContext.getRenderingScale();
OpenGLHelpers::clear (backgroundColour);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport (0, 0, roundToInt (desktopScale * getWidth()), roundToInt (desktopScale * getHeight()));
glLoadIdentity();
glTranslated(0.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP); // render the waveform display
glColor3f(waveformColour.getFloatRed(), waveformColour.getFloatGreen(), waveformColour.getFloatBlue());
for (int i = 0; i < channels.size(); ++i)
{
const ChannelInfo& c = *channels.getUnchecked(i);
int numLevels = c.levels.size();
int nextSample = c.nextSample;
Range<float>* levels = c.levels.begin();
for (int i = 0; i < numLevels; ++i)
{
const float level = -(levels[(nextSample + i) % numLevels].getEnd());
float x = ((2.0 / numLevels) * i) - 1;
if (i == 0)
glVertex3f(x, 0, 0);
else
glVertex3f((float) x, level, 0);
}
for (int i = numLevels; --i >= 0;) {
float x = ((2.0 / numLevels) * i) - 1;
glVertex3f((float) x, -(levels[(nextSample + i) % numLevels].getStart()), 0);
}
}
glEnd();
}
/*
==============================================================================
OpenGLAudioVisualiserComponent.h
Created: 15 Apr 2017 8:00:56pm
Author: Will Townsend
==============================================================================
*/
#ifndef OPENGLAUDIOVISUALISERCOMPONENT_H_INCLUDED
#define OPENGLAUDIOVISUALISERCOMPONENT_H_INCLUDED
#include "../JuceLibraryCode/JuceHeader.h"
//
////==============================================================================
///*
//*/
//class OpenGLAudioVisualiserComponent : public Component
//{
//public:
// OpenGLAudioVisualiserComponent();
// ~OpenGLAudioVisualiserComponent();
//
// void paint (Graphics&) override;
// void resized() override;
//
//private:
// JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLAudioVisualiserComponent)
//};
#endif // OPENGLAUDIOVISUALISERCOMPONENT_H_INCLUDED
class OpenGLAudioVisualiserComponent : public Component,
public OpenGLRenderer, public Timer
{
public:
/** Creates a visualiser with the given number of channels. */
OpenGLAudioVisualiserComponent (int initialNumChannels);
/** Destructor. */
~OpenGLAudioVisualiserComponent();
/** Changes the number of channels that the visualiser stores. */
void setNumChannels (int numChannels);
/** Changes the number of samples that the visualiser keeps in its history.
Note that this value refers to the number of averaged sample blocks, and each
block is calculated as the peak of a number of incoming audio samples. To set
the number of incoming samples per block, use setSamplesPerBlock().
*/
void setBufferSize (int bufferSize);
/** */
void setSamplesPerBlock (int newNumInputSamplesPerBlock) noexcept;
/** */
int getSamplesPerBlock() const noexcept { return inputSamplesPerBlock; }
/** Clears the contents of the buffers. */
void clear();
/** Pushes a buffer of channels data.
The number of channels provided here is expected to match the number of channels
that this AudioVisualiserComponent has been told to use.
*/
void pushBuffer (const AudioSampleBuffer& bufferToPush);
/** Pushes a buffer of channels data.
The number of channels provided here is expected to match the number of channels
that this AudioVisualiserComponent has been told to use.
*/
void pushBuffer (const AudioSourceChannelInfo& bufferToPush);
/** Pushes a buffer of channels data.
The number of channels provided here is expected to match the number of channels
that this AudioVisualiserComponent has been told to use.
*/
void pushBuffer (const float** channelData, int numChannels, int numSamples);
/** Pushes a single sample (per channel).
The number of channels provided here is expected to match the number of channels
that this AudioVisualiserComponent has been told to use.
*/
void pushSample (const float* samplesForEachChannel, int numChannels);
/** Sets the colours used to paint the */
void setColours (Colour backgroundColour, Colour waveformColour) noexcept;
/** Sets the frequency at which the component repaints itself. */
void setRepaintRate (int frequencyInHz);
/** Draws a channel of audio data in the given bounds.
The default implementation just calls getChannelAsPath() and fits this into the given
area. You may want to override this to draw things differently.
*/
// virtual void paintChannel (Graphics&, Rectangle<float> bounds,
// const Range<float>* levels, int numLevels, int nextSample);
/** Creates a path which contains the waveform shape of a given set of range data.
The path is normalised so that -1 and +1 are its upper and lower bounds, and it
goes from 0 to numLevels on the X axis.
*/
// void getChannelAsPath (Path& result, const Range<float>* levels, int numLevels, int nextSample);
//==============================================================================
/** @internal */
void paint (Graphics&) override;
void newOpenGLContextCreated() override;
void openGLContextClosing() override;
void renderOpenGL() override;
private:
OpenGLContext openGLContext;
struct ChannelInfo;
friend struct ChannelInfo;
friend struct ContainerDeletePolicy<ChannelInfo>;
OwnedArray<ChannelInfo> channels;
int numSamples, inputSamplesPerBlock;
Colour backgroundColour, waveformColour;
void timerCallback() override;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLAudioVisualiserComponent)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment