The Red Penguin

Draw the thumbnail


Tuesday 4 August 2020

1. The first thing to do is put a bit of logic in. So in WaveformDisplay.h set up a variable in private: called
bool fileLoaded;

2. We can go back to the code in WD.cpp where we set up a local variable loaded and replace it with fileLoaded

3. In the WaveformDisplay::paint function this should be the whole code:
    g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));   // clear the background

g.setColour (juce::Colours::grey);
g.drawRect (getLocalBounds(), 1); // draw an outline around the component

g.setColour (juce::Colours::orange);

if (fileLoaded)
{
audioThumb.drawChannel(g,
getLocalBounds(),
0,
audioThumb.getTotalLength(),
0,
1.0f
);
}
else
{
g.setFont(20.0f);
g.drawText("File not loaded ...", getLocalBounds(),
juce::Justification::centred, true); // draw some placeholder text
}

At the moment you won't see the waveform unless you resize the window. Unlike p5, paint is not being called continuously.

4. In WaveformDisplay.h we need to change the class inheritance as follows:
class WaveformDisplay  : public juce::Component,
public juce::ChangeListener

5. I need to add a new function in public in WD.h too:
void changeListenerCallback (juce::ChangeBroadcaster *source) override;

6. Then we need to implement this function in WD.cpp:
void WaveformDisplay::changeListenerCallback(juce::ChangeBroadcaster* source)
{
DBG("wfd change!");
repaint()
}

7. We need to register with the AudioThumbnail for changes. In WaveformDisplay::WaveformDisplay type
audioThumb.addChangeListener(this);

8. We add fileLoaded(false) to the initialisation list in WaveformDisplay.h because we want this variable to start as false, so we get our "not loaded" messages back.
WaveformDisplay::WaveformDisplay(juce::AudioFormatManager& formatManagerToUse,
juce::AudioThumbnailCache& cacheToUse) :
audioThumb(1000, formatManagerToUse, cacheToUse),
fileLoaded(false)