|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the YUP library. |
| 5 | + Copyright (c) 2026 - kunitoki@gmail.com |
| 6 | +
|
| 7 | + YUP is an open source library subject to open-source licensing. |
| 8 | +
|
| 9 | + The code included in this file is provided under the terms of the ISC license |
| 10 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 11 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 12 | + without fee is hereby granted provided that the above copyright notice and |
| 13 | + this permission notice appear in all copies. |
| 14 | +
|
| 15 | + YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER |
| 16 | + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE |
| 17 | + DISCLAIMED. |
| 18 | +
|
| 19 | + ============================================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +namespace yup |
| 23 | +{ |
| 24 | + |
| 25 | +void FilterBank::build (int bandsPerOctave, float fMin, float fMax, int numFFTBins_, float sampleRate, bool equalizeArea) |
| 26 | +{ |
| 27 | + jassert (bandsPerOctave > 0); |
| 28 | + jassert (numFFTBins_ > 0); |
| 29 | + jassert (sampleRate > 0.0f); |
| 30 | + |
| 31 | + numFFTBins = numFFTBins_; |
| 32 | + |
| 33 | + const auto nyquist = sampleRate * 0.5f; |
| 34 | + if (fMax > nyquist) |
| 35 | + fMax = nyquist; |
| 36 | + |
| 37 | + auto frequencies = generateFrequencies (bandsPerOctave, fMin, fMax); |
| 38 | + |
| 39 | + const float factor = nyquist / static_cast<float> (numFFTBins); |
| 40 | + |
| 41 | + for (auto& f : frequencies) |
| 42 | + f = std::round (f / factor); |
| 43 | + |
| 44 | + std::vector<float> uniqueFrequencies; |
| 45 | + { |
| 46 | + float prev = -1.0f; |
| 47 | + for (auto f : frequencies) |
| 48 | + { |
| 49 | + if (f != prev) |
| 50 | + uniqueFrequencies.push_back (f); |
| 51 | + prev = f; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + frequencies = std::move (uniqueFrequencies); |
| 56 | + frequencies.erase ( |
| 57 | + std::remove_if (frequencies.begin(), frequencies.end(), [&] (float f) |
| 58 | + { |
| 59 | + return f >= static_cast<float> (numFFTBins); |
| 60 | + }), |
| 61 | + frequencies.end()); |
| 62 | + |
| 63 | + const int bands = static_cast<int> (frequencies.size()) - 2; |
| 64 | + jassert (bands >= 3); |
| 65 | + |
| 66 | + matrix.assign (static_cast<std::size_t> (numFFTBins) * static_cast<std::size_t> (bands), 0.0f); |
| 67 | + numBands = bands; |
| 68 | + |
| 69 | + for (int band = 0; band < bands; ++band) |
| 70 | + { |
| 71 | + const int start = static_cast<int> (frequencies[static_cast<std::size_t> (band)]); |
| 72 | + const int mid = static_cast<int> (frequencies[static_cast<std::size_t> (band) + 1]); |
| 73 | + const int stop = static_cast<int> (frequencies[static_cast<std::size_t> (band) + 2]); |
| 74 | + |
| 75 | + if (mid <= start || stop <= mid) |
| 76 | + continue; |
| 77 | + |
| 78 | + const float height = equalizeArea ? 2.0f / static_cast<float> (stop - start) : 1.0f; |
| 79 | + |
| 80 | + for (int bin = start; bin < mid; ++bin) |
| 81 | + { |
| 82 | + const float t = static_cast<float> (bin - start) / static_cast<float> (mid - start); |
| 83 | + matrix[static_cast<std::size_t> (bin) * static_cast<std::size_t> (numBands) |
| 84 | + + static_cast<std::size_t> (band)] = t * height; |
| 85 | + } |
| 86 | + |
| 87 | + for (int bin = mid; bin < stop; ++bin) |
| 88 | + { |
| 89 | + const float t = static_cast<float> (bin - mid) / static_cast<float> (stop - mid); |
| 90 | + matrix[static_cast<std::size_t> (bin) * static_cast<std::size_t> (numBands) |
| 91 | + + static_cast<std::size_t> (band)] = (1.0f - t) * height; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void FilterBank::applySingleFrame (const float* magnitudeIn, float* magnitudeOut) const noexcept |
| 97 | +{ |
| 98 | + jassert (magnitudeIn != nullptr && magnitudeOut != nullptr); |
| 99 | + |
| 100 | + for (int band = 0; band < numBands; ++band) |
| 101 | + { |
| 102 | + float sum = 0.0f; |
| 103 | + |
| 104 | + for (int bin = 0; bin < numFFTBins; ++bin) |
| 105 | + sum += magnitudeIn[bin] * matrix[static_cast<std::size_t> (bin) * static_cast<std::size_t> (numBands) + static_cast<std::size_t> (band)]; |
| 106 | + |
| 107 | + magnitudeOut[band] = sum; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +void FilterBank::applyMultipleFrames (const float* spectrogram, float* filtered, int numFrames) const noexcept |
| 112 | +{ |
| 113 | + jassert (spectrogram != nullptr && filtered != nullptr); |
| 114 | + jassert (numFrames > 0); |
| 115 | + |
| 116 | + for (int frame = 0; frame < numFrames; ++frame) |
| 117 | + { |
| 118 | + applySingleFrame (spectrogram + static_cast<std::size_t> (frame) * static_cast<std::size_t> (numFFTBins), |
| 119 | + filtered + static_cast<std::size_t> (frame) * static_cast<std::size_t> (numBands)); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +std::vector<float> FilterBank::generateFrequencies (int bandsPerOctave, float fMin, float fMax) |
| 124 | +{ |
| 125 | + constexpr float a = 440.0f; |
| 126 | + const float factor = std::pow (2.0f, 1.0f / static_cast<float> (bandsPerOctave)); |
| 127 | + |
| 128 | + std::vector<float> frequencies; |
| 129 | + frequencies.push_back (a); |
| 130 | + |
| 131 | + float freq = a; |
| 132 | + while (freq <= fMax) |
| 133 | + { |
| 134 | + freq *= factor; |
| 135 | + frequencies.push_back (freq); |
| 136 | + } |
| 137 | + |
| 138 | + freq = a; |
| 139 | + while (freq >= fMin) |
| 140 | + { |
| 141 | + freq /= factor; |
| 142 | + frequencies.push_back (freq); |
| 143 | + } |
| 144 | + |
| 145 | + std::sort (frequencies.begin(), frequencies.end()); |
| 146 | + return frequencies; |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace yup |
0 commit comments