Skip to content

Commit

Permalink
Improve Hades distortion
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Feb 12, 2024
1 parent 0a365e6 commit a41f432
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
35 changes: 23 additions & 12 deletions lib/grit/eurorack/hades.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ auto Hades::process(Buffer const& buffer, ControlInput const& inputs) -> Control
};
}

auto Hades::Distortion::next() -> void
{
_index = Index{int(_index) + 1};
if (_index == MaxIndex) {
_index = Index{0};
}
}

auto Hades::Distortion::operator()(float sample) -> float
{
switch (_index) {
case TanhIndex: return _tanh(sample);
case HardIndex: return _hard(sample);
case FullWaveIndex: return _fullWave(sample);
case HalfWaveIndex: return _halfWave(sample);
case DiodeIndex: return _diode(sample);
default: break;
}
return sample;
}

auto Hades::Channel::setParameter(Parameter const& parameter) -> void
{
_parameter = parameter;
Expand All @@ -97,17 +118,7 @@ auto Hades::Channel::setParameter(Parameter const& parameter) -> void
});
}

auto Hades::Channel::nextDistortionAlgorithm() -> void
{
switch (_distortion.index()) {
case 0: _distortion = Distortion{HardClipperADAA1<float>{}}; break;
case 1: _distortion = Distortion{FullWaveRectifierADAA1<float>{}}; break;
case 2: _distortion = Distortion{HalfWaveRectifierADAA1<float>{}}; break;
case 3: _distortion = Distortion{DiodeRectifierADAA1<float>{}}; break;
case 4: _distortion = Distortion{TanhClipperADAA1<float>{}}; break;
default: break;
}
}
auto Hades::Channel::nextDistortionAlgorithm() -> void { _distortion.next(); }

auto Hades::Channel::prepare(float sampleRate) -> void
{
Expand All @@ -126,7 +137,7 @@ auto Hades::Channel::operator()(float sample) -> etl::pair<float, float>
auto const mixed = (noise * mix) + (vinyl * (1.0F - mix));

auto const drive = remap(_parameter.amp, 1.0F, 4.0F); // +12dB
auto const distOut = etl::visit([x = mixed * drive](auto& func) { return func(x); }, _distortion);
auto const distOut = _distortion(mixed * drive);
return {_compressor(distOut, distOut), env};
}

Expand Down
35 changes: 27 additions & 8 deletions lib/grit/eurorack/hades.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ struct Hades
[[nodiscard]] auto process(Buffer const& buffer, ControlInput const& inputs) -> ControlOutput;

private:
struct Distortion
{
Distortion() = default;

auto next() -> void;
[[nodiscard]] auto operator()(float sample) -> float;

private:
enum Index : int
{
TanhIndex = 0,
HardIndex,
FullWaveIndex,
HalfWaveIndex,
DiodeIndex,
MaxIndex,
};

Index _index{TanhIndex};
TanhClipperADAA1<float> _tanh{};
HardClipperADAA1<float> _hard{};
FullWaveRectifierADAA1<float> _fullWave{};
HalfWaveRectifierADAA1<float> _halfWave{};
DiodeRectifierADAA1<float> _diode{};
};

struct Channel
{
struct Parameter
Expand All @@ -84,19 +110,12 @@ struct Hades
[[nodiscard]] auto operator()(float sample) -> etl::pair<float, float>;

private:
using Distortion = etl::variant<
TanhClipperADAA1<float>,
HardClipperADAA1<float>,
FullWaveRectifierADAA1<float>,
HalfWaveRectifierADAA1<float>,
DiodeRectifierADAA1<float>>;

Parameter _parameter{};

EnvelopeFollower<float> _envelope{};
WhiteNoise<float> _whiteNoise{};
AirWindowsVinylDither<float> _vinyl{};
Distortion _distortion{TanhClipperADAA1<float>{}};
Distortion _distortion{};
Compressor<float> _compressor{};
};

Expand Down
1 change: 1 addition & 0 deletions lib/grit/eurorack/hades_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ TEST_CASE("grit/audio/eurorack: Hades")
.gate2 = false,
};

hades.nextDistortionAlgorithm();
auto const cv = hades.process(buffer, controls);
REQUIRE(cv.gate1 == true);
REQUIRE(cv.gate2 == false);
Expand Down

0 comments on commit a41f432

Please sign in to comment.