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)This sets the local position variable position to the incoming position pos and repaints only if pos!=position.
{
if (pos != position)
{
position = pos;
repaint();
}
}
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),
fileLoaded(false),
position(0)
7. Now we can update the paint function in WaveformDisplay.cpp, as this is the object of the exercise.
g.setColour(juce::Colours::lightgreen);The 0.05 is because we're making the width of the playhead 5% of the total width of the box.
g.drawRect(position * getWidth(), 0, getWidth() * 0.05, getHeight());
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.
More posts in cpp
- Add a component ID and converting between ints and strings
- Implement a play button and add a listener
- Implement paintRowBackground and paintCell
- Add a vector to store a list of files
- Add a TableListBox
- Create a PlaylistComponent in the Projucer project
- Threads
- Implement a timer
- Add getPosition and setPosition functions
- Refactor DJAudioPlayer to use app-scope formatManager
- Draw the thumbnail
- Hook up the load button to trigger the AudioThumbnail load
- The AudioThumbnail class in the API
- Creating a new component - WaveformDisplay
- Implementing drag and drop triggers
- Use a MixerAudioSource to play more than one file at a time
- Implement the listener interfaces to DeckGUI
- Creating a DeckGUI class
- setPosition control
- Implementing setGain and setSpeed
- Add audio playback functionality
- Writing the DJAudioPlayer class
- Creating a new JUCE class with Projucer
- Refactoring our code
- Using ResamplingAudioPlayer to implement variable speed playback
- Add stop, start and volume functionality
- Add a file chooser
- Audio file playback in JUCE
- Realtime sound synthesis in JUCE
- Adding a slider listener
- Introduction to event listeners
- Macros
- Inheritance
- Adding a GUI widget to the JUCE app
- Introduction to JUCE