Juce different coloured waveform based on play position

Hello,

I am trying to set the waveform to have a different colour before and after (my code below), but at the moment the waveform isn’t remaining static once its drawn.

Am I over complicating this?

Does anyone have any feedback?

I’m not sure if I understand correctly what you mean. Perhaps you need 2 instances of the Audiothumbnail?

I have cloned AudioThumbnail and added a bit of code. I now have what I called “MulticolorAudiothumbnail” which allows you to draw sections of the waveform in a different color.

I know it’s not ideal to copy an existing class as all changes made to the original would also need to be copied to your clone but I didn’t find a better way at the time and the solution works. My use case was to draw a waveform in a selected state eg with inverted colors.

Let me know if you want me to share it.

Yes, I would be very curious to see how you did it?

OOoh please share this, I am also in need of this.
Thank you!

1 Like

I don’t think you need to alter the code. What you can do is draw the whole waveform in one colour and then reduce the clip area and draw the selection with a different colour on top.
That results in the selection being drawn twice. But the AudioThumbnail is clever enough to only draw the not clipped area (the loop for adding the rectangles respects the clipRect).

1 Like

Very insightful Daniel! Thank you

Daniel could you give a concrete example?
For me my application slows down significantly when I try out this code:

Thank you!

void ScopeSelectionStepView::paint (juce::Graphics& g)
{
    g.setColour(Colours::black);
    g.fillAll();
    
    auto numChannels = m_thumbnail.getNumChannels();
    if (numChannels == 0) { return; }
    
    drawTotalWaveform(g, getLocalBounds());
    drawSelectionWaveform(g);
}

void ScopeSelectionStepView::drawTotalWaveform (Graphics& g, Rectangle<int> bounds)
{
    g.setColour (Colours::white);
    int duration = m_dataModel.getAudioFileDataModel().getFileDurationInSeconds();
    auto range = m_stepDataModel.getVisibleRangeDataModel().getVisibleRange();
    
    m_thumbnail.drawChannel(g,
                            bounds,
                            range.getStart() * duration,
                            range.getEnd() * duration,
                            0,
                            1.0f);
}

void ScopeSelectionStepView::drawSelectionWaveform (Graphics& g)
{
    int duration = m_dataModel.getAudioFileDataModel().getFileDurationInSeconds();
    
    auto startTime = timeToXPosition(m_stepDataModel.getSelectionStartRange().getStart());
    auto endTime = timeToXPosition(m_stepDataModel.getSelectionEndRange().getEnd());
    auto width = endTime - startTime;
    
    auto selectionArea = Rectangle<int>(startTime, 0, width, getHeight());

    g.setColour(Colours::white);
    g.fillRect(selectionArea);
    
    g.setColour (Colours::black);
    m_thumbnail.drawChannel(g,
                            selectionArea,
                            m_stepDataModel.getSelectionStartRange().getStart() * duration,
                            m_stepDataModel.getSelectionEndRange().getEnd() * duration,
                            0,
                            1.0f);
}