Skip to content
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

PitchShiftEffect: extend effect options #4901

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6513250
PitchShiftEffect: add wide range option
davidchocholaty Aug 19, 2022
ab124c3
PitchShiftEffect: add semitones mode option
davidchocholaty Aug 19, 2022
b913b7e
math: add sgn function
davidchocholaty Aug 19, 2022
cd431e6
PitchShiftEffect: add range mode option
davidchocholaty Aug 19, 2022
bc09b72
PitchShiftEffect: add a formant preserving option
davidchocholaty Aug 21, 2022
2c57997
PitchShiftEffect: remove insignificant TODO
davidchocholaty Aug 21, 2022
150f647
math: clean up sgn function
davidchocholaty Aug 21, 2022
9ea8322
PitchShiftEffect: semitones mode description
davidchocholaty Aug 21, 2022
c49712a
PitchShiftEffect: replace formant description
davidchocholaty Aug 21, 2022
ba5e5d4
PitchShiftEffect: remove range mode and wide range
davidchocholaty Aug 22, 2022
d44fc12
Pitch shift effect: add Range option
davidchocholaty Aug 22, 2022
c60407c
PitchShiftEffect: update semitones description
davidchocholaty Aug 22, 2022
0d9bc0c
PitchShiftEffect: extend formant description
davidchocholaty Aug 23, 2022
5901cfd
PitchShiftEffect: formant into formant preserving
davidchocholaty Aug 23, 2022
9c42ae2
Merge branch 'main' into extend_pitch_shift_effect_options
Swiftb0y Aug 24, 2022
7eef608
PitchShiftEffect: rename kSemitones constant
davidchocholaty Aug 25, 2022
cd26497
PitchShiftEffect: replace flip-flopping
davidchocholaty Aug 25, 2022
7d40f6d
PitchShiftEffect: fix segmentation fault
davidchocholaty Aug 26, 2022
5d98c7f
PitchShiftEffect: update version
davidchocholaty Aug 26, 2022
fe7fe7d
PitchShiftEffect: add id literals into a namespace
davidchocholaty Aug 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 71 additions & 11 deletions src/effects/backends/builtin/pitchshifteffect.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
#include "effects/backends/builtin/pitchshifteffect.h"

#include <QString>

#include "util/sample.h"

namespace {
static constexpr SINT kSemitonesPerOctave = 12;

static const QString kPitchParameterId = QStringLiteral("pitch");
static const QString kRangeParameterId = QStringLiteral("range");
static const QString kSemitonesModeParameterId = QStringLiteral("semitonesMode");
static const QString kFormantPreservingParameterId = QStringLiteral("formantPreserving");
} // anonymous namespace

PitchShiftEffect::PitchShiftEffect()
: m_currentFormant(false) {
}

