noise-gate-filter.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <media-io/audio-math.h>
  2. #include <obs-module.h>
  3. #include <math.h>
  4. #define do_log(level, format, ...) \
  5. blog(level, "[noise gate: '%s'] " format, obs_source_get_name(ng->context), ##__VA_ARGS__)
  6. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  7. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  8. /* clang-format off */
  9. #define S_OPEN_THRESHOLD "open_threshold"
  10. #define S_CLOSE_THRESHOLD "close_threshold"
  11. #define S_ATTACK_TIME "attack_time"
  12. #define S_HOLD_TIME "hold_time"
  13. #define S_RELEASE_TIME "release_time"
  14. #define MT_ obs_module_text
  15. #define TEXT_OPEN_THRESHOLD MT_("NoiseGate.OpenThreshold")
  16. #define TEXT_CLOSE_THRESHOLD MT_("NoiseGate.CloseThreshold")
  17. #define TEXT_ATTACK_TIME MT_("NoiseGate.AttackTime")
  18. #define TEXT_HOLD_TIME MT_("NoiseGate.HoldTime")
  19. #define TEXT_RELEASE_TIME MT_("NoiseGate.ReleaseTime")
  20. /* clang-format on */
  21. struct noise_gate_data {
  22. obs_source_t *context;
  23. float sample_rate_i;
  24. size_t channels;
  25. float open_threshold;
  26. float close_threshold;
  27. float decay_rate;
  28. float attack_rate;
  29. float release_rate;
  30. float hold_time;
  31. bool is_open;
  32. float attenuation;
  33. float level;
  34. float held_time;
  35. };
  36. #define VOL_MIN -96.0
  37. #define VOL_MAX 0.0
  38. static const char *noise_gate_name(void *unused)
  39. {
  40. UNUSED_PARAMETER(unused);
  41. return obs_module_text("NoiseGate");
  42. }
  43. static void noise_gate_destroy(void *data)
  44. {
  45. struct noise_gate_data *ng = data;
  46. bfree(ng);
  47. }
  48. static inline float ms_to_secf(int ms)
  49. {
  50. return (float)ms / 1000.0f;
  51. }
  52. static void noise_gate_update(void *data, obs_data_t *s)
  53. {
  54. struct noise_gate_data *ng = data;
  55. float open_threshold_db;
  56. float close_threshold_db;
  57. float sample_rate;
  58. int attack_time_ms;
  59. int hold_time_ms;
  60. int release_time_ms;
  61. open_threshold_db = (float)obs_data_get_double(s, S_OPEN_THRESHOLD);
  62. close_threshold_db = (float)obs_data_get_double(s, S_CLOSE_THRESHOLD);
  63. attack_time_ms = (int)obs_data_get_int(s, S_ATTACK_TIME);
  64. hold_time_ms = (int)obs_data_get_int(s, S_HOLD_TIME);
  65. release_time_ms = (int)obs_data_get_int(s, S_RELEASE_TIME);
  66. sample_rate = (float)audio_output_get_sample_rate(obs_get_audio());
  67. ng->sample_rate_i = 1.0f / sample_rate;
  68. ng->channels = audio_output_get_channels(obs_get_audio());
  69. ng->open_threshold = db_to_mul(open_threshold_db);
  70. ng->close_threshold = db_to_mul(close_threshold_db);
  71. ng->attack_rate = 1.0f / (ms_to_secf(attack_time_ms) * sample_rate);
  72. ng->release_rate = 1.0f / (ms_to_secf(release_time_ms) * sample_rate);
  73. const float threshold_diff = ng->open_threshold - ng->close_threshold;
  74. const float min_decay_period = (1.0f / 75.0f) * sample_rate;
  75. ng->decay_rate = threshold_diff / min_decay_period;
  76. ng->hold_time = ms_to_secf(hold_time_ms);
  77. ng->is_open = false;
  78. ng->attenuation = 0.0f;
  79. ng->level = 0.0f;
  80. ng->held_time = 0.0f;
  81. }
  82. static void *noise_gate_create(obs_data_t *settings, obs_source_t *filter)
  83. {
  84. struct noise_gate_data *ng = bzalloc(sizeof(*ng));
  85. ng->context = filter;
  86. noise_gate_update(ng, settings);
  87. return ng;
  88. }
  89. static struct obs_audio_data *noise_gate_filter_audio(void *data, struct obs_audio_data *audio)
  90. {
  91. struct noise_gate_data *ng = data;
  92. float **adata = (float **)audio->data;
  93. const float close_threshold = ng->close_threshold;
  94. const float open_threshold = ng->open_threshold;
  95. const float sample_rate_i = ng->sample_rate_i;
  96. const float release_rate = ng->release_rate;
  97. const float attack_rate = ng->attack_rate;
  98. const float decay_rate = ng->decay_rate;
  99. const float hold_time = ng->hold_time;
  100. const size_t channels = ng->channels;
  101. for (size_t i = 0; i < audio->frames; i++) {
  102. float cur_level = fabsf(adata[0][i]);
  103. for (size_t j = 0; j < channels; j++) {
  104. cur_level = fmaxf(cur_level, fabsf(adata[j][i]));
  105. }
  106. if (cur_level > open_threshold && !ng->is_open) {
  107. ng->is_open = true;
  108. }
  109. if (ng->level < close_threshold && ng->is_open) {
  110. ng->held_time = 0.0f;
  111. ng->is_open = false;
  112. }
  113. ng->level = fmaxf(ng->level, cur_level) - decay_rate;
  114. if (ng->is_open) {
  115. ng->attenuation = fminf(1.0f, ng->attenuation + attack_rate);
  116. } else {
  117. ng->held_time += sample_rate_i;
  118. if (ng->held_time > hold_time) {
  119. ng->attenuation = fmaxf(0.0f, ng->attenuation - release_rate);
  120. }
  121. }
  122. for (size_t c = 0; c < channels; c++)
  123. adata[c][i] *= ng->attenuation;
  124. }
  125. return audio;
  126. }
  127. static void noise_gate_defaults(obs_data_t *s)
  128. {
  129. obs_data_set_default_double(s, S_OPEN_THRESHOLD, -26.0);
  130. obs_data_set_default_double(s, S_CLOSE_THRESHOLD, -32.0);
  131. obs_data_set_default_int(s, S_ATTACK_TIME, 25);
  132. obs_data_set_default_int(s, S_HOLD_TIME, 200);
  133. obs_data_set_default_int(s, S_RELEASE_TIME, 150);
  134. }
  135. static obs_properties_t *noise_gate_properties(void *data)
  136. {
  137. obs_properties_t *ppts = obs_properties_create();
  138. obs_property_t *p;
  139. p = obs_properties_add_float_slider(ppts, S_CLOSE_THRESHOLD, TEXT_CLOSE_THRESHOLD, VOL_MIN, VOL_MAX, 1.0);
  140. obs_property_float_set_suffix(p, " dB");
  141. p = obs_properties_add_float_slider(ppts, S_OPEN_THRESHOLD, TEXT_OPEN_THRESHOLD, VOL_MIN, VOL_MAX, 1.0);
  142. obs_property_float_set_suffix(p, " dB");
  143. p = obs_properties_add_int(ppts, S_ATTACK_TIME, TEXT_ATTACK_TIME, 0, 10000, 1);
  144. obs_property_int_set_suffix(p, " ms");
  145. p = obs_properties_add_int(ppts, S_HOLD_TIME, TEXT_HOLD_TIME, 0, 10000, 1);
  146. obs_property_int_set_suffix(p, " ms");
  147. p = obs_properties_add_int(ppts, S_RELEASE_TIME, TEXT_RELEASE_TIME, 0, 10000, 1);
  148. obs_property_int_set_suffix(p, " ms");
  149. UNUSED_PARAMETER(data);
  150. return ppts;
  151. }
  152. struct obs_source_info noise_gate_filter = {
  153. .id = "noise_gate_filter",
  154. .type = OBS_SOURCE_TYPE_FILTER,
  155. .output_flags = OBS_SOURCE_AUDIO,
  156. .get_name = noise_gate_name,
  157. .create = noise_gate_create,
  158. .destroy = noise_gate_destroy,
  159. .update = noise_gate_update,
  160. .filter_audio = noise_gate_filter_audio,
  161. .get_defaults = noise_gate_defaults,
  162. .get_properties = noise_gate_properties,
  163. };