Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
Thanks for your interest in the project! Here are a few quick tips to make sure that your PR will go smoothly:

## "Communication is the best policy"
This is a fun, scrappy project.
Things might change--quickly--including these guidelines.
If you're not sure about something or have a suggestion, reach out!
I prefer to communicate about work via the [Issues](https://github.com/sdatkinson/NeuralAmpModelerPlugin/issues).
If you want to see something make an Issue. (Please check the existing Issues first to avoid duplicating something, but also please _do_ make a new Issue if you want to discuss something that's not the same as e.g. the specific problem in a bug report.)

## Have an idea?
If you have an idea that you'd like to see in the plugin, start by [raising an Issue](https://github.com/sdatkinson/NeuralAmpModelerPlugin/issues/new?assignees=&labels=enhancement&template=feature_request.md&title=%5BFEATURE%5D) and describe what you'd like to see.
Expand All @@ -15,8 +14,15 @@ 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
This repo doesn't currently have unit tests (gasp! Sorry! If you want to help by proposing a framework, please [raise an Issue](https://github.com/sdatkinson/NeuralAmpModelerPlugin/issues/new?assignees=&labels=enhancement&template=feature_request.md&title=%5BFEATURE%5D)!)
However, there are a few things I'd appreciate if you did to make sure that everything is working as expected:
The model sample-rate adapter has standalone regression tests that do not require a plugin SDK:

```bash
cmake -S tests -B build-tests -DCMAKE_BUILD_TYPE=Release
cmake --build build-tests
ctest --test-dir build-tests --output-on-failure
```

Additionally, there are a few things I'd appreciate if you did to make sure that everything is working as expected:
- [ ] The standalone plugin builds.
- [ ] The plugin runs.
- [ ] The plugin makes sound.
Expand All @@ -29,8 +35,7 @@ However, there are a few things I'd appreciate if you did to make sure that ever
- [ ] The AU plugin builds.

## Code style
I don't care too much about the specifics of style, but it helps keep things orderly and helps make sure that the changes in a PR are real changes and not just e.g. an IDE replacing tabs with spaces.
Going on the main criterion of ease of adoption, the C++ code (`.cpp` and `.h` files) are formatted according to the LLVM code style that `clang-format` enforces.
As far as formatting, the C++ code (`.cpp` and `.h` files) are formatted according to the LLVM code style that `clang-format` enforces.
To easily apply the format to your code, run

```bash
Expand Down
116 changes: 1 addition & 115 deletions NeuralAmpModeler/NeuralAmpModeler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#include "../AudioDSPTools/dsp/NoiseGate.h"
#include "../AudioDSPTools/dsp/dsp.h"
#include "../AudioDSPTools/dsp/wav.h"
#include "../AudioDSPTools/dsp/ResamplingContainer/ResamplingContainer.h"
#include "../NeuralAmpModelerCore/NAM/dsp.h"
#include "../NeuralAmpModelerCore/NAM/slimmable.h"
#include "ResamplingNAM.h"

#include "Colors.h"
#include "ToneStack.h"
Expand Down Expand Up @@ -81,118 +79,6 @@ enum EMsgTags
kNumMsgTags
};

// Get the sample rate of a NAM model.
// Sometimes, the model doesn't know its own sample rate; this wrapper guesses 48k based on the way that most
// people have used NAM in the past.
double GetNAMSampleRate(const std::unique_ptr<nam::DSP>& model)
{
// Some models are from when we didn't have sample rate in the model.
// For those, this wraps with the assumption that they're 48k models, which is probably true.
const double assumedSampleRate = 48000.0;
const double reportedEncapsulatedSampleRate = model->GetExpectedSampleRate();
const double encapsulatedSampleRate =
reportedEncapsulatedSampleRate <= 0.0 ? assumedSampleRate : reportedEncapsulatedSampleRate;
return encapsulatedSampleRate;
};

class ResamplingNAM : public nam::DSP
{
public:
// Resampling wrapper around the NAM models
ResamplingNAM(std::unique_ptr<nam::DSP> encapsulated, const double expected_sample_rate)
: nam::DSP(encapsulated->NumInputChannels(), encapsulated->NumOutputChannels(), expected_sample_rate)
, mEncapsulated(std::move(encapsulated))
, mResampler(GetNAMSampleRate(mEncapsulated))
{
// Assign the encapsulated object's processing function to this object's member so that the resampler can use it:
auto ProcessBlockFunc = [&](NAM_SAMPLE** input, NAM_SAMPLE** output, int numFrames) {
mEncapsulated->process(input, output, numFrames);
};
mBlockProcessFunc = ProcessBlockFunc;

// Get the other information from the encapsulated NAM so that we can tell the outside world about what we're
// holding.
if (mEncapsulated->HasLoudness())
{
SetLoudness(mEncapsulated->GetLoudness());
}
if (mEncapsulated->HasInputLevel())
{
SetInputLevel(mEncapsulated->GetInputLevel());
}
if (mEncapsulated->HasOutputLevel())
{
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);
};

~ResamplingNAM() = default;

void prewarm() override { mEncapsulated->prewarm(); };

void process(NAM_SAMPLE** input, NAM_SAMPLE** output, const int num_frames) override
{
if (num_frames > mMaxExternalBlockSize)
// We can afford to be careful
throw std::runtime_error("More frames were provided than the max expected!");

if (!NeedToResample())
{
mEncapsulated->process(input, output, num_frames);
}
else
{
mResampler.ProcessBlock(input, output, num_frames, mBlockProcessFunc);
}
};

int GetLatency() const { return NeedToResample() ? mResampler.GetLatency() : 0; };

void Reset(const double sampleRate, const int maxBlockSize) override
{
mExpectedSampleRate = sampleRate;
mMaxExternalBlockSize = maxBlockSize;
mResampler.Reset(sampleRate, maxBlockSize);

// Allocations in the encapsulated model (HACK)
// Stolen some code from the resampler; it'd be nice to have these exposed as methods? :)
const double mUpRatio = sampleRate / GetEncapsulatedSampleRate();
const auto maxEncapsulatedBlockSize = static_cast<int>(std::ceil(static_cast<double>(maxBlockSize) / mUpRatio));
mEncapsulated->ResetAndPrewarm(sampleRate, maxEncapsulatedBlockSize);
};

// So that we can let the world know if we're resampling (useful for debugging)
double GetEncapsulatedSampleRate() const { return GetNAMSampleRate(mEncapsulated); };

nam::SlimmableModel* GetSlimmableModel() { return dynamic_cast<nam::SlimmableModel*>(mEncapsulated.get()); }
const nam::SlimmableModel* GetSlimmableModel() const
{
return dynamic_cast<const nam::SlimmableModel*>(mEncapsulated.get());
}

private:
bool NeedToResample() const { return GetExpectedSampleRate() != GetEncapsulatedSampleRate(); };
// The encapsulated NAM
std::unique_ptr<nam::DSP> mEncapsulated;

// The resampling wrapper
dsp::ResamplingContainer<NAM_SAMPLE, 1, 12> mResampler;

// Used to check that we don't get too large a block to process.
int mMaxExternalBlockSize = 0;

// This function is defined to conform to the interface expected by the iPlug2 resampler.
std::function<void(NAM_SAMPLE**, NAM_SAMPLE**, int)> mBlockProcessFunc;
};

class NeuralAmpModeler final : public iplug::Plugin
{
public:
Expand Down
142 changes: 142 additions & 0 deletions NeuralAmpModeler/ResamplingNAM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#pragma once

#include <algorithm>
#include <cmath>
#include <functional>
#include <memory>
#include <stdexcept>
#include <utility>

#include "../iPlug2/IPlug/IPlugConstants.h"

// AudioDSPTools uses iPlug's constants without including their declarations.
#ifndef DEFAULT_BLOCK_SIZE
namespace dsp
{
using iplug::DEFAULT_BLOCK_SIZE;
}
#endif
#include "../AudioDSPTools/dsp/ResamplingContainer/ResamplingContainer.h"
#include "../NeuralAmpModelerCore/NAM/dsp.h"
#include "../NeuralAmpModelerCore/NAM/slimmable.h"

// Get the sample rate of a NAM model.
// Sometimes, the model doesn't know its own sample rate; this wrapper guesses 48k based on the way that most
// people have used NAM in the past.
inline double GetNAMSampleRate(const std::unique_ptr<nam::DSP>& model)
{
// Some models are from when we didn't have sample rate in the model.
// For those, this wraps with the assumption that they're 48k models, which is probably true.
const double assumedSampleRate = 48000.0;
const double reportedEncapsulatedSampleRate = model->GetExpectedSampleRate();
const double encapsulatedSampleRate =
reportedEncapsulatedSampleRate <= 0.0 ? assumedSampleRate : reportedEncapsulatedSampleRate;
return encapsulatedSampleRate;
};

class ResamplingNAM : public nam::DSP
{
public:
// Resampling wrapper around the NAM models
ResamplingNAM(std::unique_ptr<nam::DSP> encapsulated, const double expected_sample_rate)
: nam::DSP(encapsulated->NumInputChannels(), encapsulated->NumOutputChannels(), expected_sample_rate)
, mEncapsulated(std::move(encapsulated))
, mSupportsArbitrarySampleRate(mEncapsulated->SupportsArbitrarySampleRate())
, mResampler(GetNAMSampleRate(mEncapsulated))
{
// Assign the encapsulated object's processing function to this object's member so that the resampler can use it:
auto ProcessBlockFunc = [&](NAM_SAMPLE** input, NAM_SAMPLE** output, int numFrames) {
mEncapsulated->process(input, output, numFrames);
};
mBlockProcessFunc = ProcessBlockFunc;

// Get the other information from the encapsulated NAM so that we can tell the outside world about what we're
// holding.
if (mEncapsulated->HasLoudness())
{
SetLoudness(mEncapsulated->GetLoudness());
}
if (mEncapsulated->HasInputLevel())
{
SetInputLevel(mEncapsulated->GetInputLevel());
}
if (mEncapsulated->HasOutputLevel())
{
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);
};

~ResamplingNAM() = default;

void prewarm() override { mEncapsulated->prewarm(); };

void process(NAM_SAMPLE** input, NAM_SAMPLE** output, const int num_frames) override
{
if (num_frames > mMaxExternalBlockSize)
// We can afford to be careful
throw std::runtime_error("More frames were provided than the max expected!");

if (!NeedToResample())
{
mEncapsulated->process(input, output, num_frames);
}
else
{
mResampler.ProcessBlock(input, output, num_frames, mBlockProcessFunc);
}
};

int GetLatency() const { return NeedToResample() ? mResampler.GetLatency() : 0; };

void Reset(const double sampleRate, const int maxBlockSize) override
{
mExpectedSampleRate = sampleRate;
mMaxExternalBlockSize = maxBlockSize;
// Adaptive models convert their own coefficients to the host rate. Fixed-rate models must be reset at the
// rate of the audio they will actually receive, including Linear children inside a Sequential model.
const double modelSampleRate = mSupportsArbitrarySampleRate ? sampleRate : GetEncapsulatedSampleRate();
int maxEncapsulatedBlockSize = maxBlockSize;
if (NeedToResample())
{
mResampler.Reset(sampleRate, maxBlockSize);
maxEncapsulatedBlockSize =
static_cast<int>(std::ceil(static_cast<double>(maxBlockSize) * modelSampleRate / sampleRate));
}
mEncapsulated->Reset(modelSampleRate, maxEncapsulatedBlockSize);
};

// So that we can let the world know if we're resampling (useful for debugging)
double GetEncapsulatedSampleRate() const { return GetNAMSampleRate(mEncapsulated); };

nam::SlimmableModel* GetSlimmableModel() { return dynamic_cast<nam::SlimmableModel*>(mEncapsulated.get()); }
const nam::SlimmableModel* GetSlimmableModel() const
{
return dynamic_cast<const nam::SlimmableModel*>(mEncapsulated.get());
}

private:
bool NeedToResample() const
{
return !mSupportsArbitrarySampleRate && GetExpectedSampleRate() != GetEncapsulatedSampleRate();
};
// The encapsulated NAM
std::unique_ptr<nam::DSP> mEncapsulated;
const bool mSupportsArbitrarySampleRate;

// The resampling wrapper
dsp::ResamplingContainer<NAM_SAMPLE, 1, 12> mResampler;

// Used to check that we don't get too large a block to process.
int mMaxExternalBlockSize = 0;

// This function is defined to conform to the interface expected by the iPlug2 resampler.
std::function<void(NAM_SAMPLE**, NAM_SAMPLE**, int)> mBlockProcessFunc;
};
14 changes: 13 additions & 1 deletion NeuralAmpModeler/projects/NeuralAmpModeler-aax.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,13 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)%(RelativeDir)</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\get_dsp.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\linear.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\lstm.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\nam_file.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\wav.cpp">
<ObjectFileName>$(IntDir)nam_wav.obj</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\sequential.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\ring_buffer.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\util.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\container.cpp" />
Expand Down Expand Up @@ -533,13 +539,19 @@
<ClInclude Include="..\NeuralAmpModeler.h" />
<ClInclude Include="..\NeuralAmpModelerControls.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\activations.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\compiler.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\conv1d.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\convnet.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\dsp.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\film.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\gating_activations.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\get_dsp.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\linear.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\lstm.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\nam_file.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\wav.h" />
<ClInclude Include="..\ResamplingNAM.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\sequential.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\registry.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\ring_buffer.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\util.h" />
Expand All @@ -565,4 +577,4 @@
<Target Name="AfterBuild">
<PaceFixLogs Condition="Exists('$(PACE_FUSION_HOME)PaceFusionUi2013.dll')" LogDirectory="$(IntDir)" />
</Target>
</Project>
</Project>
14 changes: 13 additions & 1 deletion NeuralAmpModeler/projects/NeuralAmpModeler-app.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,19 @@
<ClInclude Include="..\NeuralAmpModeler.h" />
<ClInclude Include="..\NeuralAmpModelerControls.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\activations.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\compiler.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\conv1d.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\convnet.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\dsp.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\film.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\gating_activations.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\get_dsp.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\linear.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\lstm.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\nam_file.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\wav.h" />
<ClInclude Include="..\ResamplingNAM.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\sequential.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\registry.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\ring_buffer.h" />
<ClInclude Include="..\..\NeuralAmpModelerCore\NAM\util.h" />
Expand Down Expand Up @@ -410,7 +416,13 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)%(RelativeDir)</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\get_dsp.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\linear.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\lstm.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\nam_file.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\wav.cpp">
<ObjectFileName>$(IntDir)nam_wav.obj</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\sequential.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\ring_buffer.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\util.cpp" />
<ClCompile Include="..\..\NeuralAmpModelerCore\NAM\container.cpp" />
Expand Down Expand Up @@ -439,4 +451,4 @@
<Target Name="AfterBuild">
<PaceFixLogs Condition="Exists('$(PACE_FUSION_HOME)PaceFusionUi2013.dll')" LogDirectory="$(IntDir)" />
</Target>
</Project>
</Project>
Loading
Loading