This commit is contained in:
twinaphex 2015-09-13 01:08:36 +02:00
parent 1fbc83f51e
commit c4d46ce09f
6 changed files with 93 additions and 85 deletions

View File

@ -56,29 +56,32 @@ static void chorus_process(void *data, struct dspfilter_output *output,
for (i = 0; i < input->frames; i++, out += 2)
{
unsigned delay_int;
float delay_frac, l_a, l_b, r_a, r_b;
float chorus_l, chorus_r;
float in[2] = { out[0], out[1] };
float delay = ch->delay + ch->depth * sin((2.0 * M_PI * ch->lfo_ptr++) / ch->lfo_period);
delay *= ch->input_rate;
if (ch->lfo_ptr >= ch->lfo_period)
ch->lfo_ptr = 0;
unsigned delay_int = (unsigned)delay;
delay_int = (unsigned)delay;
if (delay_int >= CHORUS_MAX_DELAY - 1)
delay_int = CHORUS_MAX_DELAY - 2;
float delay_frac = delay - delay_int;
delay_frac = delay - delay_int;
ch->old[0][ch->old_ptr] = in[0];
ch->old[1][ch->old_ptr] = in[1];
float l_a = ch->old[0][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
float l_b = ch->old[0][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
float r_a = ch->old[1][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
float r_b = ch->old[1][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
l_a = ch->old[0][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
l_b = ch->old[0][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
r_a = ch->old[1][(ch->old_ptr - delay_int - 0) & CHORUS_DELAY_MASK];
r_b = ch->old[1][(ch->old_ptr - delay_int - 1) & CHORUS_DELAY_MASK];
// Lerp introduces aliasing of the chorus component, but doing full polyphase here is probably overkill.
float chorus_l = l_a * (1.0f - delay_frac) + l_b * delay_frac;
float chorus_r = r_a * (1.0f - delay_frac) + r_b * delay_frac;
/* Lerp introduces aliasing of the chorus component, but doing full polyphase here is probably overkill. */
chorus_l = l_a * (1.0f - delay_frac) + l_b * delay_frac;
chorus_r = r_a * (1.0f - delay_frac) + r_b * delay_frac;
out[0] = ch->mix_dry * in[0] + ch->mix_wet * chorus_l;
out[1] = ch->mix_dry * in[1] + ch->mix_wet * chorus_r;
@ -90,11 +93,11 @@ static void chorus_process(void *data, struct dspfilter_output *output,
static void *chorus_init(const struct dspfilter_info *info,
const struct dspfilter_config *config, void *userdata)
{
float delay, depth, lfo_freq, drywet;
struct chorus_data *ch = (struct chorus_data*)calloc(1, sizeof(*ch));
if (!ch)
return NULL;
float delay, depth, lfo_freq, drywet;
config->get_float(userdata, "delay_ms", &delay, 25.0f);
config->get_float(userdata, "depth_ms", &depth, 1.0f);
config->get_float(userdata, "lfo_freq", &lfo_freq, 0.5f);

View File

@ -65,14 +65,17 @@ static void eq_free(void *data)
static void eq_process(void *data, struct dspfilter_output *output,
const struct dspfilter_input *input)
{
float *out;
const float *in;
unsigned input_frames;
struct eq_data *eq = (struct eq_data*)data;
output->samples = eq->buffer;
output->frames = 0;
output->samples = eq->buffer;
output->frames = 0;
float *out = eq->buffer;
const float *in = input->samples;
unsigned input_frames = input->frames;
out = eq->buffer;
in = input->samples;
input_frames = input->frames;
while (input_frames)
{

View File

@ -61,30 +61,34 @@ static void iir_free(void *data)
static void iir_process(void *data, struct dspfilter_output *output,
const struct dspfilter_input *input)
{
float b0, b1, b2, a0, a1, a2;
float xn1_l, xn2_l, yn1_l, yn2_l;
float xn1_r, xn2_r, yn1_r, yn2_r;
unsigned i;
float *out;
struct iir_data *iir = (struct iir_data*)data;
output->samples = input->samples;
output->frames = input->frames;
float *out = output->samples;
out = output->samples;
float b0 = iir->b0;
float b1 = iir->b1;
float b2 = iir->b2;
float a0 = iir->a0;
float a1 = iir->a1;
float a2 = iir->a2;
b0 = iir->b0;
b1 = iir->b1;
b2 = iir->b2;
a0 = iir->a0;
a1 = iir->a1;
a2 = iir->a2;
float xn1_l = iir->l.xn1;
float xn2_l = iir->l.xn2;
float yn1_l = iir->l.yn1;
float yn2_l = iir->l.yn2;
xn1_l = iir->l.xn1;
xn2_l = iir->l.xn2;
yn1_l = iir->l.yn1;
yn2_l = iir->l.yn2;
float xn1_r = iir->r.xn1;
float xn2_r = iir->r.xn2;
float yn1_r = iir->r.yn1;
float yn2_r = iir->r.yn2;
xn1_r = iir->r.xn1;
xn2_r = iir->r.xn2;
yn1_r = iir->r.yn1;
yn2_r = iir->r.yn2;
for (i = 0; i < input->frames; i++, out += 2)
{
@ -155,15 +159,15 @@ static void iir_filter_init(struct iir_data *iir,
float sample_rate, float freq, float qual, float gain, enum IIRFilter filter_type)
{
double omega = 2.0 * M_PI * freq / sample_rate;
double cs = cos(omega);
double sn = sin(omega);
double cs = cos(omega);
double sn = sin(omega);
double a1pha = sn / (2.0 * qual);
double A = exp(log(10.0) * gain / 40.0);
double beta = sqrt(A + A);
double A = exp(log(10.0) * gain / 40.0);
double beta = sqrt(A + A);
float b0 = 0.0, b1 = 0.0, b2 = 0.0, a0 = 0.0, a1 = 0.0, a2 = 0.0;
float b0 = 0.0, b1 = 0.0, b2 = 0.0, a0 = 0.0, a1 = 0.0, a2 = 0.0;
// Set up filter coefficients according to type
/* Set up filter coefficients according to type */
switch (filter_type)
{
case LPF:
@ -216,6 +220,7 @@ static void iir_filter_init(struct iir_data *iir,
break;
case RIAA_phono: /* http://www.dsprelated.com/showmessage/73300/3.php */
{
double y, b_re, a_re, b_im, a_im, g;
float b[3], a[3];
if ((int)sample_rate == 44100)
@ -247,21 +252,22 @@ static void iir_filter_init(struct iir_data *iir,
make_poly_from_roots(poles, 2, a);
}
b0 = b[0];
b1 = b[1];
b2 = b[2];
a0 = a[0];
a1 = a[1];
a2 = a[2];
b0 = b[0];
b1 = b[1];
b2 = b[2];
a0 = a[0];
a1 = a[1];
a2 = a[2];
/* Normalise to 0dB at 1kHz (Thanks to Glenn Davis) */
double y = 2.0 * M_PI * 1000.0 / sample_rate;
double b_re = b0 + b1 * cos(-y) + b2 * cos(-2.0 * y);
double a_re = a0 + a1 * cos(-y) + a2 * cos(-2.0 * y);
double b_im = b1 * sin(-y) + b2 * sin(-2.0 * y);
double a_im = a1 * sin(-y) + a2 * sin(-2.0 * y);
double g = 1.0 / sqrt((sqr(b_re) + sqr(b_im)) / (sqr(a_re) + sqr(a_im)));
b0 *= g; b1 *= g; b2 *= g;
y = 2.0 * M_PI * 1000.0 / sample_rate;
b_re = b0 + b1 * cos(-y) + b2 * cos(-2.0 * y);
a_re = a0 + a1 * cos(-y) + a2 * cos(-2.0 * y);
b_im = b1 * sin(-y) + b2 * sin(-2.0 * y);
a_im = a1 * sin(-y) + a2 * sin(-2.0 * y);
g = 1.0 / sqrt((sqr(b_re) + sqr(b_im)) / (sqr(a_re) + sqr(a_im)));
b0 *= g; b1 *= g; b2 *= g;
break;
}
case PEQ:
@ -319,19 +325,20 @@ static void iir_filter_init(struct iir_data *iir,
static void *iir_init(const struct dspfilter_info *info,
const struct dspfilter_config *config, void *userdata)
{
float freq, qual, gain;
enum IIRFilter filter;
char *type = NULL;
struct iir_data *iir = (struct iir_data*)calloc(1, sizeof(*iir));
if (!iir)
return NULL;
float freq, qual, gain;
config->get_float(userdata, "frequency", &freq, 1024.0f);
config->get_float(userdata, "quality", &qual, 0.707f);
config->get_float(userdata, "gain", &gain, 0.0f);
char *type = NULL;
config->get_string(userdata, "type", &type, "LPF");
enum IIRFilter filter = str_to_type(type);
filter = str_to_type(type);
config->free(type);
iir_filter_init(iir, info->input_rate, freq, qual, gain, filter);

View File

@ -33,12 +33,12 @@ static void panning_process(void *data, struct dspfilter_output *output,
const struct dspfilter_input *input)
{
unsigned i;
float *out;
struct panning_data *pan = (struct panning_data*)data;
output->samples = input->samples;
output->frames = input->frames;
float *out = output->samples;
output->samples = input->samples;
output->frames = input->frames;
out = output->samples;
for (i = 0; i < input->frames; i++, out += 2)
{
@ -52,28 +52,19 @@ static void panning_process(void *data, struct dspfilter_output *output,
static void *panning_init(const struct dspfilter_info *info,
const struct dspfilter_config *config, void *userdata)
{
static const float default_left[] = { 1.0f, 0.0f };
static const float default_right[] = { 0.0f, 1.0f };
float *left = NULL, *right = NULL;
unsigned num_left = 0, num_right = 0;
struct panning_data *pan = (struct panning_data*)calloc(1, sizeof(*pan));
if (!pan)
return NULL;
float *left = NULL, *right = NULL;
unsigned num_left = 0, num_right = 0;
static const float default_left[] = { 1.0f, 0.0f };
static const float default_right[] = { 0.0f, 1.0f };
config->get_float_array(userdata, "left_mix", &left, &num_left, default_left, 2);
config->get_float_array(userdata, "right_mix", &right, &num_right, default_right, 2);
if (num_left == 2)
memcpy(pan->left, left, sizeof(pan->left));
else
memcpy(pan->left, default_left, sizeof(pan->left));
if (num_right == 2)
memcpy(pan->right, right, sizeof(pan->right));
else
memcpy(pan->right, default_right, sizeof(pan->right));
memcpy(pan->left, (num_left == 2) ? left : default_left, sizeof(pan->left));
memcpy(pan->right, (num_right == 2) ? right : default_right, sizeof(pan->right));
config->free(left);
config->free(right);

View File

@ -245,11 +245,12 @@ static void reverb_process(void *data, struct dspfilter_output *output,
const struct dspfilter_input *input)
{
unsigned i;
float *out;
struct reverb_data *rev = (struct reverb_data*)data;
output->samples = input->samples;
output->frames = input->frames;
float *out = output->samples;
output->samples = input->samples;
output->frames = input->frames;
out = output->samples;
for (i = 0; i < input->frames; i++, out += 2)
{
@ -263,11 +264,12 @@ static void reverb_process(void *data, struct dspfilter_output *output,
static void *reverb_init(const struct dspfilter_info *info,
const struct dspfilter_config *config, void *userdata)
{
struct reverb_data *rev = (struct reverb_data*)calloc(1, sizeof(*rev));
float drytime, wettime, damping, roomwidth, roomsize;
struct reverb_data *rev = (struct reverb_data*)
calloc(1, sizeof(*rev));
if (!rev)
return NULL;
float drytime, wettime, damping, roomwidth, roomsize;
config->get_float(userdata, "drytime", &drytime, 0.43f);
config->get_float(userdata, "wettime", &wettime, 0.4f);
config->get_float(userdata, "damping", &damping, 0.8f);

View File

@ -19,7 +19,7 @@
#include <stdlib.h>
#include <string.h>
#define wahwahlfoskipsamples 30
#define WAHWAH_LFO_SKIP_SAMPLES 30
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
@ -42,25 +42,27 @@ struct wahwah_data
static void wahwah_free(void *data)
{
free(data);
if (data)
free(data);
}
static void wahwah_process(void *data, struct dspfilter_output *output,
const struct dspfilter_input *input)
{
unsigned i;
float *out;
struct wahwah_data *wah = (struct wahwah_data*)data;
output->samples = input->samples;
output->frames = input->frames;
float *out = output->samples;
output->samples = input->samples;
output->frames = input->frames;
out = output->samples;
for (i = 0; i < input->frames; i++, out += 2)
{
float out_l, out_r;
float in[2] = { out[0], out[1] };
if ((wah->skipcount++ % wahwahlfoskipsamples) == 0)
if ((wah->skipcount++ % WAHWAH_LFO_SKIP_SAMPLES) == 0)
{
float omega, sn, cs, alpha;
float frequency = (1.0 + cos(wah->skipcount * wah->lfoskip + wah->phase)) / 2.0;
@ -94,8 +96,8 @@ static void wahwah_process(void *data, struct dspfilter_output *output,
wah->r.yn2 = wah->r.yn1;
wah->r.yn1 = out_r;
out[0] = out_l;
out[1] = out_r;
out[0] = out_l;
out[1] = out_r;
}
}