|
| 1 | +#include <PluginProcessor.h> |
| 2 | +#include <catch2/catch_test_macros.hpp> |
| 3 | + |
| 4 | +#include "CodecControllers/OpusController.h" |
| 5 | +#include "parameterIds.h" |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <cmath> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +// OpusController buffers one full 20ms frame of host samples before it can encode/decode it |
| 12 | +// (see OpusController::processBlock), so that framing delay is the dominant, exactly-known |
| 13 | +// component of its reported latency. These tests cover both the pure frame-size math and the |
| 14 | +// end-to-end value MaimAudioProcessor reports to the host via getLatencySamples(). |
| 15 | + |
| 16 | +TEST_CASE ("opus latency is one 20ms frame at the nearest Opus-native rate", "[opuslatency]") |
| 17 | +{ |
| 18 | + // Exact matches to Opus's five native rates: 20ms of that exact rate. |
| 19 | + REQUIRE (OpusController::computeLatencySamples (8000.0) == 160); |
| 20 | + REQUIRE (OpusController::computeLatencySamples (12000.0) == 240); |
| 21 | + REQUIRE (OpusController::computeLatencySamples (16000.0) == 320); |
| 22 | + REQUIRE (OpusController::computeLatencySamples (24000.0) == 480); |
| 23 | + REQUIRE (OpusController::computeLatencySamples (48000.0) == 960); |
| 24 | + |
| 25 | + // 44.1kHz isn't an Opus-native rate, so it should snap to the nearest one (48000) before |
| 26 | + // computing the frame size - matching what OpusController::validate_samplerate() does. |
| 27 | + REQUIRE (OpusController::computeLatencySamples (44100.0) == 960); |
| 28 | +} |
| 29 | + |
| 30 | +TEST_CASE ("plugin reports opus's real latency instead of a hardcoded zero", "[opuslatency]") |
| 31 | +{ |
| 32 | + auto gui = juce::ScopedJuceInitialiser_GUI {}; |
| 33 | + |
| 34 | + MaimAudioProcessor testPlugin; |
| 35 | + auto& apvts = testPlugin.getValueTreeState(); |
| 36 | + *((juce::AudioParameterChoice*) apvts.getParameter (ENCODER_PARAM_ID)) = 2; // opus |
| 37 | + |
| 38 | + testPlugin.prepareToPlay (44100.0, 512); |
| 39 | + |
| 40 | + REQUIRE (testPlugin.getLatencySamples() == OpusController::computeLatencySamples (44100.0)); |
| 41 | + REQUIRE (testPlugin.getLatencySamples() > 0); |
| 42 | +} |
| 43 | + |
| 44 | +TEST_CASE ("plugin still reports blade/lame's latency correctly", "[opuslatency]") |
| 45 | +{ |
| 46 | + // Regression test: currentLatencySamples() used to have an if/if-else chain where the |
| 47 | + // second branch's else clause (meant only for opus) unconditionally overwrote whatever |
| 48 | + // the first branch had just set, so blade always reported 0 too. Guards against that. |
| 49 | + auto gui = juce::ScopedJuceInitialiser_GUI {}; |
| 50 | + |
| 51 | + MaimAudioProcessor bladePlugin; |
| 52 | + auto& bladeApvts = bladePlugin.getValueTreeState(); |
| 53 | + *((juce::AudioParameterChoice*) bladeApvts.getParameter (ENCODER_PARAM_ID)) = 0; // blade |
| 54 | + bladePlugin.prepareToPlay (44100.0, 512); |
| 55 | + REQUIRE (bladePlugin.getLatencySamples() == 2209); |
| 56 | + |
| 57 | + MaimAudioProcessor lamePlugin; |
| 58 | + auto& lameApvts = lamePlugin.getValueTreeState(); |
| 59 | + *((juce::AudioParameterChoice*) lameApvts.getParameter (ENCODER_PARAM_ID)) = 1; // lame |
| 60 | + lamePlugin.prepareToPlay (44100.0, 512); |
| 61 | + REQUIRE (lamePlugin.getLatencySamples() == 2880); |
| 62 | +} |
| 63 | + |
| 64 | +namespace { |
| 65 | + |
| 66 | +void processSilence (MaimAudioProcessor& plugin, int numSamples, int blockSize) |
| 67 | +{ |
| 68 | + int remaining = numSamples; |
| 69 | + while (remaining > 0) { |
| 70 | + int thisBlock = std::min (blockSize, remaining); |
| 71 | + juce::AudioBuffer<float> buffer (2, thisBlock); |
| 72 | + buffer.clear(); |
| 73 | + auto midi = juce::MidiBuffer(); |
| 74 | + plugin.processBlock (buffer, midi); |
| 75 | + remaining -= thisBlock; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace |
| 80 | + |
| 81 | +TEST_CASE ("measured latency roughly matches reported latency across encoders and samplerates", "[opuslatency]") |
| 82 | +{ |
| 83 | + auto gui = juce::ScopedJuceInitialiser_GUI {}; |
| 84 | + |
| 85 | + constexpr int blockSize = 512; |
| 86 | + constexpr int flushSamples = 5000; // a few thousand samples to let an encoder switch settle |
| 87 | + constexpr int silenceLeadIn = 2000; |
| 88 | + constexpr int burstLength = 200; |
| 89 | + constexpr int silenceTrailOut = 5000; |
| 90 | + constexpr int totalSamples = silenceLeadIn + burstLength + silenceTrailOut; |
| 91 | + constexpr float burstFrequency = 1000.0f; |
| 92 | + constexpr float burstAmplitude = 0.8f; |
| 93 | + constexpr float onsetThreshold = 0.05f; |
| 94 | + |
| 95 | + const std::vector<double> sampleRates {44100.0, 48000.0, 96000.0}; |
| 96 | + const std::vector<int> encoders {0, 1, 2}; // blade, lame, opus |
| 97 | + |
| 98 | + MaimAudioProcessor testPlugin; |
| 99 | + auto& apvts = testPlugin.getValueTreeState(); |
| 100 | + |
| 101 | + for (double sampleRate : sampleRates) { |
| 102 | + testPlugin.prepareToPlay (sampleRate, blockSize); |
| 103 | + |
| 104 | + std::vector<float> inputSignal (static_cast<size_t> (totalSamples), 0.0f); |
| 105 | + for (int i = 0; i < burstLength; ++i) { |
| 106 | + inputSignal[static_cast<size_t> (silenceLeadIn + i)] = |
| 107 | + burstAmplitude * std::sin (2.0f * juce::MathConstants<float>::pi * burstFrequency |
| 108 | + * static_cast<float> (i) / static_cast<float> (sampleRate)); |
| 109 | + } |
| 110 | + |
| 111 | + for (int encoder : encoders) { |
| 112 | + // Switching to a new encoder (mid-stream, on the same live plugin instance) can |
| 113 | + // trigger an internal crossfade/settle period - flush it out before measuring. |
| 114 | + *((juce::AudioParameterChoice*) apvts.getParameter (ENCODER_PARAM_ID)) = encoder; |
| 115 | + processSilence (testPlugin, flushSamples, blockSize); |
| 116 | + |
| 117 | + auto reportedLatency = testPlugin.getLatencySamples(); |
| 118 | + REQUIRE (reportedLatency > 0); |
| 119 | + |
| 120 | + std::vector<float> outputSignal; |
| 121 | + outputSignal.reserve (static_cast<size_t> (totalSamples)); |
| 122 | + |
| 123 | + int sampleIndex = 0; |
| 124 | + while (sampleIndex < totalSamples) { |
| 125 | + int thisBlock = std::min (blockSize, totalSamples - sampleIndex); |
| 126 | + juce::AudioBuffer<float> buffer (2, thisBlock); |
| 127 | + for (int s = 0; s < thisBlock; ++s) { |
| 128 | + float sample = inputSignal[static_cast<size_t> (sampleIndex + s)]; |
| 129 | + buffer.setSample (0, s, sample); |
| 130 | + buffer.setSample (1, s, sample); |
| 131 | + } |
| 132 | + auto midi = juce::MidiBuffer(); |
| 133 | + testPlugin.processBlock (buffer, midi); |
| 134 | + for (int s = 0; s < thisBlock; ++s) { |
| 135 | + outputSignal.push_back (buffer.getSample (0, s)); |
| 136 | + } |
| 137 | + sampleIndex += thisBlock; |
| 138 | + } |
| 139 | + |
| 140 | + // Measured latency = where the burst's onset actually shows up in the output, minus |
| 141 | + // where it started in the input. |
| 142 | + int measuredOnset = -1; |
| 143 | + for (int i = 0; i < totalSamples; ++i) { |
| 144 | + if (std::abs (outputSignal[static_cast<size_t> (i)]) > onsetThreshold) { |
| 145 | + measuredOnset = i; |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + REQUIRE (measuredOnset >= 0); |
| 150 | + |
| 151 | + int measuredLatency = measuredOnset - silenceLeadIn; |
| 152 | + int tolerance = std::max (300, static_cast<int> (reportedLatency * 0.5)); |
| 153 | + REQUIRE (std::abs (measuredLatency - reportedLatency) <= tolerance); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments