Skip to content

Commit

Permalink
feat: complete tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
zsliu98 committed Dec 25, 2023
1 parent fb98556 commit 6960530
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
71 changes: 71 additions & 0 deletions source/dsp/compressor/tracker/rms_tracker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ==============================================================================
// Copyright (C) 2023 - zsliu98
// This file is part of ZLEComp
//
// ZLEComp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// ZLEComp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with ZLEComp. If not, see <https://www.gnu.org/licenses/>.
// ==============================================================================

#include "rms_tracker.h"

namespace zlCompressor {

template<typename FloatType>
RMSTracker<FloatType>::~RMSTracker() {
loudnessBuffer.clear();
}

template<typename FloatType>
void RMSTracker<FloatType>::prepare(const juce::dsp::ProcessSpec &spec) {
secondPerBuffer = static_cast<FloatType>(spec.maximumBlockSize) / static_cast<FloatType>(spec.sampleRate);
reset();
}

template<typename FloatType>
void RMSTracker<FloatType>::reset() {
loudnessBuffer.clear();
mLoudness = 0;
iLoudness = 0;
numBuffer = 0;
}

template<typename FloatType>
void RMSTracker<FloatType>::setMomentarySize(size_t mSize) {
while (loudnessBuffer.size() > mSize) {
mLoudness -= loudnessBuffer.front();
loudnessBuffer.pop_front();
}
loudnessBuffer.set_capacity(mSize);
}

template<typename FloatType>
void RMSTracker<FloatType>::process(const juce::AudioBuffer<FloatType> &buffer) {
FloatType _ms = 0;
for (auto channel = 0; channel < buffer.getNumChannels(); channel++) {
auto data = buffer.getReadPointer(channel);
for (auto i = 0; i < buffer.getNumSamples(); i++) {
_ms += data[i] * data[i];
}
}

_ms = _ms / static_cast<FloatType> (buffer.getNumSamples());

if (loudnessBuffer.size() == loudnessBuffer.capacity()) {
mLoudness -= loudnessBuffer.front();
}

loudnessBuffer.push_back(_ms);
mLoudness += _ms;

iLoudness += _ms;
numBuffer += 1;
}

template
class RMSTracker<float>;

template
class RMSTracker<double>;
} // zldetector
73 changes: 73 additions & 0 deletions source/dsp/compressor/tracker/rms_tracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// ==============================================================================
// Copyright (C) 2023 - zsliu98
// This file is part of ZLEComp
//
// ZLEComp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// ZLEComp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with ZLEComp. If not, see <https://www.gnu.org/licenses/>.
// ==============================================================================

#ifndef ZLECOMP_RMS_TRACKER_H
#define ZLECOMP_RMS_TRACKER_H

#include <boost/circular_buffer.hpp>

#include "virtual_tracker.h"

namespace zlCompressor {

template<typename FloatType>
class RMSTracker : VirtualTracker<FloatType> {
public:
RMSTracker() = default;

~RMSTracker() override;

void prepare(const juce::dsp::ProcessSpec &spec) override;

void reset() override;

void setMomentarySize(size_t mSize) override;

inline size_t getMomentarySize() {
return loudnessBuffer.capacity();
}

inline FloatType getBufferPeak() override {
return juce::Decibels::gainToDecibels(peak);
}

inline FloatType getMomentaryLoudness() override {
FloatType meanSquare = 0;
if (loudnessBuffer.size() > 0) {
meanSquare = mLoudness / static_cast<FloatType>(loudnessBuffer.size());
}
return juce::Decibels::gainToDecibels(meanSquare) * static_cast<FloatType>(0.5);
}

inline FloatType getIntegratedLoudness() override {
FloatType meanSquare = 0;
if (numBuffer > 0) {
meanSquare = iLoudness / static_cast<FloatType>(numBuffer);
}
return secondPerBuffer * juce::Decibels::gainToDecibels(meanSquare) *
static_cast<FloatType>(0.5);
}

inline FloatType getIntegratedTotalLoudness() override {
return getIntegratedLoudness() * static_cast<FloatType>(numBuffer);
}

void process(const juce::AudioBuffer<FloatType> &buffer) override;

private:
size_t numBuffer = 0;
FloatType peak = 0, mLoudness = 0, iLoudness = 0;
FloatType secondPerBuffer = FloatType(0.01);
boost::circular_buffer<FloatType> loudnessBuffer;
};

} // zldetector

#endif //ZLECOMP_RMS_TRACKER_H
15 changes: 15 additions & 0 deletions source/dsp/compressor/tracker/tracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (C) 2023 - zsliu98
// This file is part of ZLEqualizer
//
// ZLEqualizer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// ZLEqualizer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with ZLEqualizer. If not, see <https://www.gnu.org/licenses/>.

#ifndef ZLEQUALIZER_TRACKER_H
#define ZLEQUALIZER_TRACKER_H

#include "rms_tracker.h"

#endif //ZLEQUALIZER_TRACKER_H
44 changes: 44 additions & 0 deletions source/dsp/compressor/tracker/virtual_tracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// ==============================================================================
// Copyright (C) 2023 - zsliu98
// This file is part of ZLEComp
//
// ZLEComp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// ZLEComp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with ZLEComp. If not, see <https://www.gnu.org/licenses/>.
// ==============================================================================

#ifndef ZLECOMP_TRACKER_H
#define ZLECOMP_TRACKER_H

#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_dsp/juce_dsp.h>

namespace zlCompressor {

template<typename FloatType>
class VirtualTracker {
public:
VirtualTracker() = default;

virtual ~VirtualTracker() = default;

virtual void prepare(const juce::dsp::ProcessSpec &spec) = 0;

virtual void reset() = 0;

virtual void setMomentarySize(size_t mSize) = 0;

virtual inline FloatType getBufferPeak() = 0;

virtual inline FloatType getMomentaryLoudness() = 0;

virtual inline FloatType getIntegratedLoudness() = 0;

virtual inline FloatType getIntegratedTotalLoudness() = 0;

virtual void process(const juce::AudioBuffer<FloatType> &buffer) = 0;
};
}

#endif //ZLECOMP_TRACKER_H

0 comments on commit 6960530

Please sign in to comment.