Skip to content

Commit

Permalink
Added dry wet to distortion (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt54 authored Jul 30, 2024
1 parent ee7d954 commit 792fbf8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Sources/CSoundpipeAudioKit/Effects/TanhDistortionDSP.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TanhDistortionParameterPostgain,
TanhDistortionParameterPositiveShapeParameter,
TanhDistortionParameterNegativeShapeParameter,
TanhDistortionParameterDryWetMix,
};

class TanhDistortionDSP : public SoundpipeDSPBase {
Expand All @@ -19,13 +20,15 @@
ParameterRamper postgainRamp;
ParameterRamper positiveShapeParameterRamp;
ParameterRamper negativeShapeParameterRamp;
ParameterRamper dryWetMixRamp;

public:
TanhDistortionDSP() {
parameters[TanhDistortionParameterPregain] = &pregainRamp;
parameters[TanhDistortionParameterPostgain] = &postgainRamp;
parameters[TanhDistortionParameterPositiveShapeParameter] = &positiveShapeParameterRamp;
parameters[TanhDistortionParameterNegativeShapeParameter] = &negativeShapeParameterRamp;
parameters[TanhDistortionParameterDryWetMix] = &dryWetMixRamp;
}

void init(int channelCount, double sampleRate) override {
Expand Down Expand Up @@ -65,6 +68,10 @@ void process(FrameRange range) override {

sp_dist_compute(sp, dist0, &leftIn, &leftOut);
sp_dist_compute(sp, dist1, &rightIn, &rightOut);

float dryWetMix = dryWetMixRamp.getAndStep();
outputSample(0, i) = dryWetMix * leftOut + (1.0f - dryWetMix) * leftIn;
outputSample(1, i) = dryWetMix * rightOut + (1.0f - dryWetMix) * rightIn;
}
}
};
Expand All @@ -74,3 +81,4 @@ void process(FrameRange range) override {
AK_REGISTER_PARAMETER(TanhDistortionParameterPostgain)
AK_REGISTER_PARAMETER(TanhDistortionParameterPositiveShapeParameter)
AK_REGISTER_PARAMETER(TanhDistortionParameterNegativeShapeParameter)
AK_REGISTER_PARAMETER(TanhDistortionParameterDryWetMix)
20 changes: 18 additions & 2 deletions Sources/SoundpipeAudioKit/Effects/TanhDistortion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,38 @@ public class TanhDistortion: Node {
/// Like the positive shape parameter, only for the negative part.
@Parameter(negativeShapeParameterDef) public var negativeShapeParameter: AUValue

/// Specification details for dryWetMix
public static let dryWetMixDef = NodeParameterDef(
identifier: "dryWetMix",
name: "Dry/Wet Mix",
address: akGetParameterAddress("TanhDistortionParameterDryWetMix"),
defaultValue: 1.0,
range: 0.0 ... 1.0,
unit: .percent
)

/// Dry/Wet Mix
@Parameter(dryWetMixDef) public var dryWetMix: AUValue

// MARK: - Initialization

/// Initialize this distortion node
///
/// - Parameters:
/// - input: Input node to process
/// - pregain: Determines gain applied to the signal before waveshaping. A value of 1 gives slight distortion.
/// - postgain: Gain applied after waveshaping
/// - postgain: Gain applied after waveshaping (applied only to processed signal)
/// - positiveShapeParameter: Shape of the positive part of the signal. A value of 0 gets a flat clip.
/// - negativeShapeParameter: Like the positive shape parameter, only for the negative part.
/// - dryWetMix: Dry/Wet Mix
///
public init(
_ input: Node,
pregain: AUValue = pregainDef.defaultValue,
postgain: AUValue = postgainDef.defaultValue,
positiveShapeParameter: AUValue = positiveShapeParameterDef.defaultValue,
negativeShapeParameter: AUValue = negativeShapeParameterDef.defaultValue
negativeShapeParameter: AUValue = negativeShapeParameterDef.defaultValue,
dryWetMix: AUValue = dryWetMixDef.defaultValue
) {
self.input = input

Expand All @@ -96,5 +111,6 @@ public class TanhDistortion: Node {
self.postgain = postgain
self.positiveShapeParameter = positiveShapeParameter
self.negativeShapeParameter = negativeShapeParameter
self.dryWetMix = dryWetMix
}
}

0 comments on commit 792fbf8

Please sign in to comment.