From a305737d632e4c9190de5fdb443bba382669db99 Mon Sep 17 00:00:00 2001 From: jorshi Date: Mon, 1 Jul 2024 15:27:29 +0100 Subject: [PATCH 1/5] updates to vscode config settings for building with pytorch --- .vscode/settings.json | 5 ++++- .vscode/tasks.json | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6d3e577..87a5ec6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,7 @@ { "cmake.configureOnOpen": true, - "cmake.cmakePath": "/opt/homebrew/bin/cmake" + "cmake.cmakePath": "/opt/homebrew/bin/cmake", + "cmake.configureArgs": [ + "-DCMAKE_PREFIX_PATH=/Users/jordanm/anaconda3/envs/torchdrum/lib/python3.10/site-packages/torch/share/cmake" + ] } diff --git a/.vscode/tasks.json b/.vscode/tasks.json index a39d1bf..991c1a0 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -24,5 +24,4 @@ "command": "cmake -Bbuild -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_PREFIX_PATH=/Users/jordanm/anaconda3/envs/torchdrum/lib/python3.10/site-packages/torch/share/cmake -DCOPY_PLUGIN_AFTER_BUILD=0 && cmake --build build --config Release -j12" } ] - -} \ No newline at end of file +} From fbc6deacc8e76a17f5f2a47466d8917e516fa881 Mon Sep 17 00:00:00 2001 From: jorshi Date: Mon, 1 Jul 2024 15:29:20 +0100 Subject: [PATCH 2/5] fix compiler warnings throughout code base --- source/Biquad.cpp | 21 ++++++++------- source/Biquad.h | 26 +++++++++---------- .../FeatureExtraction/FeatureExtraction.cpp | 4 +-- source/FeatureExtraction/FeatureValue.h | 5 +++- source/FeatureExtraction/SpectralExtractor.h | 24 ++++++++--------- source/NeuralNetwork.cpp | 20 +++++++------- source/PluginEditor.cpp | 12 ++++----- source/PluginEditor.h | 4 +-- source/Synth/Modules/Noise.h | 2 +- source/Synth/Modules/Tonal.h | 2 +- source/Synth/Snare808.cpp | 4 +-- source/Synth/SynthParameterBase.h | 6 ++--- source/SynthController.cpp | 6 ++--- 13 files changed, 70 insertions(+), 66 deletions(-) diff --git a/source/Biquad.cpp b/source/Biquad.cpp index f241e29..42d1667 100644 --- a/source/Biquad.cpp +++ b/source/Biquad.cpp @@ -26,8 +26,9 @@ template void BiquadCoeffT::calc() { sample_t norm; - sample_t V = pow(10, fabs(peakGain) / 20.0); - sample_t K = tan(M_PI * Fc); + sample_t V = (sample_t) pow(10, fabs(peakGain) / 20.0); + sample_t K = (sample_t) tan(M_PI * Fc); + sample_t sqrt2 = (sample_t) sqrt(2.0); switch (this->type) { case lowpass: @@ -85,19 +86,19 @@ void BiquadCoeffT::calc() case lowshelf: if (peakGain >= 0) { // boost - norm = 1 / (1 + sqrt(2) * K + K * K); + norm = 1 / (1 + sqrt2 * K + K * K); a0 = (1 + sqrt(2 * V) * K + V * K * K) * norm; a1 = 2 * (V * K * K - 1) * norm; a2 = (1 - sqrt(2 * V) * K + V * K * K) * norm; b1 = 2 * (K * K - 1) * norm; - b2 = (1 - sqrt(2) * K + K * K) * norm; + b2 = (1 - sqrt2 * K + K * K) * norm; } else { // cut norm = 1 / (1 + sqrt(2 * V) * K + V * K * K); - a0 = (1 + sqrt(2) * K + K * K) * norm; + a0 = (1 + sqrt2 * K + K * K) * norm; a1 = 2 * (K * K - 1) * norm; - a2 = (1 - sqrt(2) * K + K * K) * norm; + a2 = (1 - sqrt2 * K + K * K) * norm; b1 = 2 * (V * K * K - 1) * norm; b2 = (1 - sqrt(2 * V) * K + V * K * K) * norm; } @@ -105,19 +106,19 @@ void BiquadCoeffT::calc() case highshelf: if (peakGain >= 0) { // boost - norm = 1 / (1 + sqrt(2) * K + K * K); + norm = 1 / (1 + sqrt2 * K + K * K); a0 = (V + sqrt(2 * V) * K + K * K) * norm; a1 = 2 * (K * K - V) * norm; a2 = (V - sqrt(2 * V) * K + K * K) * norm; b1 = 2 * (K * K - 1) * norm; - b2 = (1 - sqrt(2) * K + K * K) * norm; + b2 = (1 - sqrt2 * K + K * K) * norm; } else { // cut norm = 1 / (V + sqrt(2 * V) * K + K * K); - a0 = (1 + sqrt(2) * K + K * K) * norm; + a0 = (1 + sqrt2 * K + K * K) * norm; a1 = 2 * (K * K - 1) * norm; - a2 = (1 - sqrt(2) * K + K * K) * norm; + a2 = (1 - sqrt2 * K + K * K) * norm; b1 = 2 * (K * K - V) * norm; b2 = (V - sqrt(2 * V) * K + K * K) * norm; } diff --git a/source/Biquad.h b/source/Biquad.h index c940493..df61bc8 100644 --- a/source/Biquad.h +++ b/source/Biquad.h @@ -54,32 +54,32 @@ class BiquadCoeffT : public BiquadCoeff int setup(const Settings& settings) { type = settings.type; - Fs = settings.fs; - Fc = settings.cutoff / Fs; - Q = settings.q; - peakGain = settings.peakGainDb; + Fs = (sample_t) settings.fs; + Fc = (sample_t) settings.cutoff / Fs; + Q = (sample_t) settings.q; + peakGain = (sample_t) settings.peakGainDb; calc(); return 0; } - void setType(Type type) + void setType(Type newValue) { - this->type = type; + this->type = newValue; calc(); } - void setQ(sample_t Q) + void setQ(sample_t newValue) { - this->Q = Q; + this->Q = newValue; calc(); } - void setFc(sample_t Fc) + void setFc(sample_t newValue) { - this->Fc = Fc / this->Fs; + this->Fc = newValue / this->Fs; calc(); } - void setPeakGain(sample_t peakGainDB) + void setPeakGain(sample_t newValue) { - this->peakGain = peakGainDB; + this->peakGain = newValue; calc(); } @@ -105,7 +105,7 @@ class BiquadCoeffT : public BiquadCoeff class Biquad : public BiquadCoeffT { public: - Biquad() {}; + Biquad() {} Biquad(const Settings& s) { setup(s); } /** * Process one input sample and return one output sample. diff --git a/source/FeatureExtraction/FeatureExtraction.cpp b/source/FeatureExtraction/FeatureExtraction.cpp index 0b955c6..308a2a5 100644 --- a/source/FeatureExtraction/FeatureExtraction.cpp +++ b/source/FeatureExtraction/FeatureExtraction.cpp @@ -22,7 +22,7 @@ void FeatureExtraction::process(const juce::AudioBuffer& buffer, FeatureE jassert(buffer.getNumChannels() == 1 && buffer.getNumSamples() == frameSize); // Calculate RMS - double rms = 0.0; + float rms = 0.0; auto* audio = buffer.getReadPointer(0); for (int i = 0; i < buffer.getNumSamples(); ++i) { @@ -35,7 +35,7 @@ void FeatureExtraction::process(const juce::AudioBuffer& buffer, FeatureE // Convert to dB with epsilon to avoid log(0) and floor at -80 dB rms = 20.0f * std::log10(rms + 1e-8f); - rms = std::max(rms, -80.0); + rms = std::max(rms, -80.0f); // Update the results results.rmsMean.set(rms, true); diff --git a/source/FeatureExtraction/FeatureValue.h b/source/FeatureExtraction/FeatureValue.h index 2bd1f8b..f035066 100644 --- a/source/FeatureExtraction/FeatureValue.h +++ b/source/FeatureExtraction/FeatureValue.h @@ -43,8 +43,11 @@ class FeatureValue // Get the normalized value T getNormalized() const { - if (minValue == maxValue || minValue == std::numeric_limits::max()) + // If the minimum / maximum values have yet to be set (or not different) + if (juce::approximatelyEqual(minValue, maxValue) || juce::approximatelyEqual(minValue, std::numeric_limits::max())) + { return 0.5; + } T normalized = (value - minValue) / (maxValue - minValue); return juce::jlimit(static_cast(0.0), static_cast(1.0), normalized); diff --git a/source/FeatureExtraction/SpectralExtractor.h b/source/FeatureExtraction/SpectralExtractor.h index 61e4e2c..9f321b8 100644 --- a/source/FeatureExtraction/SpectralExtractor.h +++ b/source/FeatureExtraction/SpectralExtractor.h @@ -24,15 +24,15 @@ class SpectralExtractor fftSize = size; // Initialize FFT - int fftOrder = std::log2(fftSize); + int fftOrder = (int) std::log2(fftSize); fft = std::make_unique(fftOrder); - fftBuffer.resize(fftSize * 2); - fftWindow.resize(fftSize); + fftBuffer.resize((size_t) fftSize * 2); + fftWindow.resize((size_t) fftSize); // Initialize window function juce::dsp::WindowingFunction::fillWindowingTables( fftWindow.data(), - fftSize, + (size_t) fftSize, juce::dsp::WindowingFunction::hann, false); @@ -49,8 +49,8 @@ class SpectralExtractor return; // Apply window function and copy to FFT buffer - for (int i = 0; i < fftSize; ++i) - fftBuffer[i] = buffer.getSample(0, i) * fftWindow[i]; + for (size_t i = 0; i < (size_t) fftSize; ++i) + fftBuffer[i] = buffer.getSample(0, (int) i) * fftWindow[i]; // Perform FFT fft->performFrequencyOnlyForwardTransform(fftBuffer.data(), true); @@ -66,8 +66,8 @@ class SpectralExtractor // Calculate spectral centroid based on current frequency magnitude buffer float weightedSum = 0.0; float norm = 0.0; - int realSize = (fftSize / 2) + 1; - for (int n = 0; n < realSize; n++) + size_t realSize = (size_t) (fftSize / 2) + 1; + for (size_t n = 0; n < realSize; n++) { jassert(fftBuffer[n] >= 0.0f); weightedSum += n * fftBuffer[n]; @@ -80,7 +80,7 @@ class SpectralExtractor float centroid = weightedSum / norm; // Convert to Hz - centroid = centroid * sampleRate / (float) fftSize; + centroid = centroid * (float) sampleRate / fftSize; // Convert to semitones centroid = 12.0f * std::log2(centroid / 440.0f) + 69.0f; @@ -92,8 +92,8 @@ class SpectralExtractor { float geometricMean = 0.0; float arithmeticMean = 0.0; - int realSize = (fftSize / 2) + 1; - for (int n = 0; n < realSize; n++) + size_t realSize = (size_t) (fftSize / 2) + 1; + for (size_t n = 0; n < realSize; n++) { float power = std::max(fftBuffer[n] * fftBuffer[n], 1e-10f); geometricMean += std::log(power); @@ -106,7 +106,7 @@ class SpectralExtractor arithmeticMean /= (float) realSize; float flatness = geometricMean / arithmeticMean; - flatness = 20.0 * std::log10(flatness); + flatness = 20.0f * std::log10(flatness); return flatness; } diff --git a/source/NeuralNetwork.cpp b/source/NeuralNetwork.cpp index c3878ca..009e427 100644 --- a/source/NeuralNetwork.cpp +++ b/source/NeuralNetwork.cpp @@ -29,18 +29,18 @@ void NeuralNetwork::process(const std::vector& input, std::vector(); + output[i] = outputTensor[0][(int64_t) i].item(); } } @@ -54,9 +54,9 @@ void NeuralNetwork::getCurrentPatch(std::vector par } jassert(currentPatch.size() == parameters.size()); - for (int i = 0; i < parameters.size(); ++i) + for (size_t i = 0; i < parameters.size(); ++i) { - parameters[i]->setValueNotifyingHost(currentPatch[i]); + parameters[i]->setValueNotifyingHost((float) currentPatch[i]); } } @@ -95,10 +95,10 @@ void NeuralNetwork::_testModel() // Update the current patch from network output currentPatch.clear(); - currentPatch.resize(outputFeatures); - for (int i = 0; i < outputFeatures; ++i) + currentPatch.resize((size_t) outputFeatures); + for (size_t i = 0; i < (size_t) outputFeatures; ++i) { - currentPatch[i] = output[1][i].item(); + currentPatch[i] = output[1][(int64_t) i].item(); } } catch (const c10::Error& e) diff --git a/source/PluginEditor.cpp b/source/PluginEditor.cpp index 77fcc3b..3590bf3 100644 --- a/source/PluginEditor.cpp +++ b/source/PluginEditor.cpp @@ -2,7 +2,7 @@ #include "PluginProcessor.h" TorchDrumEditor::TorchDrumEditor(TorchDrumProcessor& p) - : AudioProcessorEditor(&p), processor(p) + : AudioProcessorEditor(&p), drumProcessor(p) { fileChooser = std::make_unique( "File Browser", @@ -28,25 +28,25 @@ TorchDrumEditor::TorchDrumEditor(TorchDrumProcessor& p) // Setup the reset normalizer button resetNormButton.onClick = [this] { - processor.getSynthController().resetFeatureNormalizers(); + drumProcessor.getSynthController().resetFeatureNormalizers(); }; // Add the action listener to the SynthController - processor.getSynthController().getBroadcaster().addActionListener(this); + drumProcessor.getSynthController().getBroadcaster().addActionListener(this); setSize(400, 600); } TorchDrumEditor::~TorchDrumEditor() { - processor.getSynthController().getBroadcaster().removeActionListener(this); + drumProcessor.getSynthController().getBroadcaster().removeActionListener(this); } void TorchDrumEditor::chooserCallback(const juce::FileChooser& chooser) { auto result = chooser.getResult(); auto resultPath = result.getFullPathName().toStdString(); - processor.getSynthController().updateModel(resultPath); + drumProcessor.getSynthController().updateModel(resultPath); } void TorchDrumEditor::paint(juce::Graphics& g) @@ -54,7 +54,7 @@ void TorchDrumEditor::paint(juce::Graphics& g) g.fillAll( getLookAndFeel().findColour(juce::ResizableWindow::backgroundColourId)); - auto& results = processor.getSynthController().getFeatures(); + auto& results = drumProcessor.getSynthController().getFeatures(); float rms = results.rmsMean.getNormalized(); float sc = results.spectralCentroidMean.getNormalized(); float sf = results.spectralFlatnessMean.getNormalized(); diff --git a/source/PluginEditor.h b/source/PluginEditor.h index e0369b1..c8911a0 100644 --- a/source/PluginEditor.h +++ b/source/PluginEditor.h @@ -28,8 +28,8 @@ class TorchDrumEditor : public juce::AudioProcessorEditor, juce::ActionListener juce::File getPresetFolder(); - TorchDrumProcessor& processor; - juce::GenericAudioProcessorEditor editor { processor }; + TorchDrumProcessor& drumProcessor; + juce::GenericAudioProcessorEditor editor { drumProcessor }; juce::TextButton loadModelButton { "Load Model" }; juce::TextButton resetNormButton { "Reset Normalizer" }; std::unique_ptr fileChooser; diff --git a/source/Synth/Modules/Noise.h b/source/Synth/Modules/Noise.h index 4574e2b..cedccea 100644 --- a/source/Synth/Modules/Noise.h +++ b/source/Synth/Modules/Noise.h @@ -14,6 +14,6 @@ class WhiteNoise float process() { - return 2.0 * ((float) rand() / (float) RAND_MAX) - 1.0; + return (float) 2 * rand() / RAND_MAX - 1; } }; diff --git a/source/Synth/Modules/Tonal.h b/source/Synth/Modules/Tonal.h index fe5a2a9..3745db6 100644 --- a/source/Synth/Modules/Tonal.h +++ b/source/Synth/Modules/Tonal.h @@ -20,7 +20,7 @@ class Tonal void setTuning(float newValue); // Destructor - ~Tonal() {}; + ~Tonal() {} private: double sampleRate; diff --git a/source/Synth/Snare808.cpp b/source/Synth/Snare808.cpp index f9a1205..c674132 100644 --- a/source/Synth/Snare808.cpp +++ b/source/Synth/Snare808.cpp @@ -84,7 +84,7 @@ float Snare808::process() // Noise Signal float n = noise.process(); - n = noiseFilter.process(n); + n = (float) noiseFilter.process(n); n = n * noiseEnv.process(); n = noiseGain.process(n); @@ -109,7 +109,7 @@ void Snare808::trigger() // Update the filter frequency void Snare808::updateFilterFreq(float value) { - float nyquist = 0.5f * sampleRate; + float nyquist = 0.5f * (float) sampleRate; float freq = std::min(value, nyquist); noiseFilter.setFc(freq); } diff --git a/source/Synth/SynthParameterBase.h b/source/Synth/SynthParameterBase.h index 51e703c..0633500 100644 --- a/source/Synth/SynthParameterBase.h +++ b/source/Synth/SynthParameterBase.h @@ -33,7 +33,7 @@ struct SynthParameterBase void updateAllParameters() { - for (int i = 0; i < parameters.size(); ++i) + for (size_t i = 0; i < parameters.size(); ++i) { auto* param = parameters[i]; auto& callback = callbacks[i]; @@ -44,7 +44,7 @@ struct SynthParameterBase void updateAllParametersWithModulation(const std::vector& modulation, float sensitivity = 1.0f) { jassert(modulation.size() == parameters.size()); - for (int i = 0; i < parameters.size(); ++i) + for (size_t i = 0; i < parameters.size(); ++i) { auto* param = parameters[i]; auto& callback = callbacks[i]; @@ -54,7 +54,7 @@ struct SynthParameterBase } } - void addCallback(int index, std::function callback) + void addCallback(size_t index, std::function callback) { jassert(index < callbacks.size()); callbacks[index] = callback; diff --git a/source/SynthController.cpp b/source/SynthController.cpp index 6996149..e5d8310 100644 --- a/source/SynthController.cpp +++ b/source/SynthController.cpp @@ -1,7 +1,7 @@ #include "SynthController.h" -SynthController::SynthController(SynthBase& synth, Parameters& params) - : synth(synth), parameters(params), modelPath("") +SynthController::SynthController(SynthBase& synthesizer, Parameters& params) + : synth(synthesizer), parameters(params), modelPath("") { // Prepare input and output features for NN size_t numSynthParams = synth.getParameters().parameters.size(); @@ -9,7 +9,7 @@ SynthController::SynthController(SynthBase& synth, Parameters& params) neuralOutput.resize(numSynthParams); // Update input and output features for the neural network - neuralMapper.setInOutFeatures(3, numSynthParams); + neuralMapper.setInOutFeatures(3, (int) numSynthParams); } void SynthController::prepare(double sr, int samplesPerBlock) From d444ab1a68589b984e72d5b8e19809acda8aa0b1 Mon Sep 17 00:00:00 2001 From: jorshi Date: Mon, 1 Jul 2024 15:31:22 +0100 Subject: [PATCH 3/5] trying to limit line length --- source/.clang-format | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/.clang-format b/source/.clang-format index 94664bb..da04525 100644 --- a/source/.clang-format +++ b/source/.clang-format @@ -24,7 +24,7 @@ BreakBeforeBraces: Allman BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false BreakStringLiterals: false -ColumnLimit: 0 +ColumnLimit: 88 ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 From 92860f0ea5101678d75989e7b947129a172fa391 Mon Sep 17 00:00:00 2001 From: jorshi Date: Mon, 1 Jul 2024 15:32:14 +0100 Subject: [PATCH 4/5] splitting long line --- source/FeatureExtraction/FeatureValue.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/FeatureExtraction/FeatureValue.h b/source/FeatureExtraction/FeatureValue.h index f035066..261210a 100644 --- a/source/FeatureExtraction/FeatureValue.h +++ b/source/FeatureExtraction/FeatureValue.h @@ -44,7 +44,8 @@ class FeatureValue T getNormalized() const { // If the minimum / maximum values have yet to be set (or not different) - if (juce::approximatelyEqual(minValue, maxValue) || juce::approximatelyEqual(minValue, std::numeric_limits::max())) + if (juce::approximatelyEqual(minValue, maxValue) + || juce::approximatelyEqual(minValue, std::numeric_limits::max())) { return 0.5; } From df2aa030f3421f5cb3ed01dda857eddd55973e4e Mon Sep 17 00:00:00 2001 From: jorshi Date: Mon, 1 Jul 2024 15:57:51 +0100 Subject: [PATCH 5/5] torchdrumlib ignores --- .vscode/settings.json | 9 +++++++-- source/Utils/NeuralNetworkMock.cpp | 6 +++++- source/Utils/NeuralNetworkMock.h | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 87a5ec6..28134b6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,11 @@ "cmake.configureOnOpen": true, "cmake.cmakePath": "/opt/homebrew/bin/cmake", "cmake.configureArgs": [ - "-DCMAKE_PREFIX_PATH=/Users/jordanm/anaconda3/envs/torchdrum/lib/python3.10/site-packages/torch/share/cmake" - ] + "-DCMAKE_PREFIX_PATH=/Users/jordanm/anaconda3/envs/torchdrum/lib/python3.10/site-packages/torch/share/cmake", + "-DBUILD_TORCHDRUM_LIB=ON" + ], + "files.associations": { + "*.m": "matlab", + "vector": "cpp" + } } diff --git a/source/Utils/NeuralNetworkMock.cpp b/source/Utils/NeuralNetworkMock.cpp index d8332b2..28c2482 100644 --- a/source/Utils/NeuralNetworkMock.cpp +++ b/source/Utils/NeuralNetworkMock.cpp @@ -2,15 +2,19 @@ bool NeuralNetwork::loadModel(const std::string& path) { + (void) path; // Unused in mock modelLoaded = true; return modelLoaded; } -void NeuralNetwork::process(const std::vector& input, std::vector& output) +void NeuralNetwork::process(const std::vector& input, + std::vector& output) { + (void) input; // Unused in mock std::fill(output.begin(), output.end(), 0.0); } void NeuralNetwork::_loadModel(const std::string& path) { + (void) path; // Unused in mock } diff --git a/source/Utils/NeuralNetworkMock.h b/source/Utils/NeuralNetworkMock.h index ac6c090..c09636e 100644 --- a/source/Utils/NeuralNetworkMock.h +++ b/source/Utils/NeuralNetworkMock.h @@ -16,7 +16,10 @@ class NeuralNetwork bool loadModel(const std::string& path); void process(const std::vector& input, std::vector& output); - void getCurrentPatch(std::vector parameters) {} + void getCurrentPatch(std::vector parameters) + { + (void) parameters; // Unused in mock + } void setInOutFeatures(int in, int out) {