Add libclipper, a library to 'smoothly' clip overdriven audio levels

pull/1/head
Andreas Eversberg 5 years ago
parent 9197320ee6
commit 3fd0707a3b
  1. 1
      .gitignore
  2. 1
      configure.ac
  3. 3
      src/Makefile.am
  4. 6
      src/libclipper/Makefile.am
  5. 81
      src/libclipper/clipper.c
  6. 4
      src/libclipper/clipper.h

1
.gitignore vendored

@ -44,6 +44,7 @@ src/libmncc/libmncc.a
src/libsound/libsound.a
src/libsdr/libsdr.a
src/libsample/libsample.a
src/libclipper/libclipper.a
src/anetz/libgermanton.a
src/anetz/anetz
src/bnetz/bnetz

@ -73,6 +73,7 @@ AC_OUTPUT(
src/libsound/Makefile
src/libsdr/Makefile
src/libsample/Makefile
src/libclipper/Makefile
src/anetz/Makefile
src/bnetz/Makefile
src/cnetz/Makefile

@ -22,7 +22,8 @@ SUBDIRS = \
libfilter \
libwave \
libfft \
libmncc
libmncc \
libclipper
if HAVE_ALSA
SUBDIRS += \

@ -0,0 +1,6 @@
AM_CPPFLAGS = -Wall -Wextra -g $(all_includes)
noinst_LIBRARIES = libclipper.a
libclipper_a_SOURCES = \
clipper.c

@ -0,0 +1,81 @@
/* Clipper implementation, based on code by Jonathan Olds
*
* (C) 2017 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include "../libsample/sample.h"
#include "clipper.h"
static double clipper_lut[6000];
static double clipper_point = NAN;
void clipper_init(double point)
{
double a;
int i;
if (point > 0.99)
point = 0.99;
if (point < 0.01)
point = 0.01;
clipper_point = point;
a = M_PI / (2.0 * (1.0 - clipper_point));
for (i = 0; i < 6000; i++)
clipper_lut[i] = clipper_point + atan(a * i / 1000.0) / a;
}
void clipper_process(sample_t *samples, int length)
{
int i;
double val, inv, shiftmultval;
int n, q;
if (isnan(clipper_point)) {
fprintf(stderr, "Clipper not initialized, aborting!\n");
abort();
}
for (i = 0; i < length; i++) {
val = samples[i];
if (val < 0) {
inv = -1.0;
val = -val;
} else
inv = 1.0;
shiftmultval = (val - clipper_point) * 1000.0;
/* no clipping up to clipping point */
if (shiftmultval <= 0.0)
continue;
n = (int)shiftmultval;
q = n + 1;
if (q >= 6000) {
samples[i] = inv;
continue;
}
/* get clipped value from lut, interpolate between table entries */
val = clipper_lut[n] + (shiftmultval - (double)n) * (clipper_lut[q] - clipper_lut[n]);
samples[i] = val * inv;
}
}

@ -0,0 +1,4 @@
void clipper_init(double point);
void clipper_process(sample_t *samples, int length);
Loading…
Cancel
Save