diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2f184cf9c..63ef33c99 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,7 +14,7 @@ If you'd like to work on an [existing Issue](https://github.com/sdatkinson/Neura I would like to ask that you please try to give me a timeline for your work--I'd hate to have you duplicate work if I know that I'm going to e.g. get to it today and beat you to the punch. ## Testing -The model sample-rate adapter has standalone regression tests that do not require a plugin SDK: +The sample-rate adapter and cabinet loader have standalone regression tests that do not require a plugin SDK: ```bash cmake -S tests -B build-tests -DCMAKE_BUILD_TYPE=Release diff --git a/NeuralAmpModeler/NeuralAmpModeler.cpp b/NeuralAmpModeler/NeuralAmpModeler.cpp index e032e5c08..5c1436fd6 100644 --- a/NeuralAmpModeler/NeuralAmpModeler.cpp +++ b/NeuralAmpModeler/NeuralAmpModeler.cpp @@ -1,7 +1,9 @@ #include // std::clamp, std::min +#include #include // pow #include #include +#include #include #include "Colors.h" @@ -89,7 +91,7 @@ NeuralAmpModeler::NeuralAmpModeler(const InstanceInfo& info) GetParam(kNoiseGateActive)->InitBool("NoiseGateActive", true); GetParam(kEQActive)->InitBool("ToneStack", true); GetParam(kOutputMode)->InitEnum("OutputMode", 1, {"Raw", "Normalized", "Calibrated"}); // TODO DRY w/ control - GetParam(kIRToggle)->InitBool("IRToggle", true); + GetParam(kCabToggle)->InitBool("IRToggle", true); GetParam(kCalibrateInput)->InitBool(kCalibrateInputParamName.c_str(), kDefaultCalibrateInput); GetParam(kInputCalibrationLevel) ->InitDouble(kInputCalibrationLevelParamName.c_str(), kDefaultInputCalibrationLevel, -60.0, 60.0, 0.1, "dBu"); @@ -125,8 +127,8 @@ NeuralAmpModeler::NeuralAmpModeler(const InstanceInfo& info) const auto rightArrowSVG = pGraphics->LoadSVG(RIGHT_ARROW_FN); const auto leftArrowSVG = pGraphics->LoadSVG(LEFT_ARROW_FN); const auto modelIconSVG = pGraphics->LoadSVG(MODEL_ICON_FN); - const auto irIconOnSVG = pGraphics->LoadSVG(IR_ICON_ON_FN); - const auto irIconOffSVG = pGraphics->LoadSVG(IR_ICON_OFF_FN); + const auto cabIconOnSVG = pGraphics->LoadSVG(IR_ICON_ON_FN); + const auto cabIconOffSVG = pGraphics->LoadSVG(IR_ICON_OFF_FN); const auto slimIconSVG = pGraphics->LoadSVG(SLIMMABLE_ICON_FN); const auto backgroundBitmap = pGraphics->LoadBitmap(BACKGROUND_FN); @@ -162,17 +164,17 @@ NeuralAmpModeler::NeuralAmpModeler(const InstanceInfo& info) noiseGateArea.GetVShifted(noiseGateArea.H()).SubRectVertical(2, 0).GetReducedFromTop(10.0f); const auto eqToggleArea = midKnobArea.GetVShifted(midKnobArea.H()).SubRectVertical(2, 0).GetReducedFromTop(10.0f); - // Areas for model and IR + // Areas for model and cab const auto fileWidth = 200.0f; const auto fileHeight = 30.0f; - const auto irYOffset = 38.0f; + const auto cabYOffset = 38.0f; const auto modelArea = contentArea.GetFromBottom((2.0f * fileHeight)).GetFromTop(fileHeight).GetMidHPadded(fileWidth).GetVShifted(-1); const auto slimIconArea = IRECT(modelArea.R + 6.f, modelArea.MH() - 14.f, modelArea.R + 6.f + 2.f * 28.f, modelArea.MH() + 14.f); const auto modelIconArea = modelArea.GetFromLeft(30).GetTranslated(-40, 10); - const auto irArea = modelArea.GetVShifted(irYOffset); - const auto irSwitchArea = irArea.GetFromLeft(30.0f).GetHShifted(-40.0f).GetScaledAboutCentre(0.6f); + const auto cabArea = modelArea.GetVShifted(cabYOffset); + const auto cabSwitchArea = cabArea.GetFromLeft(30.0f).GetHShifted(-40.0f).GetScaledAboutCentre(0.6f); // Areas for meters const auto inputMeterArea = contentArea.GetFromLeft(30).GetHShifted(-20).GetMidVPadded(100).GetVShifted(-25); @@ -187,7 +189,7 @@ NeuralAmpModeler::NeuralAmpModeler(const InstanceInfo& info) { // Sets mNAMPath and mStagedNAM const std::string msg = _StageModel(fileName); - // TODO error messages like the IR loader. + // TODO error messages like the cab loader. if (msg.size()) { std::stringstream ss; @@ -198,19 +200,16 @@ NeuralAmpModeler::NeuralAmpModeler(const InstanceInfo& info) } }; - // IR loader button - auto loadIRCompletionHandler = [&](const WDL_String& fileName, const WDL_String& path) { + // Cab loader button + auto loadCabCompletionHandler = [&](const WDL_String& fileName, const WDL_String& path) { if (fileName.GetLength()) { - mIRPath = fileName; - const dsp::wav::LoadReturnCode retCode = _StageIR(fileName); - if (retCode != dsp::wav::LoadReturnCode::SUCCESS) + const std::string error = _StageCab(fileName); + if (!error.empty()) { std::stringstream message; - message << "Failed to load IR file " << fileName.Get() << ":\n"; - message << dsp::wav::GetMsgForLoadReturnCode(retCode); - - _ShowMessageBox(GetUI(), message.str().c_str(), "Failed to load IR!", kMB_OK); + message << "Failed to load cab file " << fileName.Get() << ":\n" << error; + _ShowMessageBox(GetUI(), message.str().c_str(), "Failed to load cab!", kMB_OK); } } }; @@ -222,10 +221,10 @@ NeuralAmpModeler::NeuralAmpModeler(const InstanceInfo& info) #ifdef NAM_PICK_DIRECTORY const std::string defaultNamFileString = "Select model directory..."; - const std::string defaultIRString = "Select IR directory..."; + const std::string defaultCabString = "Select cab directory..."; #else const std::string defaultNamFileString = "Select model..."; - const std::string defaultIRString = "Select IR..."; + const std::string defaultCabString = "Select cab..."; #endif // Getting started page listing additional resources const char* const getUrl = "https://www.neuralampmodeler.com/users#comp-marb84o5"; @@ -258,12 +257,12 @@ NeuralAmpModeler::NeuralAmpModeler(const InstanceInfo& info) ->SetAnimationEndActionFunction(showSlimOverlay) ->Hide(true); - pGraphics->AttachControl(new ISVGSwitchControl(irSwitchArea, {irIconOffSVG, irIconOnSVG}, kIRToggle)); + pGraphics->AttachControl(new ISVGSwitchControl(cabSwitchArea, {cabIconOffSVG, cabIconOnSVG}, kCabToggle)); pGraphics->AttachControl( - new NAMFileBrowserControl(irArea, kMsgTagClearIR, defaultIRString.c_str(), "wav", loadIRCompletionHandler, style, - fileSVG, crossSVG, leftArrowSVG, rightArrowSVG, fileBackgroundBitmap, globeSVG, - "Get IRs", getUrl), - kCtrlTagIRFileBrowser); + new NAMFileBrowserControl(cabArea, kMsgTagClearCab, defaultCabString.c_str(), "wav nam", loadCabCompletionHandler, + style, fileSVG, crossSVG, leftArrowSVG, rightArrowSVG, fileBackgroundBitmap, globeSVG, + "Get cabs", getUrl), + kCtrlTagCabFileBrowser); pGraphics->AttachControl( new NAMSwitchControl(ngToggleArea, kNoiseGateActive, "Noise Gate", style, switchHandleBitmap)); pGraphics->AttachControl(new NAMSwitchControl(eqToggleArea, kEQActive, "EQ", style, switchHandleBitmap)); @@ -371,9 +370,13 @@ void NeuralAmpModeler::ProcessBlock(iplug::sample** inputs, iplug::sample** outp ? mToneStack->Process(gateGainOutput, numChannelsInternal, nFrames) : gateGainOutput; - sample** irPointers = toneStackOutPointers; - if (mIR != nullptr && GetParam(kIRToggle)->Value()) - irPointers = mIR->Process(toneStackOutPointers, numChannelsInternal, numFrames); + sample** cabPointers = toneStackOutPointers; + sample* cabinetOutputPointers[]{mCabinetOutput.data()}; + if (mCab != nullptr && GetParam(kCabToggle)->Bool()) + { + mCab->process(toneStackOutPointers, cabinetOutputPointers, nFrames); + cabPointers = cabinetOutputPointers; + } // And the HPF for DC offset (Issue 271) const double highPassCutoffFreq = kDCBlockerFrequency; @@ -382,7 +385,7 @@ void NeuralAmpModeler::ProcessBlock(iplug::sample** inputs, iplug::sample** outp // const recursive_linear_filter::LowPassParams lowPassParams(sampleRate, lowPassCutoffFreq); mHighPass.SetParams(highPassParams); // mLowPass.SetParams(lowPassParams); - sample** hpfPointers = mHighPass.Process(irPointers, numChannelsInternal, numFrames); + sample** hpfPointers = mHighPass.Process(cabPointers, numChannelsInternal, numFrames); // sample** lpfPointers = mLowPass.Process(hpfPointers, numChannelsInternal, numFrames); // restore previous floating point state @@ -402,17 +405,12 @@ void NeuralAmpModeler::OnReset() const auto sampleRate = GetSampleRate(); const int maxBlockSize = GetBlockSize(); - // Tail is because the HPF DC blocker has a decay. - // 10 cycles should be enough to pass the VST3 tests checking tail behavior. - // I'm ignoring the model & IR, but it's not the end of the world. - const int tailCycles = 10; - SetTailSize(tailCycles * (int)(sampleRate / kDCBlockerFrequency)); mInputSender.Reset(sampleRate); mOutputSender.Reset(sampleRate); - // If there is a model or IR loaded, they need to be checked for resampling. - _ResetModelAndIR(sampleRate, GetBlockSize()); + // If there is a model or cab loaded, they need to be checked for resampling. + _ResetModelAndCab(sampleRate, GetBlockSize()); mToneStack->Reset(sampleRate, maxBlockSize); - _UpdateLatency(); + _UpdateLatencyAndTail(); } void NeuralAmpModeler::OnIdle() @@ -458,7 +456,7 @@ bool NeuralAmpModeler::SerializeState(IByteChunk& chunk) const // Model directory (don't serialize the model itself; we'll just load it again // when we unserialize) chunk.PutStr(mNAMPath.Get()); - chunk.PutStr(mIRPath.Get()); + chunk.PutStr(mCabPath.Get()); return SerializeParams(chunk); } @@ -493,11 +491,11 @@ void NeuralAmpModeler::OnUIOpen() SendControlMsgFromDelegate(kCtrlTagModelFileBrowser, kMsgTagLoadFailed); } - if (mIRPath.GetLength()) + if (mCabPath.GetLength()) { - SendControlMsgFromDelegate(kCtrlTagIRFileBrowser, kMsgTagLoadedIR, mIRPath.GetLength(), mIRPath.Get()); - if (mIR == nullptr && mStagedIR == nullptr) - SendControlMsgFromDelegate(kCtrlTagIRFileBrowser, kMsgTagLoadFailed); + SendControlMsgFromDelegate(kCtrlTagCabFileBrowser, kMsgTagLoadedCab, mCabPath.GetLength(), mCabPath.Get()); + if (mCab == nullptr && mStagedCab == nullptr) + SendControlMsgFromDelegate(kCtrlTagCabFileBrowser, kMsgTagLoadFailed); } if (mModel != nullptr) @@ -522,6 +520,10 @@ void NeuralAmpModeler::OnParamChange(int paramIdx) case kToneMid: mToneStack->SetParam("middle", GetParam(paramIdx)->Value()); break; case kToneTreble: mToneStack->SetParam("treble", GetParam(paramIdx)->Value()); break; case kSlim: _ApplySlimParamToLoadedNAMs(); break; + case kCabToggle: + _SetOutputGain(); + _UpdateLatencyAndTail(); + break; default: break; } } @@ -538,7 +540,7 @@ void NeuralAmpModeler::OnParamChangeUI(int paramIdx, EParamSource source) case kEQActive: pGraphics->ForControlInGroup("EQ_KNOBS", [active](IControl* pControl) { pControl->SetDisabled(!active); }); break; - case kIRToggle: pGraphics->GetControlWithTag(kCtrlTagIRFileBrowser)->SetDisabled(!active); break; + case kCabToggle: pGraphics->GetControlWithTag(kCtrlTagCabFileBrowser)->SetDisabled(!active); break; default: break; } } @@ -549,7 +551,7 @@ bool NeuralAmpModeler::OnMessage(int msgTag, int ctrlTag, int dataSize, const vo switch (msgTag) { case kMsgTagClearModel: mShouldRemoveModel = true; return true; - case kMsgTagClearIR: mShouldRemoveIR = true; return true; + case kMsgTagClearCab: mShouldRemoveCab = true; return true; case kMsgTagHighlightColor: { mHighLightColor.Set((const char*)pData); @@ -601,15 +603,18 @@ void NeuralAmpModeler::_ApplyDSPStaging() mNAMPath.Set(""); mShouldRemoveModel = false; mModelCleared = true; - _UpdateLatency(); + _UpdateLatencyAndTail(); _SetInputGain(); _SetOutputGain(); } - if (mShouldRemoveIR) + if (mShouldRemoveCab) { - mIR = nullptr; - mIRPath.Set(""); - mShouldRemoveIR = false; + mCab = nullptr; + mCabIsWav = false; + mCabPath.Set(""); + mShouldRemoveCab = false; + _UpdateLatencyAndTail(); + _SetOutputGain(); } // Move things from staged to live if (mStagedModel != nullptr) @@ -617,14 +622,17 @@ void NeuralAmpModeler::_ApplyDSPStaging() mModel = std::move(mStagedModel); mStagedModel = nullptr; mNewModelLoadedInDSP = true; - _UpdateLatency(); + _UpdateLatencyAndTail(); _SetInputGain(); _SetOutputGain(); } - if (mStagedIR != nullptr) + if (mStagedCab != nullptr) { - mIR = std::move(mStagedIR); - mStagedIR = nullptr; + mCab = std::move(mStagedCab); + mCabIsWav = mStagedCabIsWav; + mStagedCab = nullptr; + _UpdateLatencyAndTail(); + _SetOutputGain(); } } @@ -654,7 +662,7 @@ void NeuralAmpModeler::_FallbackDSP(iplug::sample** inputs, iplug::sample** outp mOutputArray[c][s] = mInputArray[c][s]; } -void NeuralAmpModeler::_ResetModelAndIR(const double sampleRate, const int maxBlockSize) +void NeuralAmpModeler::_ResetModelAndCab(const double sampleRate, const int maxBlockSize) { // Model if (mStagedModel != nullptr) @@ -666,25 +674,11 @@ void NeuralAmpModeler::_ResetModelAndIR(const double sampleRate, const int maxBl mModel->Reset(sampleRate, maxBlockSize); } - // IR - if (mStagedIR != nullptr) - { - const double irSampleRate = mStagedIR->GetSampleRate(); - if (irSampleRate != sampleRate) - { - const auto irData = mStagedIR->GetData(); - mStagedIR = std::make_unique(irData, sampleRate); - } - } - else if (mIR != nullptr) - { - const double irSampleRate = mIR->GetSampleRate(); - if (irSampleRate != sampleRate) - { - const auto irData = mIR->GetData(); - mStagedIR = std::make_unique(irData, sampleRate); - } - } + // Core retains the original model data and handles coefficient resampling on Reset. + if (mStagedCab != nullptr) + mStagedCab->Reset(sampleRate, maxBlockSize); + else if (mCab != nullptr) + mCab->Reset(sampleRate, maxBlockSize); } void NeuralAmpModeler::_SetInputGain() @@ -726,6 +720,12 @@ void NeuralAmpModeler::_SetOutputGain() default: break; } } + if (mCab != nullptr && mCabIsWav && GetParam(kCabToggle)->Bool()) + { + // Core compensates by sourceRate / hostRate when resampling IR coefficients. + // This remaining factor preserves the legacy -18 dB * 48000 / hostRate level. + gainDB += -18.0 + 20.0 * std::log10(48000.0 / mCab->GetEncapsulatedSampleRate()); + } mOutputGain = DBToAmp(gainDB); } @@ -740,6 +740,7 @@ void NeuralAmpModeler::_ApplySlimParamToLoadedNAMs() }; apply(mModel.get()); apply(mStagedModel.get()); + _UpdateLatencyAndTail(); } std::string NeuralAmpModeler::_StageModel(const WDL_String& modelPath) @@ -787,41 +788,32 @@ std::string NeuralAmpModeler::_StageModel(const WDL_String& modelPath) return ""; } -dsp::wav::LoadReturnCode NeuralAmpModeler::_StageIR(const WDL_String& irPath) +std::string NeuralAmpModeler::_StageCab(const WDL_String& cabPath) { - // FIXME it'd be better for the path to be "staged" as well. Just in case the - // path and the model got caught on opposite sides of the fence... - WDL_String previousIRPath = mIRPath; - const double sampleRate = GetSampleRate(); - dsp::wav::LoadReturnCode wavState = dsp::wav::LoadReturnCode::ERROR_OTHER; try { - mStagedIR = std::make_unique(irPath.Get(), sampleRate); - wavState = mStagedIR->GetWavState(); + // Finish loading before publishing the model, so a failed load preserves the current cab and path. + const auto path = std::filesystem::u8path(cabPath.Get()); + auto model = nam::get_dsp(path); + if (model->NumInputChannels() != 1 || model->NumOutputChannels() != 1) + throw std::runtime_error("Cab model must have 1 input and 1 output channel."); + auto cab = std::make_unique(std::move(model), GetSampleRate()); + cab->Reset(GetSampleRate(), GetBlockSize()); + auto extension = path.extension().string(); + std::transform(extension.begin(), extension.end(), extension.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + mStagedCabIsWav = extension == ".wav"; + mStagedCab = std::move(cab); + mCabPath = cabPath; + SendControlMsgFromDelegate(kCtrlTagCabFileBrowser, kMsgTagLoadedCab, mCabPath.GetLength(), mCabPath.Get()); + } + catch (const std::exception& error) + { + SendControlMsgFromDelegate(kCtrlTagCabFileBrowser, kMsgTagLoadFailed); + std::cerr << "Failed to load cab: " << error.what() << std::endl; + return error.what(); } - catch (std::runtime_error& e) - { - wavState = dsp::wav::LoadReturnCode::ERROR_OTHER; - std::cerr << "Caught unhandled exception while attempting to load IR:" << std::endl; - std::cerr << e.what() << std::endl; - } - - if (wavState == dsp::wav::LoadReturnCode::SUCCESS) - { - mIRPath = irPath; - SendControlMsgFromDelegate(kCtrlTagIRFileBrowser, kMsgTagLoadedIR, mIRPath.GetLength(), mIRPath.Get()); - } - else - { - if (mStagedIR != nullptr) - { - mStagedIR = nullptr; - } - mIRPath = previousIRPath; - SendControlMsgFromDelegate(kCtrlTagIRFileBrowser, kMsgTagLoadFailed); - } - - return wavState; + return ""; } size_t NeuralAmpModeler::_GetBufferNumChannels() const @@ -857,6 +849,7 @@ void NeuralAmpModeler::_PrepareBuffers(const size_t numChannels, const size_t nu } if (updateFrames) { + mCabinetOutput.resize(numFrames); for (auto c = 0; c < mInputArray.size(); c++) { mInputArray[c].resize(numFrames); @@ -963,14 +956,22 @@ void NeuralAmpModeler::_UpdateControlsFromModel() } } -void NeuralAmpModeler::_UpdateLatency() +void NeuralAmpModeler::_UpdateLatencyAndTail() { int latency = 0; if (mModel) { latency += mModel->GetLatency(); } - // Other things that add latency here... + const bool cabActive = mCab != nullptr && GetParam(kCabToggle)->Bool(); + if (cabActive) + latency += mCab->GetLatency(); + + // Both wrappers report their models' history lengths in host-rate samples. + // Allow another two seconds (ten cycles at 5 Hz) for the DC blocker. + const double tail = 2.0 * GetSampleRate() + (mModel ? mModel->GetPrewarmSamples() : 0) + + (cabActive ? mCab->GetPrewarmSamples() : 0); + SetTailSize(static_cast(std::min(tail, static_cast(std::numeric_limits::max())))); // Feels weird to have to do this. if (GetLatency() != latency) diff --git a/NeuralAmpModeler/NeuralAmpModeler.h b/NeuralAmpModeler/NeuralAmpModeler.h index 7e7c377bb..90a10b4de 100644 --- a/NeuralAmpModeler/NeuralAmpModeler.h +++ b/NeuralAmpModeler/NeuralAmpModeler.h @@ -1,9 +1,7 @@ #pragma once -#include "../AudioDSPTools/dsp/ImpulseResponse.h" #include "../AudioDSPTools/dsp/NoiseGate.h" #include "../AudioDSPTools/dsp/dsp.h" -#include "../AudioDSPTools/dsp/wav.h" #include "ResamplingNAM.h" #include "Colors.h" @@ -39,7 +37,7 @@ enum EParams // The rest is fine though. kNoiseGateActive, kEQActive, - kIRToggle, + kCabToggle, // Input calibration kCalibrateInput, kInputCalibrationLevel, @@ -53,7 +51,7 @@ const int numKnobs = 6; enum ECtrlTags { kCtrlTagModelFileBrowser = 0, - kCtrlTagIRFileBrowser, + kCtrlTagCabFileBrowser, kCtrlTagInputMeter, kCtrlTagOutputMeter, kCtrlTagSettingsBox, @@ -70,12 +68,12 @@ enum EMsgTags { // These tags are used from UI -> DSP kMsgTagClearModel = 0, - kMsgTagClearIR, + kMsgTagClearCab, kMsgTagHighlightColor, // The following tags are from DSP -> UI kMsgTagLoadFailed, kMsgTagLoadedModel, - kMsgTagLoadedIR, + kMsgTagLoadedCab, kNumMsgTags }; @@ -117,10 +115,9 @@ class NeuralAmpModeler final : public iplug::Plugin // Loads a NAM model and stores it to mStagedNAM // Returns an empty string on success, or an error message on failure. std::string _StageModel(const WDL_String& dspFile); - // Loads an IR and stores it to mStagedIR. - // Return status code so that error messages can be relayed if - // it wasn't successful. - dsp::wav::LoadReturnCode _StageIR(const WDL_String& irPath); + // Loads a WAV or NAM cab and stores it to mStagedCab. + // Returns an empty string on success, or an error message on failure. + std::string _StageCab(const WDL_String& cabPath); bool _HaveModel() const { return this->mModel != nullptr; }; // Prepare the input & output buffers @@ -136,8 +133,8 @@ class NeuralAmpModeler final : public iplug::Plugin // :param nChansOut: Out to external void _ProcessOutput(iplug::sample** inputs, iplug::sample** outputs, const size_t nFrames, const size_t nChansIn, const size_t nChansOut); - // Resetting for models and IRs, called by OnReset - void _ResetModelAndIR(const double sampleRate, const int maxBlockSize); + // Resetting for the model and cab, called by OnReset + void _ResetModelAndCab(const double sampleRate, const int maxBlockSize); void _SetInputGain(); void _SetOutputGain(); @@ -153,8 +150,8 @@ class NeuralAmpModeler final : public iplug::Plugin // Update all controls that depend on a model void _UpdateControlsFromModel(); - // Make sure that the latency is reported correctly. - void _UpdateLatency(); + // Report latency and tail length to the host. + void _UpdateLatencyAndTail(); // Update level meters // Called within ProcessBlock(). @@ -168,6 +165,8 @@ class NeuralAmpModeler final : public iplug::Plugin std::vector> mInputArray; // Output from NAM std::vector> mOutputArray; + // Separate cab output avoids requiring NAM architectures to support in-place processing. + std::vector mCabinetOutput; // Pointer versions iplug::sample** mInputPointers = nullptr; iplug::sample** mOutputPointers = nullptr; @@ -181,14 +180,16 @@ class NeuralAmpModeler final : public iplug::Plugin dsp::noise_gate::Gain mNoiseGateGain; // The model actually being used: std::unique_ptr mModel; - // And the IR - std::unique_ptr mIR; + // The cabinet, loaded from a WAV impulse response or NAM model. + std::unique_ptr mCab; + bool mCabIsWav = false; // Manages switching what DSP is being used. std::unique_ptr mStagedModel; - std::unique_ptr mStagedIR; + std::unique_ptr mStagedCab; + bool mStagedCabIsWav = false; // Flags to take away the modules at a safe time. std::atomic mShouldRemoveModel = false; - std::atomic mShouldRemoveIR = false; + std::atomic mShouldRemoveCab = false; std::atomic mNewModelLoadedInDSP = false; std::atomic mModelCleared = false; @@ -196,14 +197,14 @@ class NeuralAmpModeler final : public iplug::Plugin // Tone stack modules std::unique_ptr mToneStack; - // Post-IR filters + // Post-cab filters recursive_linear_filter::HighPass mHighPass; // recursive_linear_filter::LowPass mLowPass; // Path to model's config.json or model.nam WDL_String mNAMPath; - // Path to IR (.wav file) - WDL_String mIRPath; + // Path to the cab (.wav or .nam file). Keep IRPath in serialized state for compatibility. + WDL_String mCabPath; WDL_String mHighLightColor{PluginColors::NAM_THEMECOLOR.ToColorCode()}; diff --git a/NeuralAmpModeler/NeuralAmpModelerControls.h b/NeuralAmpModeler/NeuralAmpModelerControls.h index 4a42e7119..f48401c3e 100644 --- a/NeuralAmpModeler/NeuralAmpModelerControls.h +++ b/NeuralAmpModeler/NeuralAmpModelerControls.h @@ -1,7 +1,9 @@ #pragma once #include // std::round +#include #include // FILE, fclose +#include #include // std::stringstream #include // std::unordered_map #include "IControls.h" @@ -296,6 +298,48 @@ class NAMFileBrowserControl : public IDirBrowseControlBase void Draw(IGraphics& g) override { g.DrawFittedBitmap(mBitmap, mRECT); } + void SetupMenu() + { + if (std::strcmp(mExtension.Get(), "wav nam") != 0) + { + IDirBrowseControlBase::SetupMenu(); + return; + } + + // This iPlug2 version scans only one extension. Populate the cab's flat directory menu locally so the file + // picker, popup menu, and previous/next buttons all see both supported formats. + mFiles.Empty(true); + mItems.Empty(false); + mMainMenu.Clear(); + mSelectedItemIndex = -1; + for (int pathIndex = 0; pathIndex < mPaths.GetSize(); ++pathIndex) + { + std::error_code error; + const auto directory = std::filesystem::u8path(mPaths.Get(pathIndex)->Get()); + for (std::filesystem::directory_iterator entry(directory, error), end; !error && entry != end; + entry.increment(error)) + { + const auto& path = entry->path(); + const auto name = path.filename().u8string(); + std::error_code fileError; + auto extension = path.extension().string(); + std::transform(extension.begin(), extension.end(), extension.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + if (name.empty() || name[0] == '.' || !entry->is_regular_file(fileError) + || (extension != ".wav" && extension != ".nam")) + continue; + const auto label = path.stem().u8string(); + auto* item = new IPopupMenu::Item( + reinterpret_cast(label.c_str()), IPopupMenu::Item::kNoFlags, mFiles.GetSize()); + mMainMenu.AddItem(item, -2 /* sort alphabetically */); + const auto fullPath = path.u8string(); + mFiles.Add(new WDL_String(reinterpret_cast(fullPath.c_str()))); + } + } + for (int i = 0; i < mMainMenu.NItems(); ++i) + mItems.Add(mMainMenu.GetItem(i)); + } + void OnPopupMenuSelection(IPopupMenu* pSelectedMenu, int valIdx) override { if (pSelectedMenu) @@ -455,7 +499,7 @@ class NAMFileBrowserControl : public IDirBrowseControlBase } break; case kMsgTagLoadedModel: - case kMsgTagLoadedIR: + case kMsgTagLoadedCab: { WDL_String fileName, directory; fileName.Set(reinterpret_cast(pData)); diff --git a/NeuralAmpModeler/ResamplingNAM.h b/NeuralAmpModeler/ResamplingNAM.h index 7f87d2fe4..196ee066a 100644 --- a/NeuralAmpModeler/ResamplingNAM.h +++ b/NeuralAmpModeler/ResamplingNAM.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -65,10 +66,6 @@ class ResamplingNAM : public nam::DSP SetOutputLevel(mEncapsulated->GetOutputLevel()); } - // NOTE: prewarm samples doesn't mean anything--we can prewarm the encapsulated model as it likes and be good to - // go. - // _prewarm_samples = 0; - // And be ready int maxBlockSize = 2048; // Conservative Reset(expected_sample_rate, maxBlockSize); @@ -78,6 +75,16 @@ class ResamplingNAM : public nam::DSP void prewarm() override { mEncapsulated->prewarm(); }; + // Report the encapsulated model's history length in host-rate samples. + int GetPrewarmSamples() override + { + double samples = mEncapsulated->GetPrewarmSamples(); + if (NeedToResample()) + samples *= GetExpectedSampleRate() / GetEncapsulatedSampleRate(); + return static_cast(std::clamp(std::ceil(samples), 0.0, + static_cast(std::numeric_limits::max()))); + } + void process(NAM_SAMPLE** input, NAM_SAMPLE** output, const int num_frames) override { if (num_frames > mMaxExternalBlockSize) diff --git a/NeuralAmpModeler/Unserialization.cpp b/NeuralAmpModeler/Unserialization.cpp index 4dd3491a3..cd106ca48 100644 --- a/NeuralAmpModeler/Unserialization.cpp +++ b/NeuralAmpModeler/Unserialization.cpp @@ -55,19 +55,19 @@ void NeuralAmpModeler::_UnserializeApplyConfig(nlohmann::json& config) LEAVE_PARAMS_MUTEX mNAMPath.Set(static_cast(config["NAMPath"]).c_str()); - mIRPath.Set(static_cast(config["IRPath"]).c_str()); + mCabPath.Set(static_cast(config["IRPath"]).c_str()); if (mNAMPath.GetLength()) { _StageModel(mNAMPath); } - if (mIRPath.GetLength()) + if (mCabPath.GetLength()) { - _StageIR(mIRPath); + _StageCab(mCabPath); } } -// Unserialize NAM Path, IR path, then named keys +// Unserialize model path, cab path, then named keys int _UnserializePathsAndExpectedKeys(const iplug::IByteChunk& chunk, int startPos, nlohmann::json& config, std::vector& paramNames) { diff --git a/NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj b/NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj index bd349d97f..a0fa57211 100644 --- a/NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj +++ b/NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj @@ -410,10 +410,8 @@ - - @@ -523,7 +521,6 @@ - @@ -533,7 +530,6 @@ - diff --git a/NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj.filters b/NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj.filters index 0145dab12..1a46b27c8 100644 --- a/NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj.filters +++ b/NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj.filters @@ -83,18 +83,12 @@ dsp - - dsp - dsp dsp - - dsp - @@ -273,9 +267,6 @@ dsp - - dsp - dsp @@ -288,9 +279,6 @@ dsp - - dsp - IPlug\Extras diff --git a/NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj b/NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj index 4fe48ba7a..ac8bde722 100644 --- a/NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj +++ b/NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj @@ -320,7 +320,6 @@ - @@ -330,7 +329,6 @@ - @@ -399,10 +397,8 @@ - - diff --git a/NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj.filters b/NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj.filters index f55ded252..ba716ed08 100644 --- a/NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj.filters +++ b/NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj.filters @@ -101,18 +101,12 @@ dsp - - dsp - dsp dsp - - dsp - @@ -321,9 +315,6 @@ dsp - - dsp - dsp @@ -336,9 +327,6 @@ dsp - - dsp - IPlug\Extras diff --git a/NeuralAmpModeler/projects/NeuralAmpModeler-iOS.xcodeproj/project.pbxproj b/NeuralAmpModeler/projects/NeuralAmpModeler-iOS.xcodeproj/project.pbxproj index 0955b1922..18c5f028f 100644 --- a/NeuralAmpModeler/projects/NeuralAmpModeler-iOS.xcodeproj/project.pbxproj +++ b/NeuralAmpModeler/projects/NeuralAmpModeler-iOS.xcodeproj/project.pbxproj @@ -29,15 +29,11 @@ 4FB2269720A0D3A800614769 /* NeuralAmpModeler-iOS-LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4FB2269620A0D3A800614769 /* NeuralAmpModeler-iOS-LaunchScreen.storyboard */; }; 4FBDC94D29FFF143004FF203 /* Resample.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FBDC93329FFF143004FF203 /* Resample.h */; }; 4FBDC94E29FFF143004FF203 /* RecursiveLinearFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4FBDC93429FFF143004FF203 /* RecursiveLinearFilter.cpp */; }; - 4FBDC94F29FFF143004FF203 /* wav.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FBDC93529FFF143004FF203 /* wav.h */; }; 4FBDC95029FFF143004FF203 /* NoiseGate.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FBDC93629FFF143004FF203 /* NoiseGate.h */; }; 4FBDC95129FFF143004FF203 /* dsp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4FBDC93729FFF143004FF203 /* dsp.cpp */; }; 4FBDC95229FFF143004FF203 /* NoiseGate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4FBDC93829FFF143004FF203 /* NoiseGate.cpp */; }; - 4FBDC95329FFF143004FF203 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4FBDC93929FFF143004FF203 /* ImpulseResponse.cpp */; }; 4FBDC95429FFF143004FF203 /* RecursiveLinearFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FBDC93A29FFF143004FF203 /* RecursiveLinearFilter.h */; }; - 4FBDC95529FFF143004FF203 /* ImpulseResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FBDC93B29FFF143004FF203 /* ImpulseResponse.h */; }; 4FBDC95629FFF143004FF203 /* dsp.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FBDC93C29FFF143004FF203 /* dsp.h */; }; - 4FBDC95729FFF143004FF203 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4FBDC93D29FFF143004FF203 /* wav.cpp */; }; 4FBDC95829FFF143004FF203 /* util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4FBDC93F29FFF143004FF203 /* util.cpp */; }; 4FBDC95929FFF143004FF203 /* version.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FBDC94029FFF143004FF203 /* version.h */; }; 4FBDC95A29FFF143004FF203 /* dsp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4FBDC94129FFF143004FF203 /* dsp.cpp */; }; @@ -282,15 +278,11 @@ 4FBBD7F420B6ABE700A37DE2 /* config */ = {isa = PBXFileReference; lastKnownFileType = folder; name = config; path = ../config; sourceTree = ""; }; 4FBDC93329FFF143004FF203 /* Resample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Resample.h; sourceTree = ""; }; 4FBDC93429FFF143004FF203 /* RecursiveLinearFilter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RecursiveLinearFilter.cpp; sourceTree = ""; }; - 4FBDC93529FFF143004FF203 /* wav.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wav.h; sourceTree = ""; }; 4FBDC93629FFF143004FF203 /* NoiseGate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NoiseGate.h; sourceTree = ""; }; 4FBDC93729FFF143004FF203 /* dsp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = dsp.cpp; sourceTree = ""; }; 4FBDC93829FFF143004FF203 /* NoiseGate.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NoiseGate.cpp; sourceTree = ""; }; - 4FBDC93929FFF143004FF203 /* ImpulseResponse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ImpulseResponse.cpp; sourceTree = ""; }; 4FBDC93A29FFF143004FF203 /* RecursiveLinearFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecursiveLinearFilter.h; sourceTree = ""; }; - 4FBDC93B29FFF143004FF203 /* ImpulseResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImpulseResponse.h; sourceTree = ""; }; 4FBDC93C29FFF143004FF203 /* dsp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dsp.h; sourceTree = ""; }; - 4FBDC93D29FFF143004FF203 /* wav.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wav.cpp; sourceTree = ""; }; 4FBDC93F29FFF143004FF203 /* util.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = util.cpp; sourceTree = ""; }; 4FBDC94029FFF143004FF203 /* version.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = version.h; sourceTree = ""; }; 4FBDC94129FFF143004FF203 /* dsp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = dsp.cpp; sourceTree = ""; }; @@ -676,15 +668,11 @@ AA7C86032B43A42E00B5FB3A /* ResamplingContainer */, 4FBDC93729FFF143004FF203 /* dsp.cpp */, 4FBDC93C29FFF143004FF203 /* dsp.h */, - 4FBDC93929FFF143004FF203 /* ImpulseResponse.cpp */, - 4FBDC93B29FFF143004FF203 /* ImpulseResponse.h */, 4FBDC93829FFF143004FF203 /* NoiseGate.cpp */, 4FBDC93629FFF143004FF203 /* NoiseGate.h */, 4FBDC93429FFF143004FF203 /* RecursiveLinearFilter.cpp */, 4FBDC93A29FFF143004FF203 /* RecursiveLinearFilter.h */, 4FBDC93329FFF143004FF203 /* Resample.h */, - 4FBDC93D29FFF143004FF203 /* wav.cpp */, - 4FBDC93529FFF143004FF203 /* wav.h */, ); name = dsp; path = ../../AudioDSPTools/dsp; @@ -903,7 +891,6 @@ 4FBDC96229FFF143004FF203 /* activations.h in Headers */, 4FBDC95429FFF143004FF203 /* RecursiveLinearFilter.h in Headers */, 4FBDC95C29FFF143004FF203 /* lstm.h in Headers */, - 4FBDC95529FFF143004FF203 /* ImpulseResponse.h in Headers */, 4FBDC95029FFF143004FF203 /* NoiseGate.h in Headers */, 4FBDC96129FFF143004FF203 /* dsp.h in Headers */, AAB7BBB92CC4B8D6000B8B6E /* get_dsp.h in Headers */, @@ -912,7 +899,6 @@ C05510000000000000000105 /* nam_file.h in Headers */, C05510000000000000000107 /* sequential.h in Headers */, 4FBDC96029FFF143004FF203 /* util.h in Headers */, - 4FBDC94F29FFF143004FF203 /* wav.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1118,7 +1104,6 @@ 4FC6984B293BA6010076EC33 /* IGraphicsIOS_view.mm in Sources */, 4FBDC95129FFF143004FF203 /* dsp.cpp in Sources */, 4FC69851293BA8770076EC33 /* IGraphicsIOS.mm in Sources */, - 4FBDC95729FFF143004FF203 /* wav.cpp in Sources */, 4FBDC94E29FFF143004FF203 /* RecursiveLinearFilter.cpp in Sources */, 4FBDC95829FFF143004FF203 /* util.cpp in Sources */, 4FC69849293BA5F90076EC33 /* ITextEntryControl.cpp in Sources */, @@ -1141,7 +1126,6 @@ 4FC69841293BA5C40076EC33 /* IPlugAPIBase.cpp in Sources */, 4FC69842293BA5C50076EC33 /* IPlugProcessor.cpp in Sources */, 4FC69846293BA5F90076EC33 /* IGraphicsEditorDelegate.cpp in Sources */, - 4FBDC95329FFF143004FF203 /* ImpulseResponse.cpp in Sources */, 4FC69843293BA5C50076EC33 /* IPlugParameter.cpp in Sources */, 4FC69844293BA5C50076EC33 /* IPlugTimer.cpp in Sources */, AA8CA7772A452EF500F5BEF0 /* resample.cpp in Sources */, diff --git a/NeuralAmpModeler/projects/NeuralAmpModeler-macOS.xcodeproj/project.pbxproj b/NeuralAmpModeler/projects/NeuralAmpModeler-macOS.xcodeproj/project.pbxproj index d65d059d8..66eb8d24f 100644 --- a/NeuralAmpModeler/projects/NeuralAmpModeler-macOS.xcodeproj/project.pbxproj +++ b/NeuralAmpModeler/projects/NeuralAmpModeler-macOS.xcodeproj/project.pbxproj @@ -69,7 +69,6 @@ 4F2FB15B2A0047420027AB66 /* RecursiveLinearFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB13B2A0047420027AB66 /* RecursiveLinearFilter.cpp */; }; 4F2FB15C2A0047420027AB66 /* RecursiveLinearFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB13B2A0047420027AB66 /* RecursiveLinearFilter.cpp */; }; 4F2FB15D2A0047420027AB66 /* RecursiveLinearFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB13B2A0047420027AB66 /* RecursiveLinearFilter.cpp */; }; - 4F2FB15E2A0047420027AB66 /* wav.h in Headers */ = {isa = PBXBuildFile; fileRef = 4F2FB13C2A0047420027AB66 /* wav.h */; }; 4F2FB15F2A0047420027AB66 /* NoiseGate.h in Headers */ = {isa = PBXBuildFile; fileRef = 4F2FB13D2A0047420027AB66 /* NoiseGate.h */; }; 4F2FB1602A0047430027AB66 /* dsp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB13E2A0047420027AB66 /* dsp.cpp */; }; 4F2FB1622A0047430027AB66 /* dsp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB13E2A0047420027AB66 /* dsp.cpp */; }; @@ -87,25 +86,8 @@ 4F2FB16F2A0047430027AB66 /* NoiseGate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB13F2A0047420027AB66 /* NoiseGate.cpp */; }; 4F2FB1702A0047430027AB66 /* NoiseGate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB13F2A0047420027AB66 /* NoiseGate.cpp */; }; 4F2FB1712A0047430027AB66 /* NoiseGate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB13F2A0047420027AB66 /* NoiseGate.cpp */; }; - 4F2FB1722A0047430027AB66 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */; }; - 4F2FB1742A0047430027AB66 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */; }; - 4F2FB1752A0047430027AB66 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */; }; - 4F2FB1762A0047430027AB66 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */; }; - 4F2FB1772A0047430027AB66 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */; }; - 4F2FB1782A0047430027AB66 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */; }; - 4F2FB1792A0047430027AB66 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */; }; - 4F2FB17A2A0047430027AB66 /* ImpulseResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */; }; 4F2FB17B2A0047430027AB66 /* RecursiveLinearFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = 4F2FB1412A0047420027AB66 /* RecursiveLinearFilter.h */; }; - 4F2FB17C2A0047430027AB66 /* ImpulseResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 4F2FB1422A0047420027AB66 /* ImpulseResponse.h */; }; 4F2FB17D2A0047430027AB66 /* dsp.h in Headers */ = {isa = PBXBuildFile; fileRef = 4F2FB1432A0047420027AB66 /* dsp.h */; }; - 4F2FB17E2A0047430027AB66 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1442A0047420027AB66 /* wav.cpp */; }; - 4F2FB1802A0047430027AB66 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1442A0047420027AB66 /* wav.cpp */; }; - 4F2FB1812A0047430027AB66 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1442A0047420027AB66 /* wav.cpp */; }; - 4F2FB1822A0047430027AB66 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1442A0047420027AB66 /* wav.cpp */; }; - 4F2FB1832A0047430027AB66 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1442A0047420027AB66 /* wav.cpp */; }; - 4F2FB1842A0047430027AB66 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1442A0047420027AB66 /* wav.cpp */; }; - 4F2FB1852A0047430027AB66 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1442A0047420027AB66 /* wav.cpp */; }; - 4F2FB1862A0047430027AB66 /* wav.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1442A0047420027AB66 /* wav.cpp */; }; 4F2FB1872A0047430027AB66 /* util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1462A0047420027AB66 /* util.cpp */; }; 4F2FB1892A0047430027AB66 /* util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1462A0047420027AB66 /* util.cpp */; }; 4F2FB18A2A0047430027AB66 /* util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F2FB1462A0047420027AB66 /* util.cpp */; }; @@ -690,15 +672,11 @@ 4F2EA977203A50E9008E4850 /* IPlugAPP_dialog.cpp */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.cpp.cpp; name = IPlugAPP_dialog.cpp; path = ../../iPlug2/IPlug/APP/IPlugAPP_dialog.cpp; sourceTree = ""; tabWidth = 2; }; 4F2FB13A2A0047420027AB66 /* Resample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Resample.h; sourceTree = ""; }; 4F2FB13B2A0047420027AB66 /* RecursiveLinearFilter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RecursiveLinearFilter.cpp; sourceTree = ""; }; - 4F2FB13C2A0047420027AB66 /* wav.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wav.h; sourceTree = ""; }; 4F2FB13D2A0047420027AB66 /* NoiseGate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NoiseGate.h; sourceTree = ""; }; 4F2FB13E2A0047420027AB66 /* dsp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = dsp.cpp; sourceTree = ""; }; 4F2FB13F2A0047420027AB66 /* NoiseGate.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NoiseGate.cpp; sourceTree = ""; }; - 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ImpulseResponse.cpp; sourceTree = ""; }; 4F2FB1412A0047420027AB66 /* RecursiveLinearFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecursiveLinearFilter.h; sourceTree = ""; }; - 4F2FB1422A0047420027AB66 /* ImpulseResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImpulseResponse.h; sourceTree = ""; }; 4F2FB1432A0047420027AB66 /* dsp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dsp.h; sourceTree = ""; }; - 4F2FB1442A0047420027AB66 /* wav.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wav.cpp; sourceTree = ""; }; 4F2FB1462A0047420027AB66 /* util.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = util.cpp; sourceTree = ""; }; 4F2FB1472A0047420027AB66 /* version.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = version.h; sourceTree = ""; }; 4F2FB1482A0047420027AB66 /* dsp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = dsp.cpp; sourceTree = ""; }; @@ -1321,15 +1299,11 @@ AA7C85F12B439AC000B5FB3A /* ResamplingContainer */, 4F2FB13E2A0047420027AB66 /* dsp.cpp */, 4F2FB1432A0047420027AB66 /* dsp.h */, - 4F2FB1402A0047420027AB66 /* ImpulseResponse.cpp */, - 4F2FB1422A0047420027AB66 /* ImpulseResponse.h */, 4F2FB13F2A0047420027AB66 /* NoiseGate.cpp */, 4F2FB13D2A0047420027AB66 /* NoiseGate.h */, 4F2FB13B2A0047420027AB66 /* RecursiveLinearFilter.cpp */, 4F2FB1412A0047420027AB66 /* RecursiveLinearFilter.h */, 4F2FB13A2A0047420027AB66 /* Resample.h */, - 4F2FB1442A0047420027AB66 /* wav.cpp */, - 4F2FB13C2A0047420027AB66 /* wav.h */, ); name = dsp; path = ../../AudioDSPTools/dsp; @@ -2074,13 +2048,11 @@ C05500000000000000000104 /* sequential.h in Headers */, 4F78BE1222E73DD900AD537E /* NeuralAmpModelerAU.h in Headers */, 4F2FB1542A0047420027AB66 /* Resample.h in Headers */, - 4F2FB15E2A0047420027AB66 /* wav.h in Headers */, AAB7BBB72CC4B8C6000B8B6E /* get_dsp.h in Headers */, 4F2FB1902A0047430027AB66 /* version.h in Headers */, AA7C86002B439AC000B5FB3A /* ptrlist.h in Headers */, 4F4856852773C3B5005BCF8E /* IPlugAUViewController.h in Headers */, AA7C85FC2B439AC000B5FB3A /* _LanczosResampler.h in Headers */, - 4F2FB17C2A0047430027AB66 /* ImpulseResponse.h in Headers */, 4F2FB17B2A0047430027AB66 /* RecursiveLinearFilter.h in Headers */, AA341E282B9E5A530069C260 /* ToneStack.h in Headers */, 4F2FB1B12A0047430027AB66 /* activations.h in Headers */, @@ -2629,7 +2601,6 @@ buildActionMask = 2147483647; files = ( 4F2FB1A22A0047430027AB66 /* convnet.cpp in Sources */, - 4F2FB1842A0047430027AB66 /* wav.cpp in Sources */, 4F2FB1C12A0047430027AB66 /* wavenet/model.cpp in Sources */, B1D2F7072D0000020027AB66 /* wavenet/slimmable.cpp in Sources */, C2A2FAE112D0000120027AB66 /* wavenet/a2_fast.cpp in Sources */, @@ -2648,7 +2619,6 @@ 4F2FB16F2A0047430027AB66 /* NoiseGate.cpp in Sources */, 4F4856892773CA76005BCF8E /* NeuralAmpModelerAUv3Appex.m in Sources */, 4F2FB15B2A0047420027AB66 /* RecursiveLinearFilter.cpp in Sources */, - 4F2FB1782A0047430027AB66 /* ImpulseResponse.cpp in Sources */, 4F2FB1CA2A0047430027AB66 /* get_dsp.cpp in Sources */, 4F2FB1972A0047430027AB66 /* dsp.cpp in Sources */, ); @@ -2670,7 +2640,6 @@ 4F1A528C205D916F00CF2908 /* IPlugAU.cpp in Sources */, 4F2FB1632A0047430027AB66 /* dsp.cpp in Sources */, 4FD52131202A5B9B00A4D22A /* IPlugAU_view_factory.mm in Sources */, - 4F2FB1812A0047430027AB66 /* wav.cpp in Sources */, 4F2FB1582A0047420027AB66 /* RecursiveLinearFilter.cpp in Sources */, 4F2FB18A2A0047430027AB66 /* util.cpp in Sources */, 4F993F7423055C96000313AF /* IPlugProcessor.cpp in Sources */, @@ -2693,7 +2662,6 @@ 4FD52136202A5BD000A4D22A /* dfx-au-utilities.c in Sources */, 4F7C4960255DDFC500DF7588 /* ITextEntryControl.cpp in Sources */, 4F5F344420C0226200487201 /* IPlugPaths.mm in Sources */, - 4F2FB1752A0047430027AB66 /* ImpulseResponse.cpp in Sources */, 4F7C495E255DDFC500DF7588 /* IControls.cpp in Sources */, 4F35DEB0207E5C5A00867D8F /* IPlugPluginBase.cpp in Sources */, 4F78D95C13B63BA50032E0F3 /* IPlugParameter.cpp in Sources */, @@ -2742,7 +2710,6 @@ 4F3EE1D5231438D000004786 /* IPlugAPP_host.cpp in Sources */, 4F2FB15D2A0047420027AB66 /* RecursiveLinearFilter.cpp in Sources */, 4F3EE1D6231438D000004786 /* IPlugAPP.cpp in Sources */, - 4F2FB17A2A0047430027AB66 /* ImpulseResponse.cpp in Sources */, 4F2FB18F2A0047430027AB66 /* util.cpp in Sources */, 4F3EE1D7231438D000004786 /* IGraphics.cpp in Sources */, 4F3EE1D8231438D000004786 /* IPlugAPP_dialog.cpp in Sources */, @@ -2760,7 +2727,6 @@ 4F3EE1E2231438D000004786 /* IGraphicsEditorDelegate.cpp in Sources */, AA341E262B9E5A530069C260 /* ToneStack.cpp in Sources */, 4F3EE1E3231438D000004786 /* swell-gdi.mm in Sources */, - 4F2FB1862A0047430027AB66 /* wav.cpp in Sources */, 4F3EE1E4231438D000004786 /* IPlugParameter.cpp in Sources */, 4F3EE1E5231438D000004786 /* IPlugTimer.cpp in Sources */, ); @@ -2782,13 +2748,11 @@ 4F78BE1E22E7406D00AD537E /* IGraphicsNanoVG_src.m in Sources */, 4F78BE1F22E7406D00AD537E /* IGraphics.cpp in Sources */, 4F2FB1CB2A0047430027AB66 /* get_dsp.cpp in Sources */, - 4F2FB1852A0047430027AB66 /* wav.cpp in Sources */, 4F2FB1B92A0047430027AB66 /* activations.cpp in Sources */, 4F78BE2022E7406D00AD537E /* IGraphicsEditorDelegate.cpp in Sources */, 4F78BE2122E7406D00AD537E /* IControl.cpp in Sources */, 4F78BE2222E7406D00AD537E /* IPlugAUAudioUnit.mm in Sources */, 4F7C496C255DDFCB00DF7588 /* ITextEntryControl.cpp in Sources */, - 4F2FB1792A0047430027AB66 /* ImpulseResponse.cpp in Sources */, 4F78BE2322E7406D00AD537E /* IPlugAUv3.mm in Sources */, 4F78BE2422E7406D00AD537E /* IPlugAUViewController.mm in Sources */, 4F2FB1982A0047430027AB66 /* dsp.cpp in Sources */, @@ -2833,7 +2797,6 @@ 4F81597E205D50EB00393585 /* fdebug.cpp in Sources */, 4F9828B8140A9EB700F3FCC1 /* IPlugAPIBase.cpp in Sources */, 4F81597F205D50EB00393585 /* fdynlib.cpp in Sources */, - 4F2FB1802A0047430027AB66 /* wav.cpp in Sources */, 4F2FB1622A0047430027AB66 /* dsp.cpp in Sources */, 4F2FB1BD2A0047430027AB66 /* wavenet/model.cpp in Sources */, B1D2F7032D0000020027AB66 /* wavenet/slimmable.cpp in Sources */, @@ -2890,7 +2853,6 @@ 4F1A527E205D911A00CF2908 /* IPlugVST3.cpp in Sources */, 4F815988205D50EB00393585 /* coreiids.cpp in Sources */, 4F815993205D51F000393585 /* vstnoteexpressiontypes.cpp in Sources */, - 4F2FB1742A0047430027AB66 /* ImpulseResponse.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2910,13 +2872,11 @@ 4F03A5B020A4621100EBDFFB /* IGraphics.cpp in Sources */, 4F35DEB1207E5C5A00867D8F /* IPlugPluginBase.cpp in Sources */, 4F2FB1C82A0047430027AB66 /* get_dsp.cpp in Sources */, - 4F2FB1822A0047430027AB66 /* wav.cpp in Sources */, 4F2FB1B62A0047430027AB66 /* activations.cpp in Sources */, 4F5F344520C0226200487201 /* IPlugPaths.mm in Sources */, 4F1A5285205D914A00CF2908 /* IPlugAAX.cpp in Sources */, 4F0848292015129A00F9E881 /* IPlugAAX_Parameters.cpp in Sources */, 4FB1F59420E4B014004157C8 /* IGraphicsMac_view.mm in Sources */, - 4F2FB1762A0047430027AB66 /* ImpulseResponse.cpp in Sources */, 4F7C4963255DDFC600DF7588 /* ITextEntryControl.cpp in Sources */, 4F3862F32014BBEC0009F402 /* NeuralAmpModeler.cpp in Sources */, 4F2FB1952A0047430027AB66 /* dsp.cpp in Sources */, @@ -3006,7 +2966,6 @@ 4F690CA3203A45C700A4A13E /* IPlugAPP_host.cpp in Sources */, 4F2FB1552A0047420027AB66 /* RecursiveLinearFilter.cpp in Sources */, 4F1A5282205D913300CF2908 /* IPlugAPP.cpp in Sources */, - 4F2FB1722A0047430027AB66 /* ImpulseResponse.cpp in Sources */, 4F2FB1872A0047430027AB66 /* util.cpp in Sources */, 4F03A5AC20A4621100EBDFFB /* IGraphics.cpp in Sources */, 4F2EA978203A50EA008E4850 /* IPlugAPP_dialog.cpp in Sources */, @@ -3024,7 +2983,6 @@ 4F8C10E020BA2796006320CD /* IGraphicsEditorDelegate.cpp in Sources */, AA341E1D2B9E5A530069C260 /* ToneStack.cpp in Sources */, 4FF0A83221BE708700B2C9D1 /* swell-gdi.mm in Sources */, - 4F2FB17E2A0047430027AB66 /* wav.cpp in Sources */, 4F78D91813B63BA50032E0F3 /* IPlugParameter.cpp in Sources */, 4FDAC0EA207D76C600299363 /* IPlugTimer.cpp in Sources */, ); @@ -3034,7 +2992,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 4F2FB1832A0047430027AB66 /* wav.cpp in Sources */, 4F2FB16E2A0047430027AB66 /* NoiseGate.cpp in Sources */, 4FFBB90520863B0E00DDD0E7 /* baseiids.cpp in Sources */, 4FFBB90620863B0E00DDD0E7 /* IPlugTimer.cpp in Sources */, @@ -3064,7 +3021,6 @@ 4FFBB91B20863B0E00DDD0E7 /* flock.cpp in Sources */, 4FFBB91D20863B0E00DDD0E7 /* ustring.cpp in Sources */, 4FFBB91E20863B0E00DDD0E7 /* fobject.cpp in Sources */, - 4F2FB1772A0047430027AB66 /* ImpulseResponse.cpp in Sources */, 4F2FB1652A0047430027AB66 /* dsp.cpp in Sources */, 4F2FB1AB2A0047430027AB66 /* lstm.cpp in Sources */, 4F2FB1C02A0047430027AB66 /* wavenet/model.cpp in Sources */, diff --git a/NeuralAmpModeler/projects/NeuralAmpModeler-vst3.vcxproj b/NeuralAmpModeler/projects/NeuralAmpModeler-vst3.vcxproj index 1039ff0f6..d23cde32a 100644 --- a/NeuralAmpModeler/projects/NeuralAmpModeler-vst3.vcxproj +++ b/NeuralAmpModeler/projects/NeuralAmpModeler-vst3.vcxproj @@ -329,7 +329,6 @@ - @@ -339,7 +338,6 @@ - @@ -427,10 +425,8 @@ - - diff --git a/NeuralAmpModeler/projects/NeuralAmpModeler-vst3.vcxproj.filters b/NeuralAmpModeler/projects/NeuralAmpModeler-vst3.vcxproj.filters index 4f7ecfbc6..9912284d6 100644 --- a/NeuralAmpModeler/projects/NeuralAmpModeler-vst3.vcxproj.filters +++ b/NeuralAmpModeler/projects/NeuralAmpModeler-vst3.vcxproj.filters @@ -158,18 +158,12 @@ dsp - - dsp - dsp dsp - - dsp - @@ -420,9 +414,6 @@ dsp - - dsp - dsp @@ -435,9 +426,6 @@ dsp - - dsp - IPlug\Extras diff --git a/NeuralAmpModelerCore b/NeuralAmpModelerCore index 0b3d3c97b..d3189ed48 160000 --- a/NeuralAmpModelerCore +++ b/NeuralAmpModelerCore @@ -1 +1 @@ -Subproject commit 0b3d3c97b0859a3a8c92a8628c4dd89a25eb5842 +Subproject commit d3189ed482ded880c128bf312941266c4fa5ceec diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 000000000..a0c8b0beb --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.15) +project(NeuralAmpModelerTests LANGUAGES CXX) + +enable_testing() + +set(PLUGIN_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/..") +set(CORE_ROOT "${PLUGIN_ROOT}/NeuralAmpModelerCore") +file(GLOB CORE_SOURCES CONFIGURE_DEPENDS "${CORE_ROOT}/NAM/*.cpp" "${CORE_ROOT}/NAM/wavenet/*.cpp") + +# Keep every model architecture's static registry entry, even when tests use only get_dsp(). +add_library(test_core OBJECT ${CORE_SOURCES}) +target_compile_features(test_core PUBLIC cxx_std_20) +target_compile_definitions(test_core PUBLIC NAM_ENABLE_A2_FAST NOMINMAX) +target_include_directories(test_core PUBLIC + "${PLUGIN_ROOT}/NeuralAmpModeler" + "${CORE_ROOT}" + "${CORE_ROOT}/Dependencies/nlohmann" +) +# Match the Eigen headers used by the plugin, rather than Core's nested checkout. +target_include_directories(test_core SYSTEM PUBLIC "${PLUGIN_ROOT}/eigen") + +add_executable(test_resampling_nam test_resampling_nam.cpp) +target_link_libraries(test_resampling_nam PRIVATE test_core) +add_executable(test_cabinet_model test_cabinet_model.cpp) +target_link_libraries(test_cabinet_model PRIVATE test_core) +# Compare WAV playback levels with the old processor; these sources are no longer in the plugin targets. +target_sources(test_cabinet_model PRIVATE + "${PLUGIN_ROOT}/AudioDSPTools/dsp/dsp.cpp" + "${PLUGIN_ROOT}/AudioDSPTools/dsp/ImpulseResponse.cpp" + "${PLUGIN_ROOT}/AudioDSPTools/dsp/wav.cpp" +) +target_compile_definitions(test_cabinet_model PRIVATE NAM_TEST_MODELS_DIR="${CORE_ROOT}/example_models") + +add_test(NAME resampling_nam COMMAND test_resampling_nam) +add_test(NAME cabinet_model COMMAND test_cabinet_model) diff --git a/tests/test_cabinet_model.cpp b/tests/test_cabinet_model.cpp new file mode 100644 index 000000000..3dbf10337 --- /dev/null +++ b/tests/test_cabinet_model.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ResamplingNAM.h" +#include "NAM/get_dsp.h" +#include "../AudioDSPTools/dsp/ImpulseResponse.h" + +namespace +{ +void require(bool value, const char* message) +{ + if (!value) + throw std::runtime_error(message); +} + +struct Fixture +{ + std::filesystem::path directory = + std::filesystem::temp_directory_path() + / ("nam-cab-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + Fixture() { std::filesystem::create_directories(directory); } + ~Fixture() { std::filesystem::remove_all(directory); } + + std::filesystem::path wav(const std::string& name, const std::vector& samples, int rate = 48000, + int channels = 1) + { + const auto path = directory / std::filesystem::u8path(name); + std::ofstream file(path, std::ios::binary); + auto integer = [&](uint32_t value, int bytes) { + for (int i = 0; i < bytes; ++i) + file.put(static_cast((value >> (8 * i)) & 0xff)); + }; + file.write("RIFF", 4); + integer(36 + 4 * samples.size(), 4); + file.write("WAVEfmt ", 8); + integer(16, 4); + integer(3, 2); + integer(channels, 2); + integer(rate, 4); + integer(rate * channels * 4, 4); + integer(channels * 4, 2); + integer(32, 2); + file.write("data", 4); + integer(4 * samples.size(), 4); + for (float sample : samples) + { + uint32_t bits; + std::memcpy(&bits, &sample, 4); + integer(bits, 4); + } + return path; + } + + std::filesystem::path nam(const std::string& name, const nlohmann::json& config) + { + const auto path = directory / name; + std::ofstream(path) << config; + return path; + } +}; + +nlohmann::json linear(double rate = 48000.0, int inputs = 1, int outputs = 1) +{ + return {{"version", "0.7.0"}, + {"architecture", "Linear"}, + {"sample_rate", rate}, + {"config", {{"receptive_field", 1}, {"bias", false}, {"in_channels", inputs}, {"out_channels", outputs}}}, + {"weights", std::vector(std::max(inputs, outputs), 0.5f)}}; +} + +template +std::vector render(Process process, int samples = 4096, bool impulse = false) +{ + std::vector input(samples), output(samples); + for (int i = 0; i < samples; ++i) + input[i] = impulse ? (i == 0 ? 1.0 : 0.0) : 0.2 * std::sin(0.03 * i); + for (int offset = 0; offset < samples;) + { + const int count = std::min(137, samples - offset); + NAM_SAMPLE* in[]{input.data() + offset}; + NAM_SAMPLE* out[]{output.data() + offset}; + process(in, out, count); + offset += count; + } + return output; +} + +void test_wav_level_and_rate_changes(Fixture& files) +{ + for (int sourceRate : {44100, 48000, 96000}) + { + const auto file = files.wav("cab-\xf0\x9f\x8e\xb8.WAV", {0.5f, -0.25f, 0.125f, 0.0f}, sourceRate); + ResamplingNAM cab(nam::get_dsp(file), 48000.0); + // Reset must use the loaded data, even after the original file disappears. + std::filesystem::remove(file); + for (double hostRate : {44100.0, 96000.0, 48000.0, 44100.0}) + { + dsp::ImpulseResponse::IRData oldData{{0.5f, -0.25f, 0.125f, 0.0f}, static_cast(sourceRate)}; + dsp::ImpulseResponse legacy(oldData, hostRate); + cab.Reset(hostRate, 256); + require(cab.GetLatency() == 0, "WAV cab added resampler latency"); + const auto actual = render([&](auto** in, auto** out, int frames) { cab.process(in, out, frames); }); + const auto oldOutput = render([&](auto** in, auto** out, int frames) { + const auto processed = legacy.Process(in, 1, frames); + std::copy_n(processed[0], frames, out[0]); + }); + // The output-stage adjustment is independent of the host rate; Core + // already compensates for the changed tap density when resampling. + const double gainDB = -18.0 + 20.0 * std::log10(48000.0 / cab.GetEncapsulatedSampleRate()); + const double gain = std::pow(10.0, gainDB / 20.0); + for (size_t i = 0; i < actual.size(); ++i) + require(std::isfinite(actual[i]) && std::abs(actual[i] * gain - oldOutput[i]) < 1.0e-6, + "WAV playback differs from the previous processor"); + } + } +} + +void test_long_ir(Fixture& files) +{ + std::vector weights(144001, 0.0f); + weights.back() = 1.0f; + const auto path = files.wav("long.wav", weights); + ResamplingNAM cab(nam::get_dsp(path), 48000.0); + cab.Reset(48000.0, 256); + const auto output = render([&](auto** in, auto** out, int frames) { cab.process(in, out, frames); }, 144256, true); + require(std::abs(output[144000] - 1.0) < 1.0e-6, "Long IR was truncated"); + require(cab.GetPrewarmSamples() == 144001, "Long IR tail was not reported"); + cab.Reset(96000.0, 256); + require(cab.GetPrewarmSamples() == 288002, "IR tail did not adapt to the host rate"); +} + +void test_nam_architectures(Fixture& files) +{ + const auto child = linear(); + nlohmann::json sequential{{"version", "0.7.0"}, + {"architecture", "Sequential"}, + {"sample_rate", 48000.0}, + {"config", {{"models", {child, child}}}}, + {"weights", nlohmann::json::array()}}; + const auto path = files.nam("cab.nam", sequential); + ResamplingNAM cab(nam::get_dsp(path), 48000.0); + cab.Reset(48000.0, 256); + const auto result = render([&](auto** in, auto** out, int frames) { cab.process(in, out, frames); }); + for (size_t i = 0; i < result.size(); ++i) + require(std::abs(result[i] - 0.05 * std::sin(0.03 * i)) < 1.0e-6, "NAM cab's stored level changed"); + cab.Reset(44100.0, 256); + require(cab.GetLatency() > 0, "Fixed-rate NAM cab did not report its resampling latency"); + require(cab.GetPrewarmSamples() == 2, "Sequential tail was not converted to host-rate samples"); + const auto output = render([&](auto** in, auto** out, int frames) { cab.process(in, out, frames); }); + for (auto sample : output) + require(std::isfinite(sample), "Resampled NAM cab produced non-finite audio"); + + const auto lstm = std::filesystem::path(NAM_TEST_MODELS_DIR) / "lstm.nam"; + ResamplingNAM nonlinear(nam::get_dsp(lstm), 48000.0); + nonlinear.Reset(48000.0, 256); + require(nonlinear.GetPrewarmSamples() == 24000, "LSTM tail estimate was lost"); + auto reference = nam::get_dsp(lstm); + reference->Reset(48000.0, 256); + const auto actual = render([&](auto** in, auto** out, int frames) { nonlinear.process(in, out, frames); }); + const auto expected = render([&](auto** in, auto** out, int frames) { reference->process(in, out, frames); }); + for (size_t i = 0; i < actual.size(); ++i) + require(std::abs(actual[i] - expected[i]) < 1.0e-6, "Nonlinear NAM cab's stored level changed"); + for (const double rate : {44100.0, 96000.0, 48000.0}) + { + nonlinear.Reset(rate, 256); + require(nonlinear.GetPrewarmSamples() == static_cast(rate / 2), "LSTM tail did not track host rate"); + } +} + +} // namespace + +int main() +{ + try + { + Fixture files; + test_wav_level_and_rate_changes(files); + test_long_ir(files); + test_nam_architectures(files); + std::cout << "Cab slot compatibility tests passed\n"; + } + catch (const std::exception& error) + { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/tests/test_resampling_nam.cpp b/tests/test_resampling_nam.cpp new file mode 100644 index 000000000..67522a9f3 --- /dev/null +++ b/tests/test_resampling_nam.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include + +#include "ResamplingNAM.h" +#include "NAM/linear.h" +#include "NAM/sequential.h" + +namespace +{ +void require(bool condition, const char* message) +{ + if (!condition) + throw std::runtime_error(message); +} + +class RateTrackingDSP : public nam::DSP +{ +public: + RateTrackingDSP(double trainingRate, bool adaptive) + : nam::DSP(1, 1, trainingRate) + , mAdaptive(adaptive) + { + } + + bool SupportsArbitrarySampleRate() override { return mAdaptive; } + + int GetPrewarmSamples() override + { + return mAdaptive ? static_cast(std::ceil(0.1 * mProcessingRate)) : 4801; + } + + void Reset(double sampleRate, int maxBlockSize) override + { + mProcessingRate = sampleRate; + nam::DSP::Reset(sampleRate, maxBlockSize); + } + + void process(NAM_SAMPLE** input, NAM_SAMPLE** output, int frames) override + { + require(frames <= GetMaxBufferSize(), "Model received more samples than its prepared buffer size"); + std::copy_n(input[0], frames, output[0]); + } + + double mProcessingRate = 0.0; + +private: + bool mAdaptive; +}; + +std::unique_ptr make_linear() +{ + const std::vector weights{0.5f, -0.25f, 0.125f, 0.0625f}; + return std::make_unique(1, 1, static_cast(weights.size()), false, weights, 48000.0); +} + +std::unique_ptr make_sequential() +{ + std::vector> models; + models.push_back(std::make_unique(48000.0, false)); + models.push_back(make_linear()); + return std::make_unique(std::move(models), 48000.0); +} + +template +std::vector render(Process process) +{ + std::vector input(4096), output(input.size()); + for (size_t i = 0; i < input.size(); ++i) + input[i] = 0.2 * std::sin(0.071 * i) + 0.1 * std::cos(0.013 * i); + const int blockSizes[]{1, 7, 64, 137, 256, 3}; + size_t offset = 0, block = 0; + while (offset < input.size()) + { + const auto frames = std::min(blockSizes[block++ % 6], static_cast(input.size() - offset)); + NAM_SAMPLE* in[]{input.data() + offset}; + NAM_SAMPLE* out[]{output.data() + offset}; + process(in, out, frames); + offset += frames; + } + return output; +} + +void require_matching_audio(const std::vector& actual, const std::vector& expected) +{ + for (size_t i = 0; i < actual.size(); ++i) + require(std::isfinite(actual[i]) && std::abs(actual[i] - expected[i]) < 1.0e-6, + "Wrapped audio differs from processing at the correct sample rate"); +} + +void test_reset_rates_and_metadata() +{ + for (bool adaptive : {false, true}) + { + auto model = std::make_unique(48000.0, adaptive); + auto* tracked = model.get(); + model->SetInputLevel(12.0); + model->SetOutputLevel(4.0); + model->SetLoudness(-18.0); + ResamplingNAM wrapped(std::move(model), 44100.0); + for (double hostRate : {44100.0, 48000.0, 96000.0, 44100.0}) + { + wrapped.Reset(hostRate, 256); + require(tracked->mProcessingRate == (adaptive ? hostRate : 48000.0), "Incorrect model processing rate"); + require(wrapped.GetExpectedSampleRate() == hostRate, "Wrapper lost the host sample rate"); + require(wrapped.GetEncapsulatedSampleRate() == 48000.0, "Wrapper lost the training sample rate"); + require((wrapped.GetLatency() == 0) == (adaptive || hostRate == 48000.0), "Incorrect resampling latency"); + const int expectedHistory = adaptive ? static_cast(std::ceil(0.1 * hostRate)) + : static_cast(std::ceil(4801.0 * hostRate / 48000.0)); + require(wrapped.GetPrewarmSamples() == expectedHistory, "History length was not reported in host-rate samples"); + render([&](auto** in, auto** out, int frames) { wrapped.process(in, out, frames); }); + } + require(wrapped.HasInputLevel() && wrapped.GetInputLevel() == 12.0, "Input calibration was lost"); + require(wrapped.HasOutputLevel() && wrapped.GetOutputLevel() == 4.0, "Output calibration was lost"); + require(wrapped.HasLoudness() && wrapped.GetLoudness() == -18.0, "Loudness metadata was lost"); + } + + auto unknown = std::make_unique(NAM_UNKNOWN_EXPECTED_SAMPLE_RATE, false); + auto* tracked = unknown.get(); + ResamplingNAM legacy(std::move(unknown), 96000.0); + require(tracked->mProcessingRate == 48000.0 && legacy.GetEncapsulatedSampleRate() == 48000.0, + "Unknown training rates must retain the legacy 48 kHz fallback"); +} + +void test_native_linear_audio() +{ + ResamplingNAM wrapped(make_linear(), 48000.0); + auto reference = make_linear(); + for (double rate : {44100.0, 96000.0, 48000.0, 44100.0}) + { + wrapped.Reset(rate, 256); + reference->Reset(rate, 256); + require(wrapped.GetLatency() == 0, "Adaptive Linear model must not add wrapper latency"); + require(wrapped.GetPrewarmSamples() == static_cast(std::ceil(4.0 * rate / 48000.0)), + "Native Linear history was converted twice or lost"); + const auto actual = render([&](auto** in, auto** out, int frames) { wrapped.process(in, out, frames); }); + const auto expected = render([&](auto** in, auto** out, int frames) { reference->process(in, out, frames); }); + require_matching_audio(actual, expected); + } +} + +void test_fixed_rate_sequential_audio() +{ + ResamplingNAM wrapped(make_sequential(), 48000.0); + auto reference = make_sequential(); + dsp::ResamplingContainer resampler(48000.0); + for (double hostRate : {44100.0, 96000.0, 48000.0, 44100.0}) + { + wrapped.Reset(hostRate, 256); + const auto modelBlockSize = static_cast(std::ceil(256.0 * 48000.0 / hostRate)); + reference->Reset(48000.0, modelBlockSize); + if (hostRate != 48000.0) + resampler.Reset(hostRate, 256); + const auto actual = render([&](auto** in, auto** out, int frames) { wrapped.process(in, out, frames); }); + const auto expected = render([&](auto** in, auto** out, int frames) { + if (hostRate == 48000.0) + reference->process(in, out, frames); + else + resampler.ProcessBlock(in, out, frames, [&](auto** modelIn, auto** modelOut, int count) { + reference->process(modelIn, modelOut, count); + }); + }); + require_matching_audio(actual, expected); + } +} +} // namespace + +int main() +{ + try + { + test_reset_rates_and_metadata(); + test_native_linear_audio(); + test_fixed_rate_sequential_audio(); + std::cout << "Sample-rate integration tests passed\n"; + } + catch (const std::exception& error) + { + std::cerr << error.what() << '\n'; + return 1; + } +}