summaryrefslogtreecommitdiff
path: root/servers/audio/effects/audio_effect_chorus.cpp
blob: 553e1a16a0cb7142df9637d75a783122970a9e0b (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "audio_effect_chorus.h"
#include "servers/audio_server.h"
#include "math_funcs.h"

void AudioEffectChorusInstance::process(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) {

	int todo = p_frame_count;

	while(todo) {

		int to_mix = MIN(todo,256); //can't mix too much

		_process_chunk(p_src_frames,p_dst_frames,to_mix);

		p_src_frames+=to_mix;
		p_dst_frames+=to_mix;

		todo-=to_mix;
	}
}

void AudioEffectChorusInstance::_process_chunk(const AudioFrame *p_src_frames,AudioFrame *p_dst_frames,int p_frame_count) {


	//fill ringbuffer
	for(int i=0;i<p_frame_count;i++) {
		audio_buffer[(buffer_pos+i)&buffer_mask]=p_src_frames[i];
		p_dst_frames[i]=p_src_frames[i]*base->dry;
	}

	float mix_rate = AudioServer::get_singleton()->get_mix_rate();

	/* process voices */
	for (int vc=0;vc<base->voice_count;vc++) {

		AudioEffectChorus::Voice &v=base->voice[vc];


		double time_to_mix=(float)p_frame_count/mix_rate;
		double cycles_to_mix=time_to_mix*v.rate;

		unsigned int local_rb_pos=buffer_pos;
		AudioFrame *dst_buff=p_dst_frames;
		AudioFrame *rb_buff=audio_buffer.ptr();

		double delay_msec=v.delay;
		unsigned int delay_frames=Math::fast_ftoi((delay_msec/1000.0)*mix_rate);
		float max_depth_frames=(v.depth/1000.0)*mix_rate;

		uint64_t local_cycles=cycles[vc];
		uint64_t increment=llrint(cycles_to_mix/(double)p_frame_count*(double)(1<<AudioEffectChorus::CYCLES_FRAC));

		//check the LFO doesnt read ahead of the write pos
		if ((((int)max_depth_frames)+10)>delay_frames) { //10 as some threshold to avoid precision stuff
			delay_frames+=(int)max_depth_frames-delay_frames;
			delay_frames+=10; //threshold to avoid precision stuff

		}



		//low pass filter
		if (v.cutoff==0)
			continue;
		float auxlp=expf(-2.0*Math_PI*v.cutoff/mix_rate);
		float c1=1.0-auxlp;
		float c2=auxlp;
		AudioFrame h=filter_h[vc];
		if (v.cutoff>=AudioEffectChorus::MS_CUTOFF_MAX) {
			c1=1.0; c2=0.0;
		}

		//vol modifier

		AudioFrame vol_modifier=AudioFrame(base->wet,base->wet) * Math::db2linear(v.level);
		vol_modifier.l*=CLAMP( 1.0 - v.pan, 0, 1);
		vol_modifier.r*=CLAMP( 1.0 + v.pan, 0, 1);



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

			/** COMPUTE WAVEFORM **/

			float phase=(float)(local_cycles&AudioEffectChorus::CYCLES_MASK)/(float)(1<<AudioEffectChorus::CYCLES_FRAC);

			float wave_delay=sinf(phase*2.0*Math_PI)*max_depth_frames;

			int wave_delay_frames=lrint(floor(wave_delay));
			float wave_delay_frac=wave_delay-(float)wave_delay_frames;

			/** COMPUTE RINGBUFFER POS**/

			unsigned int rb_source=local_rb_pos;
			rb_source-=delay_frames;

			rb_source-=wave_delay_frames;

			/** READ FROM RINGBUFFER, LINEARLY INTERPOLATE */

			AudioFrame val=rb_buff[rb_source&buffer_mask];
			AudioFrame val_next=rb_buff[(rb_source-1)&buffer_mask];

			val+=(val_next-val)*wave_delay_frac;

			val=val*c1+h*c2;
			h=val;

			/** MIX VALUE TO OUTPUT **/

			dst_buff[i]+=val*vol_modifier;

			local_cycles+=increment;
			local_rb_pos++;

		}

		filter_h[vc]=h;
		cycles[vc]+=Math::fast_ftoi(cycles_to_mix*(double)(1<<AudioEffectChorus::CYCLES_FRAC));
	}

	buffer_pos+=p_frame_count;
}


Ref<AudioEffectInstance> AudioEffectChorus::instance() {

	Ref<AudioEffectChorusInstance> ins;
	ins.instance();
	ins->base=Ref<AudioEffectChorus>(this);
	for(int i=0;i<4;i++) {
		ins->filter_h[i]=AudioFrame(0,0);
		ins->cycles[i]=0;
	}

	float ring_buffer_max_size=AudioEffectChorus::MAX_DELAY_MS+AudioEffectChorus::MAX_DEPTH_MS+AudioEffectChorus::MAX_WIDTH_MS;

	ring_buffer_max_size*=2; //just to avoid complications
	ring_buffer_max_size/=1000.0;//convert to seconds
	ring_buffer_max_size*=AudioServer::get_singleton()->get_mix_rate();

	int ringbuff_size=ring_buffer_max_size;

	int bits=0;

	while(ringbuff_size>0) {
		bits++;
		ringbuff_size/=2;
	}

	ringbuff_size=1<<bits;
	ins->buffer_mask=ringbuff_size-1;
	ins->buffer_pos=0;
	ins->audio_buffer.resize(ringbuff_size);
	for(int i=0;i<ringbuff_size;i++) {
		ins->audio_buffer[i]=AudioFrame(0,0);
	}

	return ins;
}

