//-- AudioFiltersNative.cpp #include "pch.h" #include "AudioFiltersNative.h" #include #include static float sampleRate_; static float highCutoff_; static float lowCutoff_; extern "C" { //-- Declare the function to be exported __declspec(dllexport) void ApplyBandStopFilter(float* data, int channels, int dataLength, float* bufferLow, float* bufferHigh, float lowCutoff, float highCutoff) { highCutoff_ = highCutoff; lowCutoff_ = lowCutoff; float* dataPtr = data; float* dataEnd = data + dataLength; while (dataPtr < dataEnd) { for (int channel = 0; channel < channels; channel++) { float sample = *dataPtr; for (int i = 0; i < 3; i++) { sample = ApplyBandStopFilterInternal(sample, channel, bufferLow, bufferHigh); } *dataPtr = sample; dataPtr++; } } } extern "C" { __declspec(dllexport) void InitializeFilters(float sampleRate) { sampleRate_ = sampleRate; } } //-- Helper functions for low-pass and high-pass filters inline float ApplyLowPassFilter(float sample, float cutoff, float* buffer, int channel) { float alpha = cutoff / (cutoff + sampleRate_ / 2); float oneMinusAlpha = 1.0f - alpha; float filtered = alpha * sample + oneMinusAlpha * buffer[channel]; buffer[channel] = filtered; return filtered; } inline float ApplyHighPassFilter(float sample, float cutoff, float* buffer, int channel) { float alpha = cutoff / (cutoff + sampleRate_ / 2); float filtered = alpha * (buffer[channel] + sample - buffer[channel]); buffer[channel] = filtered; return sample - filtered; } inline float ApplyBandStopFilterInternal(float sample, int channel, float* bufferLow, float* bufferHigh) { float lowFiltered = ApplyLowPassFilter(sample, lowCutoff_, bufferLow, channel); float highFiltered = ApplyHighPassFilter(sample, highCutoff_, bufferHigh, channel); return lowFiltered - highFiltered; } }