Crossfade
Overview
Crossfade
between two signals a
and b
using some
position pos
(a value between 0 and 1). When the position
is 0, it is signal A, when position is 1, it is signal B.
Tangled Files
crossfade.c
and crossfade.h
.
<<crossfade.h>>=
#ifndef SK_CROSSFADE_H
#define SK_CROSSFADE_H
#ifndef SKFLT
#define SKFLT float
#endif
<<funcdefs>>
#endif
<<crossfade.c>>=
#include <math.h>
#include "crossfade.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define SQRT2 1.41421356237309504880
<<funcs>>
Linear Crossfade
A linear crossfade is simply linear interpolation.
This is done with sk_crossfade_linear
.
<<funcdefs>>=
SKFLT sk_crossfade_linear(SKFLT a, SKFLT b, SKFLT pos);
<<funcs>>=
SKFLT sk_crossfade_linear(SKFLT a, SKFLT b, SKFLT pos)
{
return (1 - pos)*a + pos*b;
}
Square Root Crossfade
crossfade using sqrt
.
<<funcdefs>>=
SKFLT sk_crossfade_sqrt(SKFLT a, SKFLT b, SKFLT pos);
<<funcs>>=
SKFLT sk_crossfade_sqrt(SKFLT a, SKFLT b, SKFLT pos)
{
return a * sqrt(pos) + b * sqrt(1 - pos);
}
Equal Power
Uses equal power to do crossfade.
<<funcdefs>>=
SKFLT sk_crossfade_equal(SKFLT a, SKFLT b, SKFLT pos);
<<funcs>>=
SKFLT sk_crossfade_equal(SKFLT a, SKFLT b, SKFLT pos)
{
SKFLT c;
SKFLT s;
c = cos(M_PI * pos * 0.5);
s = sin(M_PI * pos * 0.5);
a *= SQRT2 * (c + s) * 0.5;
b *= SQRT2 * (c - s) * 0.5;
return a + b;
}