reVeno/Source/Veno/GUI/Components/LCD/Waveforms.cpp

289 lines
7.5 KiB
C++
Raw Normal View History

2020-06-13 10:56:20 +02:00
//
// Created by versustune on 11.06.20.
//
#include "Waveforms.h"
#include <utility>
#include "../../../Core/Config.h"
#include "../../../Utils.h"
#include "../../../VenoInstance.h"
#include "../../../Fonts/Fonts.h"
2020-06-14 21:14:28 +02:00
Waveforms::Waveforms (const std::string& processId) : BaseComponent(processId)
{
2020-06-14 21:14:28 +02:00
setMouseCursor(MouseCursor::PointingHandCursor);
m_context.setOpenGLVersionRequired(OpenGLContext::OpenGLVersion::openGL3_2);
m_context.setRenderer(this);
m_context.setContinuousRepainting(false);
m_context.setComponentPaintingEnabled(true);
m_context.attachTo(*this);
setFps();
std::srand(unsigned(time(nullptr)));
pickRandomText = (std::rand() % RANDOM_TEXT_COUNT);
2020-06-13 10:56:20 +02:00
m_ticks = 0;
}
Waveforms::~Waveforms ()
{
2020-06-14 21:14:28 +02:00
stopTimer();
shaderProgram.reset();
m_context.detach();
2020-06-13 10:56:20 +02:00
}
void Waveforms::newOpenGLContextCreated ()
{
2020-06-14 21:14:28 +02:00
compileOpenGLShaderProgram();
2020-06-13 10:56:20 +02:00
}
void Waveforms::openGLContextClosing ()
{
2020-06-13 10:56:20 +02:00
}
void Waveforms::renderOpenGL ()
{
2020-06-14 21:14:28 +02:00
if (!isShowing() || shaderProgram == nullptr || !shaderProgram->getLastError().isEmpty())
{
2020-06-13 10:56:20 +02:00
return;
}
2020-06-14 21:14:28 +02:00
auto theme = Config::getInstance()->getCurrentTheme();
if (theme == nullptr)
{
2020-06-13 10:56:20 +02:00
return;
}
2020-06-14 21:14:28 +02:00
glViewport(0, 0, getWidth(), getHeight());
OpenGLHelpers::clear(theme->getColour(ThemeColour::lcd_bg));
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
shaderProgram->use();
auto color = theme->getColour(ThemeColour::lcd);
shaderProgram->setUniform("color", color.getFloatRed(), color.getFloatGreen(), color.getFloatBlue(),
color.getFloatAlpha());
if (m_isWelcome || m_isStarting || m_isChangingData)
{
2020-06-13 10:56:20 +02:00
return;
}
switch (m_mode)
{
2020-06-13 10:56:20 +02:00
case 1:
2020-06-14 21:14:28 +02:00
drawAudioOutput();
2020-06-13 10:56:20 +02:00
break;
case 2:
2020-06-14 21:14:28 +02:00
drawWaveTable();
2020-06-13 10:56:20 +02:00
break;
case 3:
2020-06-14 21:14:28 +02:00
drawSpectrum();
2020-06-13 10:56:20 +02:00
break;
default:
2020-06-14 21:14:28 +02:00
drawPeakMeter();
2020-06-13 10:56:20 +02:00
}
}
void Waveforms::compileOpenGLShaderProgram ()
{
2020-06-13 10:56:20 +02:00
std::unique_ptr<OpenGLShaderProgram> shaderProgramAttempt
2020-06-14 21:14:28 +02:00
= std::make_unique<OpenGLShaderProgram>(m_context);
if (shaderProgramAttempt->addVertexShader({BinaryData::WaveForm_vertex_glsl})
&& shaderProgramAttempt->addFragmentShader({BinaryData::WaveForm_fragment_glsl})
&& shaderProgramAttempt->link())
{
2020-06-14 21:14:28 +02:00
shaderProgram = std::move(shaderProgramAttempt);
2020-06-13 10:56:20 +02:00
}
}
void Waveforms::mouseDown (const MouseEvent& e)
{
if (!m_enableModeToggle)
{
2020-06-13 10:56:20 +02:00
return;
}
m_mode++;
if (m_mode > 3)
{
2020-06-13 10:56:20 +02:00
m_mode = 0;
}
}
void Waveforms::mouseDrag (const MouseEvent& e)
{
2020-06-13 10:56:20 +02:00
//do nothing... you faggot
}
void Waveforms::timerCallback ()
{
if (m_isWelcome || m_isStarting || m_isChangingData || needToClear)
{
2020-06-14 21:14:28 +02:00
repaint();
}
else
{
2020-06-14 21:14:28 +02:00
if (m_context.isAttached())
{
2020-06-14 21:14:28 +02:00
m_context.triggerRepaint();
2020-06-13 10:56:20 +02:00
}
}
2020-06-14 21:14:28 +02:00
setFps();
2020-06-13 10:56:20 +02:00
}
void Waveforms::drawWaveTable ()
{
2020-06-13 10:56:20 +02:00
// this will draw the current selected oscillator :D
}
void Waveforms::drawAudioOutput ()
{
2020-06-13 10:56:20 +02:00
// draw audio from the oscillators
2020-06-14 21:14:28 +02:00
auto instance = VenoInstance::getInstance(BaseComponent::m_processId);
auto buffer = instance->audioBuffer->getBuffer();
glBegin(GL_LINE_STRIP);
2020-06-13 10:56:20 +02:00
float posX = -1;
2020-06-14 21:14:28 +02:00
float inc = 2.0f / buffer.size();
for (float i : buffer)
{
2020-06-14 21:14:28 +02:00
glVertex2f(posX, i);
2020-06-13 10:56:20 +02:00
posX += inc;
}
2020-06-14 21:14:28 +02:00
glEnd();
2020-06-13 10:56:20 +02:00
}
void Waveforms::drawPeakMeter ()
{
2020-06-14 21:14:28 +02:00
auto theme = Config::getInstance()->getCurrentTheme();
if (theme == nullptr)
{
2020-06-13 10:56:20 +02:00
return;
}
2020-06-14 21:14:28 +02:00
auto instance = VenoInstance::getInstance(BaseComponent::m_processId);
instance->audioBuffer->calcPeak();
2020-06-13 10:56:20 +02:00
// draw peak signal
2020-06-14 21:14:28 +02:00
auto leftChannel = getdBForChannel(instance->audioBuffer->leftPeak);
selectColourByPeak(leftChannel);
glBegin(GL_TRIANGLES);
glVertex2f(-0.9f, leftChannel);
glVertex2f(-0.9f, -1.0f);
glVertex2f(-0.01f, -1.0f);
glVertex2f(-0.9f, leftChannel);
glVertex2f(-0.01f, leftChannel);
glVertex2f(-0.01f, -1.0f);
glEnd();
auto rightChannel = getdBForChannel(instance->audioBuffer->rightPeak);
selectColourByPeak(rightChannel);
glBegin(GL_TRIANGLES);
glVertex2f(0.9f, rightChannel);
glVertex2f(0.9f, -1.0f);
glVertex2f(0.01f, -1.0f);
glVertex2f(0.9f, rightChannel);
glVertex2f(0.01f, rightChannel);
glVertex2f(0.01f, -1.0f);
glEnd();
}
void Waveforms::paint (Graphics& g)
{
2020-06-14 21:14:28 +02:00
std::shared_ptr<Theme> theme = Config::getInstance()->getCurrentTheme();
auto accent = theme->getColour(ThemeColour::lcd);
g.setColour(accent);
g.setFont(*VenoFonts::getLCD());
VeNo::Utils::setFontSize(16.0f, g);
if (m_isWelcome)
{
2020-06-14 21:14:28 +02:00
drawWelcome(g, getWidth(), getHeight(), 0, 0);
2020-06-13 10:56:20 +02:00
m_ticks++;
if (m_ticks > m_time_needed_startup)
{
2020-06-13 10:56:20 +02:00
m_isWelcome = false;
m_ticks = 0;
needToClear = true;
}
}
else if (m_isStarting)
{
2020-06-14 21:14:28 +02:00
g.drawText(m_warmUpText[pickRandomText], 0, 0, getWidth(), getHeight(),
Justification::centred, true);
2020-06-13 10:56:20 +02:00
m_ticks++;
if (m_ticks > m_time_needed_startup)
{
2020-06-13 10:56:20 +02:00
m_isStarting = false;
m_ticks = 0;
needToClear = true;
}
}
else if (m_isChangingData)
{
2020-06-14 21:14:28 +02:00
drawChangedParameter(g, getWidth(), getHeight(), 0, 0);
2020-06-13 10:56:20 +02:00
m_ticks++;
if (m_ticks > m_time_needed)
{
2020-06-13 10:56:20 +02:00
m_isChangingData = false;
m_ticks = 0;
needToClear = true;
}
}
else
{
2020-06-14 21:14:28 +02:00
g.resetToDefaultState();
2020-06-13 10:56:20 +02:00
needToClear = false;
}
}
void Waveforms::drawChangedParameter (Graphics& g, int w, int h, int x, int y) const
{
2020-06-13 10:56:20 +02:00
int halfHeight = h / 2;
2020-06-14 21:14:28 +02:00
float font = VeNo::Utils::setFontSize(12, g);
g.drawText(changingParameter, x, y + halfHeight - font, w, font, Justification::centred, true);
g.drawText(std::to_string(changedValue), x, y + halfHeight + 4, w, font, Justification::centred,
true);
2020-06-13 10:56:20 +02:00
}
void Waveforms::drawWelcome (Graphics& g, int w, int h, int x, int y)
{
2020-06-14 21:14:28 +02:00
float font = VeNo::Utils::setFontSize(12, g);
2020-06-13 10:56:20 +02:00
int halfHeight = h / 2;
2020-06-14 21:14:28 +02:00
g.drawText(m_readyText, x, y + halfHeight - font, w, font, Justification::centred, true);
g.drawText(SystemStats::getLogonName(), x, y + halfHeight + 4, w, font, Justification::centred,
true);
2020-06-13 10:56:20 +02:00
}
void Waveforms::drawSpectrum ()
{
2020-06-13 10:56:20 +02:00
}
void Waveforms::selectColourByPeak (float value)
{
2020-06-14 21:14:28 +02:00
auto theme = Config::getInstance()->getCurrentTheme();
if (theme == nullptr)
{
2020-06-13 10:56:20 +02:00
return;
}
2020-06-14 21:14:28 +02:00
auto color = theme->getColour(ThemeColour::lcd);
if (value > 0.9)
{
2020-06-14 21:14:28 +02:00
color = theme->getColour(ThemeColour::clip);
2020-06-13 10:56:20 +02:00
}
2020-06-14 21:14:28 +02:00
else if (value > 0.8)
{
2020-06-14 21:14:28 +02:00
color = theme->getColour(ThemeColour::warning);
}
shaderProgram->setUniform("color", color.getFloatRed(), color.getFloatGreen(), color.getFloatBlue(),
color.getFloatAlpha());
}
float Waveforms::getdBForChannel (float value)
{
float v = VeNo::Utils::clamp(Decibels::gainToDecibels(value, -60.0f), -60.0f, 6.0f);
return jmap(v, -60.0f, 6.0f, -1.0f,
1.0f);
}
void Waveforms::setFps ()
{
if(m_currentFps != Config::getInstance()->getFps()) {
m_currentFps = Config::getInstance()->getFps();
// is something that
m_time_needed = roundToInt(4000 / (1000 / m_currentFps));
m_time_needed_startup = roundToInt(1000 / (1000 / m_currentFps));
stopTimer();
startTimer(m_currentFps);
2020-06-13 10:56:20 +02:00
}
}