Skip to content

Commit

Permalink
Cleanup AudioFileSource
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Jun 11, 2022
1 parent d2042a3 commit 493ac48
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
49 changes: 22 additions & 27 deletions src/DSP/AudioFileSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ namespace ta
{

AudioFileSource::AudioFileSource(juce::AudioFormatManager& _formatManager) : formatManager(_formatManager) {}

AudioFileSource::~AudioFileSource() {}

void AudioFileSource::prepareToPlay(int samplesPerBlockExpected, double sampleRate)
auto AudioFileSource::prepareToPlay(int samplesPerBlockExpected, double sampleRate) -> void
{
transportSource.prepareToPlay(samplesPerBlockExpected, sampleRate);
resampleSource.prepareToPlay(samplesPerBlockExpected, sampleRate);
}
void AudioFileSource::getNextAudioBlock(juce::AudioSourceChannelInfo const& bufferToFill)
auto AudioFileSource::getNextAudioBlock(juce::AudioSourceChannelInfo const& bufferToFill) -> void
{
resampleSource.getNextAudioBlock(bufferToFill);
}
void AudioFileSource::releaseResources()
auto AudioFileSource::releaseResources() -> void
{
transportSource.releaseResources();
resampleSource.releaseResources();
}

LengthAndSamplerate AudioFileSource::loadFile(juce::File audioFile)
auto AudioFileSource::loadFile(juce::File audioFile) -> LengthAndSamplerate
{
auto sr = 0.0;
auto length = 0;
Expand All @@ -39,40 +40,34 @@ LengthAndSamplerate AudioFileSource::loadFile(juce::File audioFile)
return LengthAndSamplerate{length, sr};
}

void AudioFileSource::setGain(double gain)
auto AudioFileSource::gain(double gain) -> void
{
if (gain < 0 || gain > 1.0) { std::cout << "AudioFileSource::setGain gain should be between 0 and 1" << std::endl; }
else { transportSource.setGain(gain); }
jassert(juce::isPositiveAndBelow(gain, 4.0));
transportSource.setGain(gain);
}
void AudioFileSource::setSpeed(double ratio)
auto AudioFileSource::speed(double ratio) -> void
{
if (ratio <= 0 || ratio > 100.0)
{
std::cout << "AudioFileSource::setSpeed ratio should be between 0 and 100" << std::endl;
}
else { resampleSource.setResamplingRatio(ratio); }
jassert(juce::isPositiveAndBelow(ratio, 4.0));
resampleSource.setResamplingRatio(ratio);
}
void AudioFileSource::setPosition(double posInSecs) { transportSource.setPosition(posInSecs); }

void AudioFileSource::setPositionRelative(double pos)
auto AudioFileSource::position(double posInSecs) -> void { transportSource.setPosition(posInSecs); }

auto AudioFileSource::positionRelative(double pos) -> void
{
if (pos < 0 || pos > 1.0)
{
std::cout << "AudioFileSource::setPositionRelative pos should be between 0 and 1" << std::endl;
}
else
{
double posInSecs = transportSource.getLengthInSeconds() * pos;
setPosition(posInSecs);
}
jassert(juce::isPositiveAndBelow(pos, 1.0));
position(transportSource.getLengthInSeconds() * pos);
}

void AudioFileSource::start() { transportSource.start(); }
void AudioFileSource::stop() { transportSource.stop(); }
auto AudioFileSource::startPlayback() -> void { transportSource.start(); }

double AudioFileSource::getPositionRelative()
auto AudioFileSource::stopPlayback() -> void { transportSource.stop(); }

auto AudioFileSource::positionRelative() const -> double
{
return transportSource.getCurrentPosition() / transportSource.getLengthInSeconds();
}

auto AudioFileSource::isPlaying() const -> bool { return transportSource.isPlaying(); }

} // namespace ta
26 changes: 16 additions & 10 deletions src/DSP/AudioFileSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ struct LengthAndSamplerate
double samplerate{0.0};
};

