Level Crossfade

Overview

The level crossfade blends two signals, a and b, under the control of a level, ctrl. It is given a pivot, in decibel. While ctrl is at or above the pivot, the output is a alone. Once ctrl drops below the pivot, a fades out and b fades in by ctrl - pivot decibels: the lower ctrl falls, the more of b is heard. The level crossfade is implemented by a class named level_crossfade.

Crossfade gain of two signals against the control level
Figure 1. For a pivot of -10 dB: above the pivot the gain of a is 1 and b is silent. Below it, the gain of a follows the (linear) level ctrl - pivot while b takes up the remainder. At ctrl = -13 dB the gain of a is 0.708 (a 3 dB drop).

Below the pivot, the gain of a is lin_float(ctrl - pivot), the linear equivalent of the negative decibel gap, and the gain of b is 1 - gain_of_a. The two gains sum to 1. A gap of 0 dB (at the pivot) gives all a; a gap of -3 dB gives 0.708 of a and 0.292 of b; a large gap gives nearly all b.

The typical use is a level-dependent source select: the signal for loud passages in a, the fallback for when the level sags in b, and ctrl from an envelope follower. The pivot can be changed at run time; the constructor is constexpr.

Include

#include <q/fx/level_crossfade.hpp>

Declaration

struct level_crossfade
{
   constexpr         level_crossfade(decibel pivot);

   float             operator()(float a, float b, decibel ctrl);
   void              pivot(decibel pivot_);
};

Expressions

Notation

xf, xf2

Objects of type level_crossfade.

a, b

Floating point input samples: the two signals to blend.

pivot

Object of type decibel: the crossfade threshold.

ctrl

Object of type decibel: the control level.

Constructors and Assignment

Expression Semantics

level_crossfade(pivot)

Construct a level_crossfade given pivot (the crossfade threshold).

level_crossfade(xf)

Copy construct from level_crossfade xf.

xf2 = xf

Assign xf to xf2.

C++ brace initialization may also be used.

Function Call

Expression Semantics Return Type

xf(a, b, ctrl)

Process a and b given the control level ctrl and return the blend: a alone while ctrl is at or above the pivot.

float

Mutators

Expression Semantics Return Type

xf.pivot(pivot)

Set the crossfade pivot.

void

Example

Below -10 dB, fade from the dry input samples in into a reverb return, writing the blend to out, at sps samples per second:

#include <q/fx/level_crossfade.hpp>
#include <q/fx/envelope.hpp>      // For peak_envelope_follower
#include <span>
...
namespace q = cycfi::q;
using namespace q::literals;

std::span<float const> wet;         // the reverb return
...
auto xfade = q::level_crossfade{-10_dB};                 (1)
auto env = q::peak_envelope_follower{50_ms, sps};

for (std::size_t i = 0; i != in.size(); ++i)
{
   auto level = q::lin_to_db(env(std::abs(in[i])));
   out[i] = xfade(in[i], wet[i], level);                 (2)
}
1 Construct a level crossfade with a pivot of -10 dB.
2 Blend the dry and wet samples given the level of the dry signal.