The Red Penguin

Add getPosition and setPosition functions

Wednesday 5 August 2020

We're going to add a getPosition function to the DJAudioPlayer so I can ask it what the current position is of the playback system.

And then we're going to add a setPosition function to the WaveformDisplay so that it can receive that position and use it to draw a playhead.

1. Go to DJAudioPlayer.h and add a new function in public:
double getPositionRelative();

2. We go to DJAudioPlayer.cpp and implement the function.
return transportSource.getCurrentPosition() / transportSource.getLengthInSeconds();

This tells us where are in the file (eg 10 seconds) as a decimal of the total length (eg 90 seconds) so it returns a figure between 0 and 1.

3. Go to WaveformDisplay.h and add the function in public:
void setPositionRelative(double pos);

4. We also need a variable to store the position. So in private: add
double position;

5. Then we need to implement this in WaveformDisplay.cpp. Add this at the end:
void WaveformDisplay::setPositionRelative(double pos)
if (pos != position)
position = pos;

This sets the local position variable position to the incoming position pos and repaints only if pos!=position.

6. We need to initalise the position in the constructor list to make sure it's set to something. This is like setting the original variable. So we add it at the end here:
WaveformDisplay::WaveformDisplay(juce::AudioFormatManager& formatManagerToUse,
juce::AudioThumbnailCache& cacheToUse) :
audioThumb(1000, formatManagerToUse, cacheToUse),

7. Now we can update the paint function in WaveformDisplay.cpp, as this is the object of the exercise.
g.drawRect(position * getWidth(), 0, getWidth() * 0.05, getHeight());
The 0.05 is because we're making the width of the playhead 5% of the total width of the box.

If we now build and compile we can see the playhead drawn. But it won't move, as we haven't gone into DeckGUI yet to periodically ask where the position is and to redraw.