-
Notifications
You must be signed in to change notification settings - Fork 12
True Peak Computation #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
common/processors/mix_monitoring/loudness_standards/TruePeak.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "TruePeak.h" | ||
|
|
||
| void TruePeak::reset(const double sampleRate, | ||
| const juce::AudioChannelSet& channelSet) { | ||
| channelSet_ = channelSet; | ||
| // Initialize history arrays for each channel | ||
| channelHistory_.clear(); | ||
| channelHistory_.resize(channelSet.size()); | ||
| for (auto& history : channelHistory_) { | ||
| history.fill(0.0f); | ||
| } | ||
| currentTruePeak_ = -std::numeric_limits<float>::infinity(); | ||
| } | ||
|
|
||
| float TruePeak::compute(const juce::AudioBuffer<float>& buffer) { | ||
| if (channelSet_.isDisabled()) { | ||
| return -1000; | ||
| } | ||
| if (buffer.getNumChannels() != channelSet_.size()) { | ||
| return -1000; | ||
| } | ||
|
|
||
| currentTruePeak_ = -std::numeric_limits<float>::infinity(); | ||
|
|
||
| // Process each channel | ||
| for (int ch = 0; ch < buffer.getNumChannels(); ++ch) { | ||
| // Skip LFE channel | ||
| if (channelSet_.getTypeOfChannel(ch) == juce::AudioChannelSet::LFE) { | ||
| continue; | ||
| } | ||
|
|
||
| const float* inputData = buffer.getReadPointer(ch); | ||
| int numSamples = buffer.getNumSamples(); | ||
| auto& history = channelHistory_[ch]; | ||
|
|
||
| // Phase 1: Process the first 11 samples (requires history) | ||
| int overlapCount = std::min(numSamples, kHistorySize); | ||
| for (int i = 0; i < overlapCount; ++i) { | ||
| processSingleSampleWithHistory(inputData, i, history); | ||
| } | ||
|
|
||
| // Phase 2: Process the rest of the block | ||
| for (int i = kHistorySize; i < numSamples; ++i) { | ||
| processSingleSampleLinear(inputData, i); | ||
| } | ||
|
|
||
| // Phase 3: Save the last 11 samples for the next block's history | ||
| if (numSamples >= kHistorySize) { | ||
| for (int i = 0; i < kHistorySize; ++i) { | ||
| history[i] = inputData[numSamples - kHistorySize + i]; | ||
| } | ||
| } else { | ||
| // Edge case: Block size is smaller than 11 | ||
| // Shift history left and append new samples | ||
| int shift = kHistorySize - numSamples; | ||
| for (int i = 0; i < shift; ++i) { | ||
| history[i] = history[i + numSamples]; | ||
| } | ||
| for (int i = 0; i < numSamples; ++i) { | ||
| history[shift + i] = inputData[i]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Convert to dB and sanitize outputs | ||
| if (currentTruePeak_ > 0.0f) { | ||
| float truePeakdB = 20.0f * std::log10(currentTruePeak_); | ||
| if (truePeakdB > 15.0f) { | ||
| return std::numeric_limits<float>::quiet_NaN(); | ||
| } | ||
| return truePeakdB; | ||
| } | ||
|
|
||
| return -std::numeric_limits<float>::infinity(); | ||
| } | ||
|
|
||
| void TruePeak::processSingleSampleLinear(const float* data, int currentIndex) { | ||
| // Calculate the 4 upsampled points directly from the linear buffer | ||
| float out0 = 0.0f, out1 = 0.0f, out2 = 0.0f, out3 = 0.0f; | ||
|
|
||
| for (int m = 0; m < kTapsPerPhase; ++m) { | ||
| float sample = data[currentIndex - m]; | ||
| out0 += kPhase0[m] * sample; | ||
| out1 += kPhase1[m] * sample; | ||
| out2 += kPhase2[m] * sample; | ||
| out3 += kPhase3[m] * sample; | ||
| } | ||
|
|
||
| updatePeak(out0, out1, out2, out3); | ||
| } | ||
|
|
||
| void TruePeak::processSingleSampleWithHistory( | ||
| const float* data, int currentIndex, | ||
| const std::array<float, kHistorySize>& history) { | ||
| // Calculate the 4 upsampled points using history when necessary | ||
| float out0 = 0.0f, out1 = 0.0f, out2 = 0.0f, out3 = 0.0f; | ||
|
|
||
| for (int m = 0; m < kTapsPerPhase; ++m) { | ||
| float sample; | ||
| int index = currentIndex - m; | ||
| if (index >= 0) { | ||
| sample = data[index]; | ||
| } else { | ||
| // Read from history array | ||
| sample = history[kHistorySize + index]; | ||
| } | ||
|
|
||
| out0 += kPhase0[m] * sample; | ||
| out1 += kPhase1[m] * sample; | ||
| out2 += kPhase2[m] * sample; | ||
| out3 += kPhase3[m] * sample; | ||
| } | ||
|
|
||
| updatePeak(out0, out1, out2, out3); | ||
| } | ||
|
|
||
| void TruePeak::updatePeak(float o0, float o1, float o2, float o3) { | ||
| currentTruePeak_ = std::max({currentTruePeak_, std::abs(o0), std::abs(o1), | ||
| std::abs(o2), std::abs(o3)}); | ||
| } |
73 changes: 73 additions & 0 deletions
73
common/processors/mix_monitoring/loudness_standards/TruePeak.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <juce_audio_basics/juce_audio_basics.h> | ||
|
|
||
| #include <vector> | ||
|
|
||
| /** | ||
| * @brief Computes the `true peak` measurement described in ITU-R BS.1770-4. | ||
| * Uses a polyphase filter approach similar to the FFmpeg. | ||
| * | ||
| */ | ||
|
|
||
| class TruePeak { | ||
| public: | ||
| float compute(const juce::AudioBuffer<float>& buffer); | ||
| void reset(const double sampleRate, const juce::AudioChannelSet& channelSet); | ||
|
|
||
| private: | ||
| // ITU-R BS.1770-4 48-tap FIR filter coefficients split into 4 phases of 12 | ||
| // taps | ||
| static constexpr int kHistorySize = 11; | ||
| static constexpr int kTapsPerPhase = 12; | ||
| static constexpr int kNumPhases = 4; | ||
|
|
||
| // The 4 polyphase filter coefficient arrays (12 taps each) | ||
| static constexpr float kPhase0[kTapsPerPhase] = { | ||
| 0.0017089843750f, 0.0109863281250f, -0.0196533203125f, | ||
| 0.0332031250000f, -0.0594482421875f, 0.1373291015625f, | ||
| 0.9721679687500f, -0.1022949218750f, 0.0476074218750f, | ||
| -0.0266113281250f, 0.0148925781250f, -0.0083007812500f}; | ||
|
|
||
| static constexpr float kPhase1[kTapsPerPhase] = { | ||
| -0.0291748046875f, 0.0292968750000f, -0.0517578125000f, | ||
| 0.0891113281250f, -0.1665039062500f, 0.4650878906250f, | ||
| 0.7797851562500f, -0.2003173828125f, 0.1015625000000f, | ||
| -0.0582275390625f, 0.0330810546875f, -0.0189208984375f}; | ||
|
|
||
| static constexpr float kPhase2[kTapsPerPhase] = { | ||
| -0.0189208984375f, 0.0330810546875f, -0.0582275390625f, | ||
| 0.1015625000000f, -0.2003173828125f, 0.7797851562500f, | ||
| 0.4650878906250f, -0.1665039062500f, 0.0891113281250f, | ||
| -0.0517578125000f, 0.0292968750000f, -0.0291748046875f}; | ||
|
|
||
| static constexpr float kPhase3[kTapsPerPhase] = { | ||
| -0.0083007812500f, 0.0148925781250f, -0.0266113281250f, | ||
| 0.0476074218750f, -0.1022949218750f, 0.9721679687500f, | ||
| 0.1373291015625f, -0.0594482421875f, 0.0332031250000f, | ||
| -0.0196533203125f, 0.0109863281250f, 0.0017089843750f}; | ||
|
|
||
| void processSingleSampleLinear(const float* data, int currentIndex); | ||
| void processSingleSampleWithHistory( | ||
| const float* data, int currentIndex, | ||
| const std::array<float, kHistorySize>& history); | ||
| void updatePeak(float o0, float o1, float o2, float o3); | ||
|
|
||
| juce::AudioChannelSet channelSet_ = juce::AudioChannelSet::disabled(); | ||
| std::vector<std::array<float, kHistorySize>> channelHistory_; | ||
| float currentTruePeak_ = -std::numeric_limits<float>::infinity(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These debug logs were adding a lot of unhelpful noise so I'm just removing them (which I believe is permissible under this license?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is fine