54 lines
924 B
C
54 lines
924 B
C
|
#include <stdint.h>
|
||
|
#include <stddef.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include "gate.h"
|
||
|
#include <math.h>
|
||
|
|
||
|
void gate_init(
|
||
|
struct gate * gate
|
||
|
) {
|
||
|
gate->threshold = 0.0;
|
||
|
gate->enable_calibration = false;
|
||
|
gate->cur_state = false;
|
||
|
gate->cur_avg = 0;
|
||
|
}
|
||
|
|
||
|
void gate_process(
|
||
|
struct gate * gate,
|
||
|
size_t frame_size,
|
||
|
float const * in,
|
||
|
float * out
|
||
|
) {
|
||
|
float sum = 0.0;
|
||
|
|
||
|
for( size_t i = 0; i < frame_size; ++i ) {
|
||
|
sum += fabs( in[i] );
|
||
|
}
|
||
|
|
||
|
gate->cur_avg = sum / frame_size;
|
||
|
|
||
|
if( gate->cur_avg > gate->threshold ) {
|
||
|
|
||
|
if( ! gate->cur_state ) {
|
||
|
gate->cur_state = true;
|
||
|
printf("GATE on\n");
|
||
|
}
|
||
|
|
||
|
for( size_t i = 0; i < frame_size; ++i ) {
|
||
|
out[i] = in[i];
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if( gate->cur_state ) {
|
||
|
gate->cur_state = false;
|
||
|
printf("GATE off\n");
|
||
|
}
|
||
|
|
||
|
for( size_t i = 0; i < frame_size; ++i ) {
|
||
|
out[i] = 0.0;
|
||
|
}
|
||
|
}
|
||
|
}
|