|
| 1 | +// Copyright (C) 2026 - zsliu98 |
| 2 | +// This file is part of ZLSpectrumEqualizer |
| 3 | +// |
| 4 | +// ZLSpectrumEqualizer is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 as published by the Free Software Foundation. |
| 5 | +// |
| 6 | +// ZLSpectrumEqualizer 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 Affero General Public License for more details. |
| 7 | +// |
| 8 | +// You should have received a copy of the GNU Affero General Public License along with ZLSpectrumEqualizer. If not, see <https://www.gnu.org/licenses/>. |
| 9 | + |
| 10 | +#include "filter_attach.hpp" |
| 11 | + |
| 12 | +namespace zlp { |
| 13 | + FilterAttach::FilterAttach(juce::AudioProcessor&, |
| 14 | + juce::AudioProcessorValueTreeState& parameters, |
| 15 | + Controller& controller, const size_t idx) : |
| 16 | + parameters_(parameters), |
| 17 | + controller_(controller), |
| 18 | + idx_(idx), |
| 19 | + empty_(controller.getEmptyFilters()[idx]), |
| 20 | + scale_(*parameters.getRawParameterValue(PGainScale::kID)), |
| 21 | + gain_(*parameters.getRawParameterValue(PGain::kID + std::to_string(idx))), |
| 22 | + empty_update_flag_(controller.getEmptyUpdateFlags()[idx]), |
| 23 | + spec_update_flag_(controller.getSpecResponseUpdateFlag()), |
| 24 | + whole_update_flag_(controller.getUpdateFlag()) { |
| 25 | + for (size_t i = 0; i < kIDs.size(); ++i) { |
| 26 | + const auto ID = kIDs[i] + std::to_string(idx_); |
| 27 | + parameters_.addParameterListener(ID, this); |
| 28 | + parameterChanged(ID, parameters.getRawParameterValue(ID)->load(std::memory_order::relaxed)); |
| 29 | + } |
| 30 | + parameters_.addParameterListener(PGainScale::kID, this); |
| 31 | + } |
| 32 | + |
| 33 | + FilterAttach::~FilterAttach() { |
| 34 | + for (size_t i = 0; i < kIDs.size(); ++i) { |
| 35 | + parameters_.removeParameterListener(kIDs[i] + std::to_string(idx_), this); |
| 36 | + } |
| 37 | + parameters_.removeParameterListener(PGainScale::kID, this); |
| 38 | + } |
| 39 | + |
| 40 | + void FilterAttach::parameterChanged(const juce::String& parameter_ID, const float value) { |
| 41 | + if (parameter_ID.startsWith(PFilterStatus::kID)) { |
| 42 | + controller_.setFilterStatus(idx_, static_cast<FilterStatus>(std::round(value))); |
| 43 | + } else if (parameter_ID.startsWith(PFilterType::kID)) { |
| 44 | + empty_.setFilterType(static_cast<zldsp::filter::FilterType>(std::round(value))); |
| 45 | + signal(); |
| 46 | + } else if (parameter_ID.startsWith(POrder::kID)) { |
| 47 | + empty_.setOrder(POrder::kOrderArray[static_cast<size_t>(std::round(value))]); |
| 48 | + signal(); |
| 49 | + } else if (parameter_ID.startsWith(PLRMode::kID)) { |
| 50 | + controller_.setLRMS(idx_, static_cast<FilterStereo>(std::round(value))); |
| 51 | + } else if (parameter_ID.startsWith(PFreq::kID)) { |
| 52 | + empty_.setFreq(value); |
| 53 | + signal(); |
| 54 | + } else if (parameter_ID.startsWith(PGain::kID)) { |
| 55 | + empty_.setGain(std::clamp(value * (scale_.load(std::memory_order::relaxed) / 100.f), -30.f, 30.f)); |
| 56 | + signal(); |
| 57 | + } else if (parameter_ID.startsWith(PQ::kID)) { |
| 58 | + empty_.setQ(value); |
| 59 | + signal(); |
| 60 | + } else if (parameter_ID.startsWith(PGainScale::kID)) { |
| 61 | + empty_.setGain(std::clamp(gain_.load(std::memory_order::relaxed) * (value / 100.f), -30.f, 30.f)); |
| 62 | + signal(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + void FilterAttach::signal() { |
| 67 | + empty_update_flag_.signal(); |
| 68 | + spec_update_flag_.signal(); |
| 69 | + whole_update_flag_.signal(); |
| 70 | + } |
| 71 | +} |
0 commit comments