void AudioEffectChorus::set_voice_count(int p_voices) {

	ERR_FAIL_COND(p_voices<1 || p_voices>=MAX_VOICES);
	voice_count=p_voices;
	_change_notify();
}


int AudioEffectChorus::get_voice_count() const{

	return voice_count;
}

void AudioEffectChorus::set_voice_delay_ms(int p_voice,float p_delay_ms){

	ERR_FAIL_INDEX(p_voice,MAX_VOICES);

	voice[p_voice].delay=p_delay_ms;

}
float AudioEffectChorus::get_voice_delay_ms(int p_voice) const{

	ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0);
	return voice[p_voice].delay;
}

void AudioEffectChorus::set_voice_rate_hz(int p_voice,float p_rate_hz){
	ERR_FAIL_INDEX(p_voice,MAX_VOICES);

	voice[p_voice].rate=p_rate_hz;
}
float AudioEffectChorus::get_voice_rate_hz(int p_voice) const{

	ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0);

	return voice[p_voice].rate;
}

void AudioEffectChorus::set_voice_depth_ms(int p_voice,float p_depth_ms){

	ERR_FAIL_INDEX(p_voice,MAX_VOICES);

	voice[p_voice].depth=p_depth_ms;
}
float AudioEffectChorus::get_voice_depth_ms(int p_voice) const{

	ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0);

	return voice[p_voice].depth;
}

void AudioEffectChorus::set_voice_level_db(int p_voice,float p_level_db){

	ERR_FAIL_INDEX(p_voice,MAX_VOICES);

	voice[p_voice].level=p_level_db;
}
float AudioEffectChorus::get_voice_level_db(int p_voice) const{

	ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0);

	return voice[p_voice].level;
}


void AudioEffectChorus::set_voice_cutoff_hz(int p_voice,float p_cutoff_hz){

	ERR_FAIL_INDEX(p_voice,MAX_VOICES);

	voice[p_voice].cutoff=p_cutoff_hz;
}
float AudioEffectChorus::get_voice_cutoff_hz(int p_voice) const{

	ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0);

	return voice[p_voice].cutoff;
}

void AudioEffectChorus::set_voice_pan(int p_voice,float p_pan){

	ERR_FAIL_INDEX(p_voice,MAX_VOICES);

	voice[p_voice].pan=p_pan;
}
float AudioEffectChorus::get_voice_pan(int p_voice) const{

	ERR_FAIL_INDEX_V(p_voice,MAX_VOICES,0);

	return voice[p_voice].pan;
}


void AudioEffectChorus::set_wet(float amount){


	wet=amount;
}
float AudioEffectChorus::get_wet() const{

	return wet;
}

void AudioEffectChorus::set_dry(float amount){

	dry=amount;

}
float AudioEffectChorus::get_dry() const{

	return dry;
}

void AudioEffectChorus::_validate_property(PropertyInfo& property) const {

	if (property.name.begins_with("voice/")) {
		int voice_idx = property.name.get_slice("/",1).to_int();
		if (voice_idx>voice_count) {
			property.usage=0;
		}
	}
}


