8-voice polyphonic granular synth (VST3/AU/LV2) with: - 128 grain pool per voice, Hann windowing, linear interpolation - Root note selector, sample rate correction, sustain pedal (CC64) - Scatter controls, direction modes (Fwd/Rev/PingPong), freeze - ADSR envelope, global filter (LP/HP/BP), reverb - Waveform display with grain visualization - Drag & drop sample loading, full state save/restore - CI/CD for Windows/macOS/Linux Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
202 sor
6.4 KiB
C++
202 sor
6.4 KiB
C++
#include "WaveformDisplay.h"
|
|
#include "LookAndFeel.h"
|
|
|
|
WaveformDisplay::WaveformDisplay() {}
|
|
|
|
void WaveformDisplay::setBuffer (const juce::AudioBuffer<float>* buffer)
|
|
{
|
|
audioBuffer = buffer;
|
|
if (buffer != nullptr)
|
|
{
|
|
totalSourceSamples = buffer->getNumSamples();
|
|
lastBufferSize = totalSourceSamples;
|
|
}
|
|
else
|
|
{
|
|
totalSourceSamples = 0;
|
|
lastBufferSize = 0;
|
|
}
|
|
pathDirty = true;
|
|
repaint();
|
|
}
|
|
|
|
void WaveformDisplay::setActiveGrains (const std::vector<GrainEngine::ActiveGrainInfo>& grains)
|
|
{
|
|
activeGrains = grains;
|
|
repaint();
|
|
}
|
|
|
|
void WaveformDisplay::resized()
|
|
{
|
|
pathDirty = true;
|
|
}
|
|
|
|
void WaveformDisplay::rebuildWaveformPath (juce::Rectangle<float> bounds)
|
|
{
|
|
cachedWaveformPath.clear();
|
|
|
|
if (audioBuffer == nullptr || audioBuffer->getNumSamples() == 0)
|
|
return;
|
|
|
|
const int numSamples = audioBuffer->getNumSamples();
|
|
const float width = bounds.getWidth();
|
|
const float height = bounds.getHeight();
|
|
const float midY = bounds.getCentreY();
|
|
const float* data = audioBuffer->getReadPointer (0);
|
|
|
|
// Build filled waveform path
|
|
int blockSize = std::max (1, numSamples / (int) width);
|
|
|
|
// Top line (max values)
|
|
cachedWaveformPath.startNewSubPath (bounds.getX(), midY);
|
|
for (int x = 0; x < (int) width; ++x)
|
|
{
|
|
int sampleIndex = (int) ((float) x / width * (float) numSamples);
|
|
sampleIndex = juce::jlimit (0, numSamples - 1, sampleIndex);
|
|
|
|
float maxVal = -1.0f;
|
|
for (int j = 0; j < blockSize && (sampleIndex + j) < numSamples; ++j)
|
|
maxVal = std::max (maxVal, data[sampleIndex + j]);
|
|
|
|
float topY = midY - maxVal * (height * 0.45f);
|
|
cachedWaveformPath.lineTo ((float) x + bounds.getX(), topY);
|
|
}
|
|
|
|
// Bottom line (min values, reversed)
|
|
for (int x = (int) width - 1; x >= 0; --x)
|
|
{
|
|
int sampleIndex = (int) ((float) x / width * (float) numSamples);
|
|
sampleIndex = juce::jlimit (0, numSamples - 1, sampleIndex);
|
|
|
|
float minVal = 1.0f;
|
|
for (int j = 0; j < blockSize && (sampleIndex + j) < numSamples; ++j)
|
|
minVal = std::min (minVal, data[sampleIndex + j]);
|
|
|
|
float botY = midY - minVal * (height * 0.45f);
|
|
cachedWaveformPath.lineTo ((float) x + bounds.getX(), botY);
|
|
}
|
|
|
|
cachedWaveformPath.closeSubPath();
|
|
lastWidth = getWidth();
|
|
lastHeight = getHeight();
|
|
pathDirty = false;
|
|
}
|
|
|
|
void WaveformDisplay::paint (juce::Graphics& g)
|
|
{
|
|
auto bounds = getLocalBounds().toFloat();
|
|
|
|
// Background gradient
|
|
{
|
|
juce::ColourGradient bgGrad (InstaGrainLookAndFeel::bgDark.darker (0.4f), 0, bounds.getY(),
|
|
InstaGrainLookAndFeel::bgDark.darker (0.2f), 0, bounds.getBottom(), false);
|
|
g.setGradientFill (bgGrad);
|
|
g.fillRoundedRectangle (bounds, 4.0f);
|
|
}
|
|
|
|
// Border
|
|
g.setColour (InstaGrainLookAndFeel::bgLight.withAlpha (0.3f));
|
|
g.drawRoundedRectangle (bounds, 4.0f, 1.0f);
|
|
|
|
// Grid lines
|
|
g.setColour (InstaGrainLookAndFeel::bgLight.withAlpha (0.12f));
|
|
for (int i = 1; i < 8; ++i)
|
|
{
|
|
float xLine = bounds.getX() + bounds.getWidth() * (float) i / 8.0f;
|
|
g.drawVerticalLine ((int) xLine, bounds.getY(), bounds.getBottom());
|
|
}
|
|
|
|
// Center line
|
|
g.setColour (InstaGrainLookAndFeel::bgLight.withAlpha (0.2f));
|
|
float midY = bounds.getCentreY();
|
|
g.drawHorizontalLine ((int) midY, bounds.getX(), bounds.getRight());
|
|
|
|
if (audioBuffer == nullptr || audioBuffer->getNumSamples() == 0)
|
|
{
|
|
// "Drop sample here" text
|
|
g.setColour (InstaGrainLookAndFeel::textSecondary);
|
|
g.setFont (16.0f);
|
|
g.drawText ("Drop a sample here or click Load Sample", bounds, juce::Justification::centred);
|
|
return;
|
|
}
|
|
|
|
// Rebuild path if needed
|
|
if (pathDirty || lastWidth != getWidth() || lastHeight != getHeight()
|
|
|| lastBufferSize != audioBuffer->getNumSamples())
|
|
{
|
|
lastBufferSize = audioBuffer->getNumSamples();
|
|
rebuildWaveformPath (bounds);
|
|
}
|
|
|
|
// Scatter range highlight
|
|
if (scatterRange > 0.001f)
|
|
{
|
|
float posX = bounds.getX() + grainPosition * bounds.getWidth();
|
|
float rangeW = scatterRange * bounds.getWidth();
|
|
float left = std::max (bounds.getX(), posX - rangeW);
|
|
float right = std::min (bounds.getRight(), posX + rangeW);
|
|
|
|
g.setColour (juce::Colour (0x1800ff88));
|
|
g.fillRect (left, bounds.getY(), right - left, bounds.getHeight());
|
|
}
|
|
|
|
// Waveform fill + stroke
|
|
juce::Colour waveColour (0xffff8844);
|
|
g.setColour (waveColour.withAlpha (0.5f));
|
|
g.fillPath (cachedWaveformPath);
|
|
g.setColour (waveColour.withAlpha (0.9f));
|
|
g.strokePath (cachedWaveformPath, juce::PathStrokeType (1.0f));
|
|
|
|
// Active grain rectangles
|
|
if (totalSourceSamples > 0)
|
|
{
|
|
for (const auto& grain : activeGrains)
|
|
{
|
|
if (grain.startSample < 0) continue;
|
|
float gx = bounds.getX() + ((float) grain.startSample / (float) totalSourceSamples) * bounds.getWidth();
|
|
float gw = ((float) grain.lengthSamples / (float) totalSourceSamples) * bounds.getWidth();
|
|
gw = std::max (2.0f, gw);
|
|
|
|
float alpha = 0.6f * (1.0f - grain.progress); // fade as grain progresses
|
|
g.setColour (InstaGrainLookAndFeel::accent.withAlpha (alpha));
|
|
g.fillRect (gx, bounds.getY() + 2, gw, bounds.getHeight() - 4);
|
|
}
|
|
}
|
|
|
|
// Position indicator line
|
|
{
|
|
float posX = bounds.getX() + grainPosition * bounds.getWidth();
|
|
g.setColour (InstaGrainLookAndFeel::accent);
|
|
g.drawVerticalLine ((int) posX, bounds.getY(), bounds.getBottom());
|
|
|
|
// Glow
|
|
g.setColour (InstaGrainLookAndFeel::accent.withAlpha (0.3f));
|
|
g.fillRect (posX - 2.0f, bounds.getY(), 5.0f, bounds.getHeight());
|
|
}
|
|
}
|
|
|
|
void WaveformDisplay::updatePositionFromMouse (const juce::MouseEvent& e)
|
|
{
|
|
auto bounds = getLocalBounds().toFloat();
|
|
float pos = (float) (e.x - bounds.getX()) / bounds.getWidth();
|
|
pos = juce::jlimit (0.0f, 1.0f, pos);
|
|
grainPosition = pos;
|
|
|
|
if (onPositionChanged)
|
|
onPositionChanged (pos);
|
|
|
|
repaint();
|
|
}
|
|
|
|
void WaveformDisplay::mouseDown (const juce::MouseEvent& e)
|
|
{
|
|
if (audioBuffer != nullptr && audioBuffer->getNumSamples() > 0)
|
|
updatePositionFromMouse (e);
|
|
}
|
|
|
|
void WaveformDisplay::mouseDrag (const juce::MouseEvent& e)
|
|
{
|
|
if (audioBuffer != nullptr && audioBuffer->getNumSamples() > 0)
|
|
updatePositionFromMouse (e);
|
|
}
|