TeaSpeak-Client/native/serverconnection/src/audio/filter/FilterThreshold.cpp

46 lines
1.4 KiB
C++

#include <iostream>
#include <algorithm>
#include <cmath>
#include "FilterThreshold.h"
#include "../processing/AudioVolume.h"
using namespace std;
using namespace tc::audio;
using namespace tc::audio::filter;
ThresholdFilter::ThresholdFilter(size_t a, size_t b, size_t c) : Filter(a, b, c) {}
ThresholdFilter::~ThresholdFilter() = default;
bool ThresholdFilter::initialize(std::string &, float val, size_t margin) {
this->_threshold = val;
this->_margin_samples = margin;
return true;
}
bool ThresholdFilter::process(const void *_buffer) {
auto analyze_callback = this->on_analyze;
float value = audio::audio_buffer_level((const float*) _buffer, this->_channels, this->_frame_size);
auto last_level = this->_current_level;
float smooth;
if(this->_margin_processed_samples == 0) {
/* we're in release */
smooth = this->_release_smooth;
} else {
smooth = this->_attack_smooth;
}
this->_current_level = last_level * smooth + value * (1 - smooth);
//log_trace(category::audio, "Vad level: before: {}, edit: {}, now: {}, smooth: {}", last_level, value, this->_current_level, smooth);
if(analyze_callback) {
analyze_callback(this->_current_level);
}
if(this->_current_level >= this->_threshold) {
this->_margin_processed_samples = 0;
return true;
}
return (this->_margin_processed_samples += this->_frame_size) < this->_margin_samples;
}