2#include <ossia/detail/config.hpp>
4#if defined(OSSIA_ENABLE_RUBBERBAND)
5#include <ossia/dataflow/audio_port.hpp>
6#include <ossia/dataflow/audio_stretch_mode.hpp>
7#include <ossia/dataflow/graph_node.hpp>
8#include <ossia/dataflow/nodes/media.hpp>
9#include <ossia/dataflow/token_request.hpp>
11#if __has_include(<RubberBandStretcher.h>)
12#include <RubberBandStretcher.h>
13#elif __has_include(<rubberband/RubberBandStretcher.h>)
14#include <rubberband/RubberBandStretcher.h>
23static constexpr auto get_rubberband_preset(ossia::audio_stretch_mode mode)
25 using opt_t = RubberBand::RubberBandStretcher::Option;
26 using preset_t = RubberBand::RubberBandStretcher::PresetOption;
27 uint32_t preset = opt_t::OptionProcessRealTime | opt_t::OptionThreadingNever;
30 case ossia::audio_stretch_mode::RubberBandStandard:
33 case ossia::audio_stretch_mode::RubberBandPercussive:
34 preset |= preset_t::PercussiveOptions;
37 case ossia::audio_stretch_mode::RubberBandStandardHQ:
38 preset |= RubberBand::RubberBandStretcher::OptionEngineFiner;
39 preset |= RubberBand::RubberBandStretcher::OptionPitchHighConsistency;
42 case ossia::audio_stretch_mode::RubberBandPercussiveHQ:
43 preset |= preset_t::PercussiveOptions;
44 preset |= RubberBand::RubberBandStretcher::OptionEngineFiner;
45 preset |= RubberBand::RubberBandStretcher::OptionPitchHighConsistency;
55struct rubberband_stretcher
65 enum class prime_strategy : uint8_t
73 static inline prime_strategy s_prime_strategy{prime_strategy::BareRecipe};
76 uint32_t opt, std::size_t channels, std::size_t sampleRate, int64_t pos)
77 : m_rubberBand{std::make_unique<RubberBand::RubberBandStretcher>(
78 sampleRate, channels, opt)}
79 , next_sample_to_read{pos}
82 m_channels = channels;
91 = m_rubberBand ? m_rubberBand->getPreferredStartPad() : 0;
92 if(m_channels > 0 && pad > 0)
94 m_prime_zero_storage.assign(m_channels * pad, 0.f);
95 m_prime_zero_ptrs.resize(m_channels);
96 for(std::size_t c = 0; c < m_channels; ++c)
98 m_prime_zero_ptrs[c] = m_prime_zero_storage.data() + c * pad;
103 rubberband_stretcher(
const rubberband_stretcher&) =
delete;
104 rubberband_stretcher& operator=(
const rubberband_stretcher&) =
delete;
105 rubberband_stretcher(rubberband_stretcher&&) =
default;
106 rubberband_stretcher& operator=(rubberband_stretcher&&) =
default;
108 std::unique_ptr<RubberBand::RubberBandStretcher> m_rubberBand;
109 int64_t next_sample_to_read = 0;
111 bool m_needs_prime{
true};
113 std::size_t m_channels{};
116 std::vector<float> m_prime_zero_storage;
117 std::vector<float*> m_prime_zero_ptrs;
119 [[nodiscard]] int64_t start_delay() const noexcept
121 return m_rubberBand ? int64_t(m_rubberBand->getStartDelay()) : 0;
126 void set_rate_ratio(
double ratio)
128 const double pitch = (ratio != 0.) ? 1. / ratio : 1.;
129 if(m_rubberBand && pitch != m_rubberBand->getPitchScale())
131 m_rubberBand->setPitchScale(pitch);
136 void transport(int64_t date)
138 m_rubberBand->reset();
139 next_sample_to_read = date;
140 m_needs_prime =
true;
143 template <
typename T>
145 run(T& audio_fetcher,
const ossia::token_request& t, ossia::exec_state_facade e,
146 double tempo_ratio,
double rate_ratio,
const std::size_t chan,
147 const std::size_t len, int64_t samples_to_read,
const int64_t samples_to_write,
148 const int64_t samples_offset,
const ossia::mutable_audio_span<double>& ap)
noexcept
153 const double abs_tempo_ratio
154 = std::min(70., std::abs(tempo_ratio) * std::abs(rate_ratio));
155 if(abs_tempo_ratio != m_rubberBand->getTimeRatio())
157 m_rubberBand->setTimeRatio(abs_tempo_ratio);
162 if(m_needs_prime) [[unlikely]]
164 prime(audio_fetcher, chan, t.forward());
165 m_needs_prime =
false;
170 const int max_chan = std::max(chan, m_rubberBand->getChannelCount());
171 const int frames = std::max((int64_t)16, samples_to_read);
172 float**
const input = (
float**)alloca(
sizeof(
float*) * max_chan);
173 float**
const output = (
float**)alloca(
sizeof(
float*) * max_chan);
174 for(std::size_t i = 0; i < chan; i++)
176 input[i] = (
float*)alloca(
sizeof(
float) * frames);
177 output[i] = (
float*)alloca(
sizeof(
float) * samples_to_write);
179 for(std::size_t i = chan; i < m_rubberBand->getChannelCount(); i++)
181 input[i] = (
float*)alloca(
sizeof(
float) * frames);
182 std::fill_n(input[i], frames, 0.f);
183 output[i] = (
float*)alloca(
sizeof(
float) * samples_to_write);
188 while(m_rubberBand->available() < samples_to_write)
190 audio_fetcher.fetch_audio(next_sample_to_read, samples_to_read, input);
192 m_rubberBand->process(input, samples_to_read,
false);
194 next_sample_to_read += samples_to_read;
195 samples_to_read = 16;
198 m_rubberBand->retrieve(
199 output, std::min((
int)samples_to_write, m_rubberBand->available()));
201 for(std::size_t i = 0; i < chan; i++)
203 for(int64_t j = 0; j < samples_to_write; j++)
205 ap[i][j + samples_offset] = double(output[i][j]);
212 while(m_rubberBand->available() < samples_to_write)
214 audio_fetcher.fetch_audio_backward(next_sample_to_read, samples_to_read, input);
216 m_rubberBand->process(input, samples_to_read,
false);
218 next_sample_to_read -= samples_to_read;
219 samples_to_read = 16;
222 const int retrieved = m_rubberBand->retrieve(
223 output, std::min((
int)samples_to_write, m_rubberBand->available()));
225 for(std::size_t i = 0; i < chan; i++)
227 for(int64_t j = 0; j < samples_to_write; j++)
229 ap[i][j + samples_offset] = double(output[i][j]);
237 template <
typename T>
238 void prime(T& audio_fetcher, std::size_t chan,
bool forward)
noexcept
240 if(!m_rubberBand || chan == 0)
243 const auto strategy = s_prime_strategy;
244 if(strategy == prime_strategy::NoPrime)
247 if(strategy == prime_strategy::PreRollRealAudio)
250 const int64_t window = 2 * int64_t(m_rubberBand->getPreferredStartPad());
253 if(window > 0 && (forward ? next_sample_to_read >= window : true))
255 constexpr int prime_fetch_chunk = 256;
257 = (
float**)alloca(
sizeof(
float*) * m_rubberBand->getChannelCount());
259 = (
float**)alloca(
sizeof(
float*) * m_rubberBand->getChannelCount());
260 for(std::size_t i = 0; i < m_rubberBand->getChannelCount(); i++)
262 input[i] = (
float*)alloca(
sizeof(
float) * prime_fetch_chunk);
263 output[i] = (
float*)alloca(
sizeof(
float) * prime_fetch_chunk);
267 next_sample_to_read -= window;
269 next_sample_to_read += window;
275 = std::min<int64_t>(prime_fetch_chunk, window - fed);
278 audio_fetcher.fetch_audio(next_sample_to_read, chunk, input);
279 next_sample_to_read += chunk;
283 audio_fetcher.fetch_audio_backward(next_sample_to_read, chunk, input);
284 next_sample_to_read -= chunk;
286 m_rubberBand->process(input,
int(chunk),
false);
292 const int64_t target_drain
293 = std::max<int64_t>(0, window - int64_t(m_rubberBand->getStartDelay()));
296 while(drained < target_drain && safety++ < 4096)
298 while(m_rubberBand->available() <= 0)
300 std::fill_n(input[0], prime_fetch_chunk, 0.f);
301 for(std::size_t i = 1; i < m_rubberBand->getChannelCount(); i++)
302 std::fill_n(input[i], prime_fetch_chunk, 0.f);
303 m_rubberBand->process(input, prime_fetch_chunk,
false);
305 const int avail = m_rubberBand->available();
306 const int to_take = int(std::min<int64_t>(
307 target_drain - drained,
308 std::min<int64_t>(avail, prime_fetch_chunk)));
309 m_rubberBand->retrieve(output, to_take);
316 const int64_t toPad = int64_t(m_rubberBand->getPreferredStartPad());
321 if(int64_t(m_prime_zero_storage.size()) < int64_t(chan) * toPad
322 || int64_t(m_prime_zero_ptrs.size()) < int64_t(chan))
327 m_rubberBand->process(
328 m_prime_zero_ptrs.data(),
int(toPad),
false);
330 if(strategy == prime_strategy::ZeroPadOnly
331 || strategy == prime_strategy::PreRollRealAudio)
334 int64_t drain_target;
335 if(strategy == prime_strategy::ExtendedDrain)
337 drain_target = 2 * toPad;
341 drain_target = int64_t(m_rubberBand->getStartDelay());
350 if(!(options & RubberBand::RubberBandStretcher::OptionEngineFiner))
352 drain_target += int64_t(std::llround(
353 0.375 *
double(toPad) * (1.0 - m_rubberBand->getTimeRatio())));
358 if(drain_target <= 0)
361 constexpr int prime_fetch_chunk = 256;
363 = (
float**)alloca(
sizeof(
float*) * m_rubberBand->getChannelCount());
365 = (
float**)alloca(
sizeof(
float*) * m_rubberBand->getChannelCount());
366 for(std::size_t i = 0; i < m_rubberBand->getChannelCount(); i++)
368 input[i] = (
float*)alloca(
sizeof(
float) * prime_fetch_chunk);
369 output[i] = (
float*)alloca(
sizeof(
float) * prime_fetch_chunk);
370 std::fill_n(input[i], prime_fetch_chunk, 0.f);
375 while(drained < drain_target && safety++ < 4096)
377 while(m_rubberBand->available() <= 0)
381 audio_fetcher.fetch_audio(
382 next_sample_to_read, prime_fetch_chunk, input);
383 next_sample_to_read += prime_fetch_chunk;
387 audio_fetcher.fetch_audio_backward(
388 next_sample_to_read, prime_fetch_chunk, input);
389 next_sample_to_read -= prime_fetch_chunk;
391 m_rubberBand->process(input, prime_fetch_chunk,
false);
394 const int available = m_rubberBand->available();
395 const int to_take = int(std::min<int64_t>(
396 drain_target - drained, std::min<int64_t>(available, prime_fetch_chunk)));
397 m_rubberBand->retrieve(output, to_take);
404#include <ossia/dataflow/nodes/timestretch/raw_stretcher.hpp>
408static constexpr uint32_t get_rubberband_preset(ossia::audio_stretch_mode mode)
412using rubberband_stretcher = raw_stretcher;