Skip to content

Commit

Permalink
Merge pull request #20 from jorshi/fix/warnings
Browse files Browse the repository at this point in the history
Fix/warnings
  • Loading branch information
jorshi authored Jul 1, 2024
2 parents 34e0058 + df2aa03 commit 2c776ec
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 72 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"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",
"-DBUILD_TORCHDRUM_LIB=ON"
],
"files.associations": {
"*.m": "matlab",
"vector": "cpp"
}
}
3 changes: 1 addition & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]

}
}
2 changes: 1 addition & 1 deletion source/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakStringLiterals: false
ColumnLimit: 0
ColumnLimit: 88
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Expand Down
21 changes: 11 additions & 10 deletions source/Biquad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ template <typename T>
void BiquadCoeffT<T>::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:
Expand Down Expand Up @@ -85,39 +86,39 @@ void BiquadCoeffT<T>::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;
}
break;
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;
}
Expand Down
26 changes: 13 additions & 13 deletions source/Biquad.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -105,7 +105,7 @@ class BiquadCoeffT : public BiquadCoeff
class Biquad : public BiquadCoeffT<double>
{
public:
Biquad() {};
Biquad() {}
Biquad(const Settings& s) { setup(s); }
/**
* Process one input sample and return one output sample.
Expand Down
4 changes: 2 additions & 2 deletions source/FeatureExtraction/FeatureExtraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void FeatureExtraction::process(const juce::AudioBuffer<float>& 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)
{
Expand All @@ -35,7 +35,7 @@ void FeatureExtraction::process(const juce::AudioBuffer<float>& 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);
Expand Down
6 changes: 5 additions & 1 deletion source/FeatureExtraction/FeatureValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ class FeatureValue
// Get the normalized value
T getNormalized() const
{
if (minValue == maxValue || minValue == std::numeric_limits<T>::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<T>::max()))
{
return 0.5;
}

T normalized = (value - minValue) / (maxValue - minValue);
return juce::jlimit(static_cast<T>(0.0), static_cast<T>(1.0), normalized);
Expand Down
24 changes: 12 additions & 12 deletions source/FeatureExtraction/SpectralExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<juce::dsp::FFT>(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<float>::fillWindowingTables(
fftWindow.data(),
fftSize,
(size_t) fftSize,
juce::dsp::WindowingFunction<float>::hann,
false);

Expand All @@ -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);
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
20 changes: 10 additions & 10 deletions source/NeuralNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ void NeuralNetwork::process(const std::vector<double>& input, std::vector<double

// Run prediction with model
auto& inputTensor = inputs[0].toTensor();
for (int i = 0; i < input.size(); ++i)
for (size_t i = 0; i < input.size(); ++i)
{
inputTensor[0][i] = input[i];
inputTensor[0][(int64_t) i] = input[i];
}

auto outputTensor = model.forward(inputs).toTensor();
jassert(outputTensor.sizes().size() == 2);
jassert(outputTensor.sizes()[1] == output.size());
jassert(outputTensor.sizes()[1] == (int64_t) output.size());

for (int i = 0; i < output.size(); ++i)
for (size_t i = 0; i < output.size(); ++i)
{
output[i] = outputTensor[0][i].item<double>();
output[i] = outputTensor[0][(int64_t) i].item<double>();
}
}

Expand All @@ -54,9 +54,9 @@ void NeuralNetwork::getCurrentPatch(std::vector<juce::RangedAudioParameter*> 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]);
}
}

Expand Down Expand Up @@ -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<double>();
currentPatch[i] = output[1][(int64_t) i].item<double>();
}
}
catch (const c10::Error& e)
Expand Down
12 changes: 6 additions & 6 deletions source/PluginEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "PluginProcessor.h"

TorchDrumEditor::TorchDrumEditor(TorchDrumProcessor& p)
: AudioProcessorEditor(&p), processor(p)
: AudioProcessorEditor(&p), drumProcessor(p)
{
fileChooser = std::make_unique<juce::FileChooser>(
"File Browser",
Expand All @@ -28,33 +28,33 @@ 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)
{
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();
Expand Down
4 changes: 2 additions & 2 deletions source/PluginEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<juce::FileChooser> fileChooser;
Expand Down
2 changes: 1 addition & 1 deletion source/Synth/Modules/Noise.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
2 changes: 1 addition & 1 deletion source/Synth/Modules/Tonal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Tonal
void setTuning(float newValue);

// Destructor
~Tonal() {};
~Tonal() {}

private:
double sampleRate;
Expand Down
4 changes: 2 additions & 2 deletions source/Synth/Snare808.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 2c776ec

Please sign in to comment.