diff --git a/ladspa-lv2/RubberBandLivePitchShifter.cpp b/ladspa-lv2/RubberBandLivePitchShifter.cpp new file mode 100644 index 00000000..273700a3 --- /dev/null +++ b/ladspa-lv2/RubberBandLivePitchShifter.cpp @@ -0,0 +1,582 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Rubber Band Library + An audio time-stretching and pitch-shifting library. + Copyright 2007-2023 Particular Programs Ltd. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. + + Alternatively, if you have a valid commercial licence for the + Rubber Band Library obtained by agreement with the copyright + holders, you may redistribute and/or modify it under the terms + described in that licence. + + If you wish to distribute code using the Rubber Band Library + under terms other than those of the GNU General Public License, + you must obtain a valid commercial licence before doing so. +*/ + +#include "RubberBandLivePitchShifter.h" + +#include "RubberBandLiveShifter.h" + +#include +#include + +using namespace RubberBand; + +using std::cout; +using std::cerr; +using std::endl; +using std::min; + +#ifdef RB_PLUGIN_LADSPA + +const char *const +RubberBandLivePitchShifter::portNamesMono[PortCountMono] = +{ + "latency", + "Cents", + "Semitones", + "Octaves", + "Formant Preserving", + "Wet-Dry Mix", + "Input", + "Output" +}; + +const char *const +RubberBandLivePitchShifter::portNamesStereo[PortCountStereo] = +{ + "latency", + "Cents", + "Semitones", + "Octaves", + "Formant Preserving", + "Wet-Dry Mix", + "Input L", + "Output L", + "Input R", + "Output R" +}; + +const LADSPA_PortDescriptor +RubberBandLivePitchShifter::portsMono[PortCountMono] = +{ + LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO, + LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO +}; + +const LADSPA_PortDescriptor +RubberBandLivePitchShifter::portsStereo[PortCountStereo] = +{ + LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL, + LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO, + LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO, + LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO, + LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO +}; + +const LADSPA_PortRangeHint +RubberBandLivePitchShifter::hintsMono[PortCountMono] = +{ + { 0, 0, 0 }, // latency + { LADSPA_HINT_DEFAULT_0 | // cents + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE, + -100.0, 100.0 }, + { LADSPA_HINT_DEFAULT_0 | // semitones + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE | + LADSPA_HINT_INTEGER, + -12.0, 12.0 }, + { LADSPA_HINT_DEFAULT_0 | // octaves + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE | + LADSPA_HINT_INTEGER, + -2.0, 2.0 }, + { LADSPA_HINT_DEFAULT_0 | // formant preserving + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE | + LADSPA_HINT_TOGGLED, + 0.0, 1.0 }, + { LADSPA_HINT_DEFAULT_0 | // wet-dry mix + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE, + 0.0, 1.0 }, + { 0, 0, 0 }, + { 0, 0, 0 } +}; + +const LADSPA_PortRangeHint +RubberBandLivePitchShifter::hintsStereo[PortCountStereo] = +{ + { 0, 0, 0 }, // latency + { LADSPA_HINT_DEFAULT_0 | // cents + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE, + -100.0, 100.0 }, + { LADSPA_HINT_DEFAULT_0 | // semitones + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE | + LADSPA_HINT_INTEGER, + -12.0, 12.0 }, + { LADSPA_HINT_DEFAULT_0 | // octaves + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE | + LADSPA_HINT_INTEGER, + -2.0, 2.0 }, + { LADSPA_HINT_DEFAULT_0 | // formant preserving + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE | + LADSPA_HINT_TOGGLED, + 0.0, 1.0 }, + { LADSPA_HINT_DEFAULT_0 | // wet-dry mix + LADSPA_HINT_BOUNDED_BELOW | + LADSPA_HINT_BOUNDED_ABOVE, + 0.0, 1.0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 } +}; + +const LADSPA_Properties +RubberBandLivePitchShifter::properties = LADSPA_PROPERTY_HARD_RT_CAPABLE; + +const LADSPA_Descriptor +RubberBandLivePitchShifter::ladspaDescriptorMono = +{ + 29791, // "Unique" ID + "rubberband-live-pitchshifter-mono", // Label + properties, + "Rubber Band Live Mono Pitch Shifter", // Name + "Breakfast Quay", + "GPL", + PortCountMono, + portsMono, + portNamesMono, + hintsMono, + nullptr, // Implementation data + instantiate, + connectPort, + activate, + run, + nullptr, // Run adding + nullptr, // Set run adding gain + deactivate, + cleanup +}; + +const LADSPA_Descriptor +RubberBandLivePitchShifter::ladspaDescriptorStereo = +{ + 97921, // "Unique" ID + "rubberband-live-pitchshifter-stereo", // Label + properties, + "Rubber Band Live Stereo Pitch Shifter", // Name + "Breakfast Quay", + "GPL", + PortCountStereo, + portsStereo, + portNamesStereo, + hintsStereo, + nullptr, // Implementation data + instantiate, + connectPort, + activate, + run, + nullptr, // Run adding + nullptr, // Set run adding gain + deactivate, + cleanup +}; + +const LADSPA_Descriptor * +RubberBandLivePitchShifter::getDescriptor(unsigned long index) +{ + if (index == 0) return &ladspaDescriptorMono; + if (index == 1) return &ladspaDescriptorStereo; + else return 0; +} + +#else + +const LV2_Descriptor +RubberBandLivePitchShifter::lv2DescriptorMono = +{ + "http://breakfastquay.com/rdf/lv2-rubberband-live#mono", + instantiate, + connectPort, + activate, + run, + deactivate, + cleanup, + nullptr +}; + +const LV2_Descriptor +RubberBandLivePitchShifter::lv2DescriptorStereo = +{ + "http://breakfastquay.com/rdf/lv2-rubberband-live#stereo", + instantiate, + connectPort, + activate, + run, + deactivate, + cleanup, + nullptr +}; + +const LV2_Descriptor * +RubberBandLivePitchShifter::getDescriptor(uint32_t index) +{ + if (index == 0) return &lv2DescriptorMono; + if (index == 1) return &lv2DescriptorStereo; + else return 0; +} + +#endif + +RubberBandLivePitchShifter::RubberBandLivePitchShifter(int sampleRate, size_t channels) : + m_latency(nullptr), + m_cents(nullptr), + m_semitones(nullptr), + m_octaves(nullptr), + m_formant(nullptr), + m_wetDry(nullptr), + m_ratio(1.0), + m_prevRatio(1.0), + m_currentFormant(false), + m_shifter(new RubberBandLiveShifter + (sampleRate, channels, + RubberBandLiveShifter::OptionWindowLong | + RubberBandLiveShifter::OptionPitchModeB | + RubberBandLiveShifter::OptionChannelsTogether)), + m_sampleRate(sampleRate), + m_channels(channels), + m_blockSize(0), + m_bufferSize(65536), + m_delay(0) +{ + m_input = new float *[m_channels]; + m_output = new float *[m_channels]; + + m_irb = new RingBuffer *[m_channels]; + m_orb = new RingBuffer *[m_channels]; + + m_ib = new float *[m_channels]; + m_ob = new float *[m_channels]; + + m_delayMixBuffer = new RingBuffer *[m_channels]; + + m_blockSize = m_shifter->getBlockSize(); + m_delay = m_shifter->getStartDelay(); + + for (int c = 0; c < m_channels; ++c) { + + m_irb[c] = new RingBuffer(m_bufferSize); + m_orb[c] = new RingBuffer(m_bufferSize); + m_irb[c]->zero(m_blockSize); + + m_ib[c] = new float[m_blockSize]; + m_ob[c] = new float[m_blockSize]; + + m_delayMixBuffer[c] = new RingBuffer(m_bufferSize + m_delay); + m_irb[c]->zero(m_delay); + } + + activateImpl(); +} + +RubberBandLivePitchShifter::~RubberBandLivePitchShifter() +{ + delete m_shifter; + for (int c = 0; c < m_channels; ++c) { + delete m_irb[c]; + delete m_orb[c]; + delete[] m_ib[c]; + delete[] m_ob[c]; + delete m_delayMixBuffer[c]; + } + delete[] m_irb; + delete[] m_orb; + delete[] m_ib; + delete[] m_ob; + delete[] m_delayMixBuffer; + delete[] m_output; + delete[] m_input; +} + +#ifdef RB_PLUGIN_LADSPA + +LADSPA_Handle +RubberBandLivePitchShifter::instantiate(const LADSPA_Descriptor *desc, unsigned long rate) +{ + if (desc->PortCount == ladspaDescriptorMono.PortCount) { + return new RubberBandLivePitchShifter(rate, 1); + } else if (desc->PortCount == ladspaDescriptorStereo.PortCount) { + return new RubberBandLivePitchShifter(rate, 2); + } + return nullptr; +} + +#else + +LV2_Handle +RubberBandLivePitchShifter::instantiate(const LV2_Descriptor *desc, double rate, + const char *, const LV2_Feature *const *) +{ + if (rate < 1.0) { + std::cerr << "RubberBandLivePitchShifter::instantiate: invalid sample rate " + << rate << " provided" << std::endl; + return nullptr; + } + size_t srate = size_t(round(rate)); + if (std::string(desc->URI) == lv2DescriptorMono.URI) { + return new RubberBandLivePitchShifter(srate, 1); + } else if (std::string(desc->URI) == lv2DescriptorStereo.URI) { + return new RubberBandLivePitchShifter(srate, 2); + } else { + std::cerr << "RubberBandLivePitchShifter::instantiate: unrecognised URI " + << desc->URI << " requested" << std::endl; + return nullptr; + } +} + +#endif + +#ifdef RB_PLUGIN_LADSPA +void +RubberBandLivePitchShifter::connectPort(LADSPA_Handle handle, + unsigned long port, LADSPA_Data *location) +#else +void +RubberBandLivePitchShifter::connectPort(LV2_Handle handle, + uint32_t port, void *location) +#endif +{ + RubberBandLivePitchShifter *shifter = (RubberBandLivePitchShifter *)handle; + + float **ports[PortCountStereo] = { + &shifter->m_latency, + &shifter->m_cents, + &shifter->m_semitones, + &shifter->m_octaves, + &shifter->m_formant, + &shifter->m_wetDry, + &shifter->m_input[0], + &shifter->m_output[0], + &shifter->m_input[1], + &shifter->m_output[1] + }; + + if (shifter->m_channels == 1) { + if (port >= PortCountMono) return; + } else { + if (port >= PortCountStereo) return; + } + + *ports[port] = (float *)location; + + if (shifter->m_latency) { + *(shifter->m_latency) = shifter->getLatency(); + } +} + +#ifdef RB_PLUGIN_LADSPA +void +RubberBandLivePitchShifter::activate(LADSPA_Handle handle) +#else +void +RubberBandLivePitchShifter::activate(LV2_Handle handle) +#endif +{ + RubberBandLivePitchShifter *shifter = (RubberBandLivePitchShifter *)handle; + shifter->activateImpl(); +} + +#ifdef RB_PLUGIN_LADSPA +void +RubberBandLivePitchShifter::run(LADSPA_Handle handle, unsigned long samples) +#else +void +RubberBandLivePitchShifter::run(LV2_Handle handle, uint32_t samples) +#endif +{ + RubberBandLivePitchShifter *shifter = (RubberBandLivePitchShifter *)handle; + shifter->runImpl(samples); +} + +#ifdef RB_PLUGIN_LADSPA +void +RubberBandLivePitchShifter::deactivate(LADSPA_Handle handle) +#else +void +RubberBandLivePitchShifter::deactivate(LV2_Handle handle) +#endif +{ + activate(handle); // both functions just reset the plugin +} + +#ifdef RB_PLUGIN_LADSPA +void +RubberBandLivePitchShifter::cleanup(LADSPA_Handle handle) +#else +void +RubberBandLivePitchShifter::cleanup(LV2_Handle handle) +#endif +{ + delete (RubberBandLivePitchShifter *)handle; +} + +void +RubberBandLivePitchShifter::activateImpl() +{ + updateRatio(); + m_prevRatio = m_ratio; + m_shifter->reset(); + m_shifter->setPitchScale(m_ratio); + + for (int c = 0; c < m_channels; ++c) { + m_irb[c]->reset(); + m_irb[c]->zero(m_blockSize); + m_orb[c]->reset(); + m_delayMixBuffer[c]->reset(); + m_delayMixBuffer[c]->zero(m_delay); + } +} + +void +RubberBandLivePitchShifter::updateRatio() +{ + // The octaves, semitones, and cents parameters are supposed to be + // integral: we want to enforce that, just to avoid + // inconsistencies between hosts if some respect the hints more + // than others + +#ifdef RB_PLUGIN_LADSPA + + // But we don't want to change the long-standing behaviour of the + // LADSPA plugin, so let's leave this as-is and only do "the right + // thing" for LV2 + double oct = (m_octaves ? *m_octaves : 0.0); + oct += (m_semitones ? *m_semitones : 0.0) / 12; + oct += (m_cents ? *m_cents : 0.0) / 1200; + m_ratio = pow(2.0, oct); + +#else + + // LV2 + + double octaves = round(m_octaves ? *m_octaves : 0.0); + if (octaves < -2.0) octaves = -2.0; + if (octaves > 2.0) octaves = 2.0; + + double semitones = round(m_semitones ? *m_semitones : 0.0); + if (semitones < -12.0) semitones = -12.0; + if (semitones > 12.0) semitones = 12.0; + + double cents = round(m_cents ? *m_cents : 0.0); + if (cents < -100.0) cents = -100.0; + if (cents > 100.0) cents = 100.0; + + m_ratio = pow(2.0, + octaves + + semitones / 12.0 + + cents / 1200.0); +#endif +} + +void +RubberBandLivePitchShifter::updateFormant() +{ + if (!m_formant) return; + + bool f = (*m_formant > 0.5f); + if (f == m_currentFormant) return; + + RubberBandLiveShifter *s = m_shifter; + + s->setFormantOption(f ? + RubberBandLiveShifter::OptionFormantPreserved : + RubberBandLiveShifter::OptionFormantShifted); + + m_currentFormant = f; +} + +int +RubberBandLivePitchShifter::getLatency() const +{ + return m_shifter->getStartDelay() + m_blockSize; +} + +void +RubberBandLivePitchShifter::runImpl(uint32_t insamples) +{ + updateRatio(); + if (m_ratio != m_prevRatio) { + m_shifter->setPitchScale(m_ratio); + m_prevRatio = m_ratio; + } + + updateFormant(); + + if (m_latency) { + *m_latency = getLatency(); + } + + for (int c = 0; c < m_channels; ++c) { + m_irb[c]->write(m_input[c], insamples); + m_delayMixBuffer[c]->write(m_input[c], insamples); + } + + while (m_irb[0]->getReadSpace() >= m_blockSize) { + + for (int c = 0; c < m_channels; ++c) { + m_irb[c]->read(m_ib[c], m_blockSize); + } + + m_shifter->shift(m_ib, m_ob); + + for (int c = 0; c < m_channels; ++c) { + m_orb[c]->write(m_ob[c], m_blockSize); + } + } + + for (int c = 0; c < m_channels; ++c) { + m_orb[c]->read(m_output[c], insamples); + } + + float mix = 0.0; + if (m_wetDry) mix = *m_wetDry; + + for (int c = 0; c < m_channels; ++c) { + if (mix > 0.0) { + for (uint32_t i = 0; i < insamples; ++i) { + float dry = m_delayMixBuffer[c]->readOne(); + m_output[c][i] *= (1.0 - mix); + m_output[c][i] += dry * mix; + } + } else { + m_delayMixBuffer[c]->skip(insamples); + } + } +} + diff --git a/ladspa-lv2/RubberBandLivePitchShifter.h b/ladspa-lv2/RubberBandLivePitchShifter.h new file mode 100644 index 00000000..5c9939b6 --- /dev/null +++ b/ladspa-lv2/RubberBandLivePitchShifter.h @@ -0,0 +1,148 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Rubber Band Library + An audio time-stretching and pitch-shifting library. + Copyright 2007-2023 Particular Programs Ltd. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. + + Alternatively, if you have a valid commercial licence for the + Rubber Band Library obtained by agreement with the copyright + holders, you may redistribute and/or modify it under the terms + described in that licence. + + If you wish to distribute code using the Rubber Band Library + under terms other than those of the GNU General Public License, + you must obtain a valid commercial licence before doing so. +*/ + +#ifndef RUBBER_BAND_LIVE_SHIFTER_H +#define RUBBER_BAND_LIVE_SHIFTER_H + +#ifdef RB_PLUGIN_LADSPA +#ifdef RB_PLUGIN_LV2 +#error "Only one of RB_PLUGIN_LADSPA and RB_PLUGIN_LV2 may be defined at once" +#endif +#else +#ifndef RB_PLUGIN_LV2 +#error "Including code must define either RB_PLUGIN_LADSPA or RB_PLUGIN_LV2" +#endif +#endif + +#ifdef RB_PLUGIN_LADSPA +#include +#else +#include +#endif + +#include "common/RingBuffer.h" + +namespace RubberBand { +class RubberBandLiveShifter; +} + +class RubberBandLivePitchShifter +{ +public: +#ifdef RB_PLUGIN_LADSPA + static const LADSPA_Descriptor *getDescriptor(unsigned long index); +#else + static const LV2_Descriptor *getDescriptor(uint32_t index); +#endif + +protected: + RubberBandLivePitchShifter(int sampleRate, size_t channels); + ~RubberBandLivePitchShifter(); + + enum { + LatencyPort = 0, + CentsPort = 1, + SemitonesPort = 2, + OctavesPort = 3, + FormantPort = 4, + WetDryPort = 5, + InputPort1 = 6, + OutputPort1 = 7, + PortCountMono = OutputPort1 + 1, + InputPort2 = 8, + OutputPort2 = 9, + PortCountStereo = OutputPort2 + 1 + }; + +#ifdef RB_PLUGIN_LADSPA + static const char *const portNamesMono[PortCountMono]; + static const LADSPA_PortDescriptor portsMono[PortCountMono]; + static const LADSPA_PortRangeHint hintsMono[PortCountMono]; + + static const char *const portNamesStereo[PortCountStereo]; + static const LADSPA_PortDescriptor portsStereo[PortCountStereo]; + static const LADSPA_PortRangeHint hintsStereo[PortCountStereo]; + + static const LADSPA_Properties properties; + + static const LADSPA_Descriptor ladspaDescriptorMono; + static const LADSPA_Descriptor ladspaDescriptorStereo; + + static LADSPA_Handle instantiate(const LADSPA_Descriptor *, unsigned long); + static void connectPort(LADSPA_Handle, unsigned long, LADSPA_Data *); + static void activate(LADSPA_Handle); + static void run(LADSPA_Handle, unsigned long); + static void deactivate(LADSPA_Handle); + static void cleanup(LADSPA_Handle); + +#else + + static const LV2_Descriptor lv2DescriptorMono; + static const LV2_Descriptor lv2DescriptorStereo; + + static LV2_Handle instantiate(const LV2_Descriptor *, double, + const char *, const LV2_Feature *const *); + static void connectPort(LV2_Handle, uint32_t, void *); + static void activate(LV2_Handle); + static void run(LV2_Handle, uint32_t); + static void deactivate(LV2_Handle); + static void cleanup(LV2_Handle); + +#endif + + void activateImpl(); + void runImpl(uint32_t count); + void process(); + void updateRatio(); + void updateFormant(); + + float **m_input; + float **m_output; + float *m_latency; + float *m_cents; + float *m_semitones; + float *m_octaves; + float *m_formant; + float *m_wetDry; + double m_ratio; + double m_prevRatio; + bool m_currentFormant; + + RubberBand::RubberBandLiveShifter *m_shifter; + RubberBand::RingBuffer **m_irb; + RubberBand::RingBuffer **m_orb; + float **m_ib; + float **m_ob; + RubberBand::RingBuffer **m_delayMixBuffer; + + int m_sampleRate; + int m_channels; + int m_blockSize; + int m_bufferSize; + int m_delay; + + int getLatency() const; +}; + + +#endif diff --git a/ladspa-lv2/ladspa-rubberband.cat b/ladspa-lv2/ladspa-rubberband.cat index 907081f1..ee794154 100644 --- a/ladspa-lv2/ladspa-rubberband.cat +++ b/ladspa-lv2/ladspa-rubberband.cat @@ -2,3 +2,5 @@ ladspa:ladspa-rubberband:rubberband-pitchshifter-mono::Frequency > Pitch shifter ladspa:ladspa-rubberband:rubberband-pitchshifter-stereo::Frequency > Pitch shifters ladspa:ladspa-rubberband:rubberband-r3-pitchshifter-mono::Frequency > Pitch shifters ladspa:ladspa-rubberband:rubberband-r3-pitchshifter-stereo::Frequency > Pitch shifters +ladspa:ladspa-rubberband:rubberband-live-pitchshifter-mono::Frequency > Pitch shifters +ladspa:ladspa-rubberband:rubberband-live-pitchshifter-stereo::Frequency > Pitch shifters diff --git a/ladspa-lv2/ladspa-rubberband.rdf b/ladspa-lv2/ladspa-rubberband.rdf index 23a3cefb..05c46c83 100644 --- a/ladspa-lv2/ladspa-rubberband.rdf +++ b/ladspa-lv2/ladspa-rubberband.rdf @@ -9,6 +9,10 @@ + + + + diff --git a/ladspa-lv2/libmain-ladspa.cpp b/ladspa-lv2/libmain-ladspa.cpp index ab9d42a1..a8fa946a 100644 --- a/ladspa-lv2/libmain-ladspa.cpp +++ b/ladspa-lv2/libmain-ladspa.cpp @@ -25,6 +25,7 @@ #undef RB_PLUGIN_LV2 #include "RubberBandPitchShifter.cpp" #include "RubberBandR3PitchShifter.cpp" +#include "RubberBandLivePitchShifter.cpp" #include @@ -34,8 +35,10 @@ const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) { if (index < 2) { return RubberBandPitchShifter::getDescriptor(index); - } else { + } else if (index < 4) { return RubberBandR3PitchShifter::getDescriptor(index - 2); + } else { + return RubberBandLivePitchShifter::getDescriptor(index - 4); } } diff --git a/ladspa-lv2/libmain-lv2.cpp b/ladspa-lv2/libmain-lv2.cpp index b27f124a..5acafec1 100644 --- a/ladspa-lv2/libmain-lv2.cpp +++ b/ladspa-lv2/libmain-lv2.cpp @@ -25,6 +25,7 @@ #undef RB_PLUGIN_LADSPA #include "RubberBandPitchShifter.cpp" #include "RubberBandR3PitchShifter.cpp" +#include "RubberBandLivePitchShifter.cpp" #include @@ -35,8 +36,10 @@ const LV2_Descriptor *lv2_descriptor(uint32_t index) { if (index < 2) { return RubberBandPitchShifter::getDescriptor(index); - } else { + } else if (index < 4) { return RubberBandR3PitchShifter::getDescriptor(index - 2); + } else { + return RubberBandLivePitchShifter::getDescriptor(index - 4); } } diff --git a/ladspa-lv2/rubberband.lv2/lv2-rubberband.ttl b/ladspa-lv2/rubberband.lv2/lv2-rubberband.ttl index 6bc2b536..0621e10f 100644 --- a/ladspa-lv2/rubberband.lv2/lv2-rubberband.ttl +++ b/ladspa-lv2/rubberband.lv2/lv2-rubberband.ttl @@ -201,6 +201,43 @@ rubberband:r3mono lv2:designation pg:center ; ] . +rubberband:livemono + a doap:Project, lv2:Plugin, lv2:PitchPlugin ; + doap:name "Rubber Band Live Mono Pitch Shifter" ; + doap:license ; + foaf:maker :maker ; + doap:developer :maker ; + doap:maintainer :maker ; + # Minor version will be 2x the Rubber Band API minor version + lv2:minorVersion 4 ; + lv2:microVersion 1 ; + lv2:optionalFeature lv2:hardRTCapable ; + pg:mainInput rubberband:mono_in_group ; + pg:mainOutput rubberband:mono_out_group ; + dc:replaces ; + lv2:port :latencyPort , + :centsPort , + :semitonesPort , + :octavesPort , + :formantPortR3 , + :wetDryPortR3 , + [ a lv2:AudioPort, lv2:InputPort ; + lv2:index 6 ; + lv2:symbol "input" ; + lv2:name "Input" ; + lv2:shortName "Input" ; + pg:group rubberband:mono_in_group ; + lv2:designation pg:center ; + ], [ + a lv2:AudioPort, lv2:OutputPort ; + lv2:index 7 ; + lv2:symbol "output" ; + lv2:name "Output" ; + lv2:shortName "Output" ; + pg:group rubberband:mono_out_group ; + lv2:designation pg:center ; + ] . + rubberband:stereo a doap:Project, lv2:Plugin, lv2:PitchPlugin ; doap:name "Rubber Band Stereo Pitch Shifter" ; @@ -306,3 +343,55 @@ rubberband:r3stereo lv2:designation pg:right ; ] . +rubberband:livestereo + a doap:Project, lv2:Plugin, lv2:PitchPlugin ; + doap:name "Rubber Band Live Stereo Pitch Shifter" ; + doap:license ; + foaf:maker :maker ; + doap:developer :maker ; + doap:maintainer :maker ; + # Minor version will be 2x the Rubber Band API minor version + lv2:minorVersion 4 ; + lv2:microVersion 1 ; + lv2:optionalFeature lv2:hardRTCapable ; + pg:mainInput rubberband:stereo_in_group ; + pg:mainOutput rubberband:stereo_out_group ; + dc:replaces ; + lv2:port :latencyPort , + :centsPort , + :semitonesPort , + :octavesPort , + :formantPortR3 , + :wetDryPortR3 , + [ a lv2:AudioPort, lv2:InputPort ; + lv2:index 6 ; + lv2:symbol "input_l" ; + lv2:name "Input L" ; + lv2:shortName "Input L" ; + pg:group rubberband:stereo_in_group ; + lv2:designation pg:left ; + ], [ + a lv2:AudioPort, lv2:OutputPort ; + lv2:index 7 ; + lv2:symbol "output_l" ; + lv2:name "Output L" ; + lv2:shortName "Output L" ; + pg:group rubberband:stereo_out_group ; + lv2:designation pg:left ; + ], [ a lv2:AudioPort, lv2:InputPort ; + lv2:index 8 ; + lv2:symbol "input_r" ; + lv2:name "Input R" ; + lv2:shortName "Input R" ; + pg:group rubberband:stereo_in_group ; + lv2:designation pg:right ; + ], [ + a lv2:AudioPort, lv2:OutputPort ; + lv2:index 9 ; + lv2:symbol "output_r" ; + lv2:name "Output R" ; + lv2:shortName "Output R" ; + pg:group rubberband:stereo_out_group ; + lv2:designation pg:right ; + ] . + diff --git a/ladspa-lv2/rubberband.lv2/manifest.ttl b/ladspa-lv2/rubberband.lv2/manifest.ttl index ccba9dd1..d71bcc79 100644 --- a/ladspa-lv2/rubberband.lv2/manifest.ttl +++ b/ladspa-lv2/rubberband.lv2/manifest.ttl @@ -12,6 +12,11 @@ rubberband:r3mono lv2:binary ; rdfs:seeAlso . +rubberband:livemono + a lv2:Plugin ; + lv2:binary ; + rdfs:seeAlso . + rubberband:stereo a lv2:Plugin ; lv2:binary ; @@ -22,3 +27,8 @@ rubberband:r3stereo lv2:binary ; rdfs:seeAlso . +rubberband:livestereo + a lv2:Plugin ; + lv2:binary ; + rdfs:seeAlso . + diff --git a/meson.build b/meson.build index 535b3db8..c21f0d3c 100644 --- a/meson.build +++ b/meson.build @@ -29,11 +29,13 @@ pkg = import('pkgconfig') public_headers = [ 'rubberband/rubberband-c.h', 'rubberband/RubberBandStretcher.h', + 'rubberband/RubberBandLiveShifter.h', ] library_sources = [ 'src/rubberband-c.cpp', 'src/RubberBandStretcher.cpp', + 'src/RubberBandLiveShifter.cpp', 'src/faster/AudioCurveCalculator.cpp', 'src/faster/CompoundAudioCurve.cpp', 'src/faster/HighFrequencyAudioCurve.cpp', @@ -52,6 +54,7 @@ library_sources = [ 'src/common/mathmisc.cpp', 'src/common/Thread.cpp', 'src/finer/R3Stretcher.cpp', + 'src/finer/R3LiveShifter.cpp', ] jni_sources = [ @@ -89,6 +92,7 @@ lv2_sources = [ unit_test_sources = [ 'src/test/TestAllocators.cpp', 'src/test/TestFFT.cpp', + 'src/test/TestLiveShifter.cpp', 'src/test/TestResampler.cpp', 'src/test/TestVectorOpsComplex.cpp', 'src/test/TestVectorOps.cpp', diff --git a/rubberband/RubberBandLiveShifter.h b/rubberband/RubberBandLiveShifter.h new file mode 100644 index 00000000..55c9d7fd --- /dev/null +++ b/rubberband/RubberBandLiveShifter.h @@ -0,0 +1,310 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Rubber Band Library + An audio time-stretching and pitch-shifting library. + Copyright 2007-2023 Particular Programs Ltd. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. + + Alternatively, if you have a valid commercial licence for the + Rubber Band Live Pitch Shifter obtained by agreement with the + copyright holders, you may redistribute and/or modify it under the + terms described in that licence. + + If you wish to distribute code using Rubber Band Live under terms + other than those of the GNU General Public License, you must + obtain a valid commercial licence before doing so. +*/ + +#ifndef RUBBERBAND_LIVE_SHIFTER_H +#define RUBBERBAND_LIVE_SHIFTER_H + +#define RUBBERBAND_LIVE_VERSION "0.0.1" +#define RUBBERBAND_LIVE_API_MAJOR_VERSION 0 +#define RUBBERBAND_LIVE_API_MINOR_VERSION 0 + +#undef RUBBERBAND_LIVE_DLLEXPORT +#ifdef _MSC_VER +#define RUBBERBAND_LIVE_DLLEXPORT __declspec(dllexport) +#else +#define RUBBERBAND_LIVE_DLLEXPORT +#endif + +#include +#include +#include +#include +#include + +namespace RubberBand +{ + +/** + * @mainpage RubberBandLiveShifter + */ +class RUBBERBAND_LIVE_DLLEXPORT +RubberBandLiveShifter +{ +public: + enum Option { + OptionWindowShort = 0x00000000, + OptionWindowMedium = 0x00100000, + OptionWindowLong = 0x00200000, + + OptionFormantShifted = 0x00000000, + OptionFormantPreserved = 0x01000000, + + OptionPitchModeA = 0x00000000, + OptionPitchModeB = 0x02000000, + + OptionChannelsApart = 0x00000000, + OptionChannelsTogether = 0x10000000, + + // n.b. Options is int, so we must stop before 0x80000000 + }; + + /** + * A bitwise OR of values from the RubberBandLiveShifter::Option + * enum. + */ + typedef int Options; + + enum PresetOption { + DefaultOptions = 0x00000000 + }; + + /** + * Interface for log callbacks that may optionally be provided to + * the shifter on construction. + * + * If a Logger is provided, the shifter will call one of these + * functions instead of sending output to \c cerr when there is + * something to report. This allows debug output to be diverted to + * an application's logging facilities, and/or handled in an + * RT-safe way. See setDebugLevel() for details about how and when + * RubberBandLiveShifter reports something in this way. + * + * The message text passed to each of these log functions is a + * C-style string with no particular guaranteed lifespan. If you + * need to retain it, copy it before returning. Do not free it. + * + * @see setDebugLevel + * @see setDefaultDebugLevel + */ + struct Logger { + /// Receive a log message with no numeric values. + virtual void log(const char *) = 0; + + /// Receive a log message and one accompanying numeric value. + virtual void log(const char *, double) = 0; + + /// Receive a log message and two accompanying numeric values. + virtual void log(const char *, double, double) = 0; + + virtual ~Logger() { } + }; + + /** + * Construct a pitch shifter object to run at the given sample + * rate, with the given number of channels. + */ + RubberBandLiveShifter(size_t sampleRate, size_t channels, + Options options); + + /** + * Construct a pitch shifter object with a custom debug + * logger. This may be useful for debugging if the default logger + * output (which simply goes to \c cerr) is not visible in the + * runtime environment, or if the application has a standard or + * more realtime-appropriate logging mechanism. + * + * See the documentation for the other constructor above for + * details of the arguments other than the logger. + * + * Note that although the supplied logger gets to decide what to + * do with log messages, the separately-set debug level (see + * setDebugLevel() and setDefaultDebugLevel()) still determines + * whether any given debug message is sent to the logger in the + * first place. + */ + RubberBandLiveShifter(size_t sampleRate, size_t channels, + std::shared_ptr logger, + Options options); + + ~RubberBandLiveShifter(); + + /** + * Reset the shifter's internal buffers. The shifter should + * subsequently behave as if it had just been constructed + * (although retaining the current pitch ratio). + */ + void reset(); + + /** + * Set the pitch scaling ratio for the shifter. This is the ratio + * of target frequency to source frequency. For example, a ratio + * of 2.0 would shift up by one octave; 0.5 down by one octave; or + * 1.0 leave the pitch unaffected. + * + * To put this in musical terms, a pitch scaling ratio + * corresponding to a shift of S equal-tempered semitones (where S + * is positive for an upwards shift and negative for downwards) is + * pow(2.0, S / 12.0). + * + * This function may be called at any time, so long as it is not + * called concurrently with process(). You should either call + * this function from the same thread as process(), or provide + * your own mutex or similar mechanism to ensure that + * setPitchScale and process() cannot be run at once (there is no + * internal mutex for this purpose). + */ + void setPitchScale(double scale); + + /** + * Set a pitch scale for the vocal formant envelope separately + * from the overall pitch scale. This is a ratio of target + * frequency to source frequency. For example, a ratio of 2.0 + * would shift the formant envelope up by one octave; 0.5 down by + * one octave; or 1.0 leave the formant unaffected. + * + * By default this is set to the special value of 0.0, which + * causes the scale to be calculated automatically. It will be + * treated as 1.0 / the pitch scale if OptionFormantPreserved is + * specified, or 1.0 for OptionFormantShifted. + * + * Conversely, if this is set to a value other than the default + * 0.0, formant shifting will happen regardless of the state of + * the OptionFormantPreserved/OptionFormantShifted option. + * + * This function is provided for special effects only. You do not + * need to call it for ordinary pitch shifting, with or without + * formant preservation - just specify or omit the + * OptionFormantPreserved option as appropriate. Use this function + * only if you want to shift formants by a distance other than + * that of the overall pitch shift. + */ + void setFormantScale(double scale); + + /** + * Return the last pitch scaling ratio value that was set (either + * on construction or with setPitchScale()). + */ + double getPitchScale() const; + + /** + * Return the last formant scaling ratio that was set with + * setFormantScale, or 0.0 if the default automatic scaling is in + * effect. + */ + double getFormantScale() const; + + /** + * Return the output delay of the shifter. This is the number of + * audio samples that one should discard at the start of the + * output, in order to ensure that the resulting audio has the + * expected time alignment with the input. + * + * Ensure you have set the pitch scale to its proper starting + * value before calling getStartDelay(). + */ + size_t getStartDelay() const; + + /** + * Return the number of channels this shifter was constructed + * with. + */ + size_t getChannelCount() const; + + /** + * Change an OptionFormant configuration setting. This may be + * called at any time in any mode. + * + * Note that if running multi-threaded in Offline mode, the change + * may not take effect immediately if processing is already under + * way when this function is called. + */ + void setFormantOption(Options options); + + /** + * Query the number of sample frames that must be passed to, and + * will be returned by, each process() call. This value is fixed + * for the lifetime of the shifter. + * + * Note that the blocksize refers to the number of audio sample + * frames, which may be multi-channel, not the number of + * individual samples. + */ + size_t getBlockSize() const; + + /** + * Pitch-shift a single block of sample frames. The number of + * sample frames (samples per channel) processed per call is + * constant. + * + * "input" should point to de-interleaved audio data with one + * float array per channel, with each array containing n samples + * where n is the value returned by getBlockSize(). + * + * "output" should point to a float array per channel, with each + * array having enough room to store n samples where n is the value + * returned by getBlockSize(). + * + * Sample values are conventionally expected to be in the range + * -1.0f to +1.0f. + */ + void shift(const float *const *input, float *const *output); + + /** + * Set the level of debug output. The supported values are: + * + * 0. Report errors only. + * + * 1. Report some information on construction and ratio + * change. Nothing is reported during normal processing unless + * something changes. + * + * 2. Report a significant amount of information about ongoing + * calculations during normal processing. + * + * The default is whatever has been set using + * setDefaultDebugLevel(), or 0 if that function has not been + * called. + * + * All output goes to \c cerr unless a custom + * RubberBandLiveShifter::Logger has been provided on + * construction. Because writing to \c cerr is not RT-safe, only + * debug level 0 is RT-safe in normal use by default. Debug levels + * 0 and 1 use only C-string constants as debug messages, so they + * are RT-safe if your custom logger is RT-safe. Levels 2 and 3 + * are not guaranteed to be RT-safe in any conditions as they may + * construct messages by allocation. + * + * @see Logger + * @see setDefaultDebugLevel + */ + void setDebugLevel(int level); + + /** + * Set the default level of debug output for subsequently + * constructed shifters. + * + * @see setDebugLevel + */ + static void setDefaultDebugLevel(int level); + +protected: + class Impl; + Impl *m_d; + + RubberBandLiveShifter(const RubberBandLiveShifter &) =delete; + RubberBandLiveShifter &operator=(const RubberBandLiveShifter &) =delete; +}; + +} + +#endif diff --git a/src/RubberBandLiveShifter.cpp b/src/RubberBandLiveShifter.cpp new file mode 100644 index 00000000..5b01cd4e --- /dev/null +++ b/src/RubberBandLiveShifter.cpp @@ -0,0 +1,273 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Rubber Band Library + An audio time-stretching and pitch-shifting library. + Copyright 2007-2023 Particular Programs Ltd. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. + + Alternatively, if you have a valid commercial licence for the + Rubber Band Live Pitch Shifter obtained by agreement with the copyright + holders, you may redistribute and/or modify it under the terms + described in that licence. + + If you wish to distribute code using the Rubber Band Live Pitch Shifter + under terms other than those of the GNU General Public License, + you must obtain a valid commercial licence before doing so. +*/ + +#include "../rubberband/RubberBandLiveShifter.h" +#include "finer/R3LiveShifter.h" + +#include + +namespace RubberBand { + +class RubberBandLiveShifter::Impl +{ + R3LiveShifter *m_s; + + class CerrLogger : public RubberBandLiveShifter::Logger { + public: + void log(const char *message) override { + std::cerr << "RubberBandLive: " << message << "\n"; + } + void log(const char *message, double arg0) override { + auto prec = std::cerr.precision(); + std::cerr.precision(10); + std::cerr << "RubberBandLive: " << message << ": " << arg0 << "\n"; + std::cerr.precision(prec); + } + void log(const char *message, double arg0, double arg1) override { + auto prec = std::cerr.precision(); + std::cerr.precision(10); + std::cerr << "RubberBandLive: " << message + << ": (" << arg0 << ", " << arg1 << ")" << "\n"; + std::cerr.precision(prec); + } + }; + + Log makeRBLog(std::shared_ptr logger) { + if (logger) { + return Log( + [=](const char *message) { + logger->log(message); + }, + [=](const char *message, double arg0) { + logger->log(message, arg0); + }, + [=](const char *message, double arg0, double arg1) { + logger->log(message, arg0, arg1); + } + ); + } else { + return makeRBLog(std::shared_ptr + (new CerrLogger())); + } + } + +public: + Impl(size_t sampleRate, size_t channels, + std::shared_ptr logger, + RubberBandLiveShifter::Options options) : + m_s (new R3LiveShifter + (R3LiveShifter::Parameters(double(sampleRate), channels, + options), + makeRBLog(logger))) + { + } + + ~Impl() + { + delete m_s; + } + + void reset() + { + m_s->reset(); + } + + RTENTRY__ + void + setPitchScale(double scale) + { + m_s->setPitchScale(scale); + } + + RTENTRY__ + void + setFormantScale(double scale) + { + m_s->setFormantScale(scale); + } + + RTENTRY__ + double + getPitchScale() const + { + return m_s->getPitchScale(); + } + + RTENTRY__ + double + getFormantScale() const + { + return m_s->getFormantScale(); + } + + RTENTRY__ + void + setFormantOption(RubberBandLiveShifter::Options options) + { + m_s->setFormantOption(options); + } + + RTENTRY__ + size_t + getStartDelay() const + { + return m_s->getStartDelay(); + } + + RTENTRY__ + size_t + getBlockSize() const + { + return m_s->getBlockSize(); + } + + RTENTRY__ + void + shift(const float *const *input, float *const *output) + { + m_s->shift(input, output); + } + + RTENTRY__ + size_t + getChannelCount() const + { + return m_s->getChannelCount(); + } + + void + setDebugLevel(int level) + { + m_s->setDebugLevel(level); + } + + static void + setDefaultDebugLevel(int level) + { + Log::setDefaultDebugLevel(level); + } +}; + +RubberBandLiveShifter::RubberBandLiveShifter(size_t sampleRate, + size_t channels, + Options options) : + m_d(new Impl(sampleRate, channels, nullptr, options)) +{ +} + +RubberBandLiveShifter::RubberBandLiveShifter(size_t sampleRate, + size_t channels, + std::shared_ptr logger, + Options options) : + m_d(new Impl(sampleRate, channels, logger, options)) +{ +} + +RubberBandLiveShifter::~RubberBandLiveShifter() +{ + delete m_d; +} + +void +RubberBandLiveShifter::reset() +{ + m_d->reset(); +} + +RTENTRY__ +void +RubberBandLiveShifter::setPitchScale(double scale) +{ + m_d->setPitchScale(scale); +} + +RTENTRY__ +void +RubberBandLiveShifter::setFormantScale(double scale) +{ + m_d->setFormantScale(scale); +} + +RTENTRY__ +double +RubberBandLiveShifter::getPitchScale() const +{ + return m_d->getPitchScale(); +} + +RTENTRY__ +double +RubberBandLiveShifter::getFormantScale() const +{ + return m_d->getFormantScale(); +} + +RTENTRY__ +size_t +RubberBandLiveShifter::getStartDelay() const +{ + return m_d->getStartDelay(); +} + +RTENTRY__ +void +RubberBandLiveShifter::setFormantOption(Options options) +{ + m_d->setFormantOption(options); +} + +RTENTRY__ +size_t +RubberBandLiveShifter::getBlockSize() const +{ + return m_d->getBlockSize(); +} + +RTENTRY__ +void +RubberBandLiveShifter::shift(const float *const *input, float *const *output) +{ + m_d->shift(input, output); +} + +RTENTRY__ +size_t +RubberBandLiveShifter::getChannelCount() const +{ + return m_d->getChannelCount(); +} + +void +RubberBandLiveShifter::setDebugLevel(int level) +{ + m_d->setDebugLevel(level); +} + +void +RubberBandLiveShifter::setDefaultDebugLevel(int level) +{ + Impl::setDefaultDebugLevel(level); +} + +} + diff --git a/src/finer/R3LiveShifter.cpp b/src/finer/R3LiveShifter.cpp new file mode 100644 index 00000000..d17b470b --- /dev/null +++ b/src/finer/R3LiveShifter.cpp @@ -0,0 +1,1169 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Rubber Band Library + An audio time-stretching and pitch-shifting library. + Copyright 2007-2023 Particular Programs Ltd. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. + + Alternatively, if you have a valid commercial licence for the + Rubber Band Library obtained by agreement with the copyright + holders, you may redistribute and/or modify it under the terms + described in that licence. + + If you wish to distribute code using the Rubber Band Library + under terms other than those of the GNU General Public License, + you must obtain a valid commercial licence before doing so. +*/ + +#include "R3LiveShifter.h" + +#include "../common/VectorOpsComplex.h" +#include "../common/Profiler.h" + +#include + +namespace RubberBand { + +R3LiveShifter::R3LiveShifter(Parameters parameters, Log log) : + m_log(log), + m_parameters(validateSampleRate(parameters)), + m_limits(parameters.options, m_parameters.sampleRate), + m_pitchScale(1.0), + m_formantScale(0.0), + m_guide(Guide::Parameters + (m_parameters.sampleRate, + !(m_parameters.options & RubberBandLiveShifter::OptionWindowLong)), + m_log), + m_guideConfiguration(m_guide.getConfiguration()), + m_channelAssembly(m_parameters.channels), + m_useReadahead(false), + m_contractThenExpand(false), + m_prevInhop(m_limits.maxInhopWithReadahead / 2), + m_prevOuthop(m_prevInhop), + m_firstProcess(true), + m_unityCount(0) +{ + Profiler profiler("R3LiveShifter::R3LiveShifter"); + + initialise(); +} + +void +R3LiveShifter::initialise() +{ + m_log.log(1, "R3LiveShifter::R3LiveShifter: rate, options", + m_parameters.sampleRate, m_parameters.options); + + if (!isSingleWindowed()) { + m_log.log(1, "R3LiveShifter::R3LiveShifter: multi window enabled"); + } + + if ((m_parameters.options & RubberBandLiveShifter::OptionWindowMedium) || + (m_parameters.options & RubberBandLiveShifter::OptionWindowLong)) { + m_log.log(1, "R3LiveShifter::R3LiveShifter: readahead enabled"); + m_useReadahead = true; + } + + if ((m_parameters.options & RubberBandLiveShifter::OptionPitchModeB)) { + m_log.log(1, "R3LiveShifter::R3LiveShifter: contract-then-expand enabled"); + m_contractThenExpand = true; + } + + double maxClassifierFrequency = 16000.0; + if (maxClassifierFrequency > m_parameters.sampleRate/2) { + maxClassifierFrequency = m_parameters.sampleRate/2; + } + int classificationBins = + int(floor(m_guideConfiguration.classificationFftSize * + maxClassifierFrequency / m_parameters.sampleRate)); + + BinSegmenter::Parameters segmenterParameters + (m_guideConfiguration.classificationFftSize, + classificationBins, m_parameters.sampleRate, 18); + + BinClassifier::Parameters classifierParameters + (classificationBins, 9, 1, 10, 2.0, 2.0); + + if (isSingleWindowed()) { + classifierParameters.horizontalFilterLength = 7; + } + + int inRingBufferSize = getWindowSourceSize() * 4; + int outRingBufferSize = getWindowSourceSize() * 4; + + m_channelData.clear(); + + for (int c = 0; c < m_parameters.channels; ++c) { + m_channelData.push_back(std::make_shared + (segmenterParameters, + classifierParameters, + m_guideConfiguration.longestFftSize, + getWindowSourceSize(), + inRingBufferSize, + outRingBufferSize)); + for (int b = 0; b < m_guideConfiguration.fftBandLimitCount; ++b) { + const auto &band = m_guideConfiguration.fftBandLimits[b]; + int fftSize = band.fftSize; + m_channelData[c]->scales[fftSize] = + std::make_shared + (fftSize, m_guideConfiguration.longestFftSize); + } + } + + m_scaleData.clear(); + + for (int b = 0; b < m_guideConfiguration.fftBandLimitCount; ++b) { + const auto &band = m_guideConfiguration.fftBandLimits[b]; + int fftSize = band.fftSize; + GuidedPhaseAdvance::Parameters guidedParameters + (fftSize, m_parameters.sampleRate, m_parameters.channels, + isSingleWindowed()); + m_scaleData[fftSize] = std::make_shared + (guidedParameters, m_log); + } + + createResamplers(); + + if (!m_pitchScale.is_lock_free()) { + m_log.log(0, "R3LiveShifter: WARNING: std::atomic is not lock-free"); + } +} + +WindowType +R3LiveShifter::ScaleData::analysisWindowShape() +{ + if (singleWindowMode) { + return HannWindow; + } else { + if (fftSize < 1024 || fftSize > 2048) return HannWindow; + else return NiemitaloForwardWindow; + } +} + +int +R3LiveShifter::ScaleData::analysisWindowLength() +{ + return fftSize; +} + +WindowType +R3LiveShifter::ScaleData::synthesisWindowShape() +{ + if (singleWindowMode) { + return HannWindow; + } else { + if (fftSize < 1024 || fftSize > 2048) return HannWindow; + else return NiemitaloReverseWindow; + } +} + +int +R3LiveShifter::ScaleData::synthesisWindowLength() +{ + if (singleWindowMode) { + return fftSize; + } else { + if (fftSize > 2048) return fftSize/2; + else return fftSize; + } +} + +void +R3LiveShifter::setPitchScale(double scale) +{ + m_log.log(2, "R3LiveShifter::setPitchScale", scale); + if (scale == m_pitchScale) return; + m_pitchScale = scale; +} + +void +R3LiveShifter::setFormantScale(double scale) +{ + m_log.log(2, "R3LiveShifter::setFormantScale", scale); + m_formantScale = scale; +} + +void +R3LiveShifter::setFormantOption(RubberBandLiveShifter::Options options) +{ + int mask = (RubberBandLiveShifter::OptionFormantShifted | + RubberBandLiveShifter::OptionFormantPreserved); + m_parameters.options &= ~mask; + options &= mask; + m_parameters.options |= options; +} + +void +R3LiveShifter::createResamplers() +{ + Profiler profiler("R3LiveShifter::createResamplers"); + + Resampler::Parameters resamplerParameters; + resamplerParameters.quality = Resampler::FastestTolerable; + resamplerParameters.initialSampleRate = m_parameters.sampleRate; + resamplerParameters.maxBufferSize = m_guideConfiguration.longestFftSize; + resamplerParameters.dynamism = Resampler::RatioOftenChanging; + resamplerParameters.ratioChange = Resampler::SmoothRatioChange; + + m_inResampler = std::unique_ptr + (new Resampler(resamplerParameters, m_parameters.channels)); + + m_outResampler = std::unique_ptr + (new Resampler(resamplerParameters, m_parameters.channels)); +} + +double +R3LiveShifter::getPitchScale() const +{ + return m_pitchScale; +} + +double +R3LiveShifter::getFormantScale() const +{ + return m_formantScale; +} + +size_t +R3LiveShifter::getPreferredStartPad() const +{ + //!!!??? + return 0; +} + +size_t +R3LiveShifter::getStartDelay() const +{ + int resamplerDelay = 32; + int fixed = getWindowSourceSize() / 2 + resamplerDelay; + int variable = getWindowSourceSize() / 2; + if (m_contractThenExpand) { + if (m_pitchScale < 1.0) { + return size_t(fixed + ceil(variable / m_pitchScale)); + } else { + return size_t(fixed + ceil(variable * m_pitchScale)); + } + } else { + if (m_pitchScale < 1.0) { + return size_t(fixed + ceil(variable * m_pitchScale)); + } else { + return size_t(fixed + ceil(variable / m_pitchScale)); + } + } +} + +size_t +R3LiveShifter::getChannelCount() const +{ + return m_parameters.channels; +} + +void +R3LiveShifter::reset() +{ + m_inResampler->reset(); + m_outResampler->reset(); + + m_unityCount = 0; + + m_prevInhop = m_limits.maxInhopWithReadahead / 2; + m_prevOuthop = int(round(m_prevInhop * m_pitchScale)); + m_firstProcess = true; + + for (auto &it : m_scaleData) { + it.second->guided.reset(); + } + + for (auto &cd : m_channelData) { + cd->reset(); + } +} + +size_t +R3LiveShifter::getBlockSize() const +{ + return 512; +} + +void +R3LiveShifter::shift(const float *const *input, float *const *output) +{ + Profiler profiler("R3LiveShifter::shift"); + + int incount = int(getBlockSize()); + + m_log.log(2, "R3LiveShifter::shift: start of process with incount", incount); + m_log.log(2, "R3LiveShifter::shift: initially in inbuf", m_channelData[0]->inbuf->getReadSpace()); + m_log.log(2, "R3LiveShifter::shift: initially in outbuf", m_channelData[0]->outbuf->getReadSpace()); + + int pad = 0; + int resamplerDelay = 32; + if (m_firstProcess) { + if (m_contractThenExpand) { + pad = getWindowSourceSize(); + if (m_pitchScale > 1.0) { + pad = int(ceil(pad * m_pitchScale)); + } + } else { + pad = getWindowSourceSize() / 2; + } + pad += resamplerDelay; + m_log.log(2, "R3LiveShifter::shift: extending input with pre-pad", incount, pad); + for (int c = 0; c < m_parameters.channels; ++c) { + m_channelData[c]->inbuf->zero(pad); + } + } + + readIn(input); + + double outRatio = 1.0; + + if (m_contractThenExpand) { + if (m_pitchScale < 1.0) { + outRatio = 1.0 / m_pitchScale; + } + } else { + if (m_pitchScale > 1.0) { + outRatio = 1.0 / m_pitchScale; + } + } + + int requiredInOutbuf = int(ceil(incount / outRatio)) + resamplerDelay; + generate(requiredInOutbuf); + + int got = readOut(output, incount, 0); + + if (got < incount) { + m_log.log(0, "R3LiveShifter::shift: ERROR: internal error: insufficient data at output (wanted, got)", incount, got); + for (int c = 0; c < m_parameters.channels; ++c) { + for (int i = got; i < incount; ++i) { + if (i > 0) output[c][i] = output[c][i-1] * 0.9f; + else output[c][i] = 0.f; + } + } + } + + m_log.log(2, "R3LiveShifter::shift: end of process with incount", incount); + m_log.log(2, "R3LiveShifter::shift: remaining in inbuf", m_channelData[0]->inbuf->getReadSpace()); + m_log.log(2, "R3LiveShifter::shift: remaining in outbuf", m_channelData[0]->outbuf->getReadSpace()); + m_log.log(2, "R3LiveShifter::shift: returning", got); + + m_firstProcess = false; +} + +void +R3LiveShifter::readIn(const float *const *input) +{ + int incount = int(getBlockSize()); + + int ws = m_channelData[0]->inbuf->getWriteSpace(); + if (ws < incount) { + m_log.log(0, "R3LiveShifter::process: ERROR: internal error: insufficient space in inbuf (wanted, got)", incount, ws); + return; + } + + for (int c = 0; c < m_parameters.channels; ++c) { + auto &cd = m_channelData.at(c); + m_channelAssembly.resampled[c] = cd->resampled.data(); + } + + if (useMidSide()) { + auto &c0 = m_channelData.at(0)->mixdown; + auto &c1 = m_channelData.at(1)->mixdown; + for (int i = 0; i < incount; ++i) { + float l = input[0][i]; + float r = input[1][i]; + float m = (l + r) / 2.f; + float s = (l - r) / 2.f; + c0[i] = m; + c1[i] = s; + } + m_channelAssembly.input[0] = m_channelData.at(0)->mixdown.data(); + m_channelAssembly.input[1] = m_channelData.at(1)->mixdown.data(); + } else { + for (int c = 0; c < m_parameters.channels; ++c) { + m_channelAssembly.input[c] = input[c]; + } + } + + double inRatio = 1.0; + + if (m_contractThenExpand) { + if (m_pitchScale > 1.0) { + inRatio = 1.0 / m_pitchScale; + } + } else { + if (m_pitchScale < 1.0) { + inRatio = 1.0 / m_pitchScale; + } + } + + m_log.log(2, "R3LiveShifter::readIn: ratio", inRatio); + + int resampleBufSize = int(m_channelData.at(0)->resampled.size()); + + int resampleOutput = m_inResampler->resample + (m_channelAssembly.resampled.data(), + resampleBufSize, + m_channelAssembly.input.data(), + incount, + inRatio, + false); + + m_log.log(2, "R3LiveShifter::readIn: writing to inbuf from resampled data, former read space and samples being added", m_channelData[0]->inbuf->getReadSpace(), resampleOutput); + + for (int c = 0; c < m_parameters.channels; ++c) { + m_channelData[c]->inbuf->write + (m_channelData.at(c)->resampled.data(), + resampleOutput); + } +} + +void +R3LiveShifter::generate(int requiredInOutbuf) +{ + Profiler profiler("R3LiveShifter::generate"); + + auto &cd0 = m_channelData.at(0); + int alreadyGenerated = cd0->outbuf->getReadSpace(); + if (alreadyGenerated >= requiredInOutbuf) { + m_log.log(2, "R3LiveShifter::generate: have already generated required count", alreadyGenerated, requiredInOutbuf); + return; + } + + m_log.log(2, "R3LiveShifter::generate: alreadyGenerated, requiredInOutbuf", alreadyGenerated, requiredInOutbuf); + + int toGenerate = requiredInOutbuf - alreadyGenerated; + +// int longest = m_guideConfiguration.longestFftSize; + int channels = m_parameters.channels; + + int ws = getWindowSourceSize(); + + // We always leave at least ws in inbuf, and this function is + // called after some input has just been written to inbuf, so we + // must have more than ws samples in there. + + int atInput = cd0->inbuf->getReadSpace(); + if (atInput <= ws) { + m_log.log(0, "R3LiveShifter::generate: insufficient samples at input: have and require more than", atInput, ws); + return; + } + + m_log.log(2, "R3LiveShifter::generate: atInput, toLeave", atInput, ws); + + int toConsume = atInput - ws; + + m_log.log(2, "R3LiveShifter::generate: toConsume, toGenerate", toConsume, toGenerate); + + int indicativeInhop = m_limits.maxInhopWithReadahead; + int indicativeOuthop = m_limits.maxPreferredOuthop; + + int minHopsAtInput = 1 + (toConsume - 1) / indicativeInhop; + int minHopsAtOutput = 1 + (toGenerate - 1) / indicativeOuthop; + + int hops = std::max(minHopsAtInput, minHopsAtOutput); + + m_log.log(2, "R3LiveShifter::generate: indicative inhop, outhop", indicativeInhop, indicativeOuthop); + m_log.log(2, "R3LiveShifter::generate: minHopsAtInput, minHopsAtOutput", minHopsAtInput, minHopsAtOutput); + m_log.log(2, "R3LiveShifter::generate: hops", hops); + + for (int hop = 0; hop < hops; ++hop) { + + Profiler profiler("R3LiveShifter::generate/loop"); + + if (toConsume <= 0) { + m_log.log(2, "R3LiveShifter::generate: ERROR: toConsume is zero at top of loop, hop and hops", hop, hops); + break; + } + + int inhop = 1 + (toConsume - 1) / hops; + int outhop = 1 + (toGenerate - 1) / hops; + + if (inhop > toConsume) { + inhop = toConsume; + } + + m_log.log(2, "R3LiveShifter::generate: inhop, outhop", inhop, outhop); + + // Now inhop is the distance by which the input stream will be + // advanced after our current frame has been read, and outhop + // is the distance by which the output will be advanced after + // it has been emitted; m_prevInhop and m_prevOuthop are the + // corresponding values the last time a frame was processed. + // + // Our phase adjustments need to be based on the distances we + // have advanced the input and output since the previous + // frame, not the distances we are about to advance them, so + // they use the m_prev values. + + if (inhop != m_prevInhop) { + m_log.log(2, "R3LiveShifter::generate: change in inhop", m_prevInhop, inhop); + } + if (outhop != m_prevOuthop) { + m_log.log(2, "R3LiveShifter::generate: change in outhop", m_prevOuthop, outhop); + } + + m_log.log(2, "R3LiveShifter::generate: write space and outhop", cd0->outbuf->getWriteSpace(), outhop); + + // NB our ChannelData, ScaleData, and ChannelScaleData maps + // contain shared_ptrs; whenever we retain one of them in a + // variable, we do so by reference to avoid copying the + // shared_ptr (as that is not realtime safe). Same goes for + // the map iterators + + // Analysis + + for (int c = 0; c < channels; ++c) { + analyseChannel(c, inhop, m_prevInhop, m_prevOuthop); + } + + // Phase update. This is synchronised across all channels + + for (auto &it : m_channelData[0]->scales) { + int fftSize = it.first; + for (int c = 0; c < channels; ++c) { + auto &cd = m_channelData.at(c); + auto &scale = cd->scales.at(fftSize); + m_channelAssembly.mag[c] = scale->mag.data(); + m_channelAssembly.phase[c] = scale->phase.data(); + m_channelAssembly.prevMag[c] = scale->prevMag.data(); + m_channelAssembly.guidance[c] = &cd->guidance; + m_channelAssembly.outPhase[c] = scale->advancedPhase.data(); + } + m_scaleData.at(fftSize)->guided.advance + (m_channelAssembly.outPhase.data(), + m_channelAssembly.mag.data(), + m_channelAssembly.phase.data(), + m_channelAssembly.prevMag.data(), + m_guideConfiguration, + m_channelAssembly.guidance.data(), + useMidSide(), + m_prevInhop, + m_prevOuthop); + } + + for (int c = 0; c < channels; ++c) { + adjustPreKick(c); + } + + // Resynthesis + + for (int c = 0; c < channels; ++c) { + synthesiseChannel(c, outhop, false); + } + + // Emit + + int writeCount = outhop; + int advanceCount = inhop; + + for (int c = 0; c < channels; ++c) { + auto &cd = m_channelData.at(c); + cd->outbuf->write(cd->mixdown.data(), writeCount); + cd->inbuf->skip(advanceCount); + } + + m_log.log(2, "R3LiveShifter::generate: writing and advancing", writeCount, advanceCount); + + m_prevInhop = inhop; + m_prevOuthop = outhop; + } + + m_log.log(2, "R3LiveShifter::generate: returning, now have generated", cd0->outbuf->getReadSpace()); +} + +int +R3LiveShifter::readOut(float *const *output, int outcount, int origin) +{ + double outRatio = 1.0; + + if (m_contractThenExpand) { + if (m_pitchScale < 1.0) { + outRatio = 1.0 / m_pitchScale; + } + } else { + if (m_pitchScale > 1.0) { + outRatio = 1.0 / m_pitchScale; + } + } + + m_log.log(2, "R3LiveShifter::readOut: outcount and ratio", outcount, outRatio); + + int fromOutbuf = int(floor(outcount / outRatio)); + + if (fromOutbuf == 0) { + fromOutbuf = 1; + } + + int got = fromOutbuf; + + for (int c = 0; c < m_parameters.channels; ++c) { + auto &cd = m_channelData.at(c); + int gotHere = cd->outbuf->read(cd->resampled.data(), got); + if (gotHere < got) { + if (c > 0) { + m_log.log(0, "R3LiveShifter::readOut: WARNING: channel imbalance detected"); + } + got = std::min(got, std::max(gotHere, 0)); + } + + m_channelAssembly.resampled[c] = cd->resampled.data(); + m_channelAssembly.mixdown[c] = output[c] + origin; + } + + m_log.log(2, "R3LiveShifter::readOut: requested and got from outbufs", fromOutbuf, got); + m_log.log(2, "R3LiveShifter::readOut: leaving behind", m_channelData.at(0)->outbuf->getReadSpace()); + + int resampledCount = 0; + + if (got > 0) { + resampledCount = m_outResampler->resample + (m_channelAssembly.mixdown.data(), + outcount, + m_channelAssembly.resampled.data(), + got, + outRatio, + false); + } + + m_log.log(2, "R3LiveShifter::readOut: resampled to", resampledCount); + + if (resampledCount < outcount && + m_channelData.at(0)->outbuf->getReadSpace() > 0) { + int remaining = outcount - resampledCount; + m_log.log(2, "R3LiveShifter::readOut: recursing to try to get the remaining", + remaining); + resampledCount += readOut(output, remaining, origin + resampledCount); + } + + if (resampledCount < outcount) { + m_log.log(0, "R3LiveShifter::readOut: WARNING: Failed to obtain enough samples from resampler", resampledCount, outcount); + } + + if (useMidSide()) { + for (int i = 0; i < resampledCount; ++i) { + float m = output[0][i]; + float s = output[1][i]; + float l = m + s; + float r = m - s; + output[0][i] = l; + output[1][i] = r; + } + } + + return resampledCount; +} + +void +R3LiveShifter::analyseChannel(int c, int inhop, int prevInhop, int prevOuthop) +{ + Profiler profiler("R3LiveShifter::analyseChannel"); + + auto &cd = m_channelData.at(c); + + int sourceSize = cd->windowSource.size(); + process_t *buf = cd->windowSource.data(); + + int readSpace = cd->inbuf->getReadSpace(); + if (readSpace < sourceSize) { + cd->inbuf->peek(buf, readSpace); + v_zero(buf + readSpace, sourceSize - readSpace); + } else { + cd->inbuf->peek(buf, sourceSize); + } + + // We have an unwindowed time-domain frame in buf that is as long + // as required for the union of all FFT sizes and readahead + // hops. Populate the various sizes from it with aligned centres, + // windowing as we copy. The classification scale is handled + // separately because it has readahead, so skip it here. (In + // single-window mode that means we do nothing here, since the + // classification scale is the only one.) + + int longest = m_guideConfiguration.longestFftSize; + int classify = m_guideConfiguration.classificationFftSize; + + for (auto &it: cd->scales) { + int fftSize = it.first; + if (fftSize == classify) continue; + int offset = (longest - fftSize) / 2; + m_scaleData.at(fftSize)->analysisWindow.cut + (buf + offset, it.second->timeDomain.data()); + } + + auto &classifyScale = cd->scales.at(classify); + ClassificationReadaheadData &readahead = cd->readahead; + bool copyFromReadahead = false; + + if (m_useReadahead) { + + // The classification scale has a one-hop readahead, so + // populate the readahead from further down the long + // unwindowed frame. + + m_scaleData.at(classify)->analysisWindow.cut + (buf + (longest - classify) / 2 + inhop, + readahead.timeDomain.data()); + + // If inhop has changed since the previous frame, we must + // populate the classification scale (but for + // analysis/resynthesis rather than classification) anew + // rather than reuse the previous frame's readahead. + + copyFromReadahead = cd->haveReadahead; + if (inhop != prevInhop) copyFromReadahead = false; + } + + if (!copyFromReadahead) { + m_scaleData.at(classify)->analysisWindow.cut + (buf + (longest - classify) / 2, + classifyScale->timeDomain.data()); + } + + // FFT shift, forward FFT, and carry out cartesian-polar + // conversion for each FFT size. + + // For the classification scale we need magnitudes for the full + // range (polar only in a subset) and we operate in the readahead, + // pulling current values from the existing readahead (except + // where the inhop has changed as above, in which case we need to + // do both readahead and current) + + if (m_useReadahead) { + + if (copyFromReadahead) { + v_copy(classifyScale->mag.data(), + readahead.mag.data(), + classifyScale->bufSize); + v_copy(classifyScale->phase.data(), + readahead.phase.data(), + classifyScale->bufSize); + } + + v_fftshift(readahead.timeDomain.data(), classify); + m_scaleData.at(classify)->fft.forward(readahead.timeDomain.data(), + classifyScale->real.data(), + classifyScale->imag.data()); + + for (int b = 0; b < m_guideConfiguration.fftBandLimitCount; ++b) { + const auto &band = m_guideConfiguration.fftBandLimits[b]; + if (band.fftSize == classify) { + ToPolarSpec spec; + spec.magFromBin = 0; + spec.magBinCount = classify/2 + 1; + spec.polarFromBin = band.b0min; + spec.polarBinCount = band.b1max - band.b0min + 1; + convertToPolar(readahead.mag.data(), + readahead.phase.data(), + classifyScale->real.data(), + classifyScale->imag.data(), + spec); + + v_scale(classifyScale->mag.data(), + 1.0 / double(classify), + classifyScale->mag.size()); + break; + } + } + + cd->haveReadahead = true; + } + + // For the others (and the classify as well, if the inhop has + // changed or we aren't using readahead or haven't filled the + // readahead yet) we operate directly in the scale data and + // restrict the range for cartesian-polar conversion + + for (auto &it: cd->scales) { + int fftSize = it.first; + if (fftSize == classify && copyFromReadahead) { + continue; + } + + auto &scale = it.second; + + v_fftshift(scale->timeDomain.data(), fftSize); + + m_scaleData.at(fftSize)->fft.forward(scale->timeDomain.data(), + scale->real.data(), + scale->imag.data()); + + for (int b = 0; b < m_guideConfiguration.fftBandLimitCount; ++b) { + const auto &band = m_guideConfiguration.fftBandLimits[b]; + if (band.fftSize == fftSize) { + + ToPolarSpec spec; + + // For the classify scale we always want the full + // range, as all the magnitudes (though not + // necessarily all phases) are potentially relevant to + // classification and formant analysis. But this case + // here only happens if we don't copyFromReadahead - + // the normal case is above and, er, copies from the + // previous readahead. + if (fftSize == classify) { + spec.magFromBin = 0; + spec.magBinCount = classify/2 + 1; + spec.polarFromBin = band.b0min; + spec.polarBinCount = band.b1max - band.b0min + 1; + } else { + spec.magFromBin = band.b0min; + spec.magBinCount = band.b1max - band.b0min + 1; + spec.polarFromBin = spec.magFromBin; + spec.polarBinCount = spec.magBinCount; + } + + convertToPolar(scale->mag.data(), + scale->phase.data(), + scale->real.data(), + scale->imag.data(), + spec); + + v_scale(scale->mag.data() + spec.magFromBin, + 1.0 / double(fftSize), + spec.magBinCount); + + break; + } + } + } + + if (m_parameters.options & RubberBandLiveShifter::OptionFormantPreserved) { + analyseFormant(c); + adjustFormant(c); + } + + // Use the classification scale to get a bin segmentation and + // calculate the adaptive frequency guide for this channel + + v_copy(cd->classification.data(), cd->nextClassification.data(), + cd->classification.size()); + + if (m_useReadahead) { + cd->classifier->classify(readahead.mag.data(), + cd->nextClassification.data()); + } else { + cd->classifier->classify(classifyScale->mag.data(), + cd->nextClassification.data()); + } + + cd->prevSegmentation = cd->segmentation; + cd->segmentation = cd->nextSegmentation; + cd->nextSegmentation = cd->segmenter->segment(cd->nextClassification.data()); +/* + if (c == 0) { + double pb = cd->nextSegmentation.percussiveBelow; + double pa = cd->nextSegmentation.percussiveAbove; + double ra = cd->nextSegmentation.residualAbove; + int pbb = binForFrequency(pb, classify, m_parameters.sampleRate); + int pab = binForFrequency(pa, classify, m_parameters.sampleRate); + int rab = binForFrequency(ra, classify, m_parameters.sampleRate); + std::cout << "pb = " << pb << ", pbb = " << pbb << std::endl; + std::cout << "pa = " << pa << ", pab = " << pab << std::endl; + std::cout << "ra = " << ra << ", rab = " << rab << std::endl; + std::cout << "s:"; + for (int i = 0; i <= classify/2; ++i) { + if (i > 0) std::cout << ","; + if (i < pbb || (i >= pab && i <= rab)) { + std::cout << "1"; + } else { + std::cout << "0"; + } + } + std::cout << std::endl; + } +*/ + + double ratio = m_pitchScale; + + if (fabs(ratio - 1.0) < 1.0e-7) { + ++m_unityCount; + } else { + m_unityCount = 0; + } + + bool tighterChannelLock = + m_parameters.options & RubberBandLiveShifter::OptionChannelsTogether; + + double magMean = v_mean(classifyScale->mag.data() + 1, classify/2); + + bool resetOnSilence = true; + if (useMidSide() && c == 1) { + // Do not phase reset on silence in the side channel - the + // reset is propagated across to the mid channel, giving + // constant resets for e.g. mono material in a stereo + // configuration + resetOnSilence = false; + } + + if (m_useReadahead) { + m_guide.updateGuidance(ratio, + prevOuthop, + classifyScale->mag.data(), + classifyScale->prevMag.data(), + cd->readahead.mag.data(), + cd->segmentation, + cd->prevSegmentation, + cd->nextSegmentation, + magMean, + m_unityCount, + true, + tighterChannelLock, + resetOnSilence, + cd->guidance); + } else { + m_guide.updateGuidance(ratio, + prevOuthop, + classifyScale->prevMag.data(), + classifyScale->prevMag.data(), + classifyScale->mag.data(), + cd->segmentation, + cd->prevSegmentation, + cd->nextSegmentation, + magMean, + m_unityCount, + true, + tighterChannelLock, + resetOnSilence, + cd->guidance); + } + +/* + if (c == 0) { + if (cd->guidance.kick.present) { + std::cout << "k:2" << std::endl; + } else if (cd->guidance.preKick.present) { + std::cout << "k:1" << std::endl; + } else { + std::cout << "k:0" << std::endl; + } + } +*/ +} + +void +R3LiveShifter::analyseFormant(int c) +{ + Profiler profiler("R3LiveShifter::analyseFormant"); + + auto &cd = m_channelData.at(c); + auto &f = *cd->formant; + + int fftSize = f.fftSize; + int binCount = fftSize/2 + 1; + + auto &scale = cd->scales.at(fftSize); + auto &scaleData = m_scaleData.at(fftSize); + + scaleData->fft.inverseCepstral(scale->mag.data(), f.cepstra.data()); + + int cutoff = int(floor(m_parameters.sampleRate / 650.0)); + if (cutoff < 1) cutoff = 1; + + f.cepstra[0] /= 2.0; + f.cepstra[cutoff-1] /= 2.0; + for (int i = cutoff; i < fftSize; ++i) { + f.cepstra[i] = 0.0; + } + v_scale(f.cepstra.data(), 1.0 / double(fftSize), cutoff); + + scaleData->fft.forward(f.cepstra.data(), f.envelope.data(), f.spare.data()); + + v_exp(f.envelope.data(), binCount); + v_square(f.envelope.data(), binCount); + + for (int i = 0; i < binCount; ++i) { + if (f.envelope[i] > 1.0e10) f.envelope[i] = 1.0e10; + } +} + +void +R3LiveShifter::adjustFormant(int c) +{ + Profiler profiler("R3LiveShifter::adjustFormant"); + + auto &cd = m_channelData.at(c); + + for (auto &it : cd->scales) { + + int fftSize = it.first; + auto &scale = it.second; + + int highBin = int(floor(fftSize * 10000.0 / m_parameters.sampleRate)); + process_t targetFactor = process_t(cd->formant->fftSize) / process_t(fftSize); + process_t formantScale = m_formantScale; + if (formantScale == 0.0) formantScale = 1.0 / m_pitchScale; + process_t sourceFactor = targetFactor / formantScale; + process_t maxRatio = 60.0; + process_t minRatio = 1.0 / maxRatio; + + for (int b = 0; b < m_guideConfiguration.fftBandLimitCount; ++b) { + const auto &band = m_guideConfiguration.fftBandLimits[b]; + if (band.fftSize != fftSize) continue; + for (int i = band.b0min; i < band.b1max && i < highBin; ++i) { + process_t source = cd->formant->envelopeAt(i * sourceFactor); + process_t target = cd->formant->envelopeAt(i * targetFactor); + if (target > 0.0) { + process_t ratio = source / target; + if (ratio < minRatio) ratio = minRatio; + if (ratio > maxRatio) ratio = maxRatio; + scale->mag[i] *= ratio; + } + } + } + } +} + +void +R3LiveShifter::adjustPreKick(int c) +{ + if (isSingleWindowed()) return; + + Profiler profiler("R3LiveShifter::adjustPreKick"); + + auto &cd = m_channelData.at(c); + auto fftSize = cd->guidance.fftBands[0].fftSize; + if (cd->guidance.preKick.present) { + auto &scale = cd->scales.at(fftSize); + int from = binForFrequency(cd->guidance.preKick.f0, + fftSize, m_parameters.sampleRate); + int to = binForFrequency(cd->guidance.preKick.f1, + fftSize, m_parameters.sampleRate); + for (int i = from; i <= to; ++i) { + process_t diff = scale->mag[i] - scale->prevMag[i]; + if (diff > 0.0) { + scale->pendingKick[i] = diff; + scale->mag[i] -= diff; + } + } + } else if (cd->guidance.kick.present) { + auto &scale = cd->scales.at(fftSize); + int from = binForFrequency(cd->guidance.preKick.f0, + fftSize, m_parameters.sampleRate); + int to = binForFrequency(cd->guidance.preKick.f1, + fftSize, m_parameters.sampleRate); + for (int i = from; i <= to; ++i) { + scale->mag[i] += scale->pendingKick[i]; + scale->pendingKick[i] = 0.0; + } + } +} + +void +R3LiveShifter::synthesiseChannel(int c, int outhop, bool draining) +{ + Profiler profiler("R3LiveShifter::synthesiseChannel"); + + int longest = m_guideConfiguration.longestFftSize; + + auto &cd = m_channelData.at(c); + + for (int b = 0; b < cd->guidance.fftBandCount; ++b) { + + const auto &band = cd->guidance.fftBands[b]; + int fftSize = band.fftSize; + + auto &scale = cd->scales.at(fftSize); + auto &scaleData = m_scaleData.at(fftSize); + + // copy to prevMag before filtering + v_copy(scale->prevMag.data(), + scale->mag.data(), + scale->bufSize); + + process_t winscale = process_t(outhop) / scaleData->windowScaleFactor; + + // The frequency filter is applied naively in the frequency + // domain. Aliasing is reduced by the shorter resynthesis + // window. We resynthesise each scale individually, then sum - + // it's easier to manage scaling for in situations with a + // varying resynthesis hop + + int lowBin = binForFrequency(band.f0, fftSize, m_parameters.sampleRate); + int highBin = binForFrequency(band.f1, fftSize, m_parameters.sampleRate); + if (highBin % 2 == 0 && highBin > 0) --highBin; + + int n = scale->mag.size(); + if (lowBin >= n) lowBin = n - 1; + if (highBin >= n) highBin = n - 1; + if (highBin < lowBin) highBin = lowBin; + + if (lowBin > 0) { + v_zero(scale->real.data(), lowBin); + v_zero(scale->imag.data(), lowBin); + } + + v_scale(scale->mag.data() + lowBin, winscale, highBin - lowBin); + + v_polar_to_cartesian(scale->real.data() + lowBin, + scale->imag.data() + lowBin, + scale->mag.data() + lowBin, + scale->advancedPhase.data() + lowBin, + highBin - lowBin); + + if (highBin < scale->bufSize) { + v_zero(scale->real.data() + highBin, scale->bufSize - highBin); + v_zero(scale->imag.data() + highBin, scale->bufSize - highBin); + } + + scaleData->fft.inverse(scale->real.data(), + scale->imag.data(), + scale->timeDomain.data()); + + v_fftshift(scale->timeDomain.data(), fftSize); + + // Synthesis window may be shorter than analysis window, so + // copy and cut only from the middle of the time-domain frame; + // and the accumulator length always matches the longest FFT + // size, so as to make mixing straightforward, so there is an + // additional offset needed for the target + + int synthesisWindowSize = scaleData->synthesisWindow.getSize(); + int fromOffset = (fftSize - synthesisWindowSize) / 2; + int toOffset = (longest - synthesisWindowSize) / 2; + + scaleData->synthesisWindow.cutAndAdd + (scale->timeDomain.data() + fromOffset, + scale->accumulator.data() + toOffset); + } + + // Mix this channel and move the accumulator along + + float *mixptr = cd->mixdown.data(); + v_zero(mixptr, outhop); + + for (auto &it : cd->scales) { + auto &scale = it.second; + + process_t *accptr = scale->accumulator.data(); + for (int i = 0; i < outhop; ++i) { + mixptr[i] += float(accptr[i]); + } + + int n = scale->accumulator.size() - outhop; + v_move(accptr, accptr + outhop, n); + v_zero(accptr + n, outhop); + + if (draining) { + if (scale->accumulatorFill > outhop) { + auto newFill = scale->accumulatorFill - outhop; + m_log.log(2, "draining: reducing accumulatorFill from, to", scale->accumulatorFill, newFill); + scale->accumulatorFill = newFill; + } else { + scale->accumulatorFill = 0; + } + } else { + scale->accumulatorFill = scale->accumulator.size(); + } + } +} + +} + diff --git a/src/finer/R3LiveShifter.h b/src/finer/R3LiveShifter.h new file mode 100644 index 00000000..54239a39 --- /dev/null +++ b/src/finer/R3LiveShifter.h @@ -0,0 +1,413 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Rubber Band Library + An audio time-stretching and pitch-shifting library. + Copyright 2007-2023 Particular Programs Ltd. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. + + Alternatively, if you have a valid commercial licence for the + Rubber Band Library obtained by agreement with the copyright + holders, you may redistribute and/or modify it under the terms + described in that licence. + + If you wish to distribute code using the Rubber Band Library + under terms other than those of the GNU General Public License, + you must obtain a valid commercial licence before doing so. +*/ + +#ifndef RUBBERBAND_R3_LIVE_SHIFTERIMPL_H +#define RUBBERBAND_R3_LIVE_SHIFTERIMPL_H + +#include "BinSegmenter.h" +#include "Guide.h" +#include "Peak.h" +#include "PhaseAdvance.h" + +#include "../common/Resampler.h" +#include "../common/FFT.h" +#include "../common/FixedVector.h" +#include "../common/Allocators.h" +#include "../common/Window.h" +#include "../common/VectorOpsComplex.h" +#include "../common/Log.h" + +#include "../../rubberband/RubberBandLiveShifter.h" + +#include +#include +#include + +namespace RubberBand +{ + +class R3LiveShifter +{ +public: + struct Parameters { + double sampleRate; + int channels; + RubberBandLiveShifter::Options options; + Parameters(double _sampleRate, int _channels, + RubberBandLiveShifter::Options _options) : + sampleRate(_sampleRate), channels(_channels), options(_options) { } + }; + + R3LiveShifter(Parameters parameters, Log log); + ~R3LiveShifter() { } + + void reset(); + + void setPitchScale(double scale); + void setFormantScale(double scale); + + double getPitchScale() const; + double getFormantScale() const; + + void setFormantOption(RubberBandLiveShifter::Options); + + size_t getBlockSize() const; + void shift(const float *const *input, float *const *output); + + size_t getPreferredStartPad() const; + size_t getStartDelay() const; + + size_t getChannelCount() const; + + void setDebugLevel(int level) { + m_log.setDebugLevel(level); + for (auto &sd : m_scaleData) { + sd.second->guided.setDebugLevel(level); + } + m_guide.setDebugLevel(level); + } + +protected: + struct Limits { + int minPreferredOuthop; + int maxPreferredOuthop; + int minInhop; + int maxInhopWithReadahead; + int maxInhop; + Limits(RubberBandLiveShifter::Options options, double rate) : + // commented values are results when rate = 44100 or 48000 + minPreferredOuthop(roundUpDiv(rate, 512)), // 128 + maxPreferredOuthop(roundUpDiv(rate, 128)), // 512 + minInhop(1), + maxInhopWithReadahead(roundUpDiv(rate, 64)), // 1024 + maxInhop(roundUpDiv(rate, 32)) // 2048 + { + if (!(options & RubberBandLiveShifter::OptionWindowLong)) { + minPreferredOuthop = roundUpDiv(rate, 256); // 256 + maxPreferredOuthop = (roundUpDiv(rate, 128) * 5) / 4; // 640 + maxInhopWithReadahead = roundUpDiv(rate, 128); // 512 + maxInhop = (roundUpDiv(rate, 64) * 3) / 2; // 1536 + } + } + }; + + struct ClassificationReadaheadData { + FixedVector timeDomain; + FixedVector mag; + FixedVector phase; + ClassificationReadaheadData(int _fftSize) : + timeDomain(_fftSize, 0.f), + mag(_fftSize/2 + 1, 0.f), + phase(_fftSize/2 + 1, 0.f) + { } + + private: + ClassificationReadaheadData(const ClassificationReadaheadData &) =delete; + ClassificationReadaheadData &operator=(const ClassificationReadaheadData &) =delete; + }; + + struct ChannelScaleData { + int fftSize; + int bufSize; // size of every freq-domain array here: fftSize/2 + 1 + FixedVector timeDomain; + FixedVector real; + FixedVector imag; + FixedVector mag; + FixedVector phase; + FixedVector advancedPhase; + FixedVector prevMag; + FixedVector pendingKick; + FixedVector accumulator; + int accumulatorFill; + + ChannelScaleData(int _fftSize, int _longestFftSize) : + fftSize(_fftSize), + bufSize(fftSize/2 + 1), + timeDomain(fftSize, 0.f), + real(bufSize, 0.f), + imag(bufSize, 0.f), + mag(bufSize, 0.f), + phase(bufSize, 0.f), + advancedPhase(bufSize, 0.f), + prevMag(bufSize, 0.f), + pendingKick(bufSize, 0.f), + accumulator(_longestFftSize, 0.f), + accumulatorFill(0) + { } + + void reset() { + v_zero(prevMag.data(), prevMag.size()); + v_zero(pendingKick.data(), pendingKick.size()); + v_zero(accumulator.data(), accumulator.size()); + accumulatorFill = 0; + } + + private: + ChannelScaleData(const ChannelScaleData &) =delete; + ChannelScaleData &operator=(const ChannelScaleData &) =delete; + }; + + struct FormantData { + int fftSize; + FixedVector cepstra; + FixedVector envelope; + FixedVector spare; + + FormantData(int _fftSize) : + fftSize(_fftSize), + cepstra(_fftSize, 0.0), + envelope(_fftSize/2 + 1, 0.0), + spare(_fftSize/2 + 1, 0.0) { } + + process_t envelopeAt(process_t bin) const { + int b0 = int(floor(bin)), b1 = int(ceil(bin)); + if (b0 < 0 || b0 > fftSize/2) { + return 0.0; + } else if (b1 == b0 || b1 > fftSize/2) { + return envelope.at(b0); + } else { + process_t diff = bin - process_t(b0); + return envelope.at(b0) * (1.0 - diff) + envelope.at(b1) * diff; + } + } + }; + + struct ChannelData { + std::map> scales; + FixedVector windowSource; + ClassificationReadaheadData readahead; + bool haveReadahead; + std::unique_ptr classifier; + FixedVector classification; + FixedVector nextClassification; + std::unique_ptr segmenter; + BinSegmenter::Segmentation segmentation; + BinSegmenter::Segmentation prevSegmentation; + BinSegmenter::Segmentation nextSegmentation; + Guide::Guidance guidance; + FixedVector mixdown; + FixedVector resampled; + std::unique_ptr> inbuf; + std::unique_ptr> outbuf; + std::unique_ptr formant; + ChannelData(BinSegmenter::Parameters segmenterParameters, + BinClassifier::Parameters classifierParameters, + int longestFftSize, + int windowSourceSize, + int inRingBufferSize, + int outRingBufferSize) : + scales(), + windowSource(windowSourceSize, 0.0), + readahead(segmenterParameters.fftSize), + haveReadahead(false), + classifier(new BinClassifier(classifierParameters)), + classification(classifierParameters.binCount, + BinClassifier::Classification::Residual), + nextClassification(classifierParameters.binCount, + BinClassifier::Classification::Residual), + segmenter(new BinSegmenter(segmenterParameters)), + segmentation(), prevSegmentation(), nextSegmentation(), + mixdown(longestFftSize, 0.f), + resampled(outRingBufferSize, 0.f), + inbuf(new RingBuffer(inRingBufferSize)), + outbuf(new RingBuffer(outRingBufferSize)), + formant(new FormantData(segmenterParameters.fftSize)) { } + void reset() { + haveReadahead = false; + classifier->reset(); + segmentation = BinSegmenter::Segmentation(); + prevSegmentation = BinSegmenter::Segmentation(); + nextSegmentation = BinSegmenter::Segmentation(); + for (size_t i = 0; i < nextClassification.size(); ++i) { + nextClassification[i] = BinClassifier::Classification::Residual; + } + inbuf->reset(); + outbuf->reset(); + for (auto &s : scales) { + s.second->reset(); + } + } + }; + + struct ChannelAssembly { + // Vectors of bare pointers, used to package container data + // from different channels into arguments for PhaseAdvance + FixedVector input; + FixedVector mag; + FixedVector phase; + FixedVector prevMag; + FixedVector guidance; + FixedVector outPhase; + FixedVector mixdown; + FixedVector resampled; + ChannelAssembly(int channels) : + input(channels, nullptr), + mag(channels, nullptr), phase(channels, nullptr), + prevMag(channels, nullptr), guidance(channels, nullptr), + outPhase(channels, nullptr), mixdown(channels, nullptr), + resampled(channels, nullptr) { } + }; + + struct ScaleData { + int fftSize; + bool singleWindowMode; + FFT fft; + Window analysisWindow; + Window synthesisWindow; + process_t windowScaleFactor; + GuidedPhaseAdvance guided; + + ScaleData(GuidedPhaseAdvance::Parameters guidedParameters, + Log log) : + fftSize(guidedParameters.fftSize), + singleWindowMode(guidedParameters.singleWindowMode), + fft(fftSize), + analysisWindow(analysisWindowShape(), + analysisWindowLength()), + synthesisWindow(synthesisWindowShape(), + synthesisWindowLength()), + windowScaleFactor(0.0), + guided(guidedParameters, log) + { + int asz = analysisWindow.getSize(), ssz = synthesisWindow.getSize(); + int off = (asz - ssz) / 2; + for (int i = 0; i < ssz; ++i) { + windowScaleFactor += analysisWindow.getValue(i + off) * + synthesisWindow.getValue(i); + } + } + + WindowType analysisWindowShape(); + int analysisWindowLength(); + WindowType synthesisWindowShape(); + int synthesisWindowLength(); + }; + + Log m_log; + Parameters m_parameters; + const Limits m_limits; + + std::atomic m_pitchScale; + std::atomic m_formantScale; + + std::vector> m_channelData; + std::map> m_scaleData; + Guide m_guide; + Guide::Configuration m_guideConfiguration; + ChannelAssembly m_channelAssembly; + std::unique_ptr m_inResampler; + std::unique_ptr m_outResampler; + bool m_useReadahead; + int m_prevInhop; + int m_prevOuthop; + bool m_contractThenExpand; // otherwise expand then contract + bool m_firstProcess; + uint32_t m_unityCount; + + void initialise(); + + void readIn(const float *const *input); + void generate(int required); + int readOut(float *const *output, int outcount, int origin); + + void createResamplers(); + void analyseChannel(int channel, int inhop, int prevInhop, int prevOuthop); + void analyseFormant(int channel); + void adjustFormant(int channel); + void adjustPreKick(int channel); + void synthesiseChannel(int channel, int outhop, bool draining); + + struct ToPolarSpec { + int magFromBin; + int magBinCount; + int polarFromBin; + int polarBinCount; + }; + + Parameters validateSampleRate(const Parameters ¶ms) { + Parameters validated { params }; + double minRate = 8000.0, maxRate = 192000.0; + if (params.sampleRate < minRate) { + m_log.log(0, "R3LiveShifter: WARNING: Unsupported sample rate", params.sampleRate); + m_log.log(0, "R3LiveShifter: Minimum rate is", minRate); + validated.sampleRate = minRate; + } else if (params.sampleRate > maxRate) { + m_log.log(0, "R3LiveShifter: WARNING: Unsupported sample rate", params.sampleRate); + m_log.log(0, "R3LiveShifter: Maximum rate is", maxRate); + validated.sampleRate = maxRate; + } + return validated; + } + + void convertToPolar(process_t *mag, process_t *phase, + const process_t *real, const process_t *imag, + const ToPolarSpec &s) const { + v_cartesian_to_polar(mag + s.polarFromBin, + phase + s.polarFromBin, + real + s.polarFromBin, + imag + s.polarFromBin, + s.polarBinCount); + if (s.magFromBin < s.polarFromBin) { + v_cartesian_to_magnitudes(mag + s.magFromBin, + real + s.magFromBin, + imag + s.magFromBin, + s.polarFromBin - s.magFromBin); + } + if (s.magFromBin + s.magBinCount > s.polarFromBin + s.polarBinCount) { + v_cartesian_to_magnitudes(mag + s.polarFromBin + s.polarBinCount, + real + s.polarFromBin + s.polarBinCount, + imag + s.polarFromBin + s.polarBinCount, + s.magFromBin + s.magBinCount - + s.polarFromBin - s.polarBinCount); + } + } + + bool useMidSide() const { + return m_parameters.channels == 2 && + (m_parameters.options & + RubberBandLiveShifter::OptionChannelsTogether); + } + + bool isSingleWindowed() const { + return !(m_parameters.options & + RubberBandLiveShifter::OptionWindowLong); + } + + int getWindowSourceSize() const { + if (m_useReadahead) { + int sz = m_guideConfiguration.classificationFftSize + + m_limits.maxInhopWithReadahead; + if (m_guideConfiguration.longestFftSize > sz) { + return m_guideConfiguration.longestFftSize; + } else { + return sz; + } + } else { + return m_guideConfiguration.longestFftSize; + } + } +}; + +} + +#endif diff --git a/src/test/TestLiveShifter.cpp b/src/test/TestLiveShifter.cpp new file mode 100644 index 00000000..794a32b6 --- /dev/null +++ b/src/test/TestLiveShifter.cpp @@ -0,0 +1,1272 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Rubber Band Library + An audio time-stretching and pitch-shifting library. + Copyright 2007-2023 Particular Programs Ltd. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. + + Alternatively, if you have a valid commercial licence for the + Rubber Band Library obtained by agreement with the copyright + holders, you may redistribute and/or modify it under the terms + described in that licence. + + If you wish to distribute code using the Rubber Band Library + under terms other than those of the GNU General Public License, + you must obtain a valid commercial licence before doing so. +*/ + +#ifndef BOOST_TEST_DYN_LINK +#define BOOST_TEST_DYN_LINK +#endif +#include + +#include "../../rubberband/RubberBandLiveShifter.h" + +#include + +#include + +using namespace RubberBand; + +using std::vector; +using std::cerr; +using std::endl; + +namespace tt = boost::test_tools; + +BOOST_AUTO_TEST_SUITE(TestLiveShifter) + +BOOST_AUTO_TEST_CASE(sinusoid_unchanged) +{ + bool printDebug = true; +// bool printDebug = false; + + int n = 100000; + float freq = 440.f; + int rate = 44100; + RubberBandLiveShifter shifter + (rate, 1, RubberBandLiveShifter::OptionPitchModeB); + + //!!! + shifter.setPitchScale(2.0); + + if (printDebug) { + shifter.setDebugLevel(2); + } + + int blocksize = shifter.getBlockSize(); + BOOST_TEST(blocksize == 2560); + + n = (n / blocksize + 1) * blocksize; + + vector in(n), out(n); + for (int i = 0; i < n; ++i) { + in[i] = sinf(float(i) * freq * M_PI * 2.f / float(rate)); + } + + for (int i = 0; i < n; i += blocksize) { + float *inp = in.data() + i; + float *outp = out.data() + i; + shifter.shift(&inp, &outp); + } + + int delay = shifter.getStartDelay(); + + // We now have n samples of a simple sinusoid with stretch factor + // 1.0; obviously we expect the output to be essentially the same + // thing. It will have lower precision for a while at the start + // and end because of windowing factors, so we check those with a + // threshold of 0.1; in the middle we expect better + // precision. Note that these are relative tolerances, not + // absolute, i.e. 0.001 means 0.001x the smaller value - so they + // are tighter than they appear. + + // This syntax for comparing containers with a certain tolerance + // using BOOST_TEST is just bonkers. I can't find the << syntax to + // combine manipulators documented anywhere other than in a + // release note, but it does work. Well, sort of - it works this + // way around but not as per_element << tolerance. And + // tolerance(0.1) doesn't do what you'd expect if the things + // you're comparing are floats (it sets the tolerance for doubles, + // leaving float comparison unchanged). Clever... too clever. + +// BOOST_TEST(out == in, +// tt::tolerance(0.1f) << tt::per_element()); + + BOOST_TEST(vector(out.begin() + delay, out.begin() + n) == + vector(in.begin(), in.begin() + n - delay), + tt::tolerance(0.001f) << tt::per_element()); + + if (printDebug) { + // The initial # is to allow grep on the test output + std::cout << "#sample\tV" << std::endl; + for (int i = 0; i < out.size(); ++i) { + std::cout << "#" << i << "\t" << out[i] << std::endl; + } + } + + if (printDebug) { + // The initial @ is to allow grep on the test output + std::cout << "@sample\tV" << std::endl; + for (int i = 0; i + delay < in.size(); ++i) { + std::cout << "@" << i << "\t" << out[i + delay] - in[i] << std::endl; + } + } +} + +//!!! +#ifdef NOT_DEFINED + +BOOST_AUTO_TEST_CASE(sinusoid_unchanged_offline_finer) +{ + int n = 10000; + float freq = 440.f; + int rate = 44100; + + RubberBandLiveShifter shifter + (rate, 1, RubberBandLiveShifter::OptionEngineFiner); + + vector in(n), out(n); + for (int i = 0; i < n; ++i) { + in[i] = sinf(float(i) * freq * M_PI * 2.f / float(rate)); + } + float *inp = in.data(), *outp = out.data(); + + shifter.setMaxProcessSize(n); + shifter.setExpectedInputDuration(n); + BOOST_TEST(shifter.available() == 0); + + shifter.study(&inp, n, true); + BOOST_TEST(shifter.available() == 0); + + shifter.process(&inp, n, true); + BOOST_TEST(shifter.available() == n); + + BOOST_TEST(shifter.getStartDelay() == 0); // offline mode + + size_t got = shifter.retrieve(&outp, n); + BOOST_TEST(got == n); + BOOST_TEST(shifter.available() == -1); + + // The R3 engine is actually less precise than R2 here because of + // its different windowing design, though see the note above about + // what these tolerances mean + + BOOST_TEST(out == in, + tt::tolerance(0.35f) << tt::per_element()); + + BOOST_TEST(vector(out.begin() + 1024, out.begin() + n - 1024) == + vector(in.begin() + 1024, in.begin() + n - 1024), + tt::tolerance(0.01f) << tt::per_element()); + +// std::cout << "ms\tV" << std::endl; +// for (int i = 0; i < n; ++i) { +// std::cout << i << "\t" << out[i] - in[i] << std::endl; +// } +} + +BOOST_AUTO_TEST_CASE(sinusoid_2x_offline_finer) +{ + int n = 10000; + float freq = 441.f; // so a cycle is an exact number of samples + int rate = 44100; + + RubberBandLiveShifter shifter + (rate, 1, RubberBandLiveShifter::OptionEngineFiner); + + shifter.setTimeRatio(2.0); + + vector in(n), out(n*2); + for (int i = 0; i < n*2; ++i) { + out[i] = sinf(float(i) * freq * M_PI * 2.f / float(rate)); + if (i < n) { + in[i] = out[i]; + } + } + float *inp = in.data(), *outp = out.data(); + + shifter.setMaxProcessSize(n); + shifter.setExpectedInputDuration(n); + BOOST_TEST(shifter.available() == 0); + + shifter.study(&inp, n, true); + BOOST_TEST(shifter.available() == 0); + + shifter.process(&inp, n, true); + BOOST_TEST(shifter.available() == n*2); + + BOOST_TEST(shifter.getStartDelay() == 0); // offline mode + + size_t got = shifter.retrieve(&outp, n*2); + BOOST_TEST(got == n*2); + BOOST_TEST(shifter.available() == -1); + + int period = -1; + for (int i = 1000; i < 2000; ++i) { + if (period >= 0) ++period; + if (out[i] <= 0.f && out[i+1] > 0.f) { + if (period == -1) period = 0; + else break; + } + } + BOOST_TEST(period == 100); + + int offset = 0; + for (int i = 0; i < 200; ++i) { + if (out[i] <= 0.f && out[i+1] > -0.01f) { + offset = i + 1; + break; + } + } + + // overall + + double rms = 0.0; + for (int i = 0; i < n - offset; ++i) { + double diff = out[i + offset] - in[i]; + rms += diff * diff; + } + rms = sqrt(rms / double(n - offset)); + BOOST_TEST(rms < 0.2); + + // steady state + + rms = 0.0; + for (int i = 1500; i < n - offset - 3000; ++i) { + double diff = out[i + offset + 1500] - in[i + 1500]; + rms += diff * diff; + } + rms = sqrt(rms / double(n - offset - 3000)); + BOOST_TEST(rms < 0.1); +} + +static vector process_realtime(RubberBandLiveShifter &shifter, + const vector &in, + int nOut, + int bs, + bool printDebug) +{ + int n = in.size(); + vector out(nOut, 0.f); + + // Prime the start + { + float *source = out.data(); // just reuse out because it's silent + shifter.process(&source, shifter.getPreferredStartPad(), false); + } + + int toSkip = shifter.getStartDelay(); + + int inOffset = 0, outOffset = 0; + + while (outOffset < nOut) { + + // Obtain a single block of size bs, simulating realtime + // playback. The following might be the content of a + // sound-producing callback function + + int needed = std::min(bs, nOut - outOffset); + int obtained = 0; + + while (obtained < needed) { + + int available = shifter.available(); + + if (available < 0) { // finished + for (int i = obtained; i < needed; ++i) { + out[outOffset++] = 0.f; + } + break; + + } else if (available == 0) { // need to provide more input + int required = shifter.getSamplesRequired(); + BOOST_TEST(required > 0); // because available == 0 + int toProcess = std::min(required, n - inOffset); + const float *const source = in.data() + inOffset; + bool final = (toProcess < required); + shifter.process(&source, toProcess, final); + inOffset += toProcess; + BOOST_TEST(shifter.available() > 0); + continue; + + } else if (toSkip > 0) { // available > 0 && toSkip > 0 + float *target = out.data() + outOffset; + int toRetrieve = std::min(toSkip, available); + int retrieved = shifter.retrieve(&target, toRetrieve); + BOOST_TEST(retrieved == toRetrieve); + toSkip -= retrieved; + + } else { // available > 0 + float *target = out.data() + outOffset; + int toRetrieve = std::min(needed - obtained, available); + int retrieved = shifter.retrieve(&target, toRetrieve); + BOOST_TEST(retrieved == toRetrieve); + obtained += retrieved; + outOffset += retrieved; + } + } + } + + if (printDebug) { + // The initial # is to allow grep on the test output + std::cout << "#sample\tV" << std::endl; + for (int i = 0; i < nOut; ++i) { + std::cout << "#" << i << "\t" << out[i] << std::endl; + } + } + + return out; +} + +static void sinusoid_realtime(RubberBandLiveShifter::Options options, + double timeRatio, + double pitchScale, + bool printDebug) +{ + int n = (timeRatio < 1.0 ? 80000 : 40000); + int nOut = int(ceil(n * timeRatio)); + float freq = 441.f; + int rate = 44100; + int bs = 512; + + // This test simulates block-by-block realtime processing with + // latency compensation, and checks that the output is all in the + // expected place + + RubberBandLiveShifter shifter(rate, 1, options, timeRatio, pitchScale); + shifter.setMaxProcessSize(bs); + + if (printDebug) { + shifter.setDebugLevel(2); + } + + // The input signal is a fixed frequency sinusoid that steps up in + // amplitude every 1/10 of the total duration - from 0.1 at the + // start, via increments of 0.1, to 1.0 at the end + + vector in(n); + for (int i = 0; i < n; ++i) { + float amplitude = float((i / (n/10)) + 1) / 10.f; + float sample = amplitude * + sinf(float(i) * freq * M_PI * 2.f / float(rate)); + in[i] = sample; + } + + vector out = process_realtime(shifter, in, nOut, bs, printDebug); + + // Step through the output signal in chunk of 1/20 of its duration + // (i.e. a rather arbitrary two per expected 0.1 increment in + // amplitude) and for each chunk, verify that the frequency is + // right and the amplitude is what we expect at that point + + for (int chunk = 0; chunk < 20; ++chunk) { + +// cerr << "chunk " << chunk << " of 20" << endl; + + int i0 = (nOut * chunk) / 20; + int i1 = (nOut * (chunk + 1)) / 20; + + // frequency + + int positiveCrossings = 0; + for (int i = i0; i + 1 < i1; ++i) { + if (out[i] <= 0.f && out[i+1] > 0.f) { + ++positiveCrossings; + } + } + + int expectedCrossings = int(round((freq * pitchScale * + double(i1 - i0)) / rate)); + + bool highSpeedPitch = + ! ((options & RubberBandLiveShifter::OptionPitchHighQuality) || + (options & RubberBandLiveShifter::OptionPitchHighConsistency)); + + // The check here has to depend on whether we are in Finer or + // Faster mode. In Finer mode, we expect to be generally exact + // but in the first and last chunks we can be out by one + // crossing if slowing, more if speeding up. In Faster mode we + // need to cut more slack + + int slack = 0; + + if (options & RubberBandLiveShifter::OptionEngineFiner) { + if (options & RubberBandLiveShifter::OptionWindowShort) { + slack = 2; + } else if (chunk == 0 || chunk == 19 || highSpeedPitch) { + slack = 1; + } + } else { + slack = 1; + if (chunk == 0) { + slack = (timeRatio < 1.0 ? 3 : 2); + } else if (chunk == 19) { + // all bets are off, practically + slack = expectedCrossings / 2; + } else { + slack = 1; + } + } + + BOOST_TEST(positiveCrossings <= expectedCrossings + slack); + BOOST_TEST(positiveCrossings >= expectedCrossings - slack); + + // amplitude + + double rms = 0.0; + for (int i = i0; i < i1; ++i) { + rms += out[i] * out[i]; + } + rms = sqrt(rms / double(i1 - i0)); + + double expected = (chunk/2 + 1) * 0.05 * sqrt(2.0); + + double maxOver = 0.01; + double maxUnder = 0.1; + + if (!(options & RubberBandLiveShifter::OptionEngineFiner) || + (options & RubberBandLiveShifter::OptionWindowShort)) { + maxUnder = 0.2; + } + + BOOST_TEST(rms - expected < maxOver); + BOOST_TEST(expected - rms < maxUnder); + } +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_samepitch_realtime_finer) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 8.0, 1.0, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_samepitch_realtime_finer) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 0.5, 1.0, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_higher_realtime_finer) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_higher_realtime_finer_hqpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighQuality, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_higher_realtime_finer_hcpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_higher_realtime_finer) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 0.5, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_higher_realtime_finer_hqpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighQuality, + 0.5, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_higher_realtime_finer_hcpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.5, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_lower_realtime_finer) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_lower_realtime_finer_hqpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighQuality, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_lower_realtime_finer_hcpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_samepitch_realtime_finer_short) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionWindowShort, + 8.0, 1.0, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_samepitch_realtime_finer_short) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionWindowShort, + 0.5, 1.0, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_higher_realtime_finer_short) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionWindowShort, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_higher_realtime_finer_short_hcpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionWindowShort | + RubberBandLiveShifter::OptionPitchHighConsistency, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_higher_realtime_finer_short) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionWindowShort, + 0.5, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_higher_realtime_finer_hcpitch_short) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionWindowShort | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.5, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_lower_realtime_finer_short) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionWindowShort, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_lower_realtime_finer_short_hcpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionWindowShort | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_samepitch_realtime_faster) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime, + 8.0, 1.0, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_samepitch_realtime_faster) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime, + 0.5, 1.0, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_higher_realtime_faster) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_higher_realtime_faster_hqpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighQuality, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_higher_realtime_faster_hcpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_higher_realtime_faster) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime, + 0.5, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_higher_realtime_faster_hqpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighQuality, + 0.5, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_fast_higher_realtime_faster_hcpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.5, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_lower_realtime_faster) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_lower_realtime_faster_hqpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighQuality, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(sinusoid_slow_lower_realtime_faster_hcpitch) +{ + sinusoid_realtime(RubberBandLiveShifter::OptionEngineFaster | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(impulses_2x_offline_faster) +{ + int n = 10000; + int rate = 44100; + RubberBandLiveShifter shifter + (rate, 1, RubberBandLiveShifter::OptionEngineFaster); + + shifter.setTimeRatio(2.0); + + vector in(n, 0.f), out(n * 2, 0.f); + + in[100] = 1.f; + in[101] = -1.f; + + in[5000] = 1.f; + in[5001] = -1.f; + + in[9900] = 1.f; + in[9901] = -1.f; + + float *inp = in.data(), *outp = out.data(); + + shifter.setMaxProcessSize(n); + shifter.setExpectedInputDuration(n); + BOOST_TEST(shifter.available() == 0); + + shifter.study(&inp, n, true); + BOOST_TEST(shifter.available() == 0); + + shifter.process(&inp, n, true); + BOOST_TEST(shifter.available() == n * 2); + + BOOST_TEST(shifter.getStartDelay() == 0); // offline mode + + size_t got = shifter.retrieve(&outp, n * 2); + BOOST_TEST(got == n * 2); + BOOST_TEST(shifter.available() == -1); + + int peak0 = -1, peak1 = -1, peak2 = -1; + float max; + + max = -2.f; + for (int i = 0; i < n/2; ++i) { + if (out[i] > max) { max = out[i]; peak0 = i; } + } + + max = -2.f; + for (int i = n/2; i < (n*3)/2; ++i) { + if (out[i] > max) { max = out[i]; peak1 = i; } + } + + max = -2.f; + for (int i = (n*3)/2; i < n*2; ++i) { + if (out[i] > max) { max = out[i]; peak2 = i; } + } + + BOOST_TEST(peak0 == 100); + BOOST_TEST(peak1 > n - 400); + BOOST_TEST(peak1 < n + 50); + BOOST_TEST(peak2 > n*2 - 600); + BOOST_TEST(peak2 < n*2); +/* + std::cout << "ms\tV" << std::endl; + for (int i = 0; i < n*2; ++i) { + std::cout << i << "\t" << out[i] << std::endl; + } +*/ +} + +BOOST_AUTO_TEST_CASE(impulses_2x_offline_finer) +{ + int n = 10000; + int rate = 44100; + RubberBandLiveShifter shifter + (rate, 1, RubberBandLiveShifter::OptionEngineFiner); + + shifter.setTimeRatio(2.0); + + vector in(n, 0.f), out(n * 2, 0.f); + + in[100] = 1.f; + in[101] = -1.f; + + in[5000] = 1.f; + in[5001] = -1.f; + + in[9900] = 1.f; + in[9901] = -1.f; + + float *inp = in.data(), *outp = out.data(); + + shifter.setMaxProcessSize(n); + shifter.setExpectedInputDuration(n); + BOOST_TEST(shifter.available() == 0); + + shifter.study(&inp, n, true); + BOOST_TEST(shifter.available() == 0); + + shifter.process(&inp, n, true); + BOOST_TEST(shifter.available() == n * 2); + + BOOST_TEST(shifter.getStartDelay() == 0); // offline mode + + size_t got = shifter.retrieve(&outp, n * 2); + BOOST_TEST(got == n * 2); + BOOST_TEST(shifter.available() == -1); + + int peak0 = -1, peak1 = -1, peak2 = -1; + float max; + + max = -2.f; + for (int i = 0; i < n/2; ++i) { + if (out[i] > max) { max = out[i]; peak0 = i; } + } + + max = -2.f; + for (int i = n/2; i < (n*3)/2; ++i) { + if (out[i] > max) { max = out[i]; peak1 = i; } + } + + max = -2.f; + for (int i = (n*3)/2; i < n*2; ++i) { + if (out[i] > max) { max = out[i]; peak2 = i; } + } + + BOOST_TEST(peak0 == 100); + BOOST_TEST(peak1 > n - 400); + BOOST_TEST(peak1 < n + 50); + BOOST_TEST(peak2 > n*2 - 600); + BOOST_TEST(peak2 < n*2); +/* + std::cout << "ms\tV" << std::endl; + for (int i = 0; i < n*2; ++i) { + std::cout << i << "\t" << out[i] << std::endl; + } +*/ +} + +BOOST_AUTO_TEST_CASE(impulses_2x_5up_offline_finer) +{ + int n = 10000; + int rate = 44100; + RubberBandLiveShifter shifter + (rate, 1, RubberBandLiveShifter::OptionEngineFiner); + + shifter.setTimeRatio(2.0); + shifter.setPitchScale(1.5); + + vector in(n, 0.f), out(n * 2, 0.f); + + in[100] = 1.f; + in[101] = -1.f; + + in[5000] = 1.f; + in[5001] = -1.f; + + in[9900] = 1.f; + in[9901] = -1.f; + + float *inp = in.data(), *outp = out.data(); + + shifter.setMaxProcessSize(n); + shifter.setExpectedInputDuration(n); + BOOST_TEST(shifter.available() == 0); + + shifter.study(&inp, n, true); + BOOST_TEST(shifter.available() == 0); + + shifter.process(&inp, n, true); + BOOST_TEST(shifter.available() == n * 2); + + BOOST_TEST(shifter.getStartDelay() == 0); // offline mode + + size_t got = shifter.retrieve(&outp, n * 2); + BOOST_TEST(got == n * 2); + BOOST_TEST(shifter.available() == -1); + + int peak0 = -1, peak1 = -1, peak2 = -1; + float max; + + max = -2.f; + for (int i = 0; i < n/2; ++i) { + if (out[i] > max) { max = out[i]; peak0 = i; } + } + + max = -2.f; + for (int i = n/2; i < (n*3)/2; ++i) { + if (out[i] > max) { max = out[i]; peak1 = i; } + } + + max = -2.f; + for (int i = (n*3)/2; i < n*2; ++i) { + if (out[i] > max) { max = out[i]; peak2 = i; } + } + + BOOST_TEST(peak0 < 100); + BOOST_TEST(peak1 > n - 400); + BOOST_TEST(peak1 < n + 50); + BOOST_TEST(peak2 > n*2 - 600); + BOOST_TEST(peak2 < n*2); +/* + std::cout << "ms\tV" << std::endl; + for (int i = 0; i < n*2; ++i) { + std::cout << i << "\t" << out[i] << std::endl; + } +*/ +} + +static void impulses_realtime(RubberBandLiveShifter::Options options, + double timeRatio, + double pitchScale, + bool printDebug) +{ + int n = 10000; + int nOut = int(ceil(n * timeRatio)); + int rate = 48000; + int bs = 1024; + + RubberBandLiveShifter shifter(rate, 1, options, timeRatio, pitchScale); + + if (printDebug) { + shifter.setDebugLevel(2); + } + + vector in(n, 0.f); + + in[100] = 1.f; + in[101] = -1.f; + + in[5000] = 1.f; + in[5001] = -1.f; + + in[9900] = 1.f; + in[9901] = -1.f; + + vector out = process_realtime(shifter, in, nOut, bs, printDebug); + + int peak0 = -1, peak1 = -1, peak2 = -1; + float max; + + max = -2.f; + for (int i = 0; i < nOut/4; ++i) { + if (out[i] > max) { max = out[i]; peak0 = i; } + } + + max = -2.f; + for (int i = nOut/4; i < (nOut*3)/4; ++i) { + if (out[i] > max) { max = out[i]; peak1 = i; } + } + + max = -2.f; + for (int i = (nOut*3)/4; i < nOut; ++i) { + if (out[i] > max) { max = out[i]; peak2 = i; } + } + + // These limits aren't alarming, but it be worth tightening them + // and and taking a look at the waveforms + + BOOST_TEST(peak0 < int(ceil(200 * timeRatio))); + BOOST_TEST(peak0 > int(ceil(50 * timeRatio))); + + BOOST_TEST(peak1 < int(ceil(5070 * timeRatio))); + BOOST_TEST(peak1 > int(ceil(4840 * timeRatio))); + + BOOST_TEST(peak2 < int(ceil(9970 * timeRatio))); + BOOST_TEST(peak2 > int(ceil(9770 * timeRatio))); + +/* + std::cout << "ms\tV" << std::endl; + for (int i = 0; i < n*2; ++i) { + std::cout << i << "\t" << out[i] << std::endl; + } +*/ +} + +BOOST_AUTO_TEST_CASE(impulses_slow_samepitch_realtime_finer) +{ + impulses_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 8.0, 1.0, + false); +} + +BOOST_AUTO_TEST_CASE(impulses_fast_samepitch_realtime_finer) +{ + impulses_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 0.5, 1.0, + false); +} + +BOOST_AUTO_TEST_CASE(impulses_slow_higher_realtime_finer) +{ + impulses_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(impulses_slow_higher_realtime_finer_hqpitch) +{ + impulses_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighQuality, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(impulses_slow_higher_realtime_finer_hcpitch) +{ + impulses_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 4.0, 1.5, + false); +} + +BOOST_AUTO_TEST_CASE(impulses_slow_lower_realtime_finer) +{ + impulses_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(impulses_slow_lower_realtime_finer_hqpitch) +{ + impulses_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighQuality, + 8.0, 0.5, + false); +} + +BOOST_AUTO_TEST_CASE(impulses_slow_lower_realtime_finer_hcpitch) +{ + impulses_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 0.5, + false); +} + +static void final_realtime(RubberBandLiveShifter::Options options, + double timeRatio, + double pitchScale, + bool finalAfterFinishing, + bool printDebug) +{ + int n = 10000; + float freq = 440.f; + int rate = 44100; + int blocksize = 700; + RubberBandLiveShifter shifter(rate, 1, options); + + if (printDebug) { + shifter.setDebugLevel(2); + } + + shifter.setTimeRatio(timeRatio); + shifter.setPitchScale(pitchScale); + + int nOut = int(ceil(n * timeRatio)); + int excess = std::max(nOut, n); + vector in(n, 0.f), out(nOut + excess, 0.f); + + for (int i = 0; i < 100; ++i) { + in[n - 101 + i] = sinf(float(i) * freq * M_PI * 2.f / float(rate)); + } + + // Prime the start + { + float *source = out.data(); // just reuse out because it's silent + shifter.process(&source, shifter.getPreferredStartPad(), false); + } + + float *inp = in.data(), *outp = out.data(); + + shifter.setMaxProcessSize(blocksize); + BOOST_TEST(shifter.available() == 0); + + int toSkip = shifter.getStartDelay(); + + int incount = 0, outcount = 0; + while (true) { + + int inbs = std::min(blocksize, n - incount); + + bool final; + if (finalAfterFinishing) { + BOOST_TEST(inbs >= 0); + final = (inbs == 0); + } else { + BOOST_TEST(inbs > 0); + final = (incount + inbs >= n); + } + + float *in = inp + incount; + shifter.process(&in, inbs, final); + incount += inbs; + + int avail = shifter.available(); + BOOST_TEST(avail >= 0); + BOOST_TEST(outcount + avail < nOut + excess); + +// cerr << "in = " << inbs << ", incount now = " << incount << ", avail = " << avail << endl; + + float *out = outp + outcount; + + if (toSkip > 0) { + int skipHere = std::min(toSkip, avail); + size_t got = shifter.retrieve(&out, skipHere); + BOOST_TEST(got == size_t(skipHere)); + toSkip -= got; +// cerr << "got = " << got << ", toSkip now = " << toSkip << ", n = " << n << endl; + } + + avail = shifter.available(); + if (toSkip == 0 && avail > 0) { + size_t got = shifter.retrieve(&out, avail); + BOOST_TEST(got == size_t(avail)); + outcount += got; +// cerr << "got = " << got << ", outcount = " << outcount << ", n = " << n << endl; + if (final) { + BOOST_TEST(shifter.available() == -1); + } else { + BOOST_TEST(shifter.available() == 0); + } + } + + if (final) break; + } + + BOOST_TEST(outcount >= nOut); + + if (printDebug) { + // The initial # is to allow grep on the test output + std::cout << "#sample\tV" << std::endl; + for (int i = 0; i < outcount; ++i) { + std::cout << "#" << i << "\t" << out[i] << std::endl; + } + } + +} + +BOOST_AUTO_TEST_CASE(final_slow_samepitch_realtime_finer) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 1.0, + false, + false); +} + +BOOST_AUTO_TEST_CASE(final_slow_samepitch_realtime_finer_after) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 1.0, + true, + false); +} + +BOOST_AUTO_TEST_CASE(final_fast_samepitch_realtime_finer) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.2, 1.0, + false, + false); +} + +BOOST_AUTO_TEST_CASE(final_fast_samepitch_realtime_finer_after) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.2, 1.0, + true, + false); +} + +BOOST_AUTO_TEST_CASE(final_slow_higher_realtime_finer) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 1.5, + false, + false); +} + +BOOST_AUTO_TEST_CASE(final_slow_higher_realtime_finer_after) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 1.5, + true, + false); +} + +BOOST_AUTO_TEST_CASE(final_fast_higher_realtime_finer) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.2, 1.5, + false, + false); +} + +BOOST_AUTO_TEST_CASE(final_fast_higher_realtime_finer_after) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.2, 1.5, + true, + false); +} + +BOOST_AUTO_TEST_CASE(final_slow_lower_realtime_finer) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 0.5, + false, + false); +} + +BOOST_AUTO_TEST_CASE(final_slow_lower_realtime_finer_after) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 8.0, 0.5, + true, + false); +} + +BOOST_AUTO_TEST_CASE(final_fast_lower_realtime_finer) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.2, 0.5, + false, + false); +} + +BOOST_AUTO_TEST_CASE(final_fast_lower_realtime_finer_after) +{ + final_realtime(RubberBandLiveShifter::OptionEngineFiner | + RubberBandLiveShifter::OptionProcessRealTime | + RubberBandLiveShifter::OptionPitchHighConsistency, + 0.2, 0.5, + true, + false); +} +#endif // NOT_DEFINED + +BOOST_AUTO_TEST_SUITE_END()