Skip to content

Commit f5438ed

Browse files
committed
Make normalization mode an enum
Realized that the ComboBoxModel entries weren't translated, and that if they were, the entire normalization logic would break since it depended on strings lol. Upside is that documentation for the normalization modes is now in the code itself rather than just in the commit log.
1 parent b256067 commit f5438ed

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

plugins/BitInvader/BitInvader.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ BitInvader::BitInvader(InstrumentTrack* instrumentTrack)
8080
, m_interpolation(false, this, tr("Interpolation"))
8181
, m_normalizeMode(this, tr("Normalize"))
8282
{
83-
m_normalizeMode.addItem("Off");
84-
m_normalizeMode.addItem("Full");
85-
m_normalizeMode.addItem("Length only");
86-
m_normalizeMode.addItem("Legacy");
87-
m_normalizeMode.setValue(m_normalizeMode.findText("Length only"));
83+
// The order these items are added must correspond to the modes in BitInvader::NormalizeMode
84+
m_normalizeMode.addItem(tr("Off")); // NormalizeMode::Off
85+
m_normalizeMode.addItem(tr("Full")); // NormalizeMode::Full
86+
m_normalizeMode.addItem(tr("Length only")); // NormalizeMode::LengthOnly
87+
m_normalizeMode.addItem(tr("Legacy")); // NormalizeMode::Legacy
88+
m_normalizeMode.setValue(static_cast<int>(NormalizeMode::LengthOnly));
8889

8990
m_graph.setWaveToSine();
9091
lengthChanged();
@@ -131,9 +132,9 @@ void BitInvader::loadSettings(const QDomElement& el)
131132
m_interpolation.loadSettings(el, "interpolation");
132133
m_normalizeMode.loadSettings(el, "normalize");
133134
// If normalization was enabled on an old preset, change it to "Legacy" normalization mode
134-
if (el.attribute("version") == "0.1")
135+
if (el.attribute("version") == "0.1") // TODO: Make new version 1 instead, cast to int and compare < 1
135136
{
136-
m_normalizeMode.setValue(m_normalizeMode.value() * m_normalizeMode.findText("Legacy"));
137+
m_normalizeMode.setValue(m_normalizeMode.value() * static_cast<int>(NormalizeMode::Legacy);
137138
}
138139
}
139140

@@ -151,19 +152,21 @@ void BitInvader::lengthChanged()
151152

152153
void BitInvader::normalize()
153154
{
154-
if (m_normalizeMode.value() == m_normalizeMode.findText("Off"))
155+
if (m_normalizeMode.value() == static_cast<int>(NormalizeMode::Off))
155156
{
156157
m_normalizeFactor = 1.f;
157158
m_normalizeOffset = 0.f;
158159
return;
159160
}
160161

161-
const auto len = m_normalizeMode.value() == m_normalizeMode.findText("Length only")
162-
? static_cast<std::size_t>(m_sampleLength.value())
163-
: wavetableSize;
164-
const auto samples = std::span<const float>{ m_graph.samples(), len };
162+
const auto samples = std::span<const float>{
163+
m_graph.samples(),
164+
m_normalizeMode.value() == static_cast<int>(NormalizeMode::LengthOnly)
165+
? static_cast<std::size_t>(m_sampleLength.value())
166+
: wavetableSize
167+
};
165168

166-
if (m_normalizeMode.value() == m_normalizeMode.findText("Legacy"))
169+
if (m_normalizeMode.value() == static_cast<int>(NormalizeMode::Legacy))
167170
{
168171
m_normalizeOffset = 0.f;
169172
m_normalizeFactor = 1.f / std::max_element(samples.begin(), samples.end(), [](auto a, auto b){ return std::abs(a) < std::abs(b); })[0];
@@ -318,12 +321,12 @@ BitInvaderView::BitInvaderView(Instrument* instrument, QWidget* parent)
318321
auto normalizeLabel = new QLabel(tr("Normalization:"), this);
319322
normalizeLabel->setFont(adjustedToPixelSize(normalizeLabel->font(), DEFAULT_FONT_SIZE));
320323
// FIXME: Evil awful terrible layout. This should be fine for now,
321-
// since a full UI overhaul for Bit Invader is planned.
324+
// since a full UI overhaul for Bit Invader is planned in #8122.
322325
auto normalizeLayout = new QGridLayout(this);
323326
normalizeLayout->setContentsMargins(6, 6, 6, 6);
324327
normalizeLayout->setVerticalSpacing(1);
325328
normalizeLayout->setAlignment(Qt::AlignBottom);
326-
normalizeLayout->addItem(new QSpacerItem(250 - 131, 1), 0, 0); // Do NOT ever do this LMAO
329+
normalizeLayout->addItem(new QSpacerItem(250 - 131, 1), 0, 0); // HACK: Do NOT ever do this LMAO
327330
normalizeLayout->addWidget(normalizeLabel, 0, 1);
328331
normalizeLayout->addWidget(m_normalizeMode, 1, 1);
329332

plugins/BitInvader/BitInvader.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,39 @@ class BitInvader : public Instrument
6565

6666
protected slots:
6767
void lengthChanged();
68+
69+
//! @brief Normalize the wavetable according to the current normalization mode.
70+
//! @see NormalizeMode
71+
//! @see m_graph
72+
//! @see m_normalizeMode
6873
void normalize();
6974

7075
private:
76+
//! @brief A kind of normalization to apply to BitInvader's wavetable.
77+
enum class NormalizeMode
78+
{
79+
//! No normalization.
80+
Off,
81+
82+
//! Normalization is applied and DC offset removed, such that
83+
//! the entire waveform has a range of [-1, 1].
84+
Full,
85+
86+
//! Normalization is applied and DC offset removed, such that
87+
//! the waveform within domain [0, L] has a range of [-1, 1],
88+
//! with L being the value of @ref m_sampleLength.
89+
LengthOnly,
90+
91+
//! Normalization is applied, such that the absolute value of
92+
//! the entire waveform has a range of [0, 1]. DC offset is
93+
//! preserved.
94+
Legacy
95+
};
96+
7197
FloatModel m_sampleLength;
7298
graphModel m_graph;
7399
BoolModel m_interpolation;
74-
ComboBoxModel m_normalizeMode;
100+
ComboBoxModel m_normalizeMode; //!< Which normalization mode to use. @see NormalizeMode
75101

76102
float m_normalizeFactor; //!< Factor by which to amplify output such that the output is normalized
77103
float m_normalizeOffset; //!< Amount by which to offset the output such that no DC offset is produced when normalized

0 commit comments

Comments
 (0)