File: effect_phaser.cpp

package info (click to toggle)
zytrax 0%2Bgit20201215-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,488 kB
  • sloc: cpp: 41,800; ansic: 3,387; makefile: 8; sh: 3
file content (169 lines) | stat: -rw-r--r-- 4,987 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "effect_phaser.h"

#include "dsp/db.h"
#include <math.h>
//process
bool AudioEffectPhaser::has_secondary_input() const {
	return false;
}

void AudioEffectPhaser::process(const Event *p_events, int p_event_count, const AudioFrame *p_in, AudioFrame *p_out, bool p_prev_active) {

	float dmin = control_ports[CONTROL_PORT_RANGE_MIN_HZ].value / (sampling_rate / 2.0);
	float dmax = control_ports[CONTROL_PORT_RANGE_MAX_HZ].value / (sampling_rate / 2.0);

	float increment = 2.f * M_PI * (control_ports[CONTROL_PORT_RATE_HZ].value / sampling_rate);
	float depth = control_ports[CONTROL_PORT_DEPTH].value;
	float feedback = control_ports[CONTROL_PORT_FEEDBACK].value;

	for (int i = 0; i < block_size; i++) {

		phase += increment;

		while (phase >= M_PI * 2.f) {
			phase -= M_PI * 2.f;
		}

		float d = dmin + (dmax - dmin) * ((sin(phase) + 1.f) / 2.f);

		//update filter coeffs
		for (int j = 0; j < 6; j++) {
			allpass[0][j].delay(d);
			allpass[1][j].delay(d);
		}

		//calculate output
		float y = allpass[0][0].update(
				allpass[0][1].update(
						allpass[0][2].update(
								allpass[0][3].update(
										allpass[0][4].update(
												allpass[0][5].update(p_in[i].l + h.l * feedback))))));
		h.l = y;

		p_out[i].l = p_in[i].l + y * depth;

		y = allpass[1][0].update(
				allpass[1][1].update(
						allpass[1][2].update(
								allpass[1][3].update(
										allpass[1][4].update(
												allpass[1][5].update(p_in[i].r + h.r * feedback))))));
		h.r = y;

		p_out[i].r = p_in[i].r + y * depth;
	}
}

void AudioEffectPhaser::process_with_secondary(const Event *p_events, int p_event_count, const AudioFrame *p_in, const AudioFrame *p_secondary, AudioFrame *p_out, bool p_prev_active) {
}

void AudioEffectPhaser::set_process_block_size(int p_size) {
	block_size = p_size;
}
void AudioEffectPhaser::set_sampling_rate(int p_hz) {
	if (sampling_rate != p_hz) {
		sampling_rate = p_hz;
		_update_buffers();
	}
}
//info
String AudioEffectPhaser::get_name() const {
	return "Phaser";
}
String AudioEffectPhaser::get_unique_id() const {
	return "phaser";
}
String AudioEffectPhaser::get_provider_id() const {
	return "internal";
}

int AudioEffectPhaser::get_control_port_count() const {
	return CONTROL_PORT_MAX;
}
ControlPort *AudioEffectPhaser::get_control_port(int p_port) {
	ERR_FAIL_INDEX_V(p_port, CONTROL_PORT_MAX, NULL);
	return &control_ports[p_port];
}

void AudioEffectPhaser::reset() {
	_update_buffers();
}

/* Load/Save */

JSON::Node AudioEffectPhaser::to_json() const {
	JSON::Node node = JSON::object();

	for (int i = 0; i < CONTROL_PORT_MAX; i++) {
		node.add(control_ports[i].identifier.utf8().get_data(), control_ports[i].value);
	}
	return node;
}
Error AudioEffectPhaser::from_json(const JSON::Node &node) {

	for (int i = 0; i < CONTROL_PORT_MAX; i++) {
		std::string key = control_ports[i].identifier.utf8().get_data();
		if (node.has(key)) {
			control_ports[i].value = node.get(key).toFloat();
		}
	}
	return OK;
}

void AudioEffectPhaser::_update_buffers() {

	h = AudioFrame(0, 0);
	phase = 0;
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 6; j++) {
			allpass[i][j] = AllpassDelay();
		}
	}
}
AudioEffectPhaser::AudioEffectPhaser() {

	control_ports[CONTROL_PORT_RANGE_MIN_HZ].name = "Range Min (hz)";
	control_ports[CONTROL_PORT_RANGE_MIN_HZ].identifier = "range_min";
	control_ports[CONTROL_PORT_RANGE_MIN_HZ].min = 10;
	control_ports[CONTROL_PORT_RANGE_MIN_HZ].max = 10000;
	control_ports[CONTROL_PORT_RANGE_MIN_HZ].step = 1;
	control_ports[CONTROL_PORT_RANGE_MIN_HZ].value = 440;

	control_ports[CONTROL_PORT_RANGE_MAX_HZ].name = "Range Max (hz)";
	control_ports[CONTROL_PORT_RANGE_MAX_HZ].identifier = "range_max";
	control_ports[CONTROL_PORT_RANGE_MAX_HZ].min = 10;
	control_ports[CONTROL_PORT_RANGE_MAX_HZ].max = 10000;
	control_ports[CONTROL_PORT_RANGE_MAX_HZ].step = 1;
	control_ports[CONTROL_PORT_RANGE_MAX_HZ].value = 1600;

	control_ports[CONTROL_PORT_RATE_HZ].name = "Rate (hz)";
	control_ports[CONTROL_PORT_RATE_HZ].identifier = "rate";
	control_ports[CONTROL_PORT_RATE_HZ].min = 0.01;
	control_ports[CONTROL_PORT_RATE_HZ].max = 20;
	control_ports[CONTROL_PORT_RATE_HZ].step = 0.01;
	control_ports[CONTROL_PORT_RATE_HZ].value = 0.5;

	control_ports[CONTROL_PORT_FEEDBACK].name = "Feedback";
	control_ports[CONTROL_PORT_FEEDBACK].identifier = "feedback";
	control_ports[CONTROL_PORT_FEEDBACK].min = 0.1;
	control_ports[CONTROL_PORT_FEEDBACK].max = 0.9;
	control_ports[CONTROL_PORT_FEEDBACK].step = 0.1;
	control_ports[CONTROL_PORT_FEEDBACK].value = 0.7;

	control_ports[CONTROL_PORT_DEPTH].name = "Depth";
	control_ports[CONTROL_PORT_DEPTH].identifier = "depth";
	control_ports[CONTROL_PORT_DEPTH].min = 0.1;
	control_ports[CONTROL_PORT_DEPTH].max = 4;
	control_ports[CONTROL_PORT_DEPTH].step = 0.1;
	control_ports[CONTROL_PORT_DEPTH].value = 1;

	block_size = 128;
	sampling_rate = 0;
	set_sampling_rate(44100);
	set_process_block_size(128);

	_update_buffers();
}
AudioEffectPhaser::~AudioEffectPhaser() {
}