File: rms.h

package info (click to toggle)
swh-plugins 0.4.17-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,264 kB
  • sloc: ansic: 23,551; xml: 12,633; perl: 1,114; sh: 964; makefile: 218
file content (35 lines) | stat: -rw-r--r-- 604 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef _RMS_H
#define _RMS_H

#include <math.h>

#define RMS_BUF_SIZE 64

typedef struct {
	float        buffer[RMS_BUF_SIZE];
	unsigned int pos;
	float        sum;
} rms_env;

rms_env *rms_env_new();

static inline float rms_env_process(rms_env *r, float x);

void rms_env_reset(rms_env *r);

void rms_env_free(rms_env *r);

inline static float rms_env_process(rms_env *r, const float x)
{
	r->sum -= r->buffer[r->pos];
	r->sum += x;
	if (r->sum < 1.0e-6) {
		r->sum = 0.0f;
	}
	r->buffer[r->pos] = x;
	r->pos = (r->pos + 1) & (RMS_BUF_SIZE - 1);

	return sqrt(r->sum / (float)RMS_BUF_SIZE);
}

#endif