|
|
@@ -0,0 +1,742 @@
|
|
|
+// Round 190un (issue #381, chlantutztetr: "software freezes when adjusting the bpm of a project when a sample is warped"):
|
|
|
+// a 3:53 song at 48 kHz on the Playlist, Audio editor › Auto-warp (119 markers at 122.46 BPM), then a drag of the tempo
|
|
|
+// display: LYNX stopped responding. Each tempo step built the clip's channel again on the message thread, and that build
|
|
|
+// rendered the whole stretch there (core/TimeStretch's mapped stretch of the whole file: seconds per step, one step per
|
|
|
+// pixel of the drag). Now the live engine keeps the channel's instrument while the stretch renders on a worker thread
|
|
|
+// (core/StretchDefer.h): debounced, the older render cancelled by a newer one, and the channel built again when it is
|
|
|
+// ready; meanwhile the kept instrument reads its old stretch at the speed of the new tempo (FL plays its precomputed
|
|
|
+// stretch the same way until it restretches, REA_rodada3 §5.1).
|
|
|
+//
|
|
|
+// The check, through the real paths (AudioEngine::editSong + clipops::followTempo, what the tempo display's drag, wheel,
|
|
|
+// typed value and the tap tempo all call; the engine's timer; a stand-in audio device on its own thread):
|
|
|
+// A0. Auto-warp on the long song (keep pitch turned on): the edit returns at once, the first stretch renders off it;
|
|
|
+// A. before (stretchdefer off, the build of 0.1.32): one tempo step blocks the message thread for the whole render;
|
|
|
+// B. 20 tempo changes in 1 s while stopped: no call on the message thread takes 50 ms or more; at most two renders
|
|
|
+// start (the debounce), the final stretch is ready, the channel is built from it, and it is bit for bit a fresh render
|
|
|
+// at the final tempo;
|
|
|
+// C. the same while playing: no call takes 50 ms, the clip never goes silent (before, during and after the new stretch
|
|
|
+// is swapped in), and the final render is right; C2: without the resume (0.1.32) the clip went silent at the swap;
|
|
|
+// D. tempo automation while playing: nothing is built again (the tempo follows in real time, issue #187's hook);
|
|
|
+// E. the other cases on the same path: the file at 44.1 kHz, the Beats mode, Stretch without markers (Time set), a
|
|
|
+// Resample clip (nothing renders) and a Clip-launch clip, each with a tempo burst.
|
|
|
+
|
|
|
+#include "engine/AudioEngine.h"
|
|
|
+
|
|
|
+#include "core/AutoWarp.h"
|
|
|
+#include "core/ClipLauncher.h"
|
|
|
+#include "core/ClipOps.h"
|
|
|
+#include "core/StretchDefer.h"
|
|
|
+#include "core/TimeStretch.h"
|
|
|
+#include "core/WarpEdit.h"
|
|
|
+
|
|
|
+namespace lynx
|
|
|
+{
|
|
|
+namespace
|
|
|
+{
|
|
|
+constexpr int block190un = 512;
|
|
|
+constexpr double deviceRate190un = 48000.0;
|
|
|
+
|
|
|
+// the stand-in device: plays the engine from its own thread, in real time, and keeps what the engine wrote (left channel)
|
|
|
+struct TempoDevice final : juce::AudioIODevice, private juce::Thread
|
|
|
+{
|
|
|
+ TempoDevice() : juce::AudioIODevice ("Tempo test device", "Tempo stretch test"), juce::Thread ("tempo test device") {}
|
|
|
+ ~TempoDevice() override { close(); }
|
|
|
+
|
|
|
+ static inline std::vector<float>* capture = nullptr; // (sized by the test before Play; the device thread fills it)
|
|
|
+ static inline std::atomic<int> captured { 0 };
|
|
|
+ static inline std::atomic<bool> recording { false };
|
|
|
+
|
|
|
+ juce::StringArray getOutputChannelNames() override { return { "Out 1", "Out 2" }; }
|
|
|
+ juce::StringArray getInputChannelNames() override { return {}; }
|
|
|
+ juce::Array<double> getAvailableSampleRates() override { return { deviceRate190un }; }
|
|
|
+ juce::Array<int> getAvailableBufferSizes() override { return { block190un }; }
|
|
|
+ int getDefaultBufferSize() override { return block190un; }
|
|
|
+ juce::String open (const juce::BigInteger&, const juce::BigInteger& outs, double, int) override
|
|
|
+ {
|
|
|
+ close();
|
|
|
+ outsNow = outs;
|
|
|
+ outsNow.setRange (2, juce::jmax (0, outsNow.getHighestBit() - 1), false);
|
|
|
+ data.setSize (2, block190un);
|
|
|
+ opened = true;
|
|
|
+ startThread();
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ void close() override
|
|
|
+ {
|
|
|
+ stop();
|
|
|
+ stopThread (10000);
|
|
|
+ opened = false;
|
|
|
+ }
|
|
|
+ bool isOpen() override { return opened; }
|
|
|
+ void start (juce::AudioIODeviceCallback* cb) override
|
|
|
+ {
|
|
|
+ if (! opened || cb == nullptr)
|
|
|
+ return;
|
|
|
+ cb->audioDeviceAboutToStart (this);
|
|
|
+ const juce::ScopedLock sl (callbackLock);
|
|
|
+ callback = cb;
|
|
|
+ }
|
|
|
+ void stop() override
|
|
|
+ {
|
|
|
+ juce::AudioIODeviceCallback* old = nullptr;
|
|
|
+ {
|
|
|
+ const juce::ScopedLock sl (callbackLock);
|
|
|
+ old = std::exchange (callback, nullptr);
|
|
|
+ }
|
|
|
+ if (old != nullptr)
|
|
|
+ old->audioDeviceStopped();
|
|
|
+ }
|
|
|
+ bool isPlaying() override { return callback != nullptr; }
|
|
|
+ juce::String getLastError() override { return {}; }
|
|
|
+ int getCurrentBufferSizeSamples() override { return block190un; }
|
|
|
+ double getCurrentSampleRate() override { return deviceRate190un; }
|
|
|
+ int getCurrentBitDepth() override { return 32; }
|
|
|
+ juce::BigInteger getActiveOutputChannels() const override { return outsNow; }
|
|
|
+ juce::BigInteger getActiveInputChannels() const override { return {}; }
|
|
|
+ int getOutputLatencyInSamples() override { return block190un; }
|
|
|
+ int getInputLatencyInSamples() override { return 0; }
|
|
|
+
|
|
|
+private:
|
|
|
+ void run() override
|
|
|
+ {
|
|
|
+ const auto blockMs = 1000.0 * block190un / deviceRate190un;
|
|
|
+ auto next = juce::Time::getMillisecondCounterHiRes();
|
|
|
+ while (! threadShouldExit())
|
|
|
+ {
|
|
|
+ {
|
|
|
+ const juce::ScopedLock sl (callbackLock);
|
|
|
+ if (callback != nullptr)
|
|
|
+ {
|
|
|
+ callback->audioDeviceIOCallbackWithContext (nullptr, 0, data.getArrayOfWritePointers(), data.getNumChannels(), block190un, {});
|
|
|
+ if (recording.load() && capture != nullptr)
|
|
|
+ {
|
|
|
+ const auto at = captured.load();
|
|
|
+ const auto n = juce::jmin (block190un, (int) capture->size() - at);
|
|
|
+ if (n > 0)
|
|
|
+ {
|
|
|
+ std::copy (data.getReadPointer (0), data.getReadPointer (0) + n, capture->begin() + at);
|
|
|
+ captured.store (at + n);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // (real time: one block per block's duration, as a sound card asks)
|
|
|
+ next += blockMs;
|
|
|
+ const auto wait = next - juce::Time::getMillisecondCounterHiRes();
|
|
|
+ if (wait > 0.5)
|
|
|
+ juce::Thread::sleep ((int) wait);
|
|
|
+ else if (wait < -100.0)
|
|
|
+ next = juce::Time::getMillisecondCounterHiRes();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ juce::BigInteger outsNow;
|
|
|
+ juce::AudioBuffer<float> data;
|
|
|
+ juce::CriticalSection callbackLock;
|
|
|
+ juce::AudioIODeviceCallback* callback = nullptr;
|
|
|
+ bool opened = false;
|
|
|
+};
|
|
|
+
|
|
|
+struct TempoType final : juce::AudioIODeviceType
|
|
|
+{
|
|
|
+ TempoType() : juce::AudioIODeviceType ("Tempo stretch test") {}
|
|
|
+ void scanForDevices() override {}
|
|
|
+ juce::StringArray getDeviceNames (bool input) const override { return input ? juce::StringArray() : juce::StringArray ("Tempo test device"); }
|
|
|
+ int getDefaultDeviceIndex (bool input) const override { return input ? -1 : 0; }
|
|
|
+ int getIndexOfDevice (juce::AudioIODevice* d, bool input) const override { return d != nullptr && ! input ? 0 : -1; }
|
|
|
+ bool hasSeparateInputsAndOutputs() const override { return false; }
|
|
|
+ juce::AudioIODevice* createDevice (const juce::String& out, const juce::String&) override
|
|
|
+ {
|
|
|
+ return out == "Tempo test device" ? new TempoDevice() : nullptr;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// A song-like file: a kick on every beat at 122.46 BPM, a hat between them and a held two-note pad under them (never
|
|
|
+// silent), stereo 24-bit WAV; the same every time (a fixed noise sequence).
|
|
|
+bool writeSong (const juce::File& file, double seconds, double rate)
|
|
|
+{
|
|
|
+ const auto frames = (int) std::llround (seconds * rate);
|
|
|
+ juce::AudioBuffer<float> b (2, frames);
|
|
|
+ const auto beat = 60.0 / 122.46;
|
|
|
+ std::uint32_t noise = 190381u;
|
|
|
+ for (int i = 0; i < frames; ++i)
|
|
|
+ {
|
|
|
+ const auto t = (double) i / rate;
|
|
|
+ const auto inBeat = std::fmod (t, beat);
|
|
|
+ const auto inHalf = std::fmod (t + beat * 0.5, beat);
|
|
|
+ auto x = 0.12 * std::sin (2.0 * juce::MathConstants<double>::pi * 220.0 * t) + 0.08 * std::sin (2.0 * juce::MathConstants<double>::pi * 329.63 * t);
|
|
|
+ x += 0.6 * std::exp (-inBeat * 30.0) * std::sin (2.0 * juce::MathConstants<double>::pi * (55.0 + 90.0 * std::exp (-inBeat * 40.0)) * inBeat);
|
|
|
+ noise = noise * 1664525u + 1013904223u;
|
|
|
+ x += 0.25 * std::exp (-inHalf * 120.0) * ((double) (noise >> 8) / 8388608.0 - 1.0);
|
|
|
+ b.setSample (0, i, (float) x);
|
|
|
+ b.setSample (1, i, (float) (x * 0.9));
|
|
|
+ }
|
|
|
+ file.deleteFile();
|
|
|
+ juce::WavAudioFormat wav;
|
|
|
+ std::unique_ptr<juce::AudioFormatWriter> w (wav.createWriterFor (file.createOutputStream().release(), rate, 2, 24, {}, 0));
|
|
|
+ return w != nullptr && w->writeFromAudioSampleBuffer (b, 0, frames);
|
|
|
+}
|
|
|
+
|
|
|
+bool sameSamples (const SampleData* a, const SampleData* b)
|
|
|
+{
|
|
|
+ if (a == nullptr || b == nullptr || a->frames() != b->frames() || a->left.size() != b->left.size() || a->right.size() != b->right.size())
|
|
|
+ return false;
|
|
|
+ return std::memcmp (a->left.data(), b->left.data(), a->left.size() * sizeof (float)) == 0
|
|
|
+ && (a->right.empty() || std::memcmp (a->right.data(), b->right.data(), a->right.size() * sizeof (float)) == 0);
|
|
|
+}
|
|
|
+
|
|
|
+// the longest run of 10 ms windows whose level stays under −50 dBFS, in ms (`where`: the frame it starts at)
|
|
|
+double longestSilenceMs (const std::vector<float>& x, int from, int to, int* where = nullptr)
|
|
|
+{
|
|
|
+ const int window = (int) (deviceRate190un * 0.010);
|
|
|
+ double longest = 0.0, run = 0.0;
|
|
|
+ int runStart = 0;
|
|
|
+ for (int i = juce::jmax (0, from); i + window <= juce::jmin ((int) x.size(), to); i += window)
|
|
|
+ {
|
|
|
+ double sum = 0.0;
|
|
|
+ for (int k = 0; k < window; ++k)
|
|
|
+ sum += (double) x[(std::size_t) (i + k)] * x[(std::size_t) (i + k)];
|
|
|
+ const auto rms = std::sqrt (sum / window);
|
|
|
+ if (rms < 0.00316)
|
|
|
+ {
|
|
|
+ if (run == 0.0)
|
|
|
+ runStart = i;
|
|
|
+ run += 10.0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ run = 0.0;
|
|
|
+ if (run > longest)
|
|
|
+ {
|
|
|
+ longest = run;
|
|
|
+ if (where != nullptr)
|
|
|
+ *where = runStart;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return longest;
|
|
|
+}
|
|
|
+} // namespace
|
|
|
+
|
|
|
+juce::String AudioEngine::tempoStretch190unSelfTest (const std::function<void (const juce::String&)>& log, double seconds)
|
|
|
+{
|
|
|
+ auto say = [&log] (const juce::String& line) {
|
|
|
+ if (log)
|
|
|
+ log (line);
|
|
|
+ };
|
|
|
+ const auto folder = juce::File::getSpecialLocation (juce::File::tempDirectory)
|
|
|
+ .getChildFile ("lynx-tempo190un-" + juce::String::toHexString (juce::Random::getSystemRandom().nextInt64()));
|
|
|
+ folder.createDirectory();
|
|
|
+ struct Cleanup
|
|
|
+ {
|
|
|
+ juce::File f;
|
|
|
+ ~Cleanup()
|
|
|
+ {
|
|
|
+ f.deleteRecursively();
|
|
|
+ TempoDevice::capture = nullptr;
|
|
|
+ TempoDevice::recording = false;
|
|
|
+ stretchdefer::setEnabled (true);
|
|
|
+ stretchdefer::setResumeEnabled (true);
|
|
|
+ }
|
|
|
+ } cleanup { folder };
|
|
|
+
|
|
|
+ juce::StringArray errors;
|
|
|
+ int checks = 0;
|
|
|
+ auto check = [&] (const juce::String& what, bool ok, const juce::String& detail = {}) {
|
|
|
+ ++checks;
|
|
|
+ say ((ok ? "ok " : "FAIL ") + what + (detail.isNotEmpty() ? " (" + detail + ")" : juce::String()));
|
|
|
+ if (! ok)
|
|
|
+ errors.add (what + (detail.isNotEmpty() ? " (" + detail + ")" : juce::String()));
|
|
|
+ };
|
|
|
+ auto ms = [] (double v) { return juce::String (v, 1) + " ms"; };
|
|
|
+ auto now = [] { return juce::Time::getMillisecondCounterHiRes(); };
|
|
|
+
|
|
|
+ const auto long48 = folder.getChildFile ("song48k.wav"), long44 = folder.getChildFile ("song44k.wav");
|
|
|
+ const auto shortSeconds = juce::jmin (seconds, 75.0); // (over a minute: Auto-warp follows its bars, as on a song)
|
|
|
+ const auto short48 = folder.getChildFile ("loop48k.wav");
|
|
|
+ if (! writeSong (long48, seconds, 48000.0) || ! writeSong (long44, shortSeconds, 44100.0) || ! writeSong (short48, shortSeconds, 48000.0))
|
|
|
+ return "ERROR can't write the test songs in " + folder.getFullPathName();
|
|
|
+
|
|
|
+ // Auto-warp as the Audio editor runs it (AudioEditor::autoWarp: autowarp::analyse on the decoded file, then editWarp:
|
|
|
+ // ensureMarkers, the markers, keep pitch, syncLength)
|
|
|
+ SampleLibrary library;
|
|
|
+ auto warped = [&] (const juce::File& file, double fileSeconds, StretchMethod method, int markersMode, double& autoMs, int& markerCount) {
|
|
|
+ // markersMode: 0 auto-warp, 1 Time set without markers (FL's Stretch), 2 Resample with Time set (no keep pitch)
|
|
|
+ ChannelDef c;
|
|
|
+ c.instrument = InstrumentKind::audioClip;
|
|
|
+ c.samplePath = file.getFullPathName().toStdString();
|
|
|
+ c.name = file.getFileNameWithoutExtension().toStdString();
|
|
|
+ c.mixerTrack = 0;
|
|
|
+ c.sampler.stretchMethod = method;
|
|
|
+ autoMs = 0.0;
|
|
|
+ markerCount = 0;
|
|
|
+ if (markersMode == 0)
|
|
|
+ {
|
|
|
+ const auto data = library.load (file.getFullPathName());
|
|
|
+ if (data == nullptr)
|
|
|
+ return c;
|
|
|
+ autowarp::Options o;
|
|
|
+ o.songBpm = 122.0;
|
|
|
+ o.beatsPerBar = 4.0;
|
|
|
+ const auto t0 = juce::Time::getMillisecondCounterHiRes();
|
|
|
+ const auto result = autowarp::analyse (*data, o);
|
|
|
+ autoMs = juce::Time::getMillisecondCounterHiRes() - t0;
|
|
|
+ markerCount = (int) result.markers.size();
|
|
|
+ warpedit::ensureMarkers (c.sampler, fileSeconds, 122.0);
|
|
|
+ if (result.markers.size() >= 2)
|
|
|
+ c.sampler.warp = result.markers;
|
|
|
+ c.sampler.keepPitch = true;
|
|
|
+ warpedit::syncLength (c.sampler, fileSeconds);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ c.sampler.stretchBeats = (float) (fileSeconds * 122.0 / 60.0);
|
|
|
+ c.sampler.keepPitch = markersMode == 1;
|
|
|
+ }
|
|
|
+ return c;
|
|
|
+ };
|
|
|
+ auto songOf = [] (const ChannelDef& channel, bool grid) {
|
|
|
+ Song s;
|
|
|
+ s.bpm = 122.0;
|
|
|
+ s.mixer = Song::defaultMixer (2);
|
|
|
+ s.mixer[0].effects.clear();
|
|
|
+ s.channels.push_back (channel);
|
|
|
+ Clip ac;
|
|
|
+ ac.kind = ClipKind::audio;
|
|
|
+ ac.channel = 0;
|
|
|
+ ac.track = 0;
|
|
|
+ ac.length = (std::int64_t) std::ceil ((double) channel.sampler.stretchBeats * ppq);
|
|
|
+ if (grid)
|
|
|
+ {
|
|
|
+ launcher::setClip (s, 0, 0, ac);
|
|
|
+ s.clipGrid->globalQuant = LaunchQuant::none;
|
|
|
+ // (a long, empty Playlist under it: with none the song loops every bar, and that jump ends every voice)
|
|
|
+ auto& p = s.patterns.emplace_back();
|
|
|
+ p.lengthTicks = s.barTicks();
|
|
|
+ Clip pc;
|
|
|
+ pc.kind = ClipKind::pattern;
|
|
|
+ pc.pattern = 0;
|
|
|
+ pc.track = 5;
|
|
|
+ pc.length = 400 * s.barTicks();
|
|
|
+ s.playlist.push_back (pc);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ s.playlist.push_back (ac);
|
|
|
+ s.mode = PlayMode::song;
|
|
|
+ return s;
|
|
|
+ };
|
|
|
+
|
|
|
+ // an engine on the stand-in device, the song published and its first stretch made (setSong builds the rack)
|
|
|
+ struct Live
|
|
|
+ {
|
|
|
+ std::unique_ptr<AudioEngine> engine;
|
|
|
+ double firstBuildMs = 0.0;
|
|
|
+ };
|
|
|
+ auto start = [&] (const Song& song, bool defer) {
|
|
|
+ Live live;
|
|
|
+ live.engine = std::make_unique<AudioEngine> (false);
|
|
|
+ auto& engine = *live.engine;
|
|
|
+ engine.deferStretches = defer;
|
|
|
+ engine.player.setProcessor (&engine.graph);
|
|
|
+ engine.deviceManager.addAudioDeviceType (std::make_unique<TempoType>());
|
|
|
+ if (const auto e = engine.deviceManager.initialise (0, 2, nullptr, true); e.isNotEmpty())
|
|
|
+ {
|
|
|
+ errors.add ("open the stand-in device: " + e);
|
|
|
+ live.engine = nullptr;
|
|
|
+ return live;
|
|
|
+ }
|
|
|
+ engine.deviceManager.addAudioCallback (&engine);
|
|
|
+ const auto t0 = juce::Time::getMillisecondCounterHiRes();
|
|
|
+ engine.setSong (song);
|
|
|
+ live.firstBuildMs = juce::Time::getMillisecondCounterHiRes() - t0;
|
|
|
+ for (int i = 0; i < 5; ++i)
|
|
|
+ {
|
|
|
+ engine.timerCallback();
|
|
|
+ juce::Thread::sleep (5);
|
|
|
+ }
|
|
|
+ return live;
|
|
|
+ };
|
|
|
+ auto finish = [] (Live& live) {
|
|
|
+ if (live.engine == nullptr)
|
|
|
+ return;
|
|
|
+ auto& engine = *live.engine;
|
|
|
+ engine.stop();
|
|
|
+ engine.timerCallback();
|
|
|
+ engine.deviceManager.removeAudioCallback (&engine);
|
|
|
+ engine.deviceManager.closeAudioDevice();
|
|
|
+ live.engine = nullptr;
|
|
|
+ };
|
|
|
+
|
|
|
+ // 20 tempo changes in 1 s, as a drag of the tempo display makes them (TempoDisplay::setBpm: editSong + followTempo),
|
|
|
+ // the engine's timer running between them; then the message thread goes on (timer only) until the stretches settle.
|
|
|
+ // Returns the longest call on the message thread; `settleMs`: from the last change until the rack plays the new stretch.
|
|
|
+ struct Burst
|
|
|
+ {
|
|
|
+ double longestMs = 0.0, longestEditMs = 0.0, settleMs = -1.0, finalBpm = 0.0;
|
|
|
+ int publishes = 0;
|
|
|
+ };
|
|
|
+ auto burst = [&] (AudioEngine& engine, double from, double step, bool waitSettle, double settleLimitMs = 120000.0) {
|
|
|
+ Burst b;
|
|
|
+ const auto publishesBefore = engine.getPublishCount();
|
|
|
+ const auto t0 = now();
|
|
|
+ for (int k = 1; k <= 20; ++k)
|
|
|
+ {
|
|
|
+ const auto bpm = from + step * k;
|
|
|
+ const auto a = now();
|
|
|
+ engine.editSong ([bpm] (Song& s) { clipops::followTempo (s, bpm); });
|
|
|
+ const auto took = now() - a;
|
|
|
+ b.longestEditMs = juce::jmax (b.longestEditMs, took);
|
|
|
+ b.longestMs = juce::jmax (b.longestMs, took);
|
|
|
+ b.finalBpm = bpm;
|
|
|
+ // the rest of this step's 50 ms: the timer, as the message loop runs it (30 Hz)
|
|
|
+ while (now() - t0 < 50.0 * k)
|
|
|
+ {
|
|
|
+ const auto c = now();
|
|
|
+ engine.timerCallback();
|
|
|
+ b.longestMs = juce::jmax (b.longestMs, now() - c);
|
|
|
+ juce::Thread::sleep (2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const auto last = now();
|
|
|
+ if (waitSettle)
|
|
|
+ while (now() - last < settleLimitMs)
|
|
|
+ {
|
|
|
+ const auto c = now();
|
|
|
+ engine.timerCallback();
|
|
|
+ b.longestMs = juce::jmax (b.longestMs, now() - c);
|
|
|
+ if (engine.stretchesSettled())
|
|
|
+ {
|
|
|
+ b.settleMs = now() - last;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ juce::Thread::sleep (10);
|
|
|
+ }
|
|
|
+ b.publishes = (int) (engine.getPublishCount() - publishesBefore);
|
|
|
+ return b;
|
|
|
+ };
|
|
|
+ // the channel's stretch at the engine's tempo now: what the rack built (through the engine's cache: its time shows
|
|
|
+ // it was already made) against a fresh render with no cache
|
|
|
+ auto finalRender = [&] (AudioEngine& engine, const juce::String& what) {
|
|
|
+ const auto& channel = engine.getSong().channels[0];
|
|
|
+ const auto bpm = engine.getSong().bpm;
|
|
|
+ const auto t0 = now();
|
|
|
+ const auto cached = playedSample (channel, bpm, engine.rackFactories());
|
|
|
+ const auto cachedMs = now() - t0;
|
|
|
+ RackState::Factories fresh;
|
|
|
+ fresh.samples = [&library] (const std::string& path) { return library.load (juce::String::fromUTF8 (path.c_str())); };
|
|
|
+ fresh.stretchedSamples = [] (const std::string& path, double ratio, StretchMethod method, double semitones, const StretchMap& map,
|
|
|
+ const StretchOptions& options) -> std::shared_ptr<const SampleData> {
|
|
|
+ SampleLibrary own;
|
|
|
+ const auto original = own.load (juce::String::fromUTF8 (path.c_str()));
|
|
|
+ if (original == nullptr)
|
|
|
+ return nullptr;
|
|
|
+ if (! stretchBakesPitch (method, options.modes))
|
|
|
+ semitones = 0.0;
|
|
|
+ return map.empty() ? timeStretch (*original, ratio, method, semitones, options) : timeStretchMapped (*original, map, method, semitones, options);
|
|
|
+ };
|
|
|
+ fresh.stretchedSpans = [] (const std::string&, const std::string&, const std::shared_ptr<const SampleData>& span, double ratio, StretchMethod method,
|
|
|
+ double semitones, const StretchOptions& options) -> std::shared_ptr<const SampleData> {
|
|
|
+ return span != nullptr ? timeStretchSpan (*span, ratio, method, semitones, options) : nullptr;
|
|
|
+ };
|
|
|
+ const auto reference = playedSample (channel, bpm, fresh);
|
|
|
+ check (what + ": the rack's stretch was already made (no render on the message thread)", cachedMs < 50.0, ms (cachedMs));
|
|
|
+ check (what + ": it is bit for bit a fresh render at the final tempo (" + juce::String (bpm, 2) + " BPM)", sameSamples (cached.get(), reference.get()),
|
|
|
+ cached != nullptr && reference != nullptr ? juce::String (cached->frames()) + " / " + juce::String (reference->frames()) + " frames" : "missing");
|
|
|
+ check (what + ": the rack plays it (no channel waits for its stretch)",
|
|
|
+ engine.lastPublishedShape.staleStretch.empty()
|
|
|
+ || std::none_of (engine.lastPublishedShape.staleStretch.begin(), engine.lastPublishedShape.staleStretch.end(), [] (std::uint8_t s) { return s != 0; }));
|
|
|
+ };
|
|
|
+ auto counts = [] (AudioEngine& engine) { return engine.stretchRenderCounts(); };
|
|
|
+
|
|
|
+ // --- the reporter's song: 3:53 at 48 kHz, auto-warped -----------------------------------------------------------------
|
|
|
+ double autoMs = 0.0;
|
|
|
+ int markerCount = 0;
|
|
|
+ const auto main = warped (long48, seconds, StretchMethod::general, 0, autoMs, markerCount);
|
|
|
+ say ("info: " + juce::String (seconds, 0) + " s at 48 kHz, auto-warp " + juce::String (markerCount) + " markers in " + ms (autoMs) + ", Time "
|
|
|
+ + juce::String (main.sampler.stretchBeats, 2) + " beats");
|
|
|
+ check ("auto-warp locks the long song bar by bar", markerCount >= 20, juce::String (markerCount) + " markers");
|
|
|
+
|
|
|
+ // A0. Auto-warp itself (the reporter's step 2; first, so nothing of this file is cached yet): the clip did not keep its
|
|
|
+ // pitch; the edit that sets its markers (the Audio editor's editWarp) returns at once, the first stretch renders off
|
|
|
+ // the message thread
|
|
|
+ {
|
|
|
+ auto plain = main;
|
|
|
+ plain.sampler.keepPitch = false;
|
|
|
+ plain.sampler.stretchBeats = 0.0f;
|
|
|
+ plain.sampler.warp.clear();
|
|
|
+ auto song = songOf (plain, false);
|
|
|
+ song.playlist[0].length = (std::int64_t) std::ceil (seconds * 122.0 / 60.0 * ppq);
|
|
|
+ auto live = start (song, true);
|
|
|
+ if (live.engine != nullptr)
|
|
|
+ {
|
|
|
+ auto& engine = *live.engine;
|
|
|
+ const auto t0 = now();
|
|
|
+ engine.editSong ([&main] (Song& s) { s.channels[0].sampler = main.sampler; });
|
|
|
+ const auto took = now() - t0;
|
|
|
+ auto longest = took;
|
|
|
+ const auto last = now();
|
|
|
+ auto settled = -1.0;
|
|
|
+ while (now() - last < 120000.0)
|
|
|
+ {
|
|
|
+ const auto c = now();
|
|
|
+ engine.timerCallback();
|
|
|
+ longest = juce::jmax (longest, now() - c);
|
|
|
+ if (engine.stretchesSettled())
|
|
|
+ {
|
|
|
+ settled = now() - last;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ juce::Thread::sleep (10);
|
|
|
+ }
|
|
|
+ check ("Auto-warp on the long song: the edit returns at once, no call on the message thread takes 50 ms", longest < 50.0,
|
|
|
+ "the edit " + ms (took) + ", longest " + ms (longest) + "; its stretch played " + ms (settled) + " later");
|
|
|
+ if (settled >= 0.0)
|
|
|
+ finalRender (engine, "Auto-warp");
|
|
|
+ }
|
|
|
+ finish (live);
|
|
|
+ }
|
|
|
+
|
|
|
+ // A. before: one tempo step with the render in place (stretchdefer off = 0.1.32's build)
|
|
|
+ {
|
|
|
+ stretchdefer::setEnabled (false);
|
|
|
+ auto live = start (songOf (main, false), true);
|
|
|
+ if (live.engine != nullptr)
|
|
|
+ {
|
|
|
+ say ("info: the first build (the stretch at 122 BPM, made in place) took " + ms (live.firstBuildMs));
|
|
|
+ const auto t0 = now();
|
|
|
+ live.engine->editSong ([] (Song& s) { clipops::followTempo (s, 123.0); });
|
|
|
+ const auto took = now() - t0;
|
|
|
+ say ("info: before the fix one tempo step blocked the message thread " + ms (took) + " (a drag makes one per pixel)");
|
|
|
+ check ("before the fix (renders in place): a tempo step blocks the message thread (the freeze this round removes)", took >= 50.0, ms (took));
|
|
|
+ }
|
|
|
+ finish (live);
|
|
|
+ stretchdefer::setEnabled (true);
|
|
|
+ }
|
|
|
+
|
|
|
+ // B. stopped: 20 tempo changes in 1 s
|
|
|
+ {
|
|
|
+ auto live = start (songOf (main, false), true);
|
|
|
+ if (live.engine != nullptr)
|
|
|
+ {
|
|
|
+ auto& engine = *live.engine;
|
|
|
+ const auto b = burst (engine, 122.0, 0.4, true);
|
|
|
+ const auto n = counts (engine);
|
|
|
+ say ("info: stopped: " + juce::String (b.publishes) + " publishes, renders started " + juce::String (n.started) + ", cancelled "
|
|
|
+ + juce::String (n.cancelled) + ", kept " + juce::String (n.kept) + "; the new stretch played " + ms (b.settleMs) + " after the last change");
|
|
|
+ check ("stopped, 20 tempo changes in 1 s: no call on the message thread takes 50 ms", b.longestMs < 50.0,
|
|
|
+ "longest " + ms (b.longestMs) + ", longest tempo edit " + ms (b.longestEditMs));
|
|
|
+ check ("stopped: the debounce renders once for the whole drag (at most 2 started)", n.started >= 1 && n.started <= 2,
|
|
|
+ juce::String (n.started) + " started");
|
|
|
+ check ("stopped: the stretch for the final tempo is built into the rack", b.settleMs >= 0.0, ms (b.settleMs));
|
|
|
+ finalRender (engine, "stopped");
|
|
|
+ }
|
|
|
+ finish (live);
|
|
|
+ }
|
|
|
+
|
|
|
+ // C. playing: the same, the clip sounding all along
|
|
|
+ {
|
|
|
+ auto live = start (songOf (main, false), true);
|
|
|
+ if (live.engine != nullptr)
|
|
|
+ {
|
|
|
+ auto& engine = *live.engine;
|
|
|
+ std::vector<float> out ((std::size_t) (deviceRate190un * 90.0), 0.0f);
|
|
|
+ TempoDevice::capture = &out;
|
|
|
+ TempoDevice::captured = 0;
|
|
|
+ engine.stop();
|
|
|
+ engine.rewind();
|
|
|
+ engine.timerCallback();
|
|
|
+ TempoDevice::recording = true;
|
|
|
+ engine.startPlayback();
|
|
|
+ auto pump = [&] (double forMs, double* longest) {
|
|
|
+ const auto t0 = now();
|
|
|
+ while (now() - t0 < forMs)
|
|
|
+ {
|
|
|
+ const auto c = now();
|
|
|
+ engine.timerCallback();
|
|
|
+ if (longest != nullptr)
|
|
|
+ *longest = juce::jmax (*longest, now() - c);
|
|
|
+ juce::Thread::sleep (5);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ double longest = 0.0;
|
|
|
+ pump (1000.0, &longest);
|
|
|
+ const auto burstAt = TempoDevice::captured.load();
|
|
|
+ const auto b = burst (engine, 122.0, -0.35, true);
|
|
|
+ const auto settledAt = TempoDevice::captured.load();
|
|
|
+ pump (1500.0, &longest);
|
|
|
+ TempoDevice::recording = false;
|
|
|
+ const auto end = TempoDevice::captured.load();
|
|
|
+ const auto n = counts (engine);
|
|
|
+ const auto beforeGap = longestSilenceMs (out, 4800, burstAt);
|
|
|
+ const auto duringGap = longestSilenceMs (out, burstAt, settledAt);
|
|
|
+ const auto afterGap = longestSilenceMs (out, settledAt, end);
|
|
|
+ say ("info: playing: renders started " + juce::String (n.started) + ", cancelled " + juce::String (n.cancelled) + "; settled "
|
|
|
+ + ms (b.settleMs) + " after the last change; captured " + juce::String ((double) end / deviceRate190un, 2) + " s");
|
|
|
+ check ("playing, 20 tempo changes in 1 s: no call on the message thread takes 50 ms", juce::jmax (b.longestMs, longest) < 50.0,
|
|
|
+ "longest " + ms (juce::jmax (b.longestMs, longest)) + ", longest tempo edit " + ms (b.longestEditMs));
|
|
|
+ check ("playing: the new stretch is swapped in", b.settleMs >= 0.0, ms (b.settleMs));
|
|
|
+ check ("playing: the clip sounds before the changes", end > burstAt && beforeGap < 30.0, "longest silence " + ms (beforeGap));
|
|
|
+ check ("playing: the clip keeps sounding during the changes (the kept stretch at the new tempo)", duringGap < 30.0, "longest silence " + ms (duringGap));
|
|
|
+ check ("playing: the clip keeps sounding after the new stretch is swapped in", afterGap < 30.0 && end - settledAt > (int) deviceRate190un,
|
|
|
+ "longest silence " + ms (afterGap));
|
|
|
+ engine.stop();
|
|
|
+ engine.timerCallback();
|
|
|
+ finalRender (engine, "playing");
|
|
|
+ }
|
|
|
+ TempoDevice::capture = nullptr;
|
|
|
+ finish (live);
|
|
|
+ }
|
|
|
+
|
|
|
+ // C2. before the fix's resume (stretchdefer::setResumeEnabled (false)): the clip went silent once its channel was built
|
|
|
+ // from the new stretch while it played (the old instrument's voice went with it) — measured on a shorter file
|
|
|
+ {
|
|
|
+ double oAuto = 0.0;
|
|
|
+ int oMarkers = 0;
|
|
|
+ stretchdefer::setResumeEnabled (false);
|
|
|
+ auto live = start (songOf (warped (short48, shortSeconds, StretchMethod::general, 0, oAuto, oMarkers), false), true);
|
|
|
+ if (live.engine != nullptr)
|
|
|
+ {
|
|
|
+ auto& engine = *live.engine;
|
|
|
+ std::vector<float> out ((std::size_t) (deviceRate190un * 40.0), 0.0f);
|
|
|
+ TempoDevice::capture = &out;
|
|
|
+ TempoDevice::captured = 0;
|
|
|
+ engine.rewind();
|
|
|
+ TempoDevice::recording = true;
|
|
|
+ engine.startPlayback();
|
|
|
+ for (int i = 0; i < 100; ++i)
|
|
|
+ {
|
|
|
+ engine.timerCallback();
|
|
|
+ juce::Thread::sleep (5);
|
|
|
+ }
|
|
|
+ const auto b = burst (engine, 122.0, 0.3, true);
|
|
|
+ const auto settledAt = TempoDevice::captured.load();
|
|
|
+ const auto t0 = now();
|
|
|
+ while (now() - t0 < 1000.0)
|
|
|
+ {
|
|
|
+ engine.timerCallback();
|
|
|
+ juce::Thread::sleep (5);
|
|
|
+ }
|
|
|
+ TempoDevice::recording = false;
|
|
|
+ const auto gap = longestSilenceMs (out, settledAt, TempoDevice::captured.load());
|
|
|
+ say ("info: without the resume the clip was silent " + ms (gap) + " after its new stretch came in (" + ms (b.settleMs) + " after the changes)");
|
|
|
+ check ("before the fix's resume: the playing clip went silent when its channel was built from the new stretch", gap >= 500.0, ms (gap));
|
|
|
+ engine.stop();
|
|
|
+ }
|
|
|
+ TempoDevice::capture = nullptr;
|
|
|
+ finish (live);
|
|
|
+ stretchdefer::setResumeEnabled (true);
|
|
|
+ }
|
|
|
+
|
|
|
+ // D. tempo automation while playing: no rebuild at all (the tempo hook follows it in real time)
|
|
|
+ {
|
|
|
+ auto song = songOf (main, false);
|
|
|
+ Automation a;
|
|
|
+ a.name = "Tempo";
|
|
|
+ a.target = { AutomationTargetKind::tempo, 0 };
|
|
|
+ const auto bar = song.barTicks();
|
|
|
+ a.points = { { 0, valueFromTempo (122.0), 0.0f }, { 2 * bar, valueFromTempo (140.0), 0.0f }, { 4 * bar, valueFromTempo (110.0), 0.0f } };
|
|
|
+ song.automations.push_back (a);
|
|
|
+ Clip ac;
|
|
|
+ ac.kind = ClipKind::automation;
|
|
|
+ ac.automation = 0;
|
|
|
+ ac.track = 2;
|
|
|
+ ac.length = 4 * bar;
|
|
|
+ song.playlist.push_back (ac);
|
|
|
+ auto live = start (song, true);
|
|
|
+ if (live.engine != nullptr)
|
|
|
+ {
|
|
|
+ auto& engine = *live.engine;
|
|
|
+ engine.rewind();
|
|
|
+ engine.startPlayback();
|
|
|
+ const auto publishes = engine.getPublishCount();
|
|
|
+ double longest = 0.0;
|
|
|
+ const auto t0 = now();
|
|
|
+ while (now() - t0 < 2000.0)
|
|
|
+ {
|
|
|
+ const auto c = now();
|
|
|
+ engine.timerCallback();
|
|
|
+ longest = juce::jmax (longest, now() - c);
|
|
|
+ juce::Thread::sleep (5);
|
|
|
+ }
|
|
|
+ check ("tempo automation while playing: nothing is built again, the message thread never waits",
|
|
|
+ engine.getPublishCount() == publishes && longest < 50.0, juce::String ((int) (engine.getPublishCount() - publishes)) + " publishes, longest " + ms (longest));
|
|
|
+ engine.stop();
|
|
|
+ }
|
|
|
+ finish (live);
|
|
|
+ }
|
|
|
+
|
|
|
+ // E. the other stretched clips on the same path (a shorter file: the path is the same, the render shorter)
|
|
|
+ struct Other
|
|
|
+ {
|
|
|
+ const char* name;
|
|
|
+ juce::File file;
|
|
|
+ StretchMethod method;
|
|
|
+ int markersMode;
|
|
|
+ bool grid;
|
|
|
+ bool renders;
|
|
|
+ };
|
|
|
+ for (const auto& o : { Other { "44.1 kHz file, auto-warped", long44, StretchMethod::general, 0, false, true },
|
|
|
+ Other { "Beats mode, auto-warped", short48, StretchMethod::beats, 0, false, true },
|
|
|
+ Other { "Stretch with Time set, no markers", short48, StretchMethod::general, 1, false, true },
|
|
|
+ Other { "Resample with Time set (nothing renders)", short48, StretchMethod::general, 2, false, false },
|
|
|
+ Other { "Clip-launch clip, auto-warped", short48, StretchMethod::general, 0, true, true } })
|
|
|
+ {
|
|
|
+ double oAuto = 0.0;
|
|
|
+ int oMarkers = 0;
|
|
|
+ const auto channel = warped (o.file, shortSeconds, o.method, o.markersMode, oAuto, oMarkers);
|
|
|
+ auto live = start (songOf (channel, o.grid), true);
|
|
|
+ if (live.engine == nullptr)
|
|
|
+ continue;
|
|
|
+ auto& engine = *live.engine;
|
|
|
+ std::vector<float> out;
|
|
|
+ int launchedAt = 0;
|
|
|
+ if (o.grid) // (launched and sounding through the whole burst, and after its new stretch is swapped in)
|
|
|
+ {
|
|
|
+ out.assign ((std::size_t) (deviceRate190un * 60.0), 0.0f);
|
|
|
+ TempoDevice::capture = &out;
|
|
|
+ TempoDevice::captured = 0;
|
|
|
+ engine.rewind();
|
|
|
+ TempoDevice::recording = true;
|
|
|
+ engine.startPlayback();
|
|
|
+ launcher::Command launch;
|
|
|
+ launch.type = launcher::Command::Type::launch;
|
|
|
+ engine.launcherCommand (launch);
|
|
|
+ for (int i = 0; i < 50; ++i)
|
|
|
+ {
|
|
|
+ engine.timerCallback();
|
|
|
+ juce::Thread::sleep (10);
|
|
|
+ }
|
|
|
+ launchedAt = TempoDevice::captured.load();
|
|
|
+ }
|
|
|
+ const auto b = burst (engine, 122.0, 0.5, true);
|
|
|
+ check (juce::String (o.name) + ": 20 tempo changes in 1 s, no call on the message thread takes 50 ms", b.longestMs < 50.0,
|
|
|
+ "longest " + ms (b.longestMs) + ", settled " + ms (b.settleMs));
|
|
|
+ if (o.grid)
|
|
|
+ {
|
|
|
+ const auto settledAt = TempoDevice::captured.load();
|
|
|
+ const auto t0 = now();
|
|
|
+ while (now() - t0 < 1500.0)
|
|
|
+ {
|
|
|
+ engine.timerCallback();
|
|
|
+ juce::Thread::sleep (5);
|
|
|
+ }
|
|
|
+ TempoDevice::recording = false;
|
|
|
+ const auto end = TempoDevice::captured.load();
|
|
|
+ int at = -1;
|
|
|
+ const auto during = longestSilenceMs (out, launchedAt, settledAt, &at), after = longestSilenceMs (out, settledAt, end);
|
|
|
+ check (juce::String (o.name) + ": the launched clip keeps sounding during the changes and after its new stretch is swapped in",
|
|
|
+ launchedAt > 0 && during < 30.0 && after < 30.0 && end - settledAt > (int) deviceRate190un,
|
|
|
+ "longest silence " + ms (during) + " / " + ms (after) + "; launched by frame " + juce::String (launchedAt) + ", silence from "
|
|
|
+ + juce::String (at) + ", burst to " + juce::String (settledAt));
|
|
|
+ TempoDevice::capture = nullptr;
|
|
|
+ }
|
|
|
+ if (o.renders)
|
|
|
+ finalRender (engine, o.name);
|
|
|
+ else
|
|
|
+ check (juce::String (o.name) + ": no stretch render was asked for", counts (engine).requested == 0, juce::String (counts (engine).requested) + " requests");
|
|
|
+ engine.stop();
|
|
|
+ finish (live);
|
|
|
+ }
|
|
|
+
|
|
|
+ say ("checks: " + juce::String (checks) + ", failed: " + juce::String (errors.size()));
|
|
|
+ return errors.isEmpty() ? "OK " + juce::String (checks) + " checks" : "FAIL: " + errors.joinIntoString ("; ");
|
|
|
+}
|
|
|
+} // namespace lynx
|