class AudioFileSource : public juce::AudioSource
struct AudioFileSource final : juce::AudioSource
{
public:
AudioFileSource(juce::AudioFormatManager& _formatManager);
explicit AudioFileSource(juce::AudioFormatManager& _formatManager);
~AudioFileSource() override;

auto loadFile(juce::File audioFile) -> LengthAndSamplerate;
auto setGain(double gain) -> void;
auto setSpeed(double ratio) -> void;
auto setPosition(double posInSecs) -> void;
auto setPositionRelative(double pos) -> void;

auto start() -> void;
auto stop() -> void;
auto gain(double gain) -> void;
auto speed(double ratio) -> void;

auto getPositionRelative() -> double;
auto position(double posInSecs) -> void;
auto positionRelative(double pos) -> void;
auto positionRelative() const -> double;

auto startPlayback() -> void;
auto stopPlayback() -> void;
auto isPlaying() const -> bool;

/// \brief Implements juce::AudioSource::prepareToPlay()
auto prepareToPlay(int samplesPerBlockExpected, double sampleRate) -> void override;

/// \brief Implements juce::AudioSource::getNextAudioBlock()
auto getNextAudioBlock(juce::AudioSourceChannelInfo const& bufferToFill) -> void override;

/// \brief Implements juce::AudioSource::releaseResources()
auto releaseResources() -> void override;

private:
Expand Down
14 changes: 10 additions & 4 deletions src/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ MainComponent::MainComponent() : _audioFileSource{_formatManager}
addAndMakeVisible(_display);
addAndMakeVisible(_jogWheel);

_sideBarLeft.onPlayClicked = [this]() { _audioFileSource.start(); };
_sideBarLeft.onCueClicked = [this]() { _audioFileSource.stop(); };
_sideBarLeft.onPlayClicked = [this]()
{
if (_audioFileSource.isPlaying()) { _audioFileSource.stopPlayback(); }
else { _audioFileSource.startPlayback(); }
};

_sideBarLeft.onCueClicked = [this]() { _audioFileSource.positionRelative(0.0); };
_sideBarRight.onTempoDeltaChanged = [this](double delta) { _audioFileSource.speed((100.0 + delta) / 100.0); };
setSize(640, 800);
}
MainComponent::~MainComponent()
{
_audioFileSource.stop();
_audioFileSource.stopPlayback();
_deviceManager.removeAudioCallback(&_audioPlayer);
_audioPlayer.setSource(nullptr);
}
Expand Down Expand Up @@ -58,5 +64,5 @@ auto MainComponent::setAudioDevices() -> void
_audioPlayer.setSource(&_audioFileSource);

_audioFileSource.loadFile(juce::File{"/home/tobante/Music/Loops/Bass.wav"});
_audioFileSource.setGain(1.0);
_audioFileSource.gain(1.0);
}
7 changes: 7 additions & 0 deletions src/UI/Section/SideBarRight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ SideBarRight::SideBarRight()
addAndMakeVisible(_vinylButton);
addAndMakeVisible(_warpButton);
addAndMakeVisible(_tempo);

_tempo.setRange({-6.0, 6.0}, 0.01);
_tempo.setValue(0.0, juce::dontSendNotification);
_tempo.onValueChange = [this]()
{
if (onTempoDeltaChanged) { onTempoDeltaChanged(_tempo.getValue()); }
};
}

auto SideBarRight::resized() -> void
Expand Down
2 changes: 2 additions & 0 deletions src/UI/Section/SideBarRight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct SideBarRight final : juce::Component

auto resized() -> void override;

std::function<void(double)> onTempoDeltaChanged{};

private:
Placeholder _placeholder{"", juce::Colours::transparentBlack};
juce::TextButton _vinylButton{"Vinyl"};
Expand Down

0 comments on commit 493ac48

Please sign in to comment.