-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilter.hpp
81 lines (63 loc) · 1.96 KB
/
Filter.hpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Filter.hpp
// AudioComponents
//
// Created by Aaron Dawson on 8/28/16.
//
//
#ifndef Filter_hpp
#define Filter_hpp
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include "SampleEffect.hpp"
#define FILTER_FREQ_MIN 10
#define FILTER_FREQ_MAX 15000
#define FILTER_FREQ_DEFAULT 500
#define FILTER_CUTOFF_CHANGE_LENGTH 2000
/* filter types */
enum {
LPF, /* low pass filter */
HPF, /* High pass filter */
BPF, /* band pass filter */
NOTCH, /* Notch Filter */
PEQ, /* Peaking band EQ filter */
LSH, /* Low shelf filter */
HSH /* High shelf filter */
};
class Filter : public SampleEffect {
private:
double filter_a0, filter_a1, filter_a2, filter_a3, filter_a4;
double filter_x1, filter_x2, filter_y1, filter_y2;
int type;
double dbGain;
double freq;
double currentOffset;
double srate;
double bandwidth;
double centerFreq;
double centerFreqChangeIncrement;
int centerFreqChangeCount;
bool changingFreq;
public:
Filter(int type, double dbGain, double freq,
double srate, double bandwidth);
double getFreq() { return freq; }
void updateCoefficients(double freqOffset);
/* Computes a BiQuad filter on a sample */
double processSample(double sample);
void changeCutoff(double newCutoff) {
if (newCutoff <= FILTER_FREQ_MAX && newCutoff >= FILTER_FREQ_MIN) {
// Find out how much the frequency should change by
double change = newCutoff - centerFreq;
// Determine increment that the gain will have to change with each sample in order to achieve change
centerFreqChangeIncrement = change/(double)FILTER_CUTOFF_CHANGE_LENGTH;
// Set count to zero
centerFreqChangeCount = 0;
// Start changing gain
changingFreq = true;
}
}
};
#endif /* Filter_hpp */