void AudioEffectChorus::_bind_methods() {

	ClassDB::bind_method(_MD("set_voice_count","voices"),&AudioEffectChorus::set_voice_count);
	ClassDB::bind_method(_MD("get_voice_count"),&AudioEffectChorus::get_voice_count);


	ClassDB::bind_method(_MD("set_voice_delay_ms","voice_idx","delay_ms"),&AudioEffectChorus::set_voice_delay_ms);
	ClassDB::bind_method(_MD("get_voice_delay_ms","voice_idx"),&AudioEffectChorus::get_voice_delay_ms);

	ClassDB::bind_method(_MD("set_voice_rate_hz","voice_idx","rate_hz"),&AudioEffectChorus::set_voice_rate_hz);
	ClassDB::bind_method(_MD("get_voice_rate_hz","voice_idx"),&AudioEffectChorus::get_voice_rate_hz);

	ClassDB::bind_method(_MD("set_voice_depth_ms","voice_idx","depth_ms"),&AudioEffectChorus::set_voice_depth_ms);
	ClassDB::bind_method(_MD("get_voice_depth_ms","voice_idx"),&AudioEffectChorus::get_voice_depth_ms);

	ClassDB::bind_method(_MD("set_voice_level_db","voice_idx","level_db"),&AudioEffectChorus::set_voice_level_db);
	ClassDB::bind_method(_MD("get_voice_level_db","voice_idx"),&AudioEffectChorus::get_voice_level_db);

	ClassDB::bind_method(_MD("set_voice_cutoff_hz","voice_idx","cutoff_hz"),&AudioEffectChorus::set_voice_cutoff_hz);
	ClassDB::bind_method(_MD("get_voice_cutoff_hz","voice_idx"),&AudioEffectChorus::get_voice_cutoff_hz);

	ClassDB::bind_method(_MD("set_voice_pan","voice_idx","pan"),&AudioEffectChorus::set_voice_pan);
	ClassDB::bind_method(_MD("get_voice_pan","voice_idx"),&AudioEffectChorus::get_voice_pan);

	ClassDB::bind_method(_MD("set_wet","amount"),&AudioEffectChorus::set_wet);
	ClassDB::bind_method(_MD("get_wet"),&AudioEffectChorus::get_wet);

	ClassDB::bind_method(_MD("set_dry","amount"),&AudioEffectChorus::set_dry);
	ClassDB::bind_method(_MD("get_dry"),&AudioEffectChorus::get_dry);

	ADD_PROPERTY(PropertyInfo(Variant::INT,"voice_count",PROPERTY_HINT_RANGE,"1,4,1"),"set_voice_count","get_voice_count");
	ADD_PROPERTY(PropertyInfo(Variant::REAL,"dry",PROPERTY_HINT_RANGE,"0,1,0.01"),"set_dry","get_dry");
	ADD_PROPERTY(PropertyInfo(Variant::REAL,"wet",PROPERTY_HINT_RANGE,"0,1,0.01"),"set_wet","get_wet");

	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/delay_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),"set_voice_delay_ms","get_voice_delay_ms",0);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/rate_hz",PROPERTY_HINT_RANGE,"0.1,20,0.1"),"set_voice_rate_hz","get_voice_rate_hz",0);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/depth_ms",PROPERTY_HINT_RANGE,"0,20,0.01"),"set_voice_depth_ms","get_voice_depth_ms",0);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/level_db",PROPERTY_HINT_RANGE,"-60,24,0.1"),"set_voice_level_db","get_voice_level_db",0);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/cutoff_hz",PROPERTY_HINT_RANGE,"1,16000,1"),"set_voice_cutoff_hz","get_voice_cutoff_hz",0);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/1/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),"set_voice_pan","get_voice_pan",0);

	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/delay_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),"set_voice_delay_ms","get_voice_delay_ms",1);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/rate_hz",PROPERTY_HINT_RANGE,"0.1,20,0.1"),"set_voice_rate_hz","get_voice_rate_hz",1);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/depth_ms",PROPERTY_HINT_RANGE,"0,20,0.01"),"set_voice_depth_ms","get_voice_depth_ms",1);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/level_db",PROPERTY_HINT_RANGE,"-60,24,0.1"),"set_voice_level_db","get_voice_level_db",1);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/cutoff_hz",PROPERTY_HINT_RANGE,"1,16000,1"),"set_voice_cutoff_hz","get_voice_cutoff_hz",1);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/2/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),"set_voice_pan","get_voice_pan",1);

	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/delay_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),"set_voice_delay_ms","get_voice_delay_ms",2);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/rate_hz",PROPERTY_HINT_RANGE,"0.1,20,0.1"),"set_voice_rate_hz","get_voice_rate_hz",2);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/depth_ms",PROPERTY_HINT_RANGE,"0,20,0.01"),"set_voice_depth_ms","get_voice_depth_ms",2);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/level_db",PROPERTY_HINT_RANGE,"-60,24,0.1"),"set_voice_level_db","get_voice_level_db",2);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/cutoff_hz",PROPERTY_HINT_RANGE,"1,16000,1"),"set_voice_cutoff_hz","get_voice_cutoff_hz",2);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/3/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),"set_voice_pan","get_voice_pan",2);

	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/delay_ms",PROPERTY_HINT_RANGE,"0,50,0.01"),"set_voice_delay_ms","get_voice_delay_ms",3);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/rate_hz",PROPERTY_HINT_RANGE,"0.1,20,0.1"),"set_voice_rate_hz","get_voice_rate_hz",3);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/depth_ms",PROPERTY_HINT_RANGE,"0,20,0.01"),"set_voice_depth_ms","get_voice_depth_ms",3);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/level_db",PROPERTY_HINT_RANGE,"-60,24,0.1"),"set_voice_level_db","get_voice_level_db",3);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/cutoff_hz",PROPERTY_HINT_RANGE,"1,16000,1"),"set_voice_cutoff_hz","get_voice_cutoff_hz",3);
	ADD_PROPERTYI(PropertyInfo(Variant::REAL,"voice/4/pan",PROPERTY_HINT_RANGE,"-1,1,0.01"),"set_voice_pan","get_voice_pan",3);

}

AudioEffectChorus::AudioEffectChorus()
{
	voice_count=2;
	voice[0].delay=15;
	voice[1].delay=20;
	voice[0].rate=0.8;
	voice[1].rate=1.2;
	voice[0].depth=2;
	voice[1].depth=3;
	voice[0].cutoff=8000;
	voice[1].cutoff=8000;
	voice[0].pan=-0.5;
	voice[1].pan=0.5;

	wet=0.5;
	dry=1.0;
}