The Red Penguin

Implement the listener interfaces to DeckGUI


Monday 3 August 2020

We now need to implement the listener interfaces and pass the messages on to the DJAudioPlayer.

1. We need to add a reference to give DeckGUI.h access to the DJAudioPlayer. Firstly we need to add
#include "DJAudioPlayer.h"


2. Then we need to add a pointer. We go to DeckGUI.h and in private: add
DJAudioPlayer* player;


3. Where do we get the player from? We need to pass it in through the constructor. So we go to public: in DeckGUI.h and instead of DeckGUI(); we type:
DeckGUI(DJAudioPlayer* player);


4. Back into the DeckGUI.cpp file and again we need to change DeckGUI:DeckGUI() to
DeckGUI::DeckGUI(DJAudioPlayer* _player) : player(_player)


5. Now the DeckGUI has access to the player. We now need to update MainComponent.h. So instead of
    DJAudioPlayer player1;
DeckGUI deckGUI1;
DeckGUI deckGUI2;
we need to add this code:
    DJAudioPlayer player1;
DeckGUI deckGUI1{ &player1 };

DJAudioPlayer player2;
DeckGUI deckGUI2{ &player2 };


6. We need to add the implementation of the listener interfaces. We need to update the inheritance code on the DeckGUI so it inherits from the listeners. In DeckGUI.h we need:
class DeckGUI  : public juce::Component, 
public juce::Button::Listener,
public juce::Slider::Listener

IMPORTANT! This needs to be taken out of MainComponent.h or it will create instantiation errors! The video did not make this clear!

7. We need to find the code in MainComponent::buttonClicked and MainComponent::sliderValueChanged that we previously commented out in MainComponent.cpp, and copy all of this into DeckGUI.cpp at the end of the file. We need to rename MainComponent:: as DeckGUI::

8. We don't need to change too much of this code, except we are not talking to player1 now, we are talking to player*, so we need to use pointer syntax. So the code in DeckGUI.cpp now looks like this:
void DeckGUI::buttonClicked(juce::Button* button)
{
if (button == &playButton)
{
DBG("Play button was clicked");
player->start();
}
if (button == &stopButton)
{
DBG("Stop button was clicked");
player->stop();
}
if (button == &loadButton)
{
juce::FileChooser chooser{ "select file" };
if (chooser.browseForFileToOpen())
{
player->loadURL(juce::URL{ chooser.getResult() });
}
}
}

void DeckGUI::sliderValueChanged(juce::Slider* slider)
{
if (slider == &volSlider)
{
player->setGain(slider->getValue());
}
if (slider == &speedSlider)
{
player->setSpeed(slider->getValue());
}
if (slider == &posSlider)
{
player->setPositionRelative(slider->getValue());
}
}

9. We need to now add DeckGUI as a listener. So in DeckGUI::DeckGUI in DeckGUI.cpp add:
playButton.addListener(this);
stopButton.addListener(this);
loadButton.addListener(this);

volSlider.addListener(this);
speedSlider.addListener(this);
posSlider.addListener(this);


10. The only thing now missing is the configuration of the sliders to see if they are in the right range. In DeckGUI::DeckGUI in DeckGUI.cpp add:
    volSlider.setRange(0.0, 1.0);
speedSlider.setRange(0.0, 100.0);
posSlider.setRange(0.0, 1.0);

The code now builds but player 2 is not working and we will fix this next time.