How to reduce background noise with AudioKit?

113 Views Asked by At

I noticed that there used to exist an AKNoiseGate filter but not anymore! I've tried DynamicRangeCompressor but I'm not happy with it! Is there a filter that does that?

I've tried to create a DSP, but the audio gets very distorted:

#include "SoundpipeDSPBase.h"
#include "ParameterRamper.h"
#include "Soundpipe.h"
#include "CSoundpipeAudioKit.h"

#define CHUNKSIZE 512

void process(FrameRange range) override {
    const float *inBuffers[2];
    float *outBuffers[2];
    inBuffers[0]  = (const float *)inputBufferLists[0]->mBuffers[0].mData + range.start;
    inBuffers[1]  = (const float *)inputBufferLists[0]->mBuffers[1].mData + range.start;
    outBuffers[0] = (float *)outputBufferList->mBuffers[0].mData + range.start;
    outBuffers[1] = (float *)outputBufferList->mBuffers[1].mData + range.start;
    
    if (!isStarted) {
        // effect bypassed: just copy input to output
        memcpy(outBuffers[0], inBuffers[0], range.count * sizeof(float));
        memcpy(outBuffers[1], inBuffers[1], range.count * sizeof(float));
        return;
    }
    
    // Convert dB to linear amplitude
    const float threshold = powf(10.0, decibelCutoff / 20.0);
    
    // process in chunks of maximum length CHUNKSIZE
    for (int frameIndex = 0; frameIndex < range.count; frameIndex += CHUNKSIZE) {
        int chunkSize = range.count - frameIndex;
        if (chunkSize > CHUNKSIZE) chunkSize = CHUNKSIZE;
        
        for (int i = 0; i < chunkSize; ++i) {
            // Check amplitude of each sample in stereo channels
            for (int channel = 0; channel < 2; ++channel) {
                float amplitude = fabs(inBuffers[channel][i]);

                // If amplitude is below threshold, mute, otherwise pass through
                outBuffers[channel][i] = (amplitude < threshold) ? 0.0 : inBuffers[channel][i];
            }
        }
        
        // advance pointers
        inBuffers[0] += chunkSize;
        inBuffers[1] += chunkSize;
        outBuffers[0] += chunkSize;
        outBuffers[1] += chunkSize;
    }
}

I have 0 signal processing knowledge, so all this is unknown to me! Any ideas?

1

There are 1 best solutions below

2
soundflix On

There is a component called AKBooster that you can use as a noise gate.
I found a Swift example here:

import AudioKit

let microphone = AKMicrophone()
let noiseGate = AKBooster(microphone,
                          gain: 0,
                          rampDuration: 0.001,
                          minimumLevel: -90)

// Set up the microphone input node
let microphoneMixer = AKMixer(noiseGate)
AudioKit.output = microphoneMixer
AudioKit.start()

minimumLevel is the gate threshold value in dBFS. Probably you need to set it around -50 to -30 to have a noticeable effect.

Note that a full-ranged Noise Gate is a very primitive form of noise reduction.