Giter VIP home page Giter VIP logo

sndfilter's Introduction

sndfilter

Algorithms for sound filters, like reverb, dynamic range compression, lowpass, highpass, notch, etc.

It's easy to find countless math equations on sound filters, but a bit harder to find simple source code. This library is my attempt at cleaning up and presenting the math-heavy filter algorithms for the programming community.

Please note that I favored simple code over fast code. Hopefully it's made it more understandable.

(MIT License)

Build Instructions

The ./build script is a simple bash script that compiles the source files using clang. It's dirt simple, I promise.

Simply run ./build and the executable should be ./tgt/sndfilter.

C++ Support

This project is pure C, but I've left PRs open for those who want C++ support. Check them out, they might save you some time:

Filters

Implementation

The reverb.c, compressor.c, and biquad.c are the core algorithms.

I do not understand the biquad math, so please don't ask me any questions :-). The core formulas were extracted from Biquad.cpp (Chromium source), and cleaned up a bit to make easier to read.

The compressor came from DynamicsCompressorKernel.cpp (also from Chromium), and cleaned up a bit more. I swapped out the adaptive release curve and simplified the knee calculations. I feel a little more comfortable with that algorithm because there isn't a whole lot of magical math involved.

The reverb effect is a complete rewrite of Freeverb3's Progenitor2 algorithm. It took quite a lot of effort to tear apart the algorithm and rebuild it, but I'm pretty sure it's right.

sndfilter's People

Contributors

buslov avatar velipso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sndfilter's Issues

Expander/Upward compressor

Your classes are very good.
How about altering the compressor to support a) upward (compress when audio is below threshold) and b) expand (increase range instead of reduce it by ratio).

reverb algorithm performance

I have ported the lib to my Android project, the algorithm works well, but it is time cost. Can you give me some suggestion about this? Should I reduce some parameters or reduce effect quality?

Something is weird with the compressor?

Are you sure this is working right?

sf_advancecomp(&STX[i], 48000, 0, -40, 0, 10, 0.25f, 0.15f,
					0.006f, // predelay
					0.090f, // releasezone1
					0.160f, // releasezone2
					0.420f, // releasezone3
					0.980f, // releasezone4
					0.000f, // postgain
					1.000f  // wet
				);

Now this is fed with a 512-sample array

image

And the result is (at sample 287)

image

Is there some extra gain added?

Sidechain compression?

It 'd be nice to be able to add a parameter for sidechain: Different input signal than output signal.

So for example, sf_compressor_process with an additional input 2: If the input 2 is above threshold, then compress input 1.

Low pass filter infinite alpha

In the biquad_makeLPF function in reverb.c:

static inline void biquad_makeLPF(sf_rv_biquad_st *biquad, int rate, float freq, float bw){
  freq = clampf(freq, 0, rate / 2);
  float omega = 2.0f * (float)M_PI * freq / (float)rate;
  float cs = cosf(omega);
  float sn = sinf(omega);
  float alpha = sn * sinhf((float)M_LN2 * 0.5f * bw * omega / sn);
  float a0inv = 1.0f / (1.0f + alpha);
  biquad->b0 = a0inv * (1.0f - cs) * 0.5f;
  biquad->b1 = 2.0f * biquad->b0;
  biquad->b2 = biquad->b0;
  biquad->a1 = a0inv * -2.0f * cs;
  biquad->a2 = a0inv * (1.0f - alpha);
  biquad->xn1 = 0;
  biquad->xn2 = 0;
  biquad->yn1 = 0;
  biquad->yn2 = 0;
}

If rate is integral multiple of freq, then omega will be integral multiple of PI, and sn = sinf(M_PI) is 0, sn is used as a divisor for calculating alpha, the result of alpha will be INFINITY, which causes any other values to be NaN when calculated with alpha.

Can I test the value of alpha with isinf(), if it is true, assign a valid value to it? But what is the valid value range of alpha? Or what is the valid value for it if it is INFINITY?

License issue?

Hello, first of all, nice work. :squirrel:
I'm working on an opensource code that will be released under a permissive license and i have add some reverberation to an audio source, your reverb implementation is exactly what im looking for, but i've read the readme and it says

The reverb effect is a complete rewrite of Freeverb3's Progenitor2 algorithm.

afaik Freeverb3 is a gnu library under gnu gpl license, due to the properties of this license, shouldn't this code be licensed under gpl aswell?

using a compressor on streaming audio

Hiya - I'm wondering how to go about using the compressor in a context of streaming since it's using a chunk size of 32 and so always return <= the input buffer.

Reverb performance issue

Hi, first of all Thank you so much for your amazing works.
I tried using your reverb and it works well and sounds very good.
But it seems like it is pretty heavy in terms of CPU usage.
I wonder if you have any suggestion to reduce it's CPU usage other than reducing the channel to mono.
Thanks!

Request Audio Wav sample test

Hi voidqk!
I'm very interesting with your dynamic range compressor project.
i'm making it like you (nuc505 and Keil C), but i have some issues and i really want to get a wav file to test your code.

Could you send me a link to download it?
Thanks
P/s: sorry about my English skill.

Possible bug in down sample function in reverb?

The function oversample_stepdown appears to put the samples through the recovery low pass filter but always returns the first input value. I think it should look more like this:

// input length must be oversample->factor
static float oversample_stepdown(sf_rv_oversample_st *oversample, float *input)
{
    if (oversample->factor == 1)
    {
        return input[0];
    }
    else
    {
        int i;
        float   accum = 0.0f;
        for (i = 0; i < oversample->factor; i++)
        {
            accum += biquad_step(&oversample->lpfD, input[i]);
        }
        accum /= (float)oversample->factor;
        return accum;
    }
}

Biquad filter is hard clipping the audio

I'm using sndfilter biquad filter as a chain of filters, i.e. combining lowshelf, peaking and highshelf filters.

In general, the equalizer should not over amplify the audio with the params I use.

I can see that you have ported Web Audio API biquad filter and the code is almost the same, but it results in completely different output.

eq-hard-clipping-diff

  1. Original
  2. GarageBand
  3. sndfilter

GarageBand is just an example, we will get almost the same result with other tools.

Could you please confirm whether this behavior is expected or maybe it's a known issue?

How to reproduce

Download drum-beat.wav

sndfilter drum-beat.wav filtered.wav lowshelf 250 1 20
sndfilter filtered.wav filtered.wav peaking 500 3 20

You can reproduce it only by using lowshelf or peaking, but it's more demonstrative when combining them too.

And also biquad filters affect only starting from around 1.3 seconds of the input audio. This is a separate issue BTW.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.