This week you will be learning about refactoring into simpler modules and class composition. Download the starter code here (Link).
To add a new class in Projucer, we first open the project in Projucer, select Source
directory, then click the +
button and choose Add New CPP & Header File…
and name the file(s) DJAudioPlayer.
Note that if you don’t first select Source
, the files will be outside of the Source
folder. You will need to manually drag them into the Source
folder.
Press CMD+S
or CTRL+S
to save the project in Projucer, then restart the IDE (Visual Studio or Xcode).
Go to DJAudioPlayer.h
file, define the DJAudioPlayer
class.
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
class DJAudioPlayer : public AudioSource {
public:
DJAudioPlayer();
~DJAudioPlayer();
//==============================================================================
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override;
void releaseResources() override;
void loadURL(URL audioURL);
void setGain(double gain);
void setSpeed(double ratio);
void setPosition(double posInSec);
void start();
void stop();
};
Next, implement the DJAudioPlayer
class skeleton in the cpp file.
#include "DJAudioPlayer.h"
DJAudioPlayer::DJAudioPlayer(){}
DJAudioPlayer::~DJAudioPlayer(){}
//==============================================================================
void DJAudioPlayer::prepareToPlay (int samplesPerBlockExpected, double sampleRate) {}
void DJAudioPlayer::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) {}
void DJAudioPlayer::releaseResources() {}
void DJAudioPlayer::loadURL(URL audioURL){}
void DJAudioPlayer::setGain(double gain){}
void DJAudioPlayer::setSpeed(double ratio){}
void DJAudioPlayer::setPosition(double posInSec){}
void DJAudioPlayer::start(){}
void DJAudioPlayer::stop(){}
Include the DJAudioPlayer
in the MainComponent.h
file to test if the program can compile.
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "DJAudioPlayer.h"
...
class MainComponent : public AudioAppComponent,
public Button::Listener,
public Slider::Listener
{
...
DJAudioPlayer player1;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
Build the project and check if you can build it successfully.