The Red Penguin

Implement a timer


Wednesday 5 August 2020

We're going to implement the timer class on the deckGUI component so that it can automatically keep calling that get position function on the DJAudioPlayer, and pass it over to WaveformDisplay.

So the waveform position, the playhead, gets continually updated as the track is playing.

1. Go to DeckGUI.h. We need to inherit from the Timer class.
class DeckGUI : public juce::Component,
public juce::Button::Listener,
public juce::Slider::Listener,
public juce::FileDragAndDropTarget,
public juce::Timer

2. Timer is an abstract class, which means it has a pure virtual function in it. So you can't actually use Timer directly as a class. You have to inherit from it and implement the concrete version of its pure virtual functions. In public in DeckGUI.h we type
void timerCallback() override;

3. We need to go to DeckGUI.cpp and implement it.
void DeckGUI::timerCallback()
{

}

4. The thread doesn't start by itself, we need to start and stop it. So in the constructor DeckGUI::DeckGUI
startTimer(500);
We've now got this function built into our class because we inherited it from the Timer class. The 500 refers to the number of msec that we want to call it, so it will call every 1/2 sec here.
5. In the destructor function ("to be polite!") we will stop the timer.
DeckGUI::~DeckGUI()
{
stopTimer();
}

6. Go back to DeckGUI::timerCallback() and add
waveformDisplay.setPositionRelative(player->getPositionRelative());

If you build and compile it works. We could reduce the 500 to make it look a bit smoother.