PitchShiftGroupState::PitchShiftGroupState(
const mixxx::EngineParameters& engineParameters)
: EffectState(engineParameters) {
Expand Down Expand Up @@ -46,12 +61,12 @@ EffectManifestPointer PitchShiftEffect::getManifest() {
pManifest->setName(QObject::tr("Pitch Shift"));
pManifest->setShortName(QObject::tr("Pitch Shift"));
pManifest->setAuthor("The Mixxx Team");
pManifest->setVersion("1.0");
pManifest->setVersion("2.0");
pManifest->setDescription(QObject::tr(
"Raises or lowers the original pitch of a sound."));

EffectManifestParameterPointer pitch = pManifest->addParameter();
pitch->setId("pitch");
pitch->setId(kPitchParameterId);
pitch->setName(QObject::tr("Pitch"));
pitch->setShortName(QObject::tr("Pitch"));
pitch->setDescription(QObject::tr(
Expand All @@ -61,12 +76,48 @@ EffectManifestPointer PitchShiftEffect::getManifest() {
pitch->setNeutralPointOnScale(0.0);
pitch->setRange(-1.0, 0.0, 1.0);

EffectManifestParameterPointer range = pManifest->addParameter();
range->setId(kRangeParameterId);
range->setName(QObject::tr("Range"));
range->setShortName(QObject::tr("Range"));
range->setDescription(QObject::tr(
"The range of the Pitch knob (0 - 2 octaves).\n"));
range->setValueScaler(EffectManifestParameter::ValueScaler::Linear);
range->setDefaultLinkType(EffectManifestParameter::LinkType::Linked);
range->setNeutralPointOnScale(1.0);
range->setRange(0.0, 1.0, 2.0);

EffectManifestParameterPointer semitonesMode = pManifest->addParameter();
semitonesMode->setId(kSemitonesModeParameterId);
semitonesMode->setName(QObject::tr("Semitones"));
semitonesMode->setShortName(QObject::tr("Semitones"));
semitonesMode->setDescription(QObject::tr(
"Change the pitch in semitone steps instead of continuously."));
semitonesMode->setValueScaler(EffectManifestParameter::ValueScaler::Toggle);
semitonesMode->setUnitsHint(EffectManifestParameter::UnitsHint::Unknown);
semitonesMode->setRange(0, 1, 1);

EffectManifestParameterPointer formantPreserving = pManifest->addParameter();
formantPreserving->setId(kFormantPreservingParameterId);
formantPreserving->setName(QObject::tr("Formant"));
formantPreserving->setShortName(QObject::tr("Formant"));
formantPreserving->setDescription(QObject::tr(
"Preserve the resonant frequencies (formants) of the human vocal tract "
"and other instruments.\n"
"Hint: compensates \"chipmunk\" or \"growling\" voices"));
formantPreserving->setValueScaler(EffectManifestParameter::ValueScaler::Toggle);
formantPreserving->setUnitsHint(EffectManifestParameter::UnitsHint::Unknown);
formantPreserving->setRange(0, 0, 1);

return pManifest;
}

void PitchShiftEffect::loadEngineEffectParameters(
const QMap<QString, EngineEffectParameterPointer>& parameters) {
m_pPitchParameter = parameters.value("pitch");
m_pPitchParameter = parameters.value(kPitchParameterId);
m_pRangeParameter = parameters.value(kRangeParameterId);
m_pSemitonesModeParameter = parameters.value(kSemitonesModeParameterId);
m_pFormantPreservingParameter = parameters.value(kFormantPreservingParameterId);
}

void PitchShiftEffect::processChannel(
Expand All @@ -79,15 +130,24 @@ void PitchShiftEffect::processChannel(
Q_UNUSED(groupFeatures);
Q_UNUSED(enableState);

const double pitchParameter = m_pPitchParameter->value();
if (const bool formantPreserving = m_pFormantPreservingParameter->toBool();
m_currentFormant != formantPreserving) {
m_currentFormant = formantPreserving;

pState->m_pRubberBand->setFormantOption(m_currentFormant
? RubberBand::RubberBandStretcher::
OptionFormantPreserved
: RubberBand::RubberBandStretcher::
OptionFormantShifted);
}

double pitchParameter = m_pPitchParameter->value() * m_pRangeParameter->value();

if (m_pSemitonesModeParameter->toBool()) {
pitchParameter = roundToFraction(pitchParameter, kSemitonesPerOctave);
}

const double pitch = 1.0 + [=] {
if (pitchParameter < 0.0) {
return pitchParameter / 2.0;
} else {
return pitchParameter;
}
}();
const double pitch = std::pow(2.0, pitchParameter);

pState->m_pRubberBand->setPitchScale(pitch);

Expand Down
7 changes: 6 additions & 1 deletion src/effects/backends/builtin/pitchshifteffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "engine/effects/engineeffectparameter.h"
#include "util/class.h"
#include "util/defs.h"
#include "util/math.h"
#include "util/sample.h"
#include "util/types.h"

Expand All @@ -30,7 +31,7 @@ class PitchShiftGroupState : public EffectState {

class PitchShiftEffect final : public EffectProcessorImpl<PitchShiftGroupState> {
public:
PitchShiftEffect() = default;
PitchShiftEffect();

static QString getId();
static EffectManifestPointer getManifest();
Expand All @@ -51,7 +52,11 @@ class PitchShiftEffect final : public EffectProcessorImpl<PitchShiftGroupState>
return getId();
}

bool m_currentFormant;
EngineEffectParameterPointer m_pPitchParameter;
EngineEffectParameterPointer m_pRangeParameter;
EngineEffectParameterPointer m_pSemitonesModeParameter;
EngineEffectParameterPointer m_pFormantPreservingParameter;

DISALLOW_COPY_AND_ASSIGN(PitchShiftEffect);
};
12 changes: 12 additions & 0 deletions src/util/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ requires std::is_floating_point_v<T>
}

#undef CMATH_CONSTEXPR

/// https://en.wikipedia.org/wiki/Sign_function
template<typename T>
requires std::is_arithmetic_v<T>
constexpr T sgn(const T a) {
// silence -Wtype-limits
if constexpr (std::is_unsigned_v<T>) {
return static_cast<T>(a > T(0));
} else {
return static_cast<T>(a > T(0)) - static_cast<T>(a < T(0));
}
}