diff --git a/COMPILING.md b/COMPILING.md index 11e295bd..0e25ee0b 100644 --- a/COMPILING.md +++ b/COMPILING.md @@ -299,7 +299,7 @@ vDSP -Dfft=vdsp -DHAVE_VDSP Default on macOS/iOS (in FFTW3 -Dfft=fftw -DHAVE_FFTW3 A bit faster than built-in, a bit slower than vDSP. - GPL licence. + GPL with commercial option. SLEEF -Dfft=sleef -DHAVE_SLEEF Usually very fast. Not as widely distributed as FFTW3. Requires diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..7fbceb50 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,44 @@ + +Rubber Band is an open source library with a commercial licence +option. It is developed and maintained with care, prioritising +reliability, output quality, compatibility across releases and +platforms, and performance, in roughly that order. + +## Bug reports + +We welcome and encourage bug reports for the Rubber Band +Library. We're happy to receive them by almost any channel. The most +usual are [as Github issues](https://github.com/breakfastquay/rubberband/issues) +or [by contacting us directly](https://breakfastquay.com/contact.html). + +Please try to provide enough information for us to reproduce the +problem. At the minimum this includes details of platform, compiler, +and library configuration, as well as a description of the expected +and problematic behaviour. We appreciate that describing issues +relating to audio output can be tricky, and we may ask you to send or +provide further information such as audio examples. + +## Pull requests + +Please be aware that we **cannot directly merge** pull requests. This +is partly for technical reasons (the Github repository for the library +is a read-only mirror of an upstream repo) and partly for control of +copyright and provenance. + +Github pull requests may still be appropriate if you have small fixes, +particularly fixes to the build system or documentation, that would be +convenient to upstream. We will gratefully read and consider PRs, +especially of this sort. + +If you have any more substantial changes you would like to see +included, please contact us directly to discuss. + +## Other ways to contribute + +An excellent way to contribute to the continued development of Rubber +Band Library is to [buy a commercial licence](https://breakfastquay.com/technology/license.html). + +For enhancements and other new development work, it may be possible to +engage [Particular Programs Ltd](https://particularprograms.co.uk/) at +a commercial rate. Please contact us for details. + diff --git a/README.md b/README.md index f0d5bf7d..9307e230 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ tempo and pitch of an audio recording independently of one another. * About Rubber Band: https://breakfastquay.com/rubberband/ * Code repository: https://hg.sr.ht/~breakfastquay/rubberband -* Issue tracker: https://todo.sr.ht/~breakfastquay/rubberband or https://github.com/breakfastquay/rubberband/issues * Github mirror: https://github.com/breakfastquay/rubberband CI builds: @@ -56,8 +55,8 @@ licences for some relevant library code are as follows, to the best of our knowledge. See also the file [COMPILING.md](COMPILING.md) for more details. - * FFTW3 - GPL; proprietary licence needed for redistribution - * Intel IPP - Proprietary; licence needed for redistribution + * FFTW3 - GPL with commercial proprietary option + * Intel IPP - Proprietary of some nature * SLEEF - BSD-like * KissFFT - BSD-like * libsamplerate - BSD-like from version 0.1.9 onwards diff --git a/cross/ios-simulator.txt b/cross/ios-simulator.txt index b03149c2..bfbc88c6 100644 --- a/cross/ios-simulator.txt +++ b/cross/ios-simulator.txt @@ -8,6 +8,9 @@ cpu = 'x86_64' system = 'darwin' endian = 'little' +[properties] +needs_exe_wrapper = true + [binaries] c = 'cc' cpp = 'c++' diff --git a/cross/ios.txt b/cross/ios.txt index c9a913a4..6e4a9d5c 100644 --- a/cross/ios.txt +++ b/cross/ios.txt @@ -8,6 +8,9 @@ cpu = 'aarch64' system = 'darwin' endian = 'little' +[properties] +needs_exe_wrapper = true + [binaries] c = 'cc' cpp = 'c++' diff --git a/ladspa-lv2/RubberBandLivePitchShifter.cpp b/ladspa-lv2/RubberBandLivePitchShifter.cpp new file mode 100644 index 00000000..9ff0432f --- /dev/null +++ b/ladspa-lv2/RubberBandLivePitchShifter.cpp @@ -0,0 +1,564 @@ +/* -*- 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::cerr; +using std::endl; +using std::string; + +#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::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) { + cerr << "RubberBandLivePitchShifter::instantiate: invalid sample rate " + << rate << " provided" << endl; + return nullptr; + } + size_t srate = size_t(round(rate)); + if (string(desc->URI) == lv2DescriptorMono.URI) { + return new RubberBandLivePitchShifter(srate, 1); + } else if (string(desc->URI) == lv2DescriptorStereo.URI) { + return new RubberBandLivePitchShifter(srate, 2); + } else { + cerr << "RubberBandLivePitchShifter::instantiate: unrecognised URI " + << desc->URI << " requested" << 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 + + 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); +} + +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/RubberBandPitchShifter.cpp b/ladspa-lv2/RubberBandPitchShifter.cpp index a2b6cca1..5f311b07 100644 --- a/ladspa-lv2/RubberBandPitchShifter.cpp +++ b/ladspa-lv2/RubberBandPitchShifter.cpp @@ -34,6 +34,7 @@ using std::cout; using std::cerr; using std::endl; using std::min; +using std::string; #ifdef RB_PLUGIN_LADSPA @@ -356,18 +357,18 @@ RubberBandPitchShifter::instantiate(const LV2_Descriptor *desc, double rate, const char *, const LV2_Feature *const *) { if (rate < 1.0) { - std::cerr << "RubberBandPitchShifter::instantiate: invalid sample rate " - << rate << " provided" << std::endl; + cerr << "RubberBandPitchShifter::instantiate: invalid sample rate " + << rate << " provided" << endl; return nullptr; } size_t srate = size_t(round(rate)); - if (std::string(desc->URI) == lv2DescriptorMono.URI) { + if (string(desc->URI) == lv2DescriptorMono.URI) { return new RubberBandPitchShifter(srate, 1); - } else if (std::string(desc->URI) == lv2DescriptorStereo.URI) { + } else if (string(desc->URI) == lv2DescriptorStereo.URI) { return new RubberBandPitchShifter(srate, 2); } else { - std::cerr << "RubberBandPitchShifter::instantiate: unrecognised URI " - << desc->URI << " requested" << std::endl; + cerr << "RubberBandPitchShifter::instantiate: unrecognised URI " + << desc->URI << " requested" << endl; return nullptr; } } @@ -642,7 +643,6 @@ RubberBandPitchShifter::runImpl(uint32_t insamples, uint32_t offset) const int samples = insamples; int processed = 0; - size_t outTotal = 0; while (processed < samples) { @@ -671,7 +671,6 @@ RubberBandPitchShifter::runImpl(uint32_t insamples, uint32_t offset) } size_t actual = m_stretcher->retrieve(m_scratch, outchunk); - outTotal += actual; for (size_t c = 0; c < m_channels; ++c) { m_outputBuffer[c]->write(m_scratch[c], actual); diff --git a/ladspa-lv2/RubberBandR3PitchShifter.cpp b/ladspa-lv2/RubberBandR3PitchShifter.cpp index 91c804f1..bb980260 100644 --- a/ladspa-lv2/RubberBandR3PitchShifter.cpp +++ b/ladspa-lv2/RubberBandR3PitchShifter.cpp @@ -30,10 +30,9 @@ using namespace RubberBand; -using std::cout; using std::cerr; using std::endl; -using std::min; +using std::string; #ifdef RB_PLUGIN_LADSPA @@ -341,18 +340,18 @@ RubberBandR3PitchShifter::instantiate(const LV2_Descriptor *desc, double rate, const char *, const LV2_Feature *const *) { if (rate < 1.0) { - std::cerr << "RubberBandR3PitchShifter::instantiate: invalid sample rate " - << rate << " provided" << std::endl; + cerr << "RubberBandR3PitchShifter::instantiate: invalid sample rate " + << rate << " provided" << endl; return nullptr; } size_t srate = size_t(round(rate)); - if (std::string(desc->URI) == lv2DescriptorMono.URI) { + if (string(desc->URI) == lv2DescriptorMono.URI) { return new RubberBandR3PitchShifter(srate, 1); - } else if (std::string(desc->URI) == lv2DescriptorStereo.URI) { + } else if (string(desc->URI) == lv2DescriptorStereo.URI) { return new RubberBandR3PitchShifter(srate, 2); } else { - std::cerr << "RubberBandR3PitchShifter::instantiate: unrecognised URI " - << desc->URI << " requested" << std::endl; + cerr << "RubberBandR3PitchShifter::instantiate: unrecognised URI " + << desc->URI << " requested" << endl; return nullptr; } } @@ -593,7 +592,6 @@ RubberBandR3PitchShifter::runImpl(uint32_t insamples, uint32_t offset) const int samples = insamples; int processed = 0; - size_t outTotal = 0; while (processed < samples) { @@ -622,7 +620,6 @@ RubberBandR3PitchShifter::runImpl(uint32_t insamples, uint32_t offset) } size_t actual = m_stretcher->retrieve(m_scratch, outchunk); - outTotal += actual; for (size_t c = 0; c < m_channels; ++c) { m_outputBuffer[c]->write(m_scratch[c], actual); 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 977275ab..c8c58037 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 4fa26c16..dc8955a1 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 cae0aeca..eff06d20 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 09ce2201..6eda474b 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 = [ @@ -93,6 +96,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..3276a7c8 --- /dev/null +++ b/rubberband/RubberBandLiveShifter.h @@ -0,0 +1,348 @@ +/* -*- 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-2024 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 +{ + +/** + * ### Summary + * + * RubberBand::RubberBandLiveShifter is an interface to the Rubber + * Band Library designed for applications that need to perform + * pitch-shifting only, without time-stretching, and to do so with the + * shortest available processing delay. + * + * RubberBandLiveShifter has a much simpler API than the general + * RubberBandStretcher. Its process function, called + * RubberBandLiveShifter::shift(), accepts a fixed number of sample + * frames on each call and always returns exactly the same number of + * sample frames. This is in contrast to the + * process/available/retrieve call sequence that RubberBandStretcher + * requires as a result of its variable output rate. + * + * The number of frames RubberBandLiveShifter::shift() accepts and + * returns is not under the caller's control: it always requires + * exactly the number given by RubberBandLiveShifter::getBlockSize(). + * However, that number is fixed for the lifetime of the shifter, so + * it only needs to be queried once and then fixed-size buffers may be + * passed. + * + * Using RubberBandLiveShifter also gives a substantially shorter + * processing delay than a typical buffering setup using + * RubberBandStretcher, making it a useful choice for some live + * situations, although it is still not a low-latency effect (and + * never will be) with a delay of 50ms or more between input and + * output signals depending on configuration. The actual value may be + * queried via RubberBandLiveShifter::getStartDelay(). The shifter is + * real-time safe in the sense of avoiding allocation, locking, or + * blocking operations in the processing path. + * + * ### Thread safety + * + * Multiple instances of RubberBandLiveShifter may be created and used + * in separate threads concurrently. However, for any single instance + * of RubberBandLiveShifter, you may not call + * RubberBandLiveShifter::shift() more than once concurrently, and you + * may not change the pitch scaling ratio using + * RubberBandLiveShifter::setPitchScale() while a + * RubberBandLiveShifter::shift() call is being executed. Changing the + * ratio is real-time safe, so when the pitch ratio is time-varying, + * it is normal to update the ratio before each shift call. + */ +class RUBBERBAND_LIVE_DLLEXPORT +RubberBandLiveShifter +{ +public: + enum Option { + OptionWindowShort = 0x00000000, + OptionWindowMedium = 0x00100000, + + OptionFormantShifted = 0x00000000, + OptionFormantPreserved = 0x01000000, + + 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 shift(). You should either call this + * function from the same thread as shift(), or provide your own + * mutex or similar mechanism to ensure that setPitchScale and + * shift() 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 shift() 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..8a300006 --- /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-2024 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/Guide.h b/src/finer/Guide.h index cf4f6862..5e1c50b7 100644 --- a/src/finer/Guide.h +++ b/src/finer/Guide.h @@ -457,7 +457,7 @@ class Guide << guidance.phaseReset.f0 << " to " << guidance.phaseReset.f1 << "]" << std::endl; - m_log.log(1, str.str().c_str()); + m_log.log(2, str.str().c_str()); */ } @@ -478,7 +478,9 @@ class Guide double m_maxHigher; void updateForSilence(Guidance &guidance) const { -// std::cout << "phase reset on silence" << std::endl; + + m_log.log(2, "Guide::updateForSilence"); + double nyquist = m_parameters.sampleRate / 2.0; if (!m_parameters.singleWindowMode) { guidance.fftBands[0].f0 = 0.0; @@ -498,7 +500,7 @@ class Guide const BinSegmenter::Segmentation &segmentation, bool realtime) const { -// std::cout << "unity" << std::endl; + m_log.log(2, "Guide::updateForUnity: realtime and single-window mode", (int)realtime, m_parameters.singleWindowMode); double nyquist = m_parameters.sampleRate / 2.0; @@ -533,9 +535,9 @@ class Guide if (!hadPhaseReset) { guidance.phaseReset.f0 = 16000.0; guidance.phaseReset.f1 = nyquist; -// std::cout << "f0 = " << guidance.phaseReset.f0 << std::endl; return; } else { + m_log.log(2, "Guide::updateForUnity: had phase reset"); guidance.phaseReset.f0 *= 0.9; guidance.phaseReset.f1 *= 1.1; } @@ -553,10 +555,7 @@ class Guide guidance.phaseReset.f0 = 0.0; } -// if (guidance.phaseReset.f0 > 0.0) { -// std::cout << unityCount << ": f0 = " << guidance.phaseReset.f0 -// << ", f1 = " << guidance.phaseReset.f1 << std::endl; -// } + m_log.log(2, "Guide::updateForUnity: f0 and f1", guidance.phaseReset.f0, guidance.phaseReset.f1); } bool checkPotentialKick(const process_t *const magnitudes, diff --git a/src/finer/R3LiveShifter.cpp b/src/finer/R3LiveShifter.cpp new file mode 100644 index 00000000..d3f0b467 --- /dev/null +++ b/src/finer/R3LiveShifter.cpp @@ -0,0 +1,1202 @@ +/* -*- 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-2024 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, true), + m_log), + m_guideConfiguration(m_guide.getConfiguration()), + m_channelAssembly(m_parameters.channels), + m_initialResamplerDelays(32, 32), + m_useReadahead(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_log.log(1, "R3LiveShifter::R3LiveShifter: readahead enabled"); + m_useReadahead = 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_channelData[c]->guidance.phaseReset.present = true; + m_channelData[c]->guidance.phaseReset.f0 = 0.0; + m_channelData[c]->guidance.phaseReset.f1 = m_parameters.sampleRate / 2.0; + } + + 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; + + if (m_firstProcess) { + measureResamplerDelay(); + } +} + +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::SuddenRatioChange; + + int debug = m_log.getDebugLevel(); + if (debug > 0) --debug; + resamplerParameters.debugLevel = debug; + + m_inResampler = std::unique_ptr + (new Resampler(resamplerParameters, m_parameters.channels)); + + m_outResampler = std::unique_ptr + (new Resampler(resamplerParameters, m_parameters.channels)); + + measureResamplerDelay(); +} + +void +R3LiveShifter::measureResamplerDelay() +{ + // Delay in the sense that the resampler at first returns fewer + // samples than requested, not that the samples are delayed within + // the returned buffer relative to their positions at input. We + // actually add the delay ourselves in the first block because we + // need a fixed block size and the resampler doesn't return one. + + int bs = getBlockSize(); + std::vector inbuf(bs * m_parameters.channels, 0.f); + auto outbuf = inbuf; + + int incount = m_inResampler->resampleInterleaved + (outbuf.data(), bs, inbuf.data(), bs, getInRatio(), false); + m_inResampler->reset(); + + int outcount = m_outResampler->resampleInterleaved + (outbuf.data(), bs, inbuf.data(), bs, getOutRatio(), false); + m_outResampler->reset(); + + m_initialResamplerDelays = { bs - incount, bs - outcount }; + + m_log.log(1, "R3LiveShifter::measureResamplerDelay: inRatio, outRatio ", getInRatio(), getOutRatio()); + m_log.log(1, "R3LiveShifter::measureResamplerDelay: measured delays ", m_initialResamplerDelays.first, m_initialResamplerDelays.second); +} + +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 inDelay = getWindowSourceSize() + m_initialResamplerDelays.first; + int outDelay = int(floor(inDelay * getOutRatio())) + m_initialResamplerDelays.second; + + int total = outDelay; + int bs = getBlockSize(); + if (m_pitchScale > 1.0) { + total += bs * (m_pitchScale - 1.0); + } else if (m_pitchScale < 1.0) { + total -= bs * (1.0 / m_pitchScale - 1.0); + } + + m_log.log(2, "R3LiveShifter::getStartDelay: inDelay, outDelay", inDelay, outDelay); + m_log.log(1, "R3LiveShifter::getStartDelay", total); + return total; +} + +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(); + } + + measureResamplerDelay(); +} + +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 shift 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; + if (m_firstProcess) { + pad = getWindowSourceSize(); + if (m_pitchScale > 1.0) { + pad = int(ceil(pad * m_pitchScale)); + } + 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 = getOutRatio(); + int requiredInOutbuf = int(ceil(incount / outRatio)); + generate(requiredInOutbuf); + + int got = readOut(output, incount); + + 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 = getInRatio(); + 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); + + if (m_firstProcess) { + int expected = floor(incount * inRatio); + if (resampleOutput < expected) { + m_log.log(2, "R3LiveShifter::readIn: resampler left us short on first process, pre-padding output: expected and obtained", expected, resampleOutput); + for (int c = 0; c < m_parameters.channels; ++c) { + m_channelData[c]->inbuf->zero(expected - 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 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(2, "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) +{ + double outRatio = getOutRatio(); + m_log.log(2, "R3LiveShifter::readOut: outcount and ratio", outcount, outRatio); + + int resampledCount = 0; + bool fillingTail = false; + + while (resampledCount < outcount) { + + int fromOutbuf; + + if (fillingTail) { + fromOutbuf = 1; + } else { + fromOutbuf = int(floor(outcount / outRatio)); + if (fromOutbuf == 0) { + fromOutbuf = 1; + } + } + + m_log.log(2, "R3LiveShifter::readOut: fillingTail and fromOutbuf", fillingTail, fromOutbuf); + + int got = fromOutbuf; + + for (int c = 0; c < m_parameters.channels; ++c) { + auto &cd = m_channelData.at(c); + int available = cd->outbuf->getReadSpace(); + int gotHere = cd->outbuf->read + (cd->resampled.data(), std::min(got, available)); + 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_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()); + + for (int c = 0; c < m_parameters.channels; ++c) { + auto &cd = m_channelData.at(c); + m_channelAssembly.resampled[c] = cd->resampled.data(); + m_channelAssembly.mixdown[c] = output[c] + resampledCount; + } + + auto resampledHere = m_outResampler->resample + (m_channelAssembly.mixdown.data(), + outcount - resampledCount, + m_channelAssembly.resampled.data(), + got, + outRatio, + false); + + m_log.log(2, "R3LiveShifter::readOut: resampledHere", resampledHere); + + if (got == 0 && resampledHere == 0) { + m_log.log(2, "R3LiveShifter::readOut: made no progress, finishing"); + break; + } + + resampledCount += resampledHere; + + fillingTail = true; + } + + 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; + } + } + + m_log.log(2, "R3LiveShifter::readOut: resampled to", resampledCount); + + if (resampledCount < outcount) { + if (m_firstProcess) { + m_log.log(2, "R3LiveShifter::readOut: resampler left us short on first process, pre-padding output: expected and obtained", outcount, resampledCount); + int prepad = outcount - resampledCount; + for (int c = 0; c < m_parameters.channels; ++c) { + v_move(output[c] + prepad, output[c], resampledCount); + v_zero(output[c], prepad); + } + resampledCount = outcount; + } else { + m_log.log(0, "R3LiveShifter::readOut: WARNING: Failed to obtain enough samples from resampler", resampledCount, outcount); + } + } + + m_log.log(2, "R3LiveShifter::readOut: returning", resampledCount); + + 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; + + m_log.log(2, "R3LiveShifter::synthesiseChannel: outhop and winscale", outhop, winscale); + + // 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..247416bb --- /dev/null +++ b/src/finer/R3LiveShifter.h @@ -0,0 +1,423 @@ +/* -*- 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-2024 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, double rate) : + // commented values are results when rate = 44100 or 48000 + minInhop(1) + { + 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; + std::pair m_initialResamplerDelays; + bool m_useReadahead; + int m_prevInhop; + int m_prevOuthop; + 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); + + void createResamplers(); + void measureResamplerDelay(); + 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 true; + } + + double getInRatio() const { + if (m_pitchScale > 1.0) { + return 1.0 / m_pitchScale; + } else { + return 1.0; + } + } + + double getOutRatio() const { + if (m_pitchScale < 1.0) { + return 1.0 / m_pitchScale; + } else { + return 1.0; + } + } + + 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/finer/R3Stretcher.cpp b/src/finer/R3Stretcher.cpp index cac3bba1..ed7d56b8 100644 --- a/src/finer/R3Stretcher.cpp +++ b/src/finer/R3Stretcher.cpp @@ -299,6 +299,10 @@ R3Stretcher::createResampler() resamplerParameters.dynamism = Resampler::RatioMostlyFixed; resamplerParameters.ratioChange = Resampler::SuddenRatioChange; } + + int debug = m_log.getDebugLevel(); + if (debug > 0) --debug; + resamplerParameters.debugLevel = debug; m_resampler = std::unique_ptr (new Resampler(resamplerParameters, m_parameters.channels)); diff --git a/src/test/TestLiveShifter.cpp b/src/test/TestLiveShifter.cpp new file mode 100644 index 00000000..5128329a --- /dev/null +++ b/src/test/TestLiveShifter.cpp @@ -0,0 +1,298 @@ +/* -*- 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-2024 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 +#include + +#include + +using namespace RubberBand; + +using std::vector; +using std::cerr; +using std::endl; +using std::string; +using std::ofstream; + +namespace tt = boost::test_tools; + +BOOST_AUTO_TEST_SUITE(TestLiveShifter) + +static void dumpTo(string basename, + const vector &data) +{ + string dir = "/tmp"; + string filename = dir + "/" + basename + ".csv"; + ofstream file(filename, std::ios::out | std::ios::binary); + if (!file) { + cerr << "dumpTo: failed to open file \"" << filename << "\" for writing" << endl; + return; + } + file << "sample,V" << endl; + for (int i = 0; i < int(data.size()); ++i) { + file << i << "," << data[i] << endl; + } +} + +static void dump(string prefix, + const vector &in, + const vector &out, + const vector &expected, + int delay) +{ + cerr << "dump: delay reported as " << delay << endl; + + if (prefix != "") { + prefix += "-"; + } + + dumpTo(prefix + "in", in); + dumpTo(prefix + "out", out); + dumpTo(prefix + "expected", expected); + + vector shifted; + vector diff; + for (int i = 0; i + delay < int(out.size()); ++i) { + shifted.push_back(out[i + delay]); + diff.push_back(out[i + delay] - expected[i]); + } + dumpTo(prefix + "shifted", shifted); + dumpTo(prefix + "diff", diff); +} + +static void check_sinusoid_unchanged(int n, int rate, float freq, + RubberBandLiveShifter::Options options, + string debugPrefix = {}) +{ + bool printDebug = (debugPrefix != ""); + + if (printDebug) { + RubberBandLiveShifter::setDefaultDebugLevel(2); + } + + RubberBandLiveShifter shifter(rate, 1, options); + + int blocksize = shifter.getBlockSize(); + BOOST_TEST(blocksize == 512); + + n = (n / blocksize + 1) * blocksize; + + vector in(n), out(n); + for (int i = 0; i < n; ++i) { + in[i] = 0.5f * 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, + // so we check that with a threshold of 0.1; after that we expect + // better precision. + + int slackpart = 2048; + float slackeps = 1.0e-1f; + float eps = 1.0e-3f; + +#ifdef USE_BQRESAMPLER + eps = 1.0e-2f; +#endif + + for (int i = 0; i < slackpart; ++i) { + float fin = in[i]; + float fout = out[delay + i]; + float err = fabsf(fin - fout); + if (err > slackeps) { + cerr << "Error at index " << i << " exceeds slack eps " + << slackeps << ": output " << fout << " - input " + << fin << " = " << fout - fin << endl; + BOOST_TEST(err < eps); + break; + } + } + + for (int i = slackpart; i < n - delay; ++i) { + float fin = in[i]; + float fout = out[delay + i]; + float err = fabsf(fin - fout); + if (err > eps) { + cerr << "Error at index " << i << " exceeds tight eps " + << eps << ": output " << fout << " - input " + << fin << " = " << fout - fin << endl; + BOOST_TEST(err < eps); + break; + } + } + + if (printDebug) { + RubberBandLiveShifter::setDefaultDebugLevel(0); + dump(debugPrefix, in, out, in, delay); + } +} + +static void check_sinusoid_shifted(int n, int rate, float freq, float shift, + RubberBandLiveShifter::Options options, + string debugPrefix = {}) +{ + bool printDebug = (debugPrefix != ""); + + if (printDebug) { + RubberBandLiveShifter::setDefaultDebugLevel(2); + } + + RubberBandLiveShifter shifter(rate, 1, options); + + shifter.setPitchScale(shift); + + int blocksize = shifter.getBlockSize(); + BOOST_TEST(blocksize == 512); + + n = (n / blocksize + 1) * blocksize; + + vector in(n), out(n), expected(n); + int endpoint = n; + if (endpoint > 20000) endpoint -= 10000; + for (int i = 0; i < n; ++i) { + float value = 0.5f * sinf(float(i) * freq * M_PI * 2.f / float(rate)); + if (i > endpoint && value > 0.f && in[i-1] <= 0.f) break; + in[i] = value; + expected[i] = 0.5f * sinf(float(i) * freq * shift * 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 reportedDelay = shifter.getStartDelay(); + int slackpart = 2048; + + double lastCrossing = -1; + + double eps = 2.0; + + int i0 = reportedDelay + slackpart; + int i1 = endpoint; + + for (int i = i0; i < i1; ++i) { + if (out[i-1] < 0.f && out[i] >= 0.f) { + double crossing = (i-1) + (out[i-1] / (out[i-1] - out[i])); + if (lastCrossing >= 0) { + double f = rate / (crossing - lastCrossing); + double diff = freq * shift - f; + double ratio = f / (freq * shift); + double cents = 1200.0 * (log(ratio)/log(2.0)); + if (fabs(cents) >= eps) { + cerr << "i = " << i << " (from " << i0 << " to " << i1 << ", out[i-1] = " << out[i-1] << ", out[i] = " << out[i] << "), f = " << f << ", in freq = " << freq << ", out freq = " << freq * shift << ", ratio = " << ratio << ", cents = " << cents << ", diff = " << diff << ", eps = " << eps << ", shift = " << shift << ", factor = " << fabs(cents)/eps << endl; + BOOST_TEST(fabs(cents) < eps); + } + } + lastCrossing = crossing; + } + } + + if (printDebug) { + RubberBandLiveShifter::setDefaultDebugLevel(0); + dump(debugPrefix, in, out, expected, reportedDelay); + } +} + +BOOST_AUTO_TEST_CASE(sinusoid_unchanged) +{ + int n = 20000; + + // delay = 2112, correct + + check_sinusoid_unchanged(n, 44100, 440.f, 0); + check_sinusoid_unchanged(n, 48000, 260.f, 0); +} + +BOOST_AUTO_TEST_CASE(sinusoid_down_octave_440) +{ + // Checked: delay = 3648, seems ok + + int n = 30000; + check_sinusoid_shifted(n, 44100, 440.f, 0.5f, 0); +} + +BOOST_AUTO_TEST_CASE(sinusoid_down_octave_260) +{ + // Checked: delay = 3648, correct + + int n = 30000; + check_sinusoid_shifted(n, 48000, 260.f, 0.5f, 0); +} + +BOOST_AUTO_TEST_CASE(sinusoid_down_2octave) +{ + // Checked: delay = 6784, sound + + int n = 30000; + check_sinusoid_shifted(n, 44100, 440.f, 0.25f, 0); + check_sinusoid_shifted(n, 48000, 260.f, 0.25f, 0); +} + +BOOST_AUTO_TEST_CASE(sinusoid_up_octave_440) +{ + // Checked: delay = 2879, correct + + int n = 30000; + check_sinusoid_shifted(n, 44100, 440.f, 2.0f, 0); +} + +BOOST_AUTO_TEST_CASE(sinusoid_up_octave_260) +{ + // Checked: delay = 2879, seems ok + + int n = 30000; + check_sinusoid_shifted(n, 44100, 260.f, 2.0f, 0); +} + +BOOST_AUTO_TEST_CASE(sinusoid_down_0_99) +{ + int n = 30000; + check_sinusoid_shifted(n, 44100, 440.f, 0.99f, 0); +} + +BOOST_AUTO_TEST_CASE(sinusoid_up_1_01) +{ + int n = 30000; + check_sinusoid_shifted(n, 44100, 440.f, 1.01f, 0); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/TestStretcher.cpp b/src/test/TestStretcher.cpp index a2352c46..aa195b8c 100644 --- a/src/test/TestStretcher.cpp +++ b/src/test/TestStretcher.cpp @@ -46,10 +46,10 @@ BOOST_AUTO_TEST_CASE(engine_version) { RubberBandStretcher s2(44100, 1, RubberBandStretcher::OptionEngineFaster); BOOST_TEST(s2.getEngineVersion() == 2); - BOOST_TEST(s2.getProcessSizeLimit() == 524288); + BOOST_TEST(s2.getProcessSizeLimit() == 524288u); RubberBandStretcher s3(44100, 1, RubberBandStretcher::OptionEngineFiner); BOOST_TEST(s3.getEngineVersion() == 3); - BOOST_TEST(s3.getProcessSizeLimit() == 524288); + BOOST_TEST(s3.getProcessSizeLimit() == 524288u); } BOOST_AUTO_TEST_CASE(sinusoid_unchanged_offline_faster) @@ -76,10 +76,10 @@ BOOST_AUTO_TEST_CASE(sinusoid_unchanged_offline_faster) stretcher.process(&inp, n, true); BOOST_TEST(stretcher.available() == n); - BOOST_TEST(stretcher.getStartDelay() == 0); // offline mode + BOOST_TEST(stretcher.getStartDelay() == 0u); // offline mode size_t got = stretcher.retrieve(&outp, n); - BOOST_TEST(got == n); + BOOST_TEST(got == size_t(n)); BOOST_TEST(stretcher.available() == -1); // We now have n samples of a simple sinusoid with stretch factor @@ -133,10 +133,10 @@ BOOST_AUTO_TEST_CASE(sinusoid_unchanged_offline_finer) stretcher.process(&inp, n, true); BOOST_TEST(stretcher.available() == n); - BOOST_TEST(stretcher.getStartDelay() == 0); // offline mode + BOOST_TEST(stretcher.getStartDelay() == 0u); // offline mode size_t got = stretcher.retrieve(&outp, n); - BOOST_TEST(got == n); + BOOST_TEST(got == size_t(n)); BOOST_TEST(stretcher.available() == -1); // The R3 engine is actually less precise than R2 here because of @@ -186,10 +186,10 @@ BOOST_AUTO_TEST_CASE(sinusoid_2x_offline_finer) stretcher.process(&inp, n, true); BOOST_TEST(stretcher.available() == n*2); - BOOST_TEST(stretcher.getStartDelay() == 0); // offline mode + BOOST_TEST(stretcher.getStartDelay() == 0u); // offline mode size_t got = stretcher.retrieve(&outp, n*2); - BOOST_TEST(got == n*2); + BOOST_TEST(got == size_t(n)*2); BOOST_TEST(stretcher.available() == -1); int period = -1; @@ -774,10 +774,10 @@ BOOST_AUTO_TEST_CASE(impulses_2x_offline_faster) stretcher.process(&inp, n, true); BOOST_TEST(stretcher.available() == n * 2); - BOOST_TEST(stretcher.getStartDelay() == 0); // offline mode + BOOST_TEST(stretcher.getStartDelay() == 0u); // offline mode size_t got = stretcher.retrieve(&outp, n * 2); - BOOST_TEST(got == n * 2); + BOOST_TEST(got == size_t(n) * 2); BOOST_TEST(stretcher.available() == -1); int peak0 = -1, peak1 = -1, peak2 = -1; @@ -843,10 +843,10 @@ BOOST_AUTO_TEST_CASE(impulses_2x_offline_finer) stretcher.process(&inp, n, true); BOOST_TEST(stretcher.available() == n * 2); - BOOST_TEST(stretcher.getStartDelay() == 0); // offline mode + BOOST_TEST(stretcher.getStartDelay() == 0u); // offline mode size_t got = stretcher.retrieve(&outp, n * 2); - BOOST_TEST(got == n * 2); + BOOST_TEST(got == size_t(n) * 2); BOOST_TEST(stretcher.available() == -1); int peak0 = -1, peak1 = -1, peak2 = -1; @@ -913,10 +913,10 @@ BOOST_AUTO_TEST_CASE(impulses_2x_5up_offline_finer) stretcher.process(&inp, n, true); BOOST_TEST(stretcher.available() == n * 2); - BOOST_TEST(stretcher.getStartDelay() == 0); // offline mode + BOOST_TEST(stretcher.getStartDelay() == 0u); // offline mode size_t got = stretcher.retrieve(&outp, n * 2); - BOOST_TEST(got == n * 2); + BOOST_TEST(got == size_t(n) * 2); BOOST_TEST(stretcher.available() == -1); int peak0 = -1, peak1 = -1, peak2 = -1; @@ -1424,7 +1424,7 @@ static void with_resets(RubberBandStretcher::Options options, stretcher->process(&inp, n, true); BOOST_TEST(stretcher->available() == nOut); - BOOST_TEST(stretcher->getStartDelay() == 0); // offline mode + BOOST_TEST(stretcher->getStartDelay() == 0u); // offline mode nActual = (int)stretcher->retrieve(&outp, nOut); BOOST_TEST(nActual == nOut);