noise-gate-filter.c 5.9 KB

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