Skip to content

Commit

Permalink
Add rubberband time stretch library
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Jun 12, 2022
1 parent 299ebe4 commit 11f012e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "3rd_party/JUCE"]
path = 3rd_party/JUCE
url = https://github.com/juce-framework/JUCE
[submodule "3rd_party/rubberband"]
path = 3rd_party/rubberband
url = https://github.com/breakfastquay/rubberband
1 change: 1 addition & 0 deletions 3rd_party/rubberband
Submodule rubberband added at 4cff17
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ include(CompilerOptions)
include(CompilerWarnings)
include(CodeCoverage)

add_library(ta_rubberband)
add_library(ta::rubberband ALIAS ta_rubberband)
target_sources(ta_rubberband PRIVATE 3rd_party/rubberband/single/RubberBandSingle.cpp)
target_include_directories(ta_rubberband PUBLIC 3rd_party/rubberband)

add_subdirectory(3rd_party/JUCE)
set_directory_properties(PROPERTIES JUCE_COMPANY_COPYRIGHT "SPDX: GPL-3.0-only")
set_directory_properties(PROPERTIES JUCE_COMPANY_NAME "tobantAudio")
Expand Down Expand Up @@ -113,6 +118,7 @@ target_link_libraries(StiggiDJ
juce::juce_audio_formats
juce::juce_audio_utils
PUBLIC
ta::rubberband
juce::juce_recommended_config_flags
juce::juce_recommended_lto_flags
juce::juce_recommended_warning_flags
Expand Down
26 changes: 24 additions & 2 deletions src/DSP/DJPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,32 @@ auto DJPlayer::prepareToPlay(int samplesPerBlockExpected, double sampleRate) ->
{
_transportSource.prepareToPlay(samplesPerBlockExpected, sampleRate);
_resampleSource.prepareToPlay(samplesPerBlockExpected, sampleRate);

_stretcher = std::make_unique<RubberBand::RubberBandStretcher>(
sampleRate, 2, RubberBand::RubberBandStretcher::OptionProcessRealTime);

_stretcherBuffer.setSize(2, samplesPerBlockExpected);
}

auto DJPlayer::getNextAudioBlock(juce::AudioSourceChannelInfo const& bufferToFill) -> void
{
_resampleSource.getNextAudioBlock(bufferToFill);
jassert(bufferToFill.buffer->getNumChannels() == 2);
jassert(bufferToFill.startSample == 0);

_stretcher->setTimeRatio(_stretchRatio.load());
_stretcherBuffer.setSize(2, bufferToFill.numSamples, false, false, true);

while (_stretcher->available() < bufferToFill.numSamples)
{
auto ctx = juce::AudioSourceChannelInfo{_stretcherBuffer};
_resampleSource.getNextAudioBlock(ctx);
_stretcher->process(ctx.buffer->getArrayOfReadPointers(), ctx.buffer->getNumSamples(), false);
}

// _stretcher->process(bufferToFill.buffer->getArrayOfReadPointers(), bufferToFill.buffer->getNumSamples(), false);
_stretcher->retrieve(bufferToFill.buffer->getArrayOfWritePointers(), bufferToFill.buffer->getNumSamples());
}

auto DJPlayer::releaseResources() -> void
{
_transportSource.releaseResources();
Expand Down Expand Up @@ -52,7 +73,8 @@ auto DJPlayer::gain(double gain) -> void
auto DJPlayer::speed(double ratio) -> void
{
jassert(juce::isPositiveAndBelow(ratio, 4.0));
_resampleSource.setResamplingRatio(ratio);
// _resampleSource.setResamplingRatio(ratio);
_stretchRatio.store(1.0 / ratio);
}

auto DJPlayer::position(double posInSecs) -> void { _transportSource.setPosition(posInSecs); }
Expand Down
6 changes: 6 additions & 0 deletions src/DSP/DJPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <juce_audio_devices/juce_audio_devices.h>
#include <juce_audio_formats/juce_audio_formats.h>

#include "rubberband/RubberBandStretcher.h"

namespace ta
{
struct LengthAndSamplerate
Expand Down Expand Up @@ -60,6 +62,10 @@ struct DJPlayer final : juce::AudioSource
juce::AudioTransportSource _transportSource;
juce::ResamplingAudioSource _resampleSource{&_transportSource, false, 2};

std::unique_ptr<RubberBand::RubberBandStretcher> _stretcher;
juce::AudioBuffer<float> _stretcherBuffer;
std::atomic<double> _stretchRatio{1.0};

juce::ListenerList<Listener> _listeners;
};
} // namespace ta

0 comments on commit 11f012e

Please sign in to comment.