Lines Matching refs:dk

31 void dk_init(struct drc_kernel *dk, float sample_rate)  in dk_init()  argument
40 dk->sample_rate = sample_rate; in dk_init()
41 dk->detector_average = 0; in dk_init()
42 dk->compressor_gain = 1; in dk_init()
43 dk->enabled = 0; in dk_init()
44 dk->processed = 0; in dk_init()
45 dk->last_pre_delay_frames = DEFAULT_PRE_DELAY_FRAMES; in dk_init()
46 dk->pre_delay_read_index = 0; in dk_init()
47 dk->pre_delay_write_index = DEFAULT_PRE_DELAY_FRAMES; in dk_init()
48 dk->max_attack_compression_diff_db = -INFINITY; in dk_init()
49 dk->ratio = uninitialized_value; in dk_init()
50 dk->slope = uninitialized_value; in dk_init()
51 dk->linear_threshold = uninitialized_value; in dk_init()
52 dk->db_threshold = uninitialized_value; in dk_init()
53 dk->db_knee = uninitialized_value; in dk_init()
54 dk->knee_threshold = uninitialized_value; in dk_init()
55 dk->ratio_base = uninitialized_value; in dk_init()
56 dk->K = uninitialized_value; in dk_init()
64 dk->pre_delay_buffers[i] = (float *)calloc(1, size); in dk_init()
68 void dk_free(struct drc_kernel *dk) in dk_free() argument
72 free(dk->pre_delay_buffers[i]); in dk_free()
76 static void set_pre_delay_time(struct drc_kernel *dk, float pre_delay_time) in set_pre_delay_time() argument
81 unsigned pre_delay_frames = pre_delay_time * dk->sample_rate; in set_pre_delay_time()
94 if (dk->last_pre_delay_frames != pre_delay_frames) { in set_pre_delay_time()
95 dk->last_pre_delay_frames = pre_delay_frames; in set_pre_delay_time()
98 memset(dk->pre_delay_buffers[i], 0, size); in set_pre_delay_time()
101 dk->pre_delay_read_index = 0; in set_pre_delay_time()
102 dk->pre_delay_write_index = pre_delay_frames; in set_pre_delay_time()
113 static float knee_curve(struct drc_kernel *dk, float x, float k) in knee_curve() argument
116 if (x < dk->linear_threshold) in knee_curve()
119 return dk->linear_threshold + in knee_curve()
120 (1 - knee_expf(-k * (x - dk->linear_threshold))) / k; in knee_curve()
127 static float slope_at(struct drc_kernel *dk, float x, float k) in slope_at() argument
129 if (x < dk->linear_threshold) in slope_at()
137 float y_db = linear_to_decibels(knee_curve(dk, x, k)); in slope_at()
138 float y2Db = linear_to_decibels(knee_curve(dk, x2, k)); in slope_at()
145 static float k_at_slope(struct drc_kernel *dk, float desired_slope) in k_at_slope() argument
147 float x_db = dk->db_threshold + dk->db_knee; in k_at_slope()
159 float slope = slope_at(dk, x, k); in k_at_slope()
176 static void update_static_curve_parameters(struct drc_kernel *dk, in update_static_curve_parameters() argument
180 if (db_threshold != dk->db_threshold || db_knee != dk->db_knee || in update_static_curve_parameters()
181 ratio != dk->ratio) { in update_static_curve_parameters()
183 dk->db_threshold = db_threshold; in update_static_curve_parameters()
184 dk->linear_threshold = decibels_to_linear(db_threshold); in update_static_curve_parameters()
185 dk->db_knee = db_knee; in update_static_curve_parameters()
188 dk->ratio = ratio; in update_static_curve_parameters()
189 dk->slope = 1 / dk->ratio; in update_static_curve_parameters()
191 float k = k_at_slope(dk, 1 / dk->ratio); in update_static_curve_parameters()
192 dk->K = k; in update_static_curve_parameters()
194 dk->knee_alpha = dk->linear_threshold + 1 / k; in update_static_curve_parameters()
195 dk->knee_beta = -expf(k * dk->linear_threshold) / k; in update_static_curve_parameters()
197 dk->knee_threshold = decibels_to_linear(db_threshold + db_knee); in update_static_curve_parameters()
199 float y0 = knee_curve(dk, dk->knee_threshold, k); in update_static_curve_parameters()
200 dk->ratio_base = y0 * powf(dk->knee_threshold, -dk->slope); in update_static_curve_parameters()
206 static float knee_curveK(struct drc_kernel *dk, float x) in knee_curveK() argument
215 return dk->knee_alpha + dk->knee_beta * knee_expf(-dk->K * x); in knee_curveK()
220 static float volume_gain(struct drc_kernel *dk, float x) in volume_gain() argument
224 if (x < dk->knee_threshold) { in volume_gain()
225 if (x < dk->linear_threshold) in volume_gain()
227 y = knee_curveK(dk, x) / x; in volume_gain()
237 y = dk->ratio_base * knee_expf(logf(x) * (dk->slope - 1)); in volume_gain()
243 void dk_set_parameters(struct drc_kernel *dk, in dk_set_parameters() argument
256 float sample_rate = dk->sample_rate; in dk_set_parameters()
258 update_static_curve_parameters(dk, db_threshold, db_knee, ratio); in dk_set_parameters()
261 float full_range_gain = volume_gain(dk, 1); in dk_set_parameters()
267 dk->master_linear_gain = decibels_to_linear(db_post_gain) * in dk_set_parameters()
272 dk->attack_frames = attack_time * sample_rate; in dk_set_parameters()
280 dk->sat_release_frames_inv_neg = -1 / sat_release_frames; in dk_set_parameters()
281 dk->sat_release_rate_at_neg_two_db = in dk_set_parameters()
282 decibels_to_linear(-2 * dk->sat_release_frames_inv_neg) - 1; in dk_set_parameters()
296 dk->kA = 0.9999999999999998f*y1 + 1.8432219684323923e-16f*y2 in dk_set_parameters()
298 dk->kB = -1.5788320352845888f*y1 + 2.3305837032074286f*y2 in dk_set_parameters()
300 dk->kC = 0.5334142869106424f*y1 - 1.272736789213631f*y2 in dk_set_parameters()
302 dk->kD = 0.08783463138207234f*y1 - 0.1694162967925622f*y2 in dk_set_parameters()
304 dk->kE = -0.042416883008123074f*y1 + 0.1115693827987602f*y2 in dk_set_parameters()
313 set_pre_delay_time(dk, pre_delay_time); in dk_set_parameters()
316 void dk_set_enabled(struct drc_kernel *dk, int enabled) in dk_set_enabled() argument
318 dk->enabled = enabled; in dk_set_enabled()
322 static void dk_update_envelope(struct drc_kernel *dk) in dk_update_envelope() argument
324 const float kA = dk->kA; in dk_update_envelope()
325 const float kB = dk->kB; in dk_update_envelope()
326 const float kC = dk->kC; in dk_update_envelope()
327 const float kD = dk->kD; in dk_update_envelope()
328 const float kE = dk->kE; in dk_update_envelope()
329 const float attack_frames = dk->attack_frames; in dk_update_envelope()
332 float desired_gain = dk->detector_average; in dk_update_envelope()
345 int is_releasing = scaled_desired_gain > dk->compressor_gain; in dk_update_envelope()
350 dk->compressor_gain / scaled_desired_gain); in dk_update_envelope()
354 dk->max_attack_compression_diff_db = -INFINITY; in dk_update_envelope()
392 dk->max_attack_compression_diff_db = max( in dk_update_envelope()
393 dk->max_attack_compression_diff_db, in dk_update_envelope()
397 max(0.5f, dk->max_attack_compression_diff_db); in dk_update_envelope()
403 dk->envelope_rate = envelope_rate; in dk_update_envelope()
404 dk->scaled_desired_gain = scaled_desired_gain; in dk_update_envelope()
506 static void dk_update_detector_average(struct drc_kernel *dk) in dk_update_detector_average() argument
509 const float sat_release_frames_inv_neg = dk->sat_release_frames_inv_neg; in dk_update_detector_average()
511 dk->sat_release_rate_at_neg_two_db; in dk_update_detector_average()
512 float detector_average = dk->detector_average; in dk_update_detector_average()
516 if (dk->pre_delay_write_index == 0) { in dk_update_detector_average()
519 div_start = dk->pre_delay_write_index - DIVISION_FRAMES; in dk_update_detector_average()
524 &dk->pre_delay_buffers[0][div_start], in dk_update_detector_average()
525 &dk->pre_delay_buffers[1][div_start]); in dk_update_detector_average()
538 float gain = volume_gain(dk, abs_input); in dk_update_detector_average()
564 dk->detector_average = detector_average; in dk_update_detector_average()
572 static void dk_compress_output(struct drc_kernel *dk) in dk_compress_output() argument
574 const float master_linear_gain = dk->master_linear_gain; in dk_compress_output()
575 const float envelope_rate = dk->envelope_rate; in dk_compress_output()
576 const float scaled_desired_gain = dk->scaled_desired_gain; in dk_compress_output()
577 const float compressor_gain = dk->compressor_gain; in dk_compress_output()
578 const int div_start = dk->pre_delay_read_index; in dk_compress_output()
579 float *ptr_left = &dk->pre_delay_buffers[0][div_start]; in dk_compress_output()
580 float *ptr_right = &dk->pre_delay_buffers[1][div_start]; in dk_compress_output()
648 dk->compressor_gain = x[3]; in dk_compress_output()
706 dk->compressor_gain = x[3]; in dk_compress_output()
711 static void dk_compress_output(struct drc_kernel *dk) in dk_compress_output() argument
713 const float master_linear_gain = dk->master_linear_gain; in dk_compress_output()
714 const float envelope_rate = dk->envelope_rate; in dk_compress_output()
715 const float scaled_desired_gain = dk->scaled_desired_gain; in dk_compress_output()
716 const float compressor_gain = dk->compressor_gain; in dk_compress_output()
717 const int div_start = dk->pre_delay_read_index; in dk_compress_output()
718 float *ptr_left = &dk->pre_delay_buffers[0][div_start]; in dk_compress_output()
719 float *ptr_right = &dk->pre_delay_buffers[1][div_start]; in dk_compress_output()
795 dk->compressor_gain = x[3]; in dk_compress_output()
866 dk->compressor_gain = x[3]; in dk_compress_output()
870 static void dk_compress_output(struct drc_kernel *dk) in dk_compress_output() argument
872 const float master_linear_gain = dk->master_linear_gain; in dk_compress_output()
873 const float envelope_rate = dk->envelope_rate; in dk_compress_output()
874 const float scaled_desired_gain = dk->scaled_desired_gain; in dk_compress_output()
875 const float compressor_gain = dk->compressor_gain; in dk_compress_output()
876 const int div_start = dk->pre_delay_read_index; in dk_compress_output()
877 float *ptr_left = &dk->pre_delay_buffers[0][div_start]; in dk_compress_output()
878 float *ptr_right = &dk->pre_delay_buffers[1][div_start]; in dk_compress_output()
917 dk->compressor_gain = x[3] + base; in dk_compress_output()
950 dk->compressor_gain = x[3]; in dk_compress_output()
961 static void dk_process_one_division(struct drc_kernel *dk) in dk_process_one_division() argument
963 dk_update_detector_average(dk); in dk_process_one_division()
964 dk_update_envelope(dk); in dk_process_one_division()
965 dk_compress_output(dk); in dk_process_one_division()
970 static void dk_copy_fragment(struct drc_kernel *dk, float *data_channels[], in dk_copy_fragment() argument
973 int write_index = dk->pre_delay_write_index; in dk_copy_fragment()
974 int read_index = dk->pre_delay_read_index; in dk_copy_fragment()
978 memcpy(&dk->pre_delay_buffers[j][write_index], in dk_copy_fragment()
982 &dk->pre_delay_buffers[j][read_index], in dk_copy_fragment()
986 dk->pre_delay_write_index = (write_index + frames_to_process) & in dk_copy_fragment()
988 dk->pre_delay_read_index = (read_index + frames_to_process) & in dk_copy_fragment()
996 static void dk_process_delay_only(struct drc_kernel *dk, float *data_channels[], in dk_process_delay_only() argument
999 int read_index = dk->pre_delay_read_index; in dk_process_delay_only()
1000 int write_index = dk->pre_delay_write_index; in dk_process_delay_only()
1013 memcpy(&dk->pre_delay_buffers[j][write_index], in dk_process_delay_only()
1017 &dk->pre_delay_buffers[j][read_index], in dk_process_delay_only()
1025 dk->pre_delay_read_index = read_index; in dk_process_delay_only()
1026 dk->pre_delay_write_index = write_index; in dk_process_delay_only()
1029 void dk_process(struct drc_kernel *dk, float *data_channels[], unsigned count) in dk_process() argument
1034 if (!dk->enabled) { in dk_process()
1035 dk_process_delay_only(dk, data_channels, count); in dk_process()
1039 if (!dk->processed) { in dk_process()
1040 dk_update_envelope(dk); in dk_process()
1041 dk_compress_output(dk); in dk_process()
1042 dk->processed = 1; in dk_process()
1045 int offset = dk->pre_delay_write_index & DIVISION_FRAMES_MASK; in dk_process()
1048 dk_copy_fragment(dk, data_channels, i, fragment); in dk_process()
1054 dk_process_one_division(dk); in dk_process()