noise-suppress-filter.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include <stdint.h>
  2. #include <inttypes.h>
  3. #include <util/circlebuf.h>
  4. #include <obs-module.h>
  5. #include <speex/speex_preprocess.h>
  6. /* -------------------------------------------------------- */
  7. #define do_log(level, format, ...) \
  8. blog(level, "[noise suppress: '%s'] " format, \
  9. obs_source_get_name(ng->context), ##__VA_ARGS__)
  10. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  11. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  12. #ifdef _DEBUG
  13. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  14. #else
  15. #define debug(format, ...)
  16. #endif
  17. /* -------------------------------------------------------- */
  18. #define S_SUPPRESS_LEVEL "suppress_level"
  19. #define MT_ obs_module_text
  20. #define TEXT_SUPPRESS_LEVEL MT_("NoiseSuppress.SuppressLevel")
  21. #define MAX_PREPROC_CHANNELS 8
  22. /* -------------------------------------------------------- */
  23. struct noise_suppress_data {
  24. obs_source_t *context;
  25. int suppress_level;
  26. uint64_t last_timestamp;
  27. size_t frames;
  28. size_t channels;
  29. struct circlebuf info_buffer;
  30. struct circlebuf input_buffers[MAX_PREPROC_CHANNELS];
  31. struct circlebuf output_buffers[MAX_PREPROC_CHANNELS];
  32. /* Speex preprocessor state */
  33. SpeexPreprocessState *states[MAX_PREPROC_CHANNELS];
  34. /* 16 bit PCM buffers */
  35. float *copy_buffers[MAX_PREPROC_CHANNELS];
  36. spx_int16_t *segment_buffers[MAX_PREPROC_CHANNELS];
  37. /* output data */
  38. struct obs_audio_data output_audio;
  39. DARRAY(float) output_data;
  40. };
  41. /* -------------------------------------------------------- */
  42. #define SUP_MIN -60
  43. #define SUP_MAX 0
  44. static const float c_32_to_16 = (float)INT16_MAX;
  45. static const float c_16_to_32 = ((float)INT16_MAX + 1.0f);
  46. /* -------------------------------------------------------- */
  47. static const char *noise_suppress_name(void *unused)
  48. {
  49. UNUSED_PARAMETER(unused);
  50. return obs_module_text("NoiseSuppress");
  51. }
  52. static void noise_suppress_destroy(void *data)
  53. {
  54. struct noise_suppress_data *ng = data;
  55. for (size_t i = 0; i < ng->channels; i++) {
  56. speex_preprocess_state_destroy(ng->states[i]);
  57. circlebuf_free(&ng->input_buffers[i]);
  58. circlebuf_free(&ng->output_buffers[i]);
  59. }
  60. bfree(ng->segment_buffers[0]);
  61. bfree(ng->copy_buffers[0]);
  62. circlebuf_free(&ng->info_buffer);
  63. da_free(ng->output_data);
  64. bfree(ng);
  65. }
  66. static inline void alloc_channel(struct noise_suppress_data *ng,
  67. uint32_t sample_rate, size_t channel,
  68. size_t frames)
  69. {
  70. ng->states[channel] =
  71. speex_preprocess_state_init((int)frames, sample_rate);
  72. circlebuf_reserve(&ng->input_buffers[channel], frames * sizeof(float));
  73. circlebuf_reserve(&ng->output_buffers[channel], frames * sizeof(float));
  74. }
  75. static void noise_suppress_update(void *data, obs_data_t *s)
  76. {
  77. struct noise_suppress_data *ng = data;
  78. uint32_t sample_rate = audio_output_get_sample_rate(obs_get_audio());
  79. size_t channels = audio_output_get_channels(obs_get_audio());
  80. size_t frames = (size_t)sample_rate / 100;
  81. ng->suppress_level = (int)obs_data_get_int(s, S_SUPPRESS_LEVEL);
  82. /* Process 10 millisecond segments to keep latency low */
  83. ng->frames = frames;
  84. ng->channels = channels;
  85. /* Ignore if already allocated */
  86. if (ng->states[0])
  87. return;
  88. /* One speex state for each channel (limit 2) */
  89. ng->copy_buffers[0] = bmalloc(frames * channels * sizeof(float));
  90. ng->segment_buffers[0] =
  91. bmalloc(frames * channels * sizeof(spx_int16_t));
  92. for (size_t c = 1; c < channels; ++c) {
  93. ng->copy_buffers[c] = ng->copy_buffers[c - 1] + frames;
  94. ng->segment_buffers[c] = ng->segment_buffers[c - 1] + frames;
  95. }
  96. for (size_t i = 0; i < channels; i++)
  97. alloc_channel(ng, sample_rate, i, frames);
  98. }
  99. static void *noise_suppress_create(obs_data_t *settings, obs_source_t *filter)
  100. {
  101. struct noise_suppress_data *ng =
  102. bzalloc(sizeof(struct noise_suppress_data));
  103. ng->context = filter;
  104. noise_suppress_update(ng, settings);
  105. return ng;
  106. }
  107. static inline void process(struct noise_suppress_data *ng)
  108. {
  109. /* Pop from input circlebuf */
  110. for (size_t i = 0; i < ng->channels; i++)
  111. circlebuf_pop_front(&ng->input_buffers[i], ng->copy_buffers[i],
  112. ng->frames * sizeof(float));
  113. /* Set args */
  114. for (size_t i = 0; i < ng->channels; i++)
  115. speex_preprocess_ctl(ng->states[i],
  116. SPEEX_PREPROCESS_SET_NOISE_SUPPRESS,
  117. &ng->suppress_level);
  118. /* Convert to 16bit */
  119. for (size_t i = 0; i < ng->channels; i++)
  120. for (size_t j = 0; j < ng->frames; j++) {
  121. float s = ng->copy_buffers[i][j];
  122. if (s > 1.0f)
  123. s = 1.0f;
  124. else if (s < -1.0f)
  125. s = -1.0f;
  126. ng->segment_buffers[i][j] =
  127. (spx_int16_t)(s * c_32_to_16);
  128. }
  129. /* Execute */
  130. for (size_t i = 0; i < ng->channels; i++)
  131. speex_preprocess_run(ng->states[i], ng->segment_buffers[i]);
  132. /* Convert back to 32bit */
  133. for (size_t i = 0; i < ng->channels; i++)
  134. for (size_t j = 0; j < ng->frames; j++)
  135. ng->copy_buffers[i][j] =
  136. (float)ng->segment_buffers[i][j] / c_16_to_32;
  137. /* Push to output circlebuf */
  138. for (size_t i = 0; i < ng->channels; i++)
  139. circlebuf_push_back(&ng->output_buffers[i], ng->copy_buffers[i],
  140. ng->frames * sizeof(float));
  141. }
  142. struct ng_audio_info {
  143. uint32_t frames;
  144. uint64_t timestamp;
  145. };
  146. static inline void clear_circlebuf(struct circlebuf *buf)
  147. {
  148. circlebuf_pop_front(buf, NULL, buf->size);
  149. }
  150. static void reset_data(struct noise_suppress_data *ng)
  151. {
  152. for (size_t i = 0; i < ng->channels; i++) {
  153. clear_circlebuf(&ng->input_buffers[i]);
  154. clear_circlebuf(&ng->output_buffers[i]);
  155. }
  156. clear_circlebuf(&ng->info_buffer);
  157. }
  158. static struct obs_audio_data *
  159. noise_suppress_filter_audio(void *data, struct obs_audio_data *audio)
  160. {
  161. struct noise_suppress_data *ng = data;
  162. struct ng_audio_info info;
  163. size_t segment_size = ng->frames * sizeof(float);
  164. size_t out_size;
  165. if (!ng->states[0])
  166. return audio;
  167. /* -----------------------------------------------
  168. * if timestamp has dramatically changed, consider it a new stream of
  169. * audio data. clear all circular buffers to prevent old audio data
  170. * from being processed as part of the new data. */
  171. if (ng->last_timestamp) {
  172. int64_t diff = llabs((int64_t)ng->last_timestamp -
  173. (int64_t)audio->timestamp);
  174. if (diff > 1000000000LL)
  175. reset_data(ng);
  176. }
  177. ng->last_timestamp = audio->timestamp;
  178. /* -----------------------------------------------
  179. * push audio packet info (timestamp/frame count) to info circlebuf */
  180. info.frames = audio->frames;
  181. info.timestamp = audio->timestamp;
  182. circlebuf_push_back(&ng->info_buffer, &info, sizeof(info));
  183. /* -----------------------------------------------
  184. * push back current audio data to input circlebuf */
  185. for (size_t i = 0; i < ng->channels; i++)
  186. circlebuf_push_back(&ng->input_buffers[i], audio->data[i],
  187. audio->frames * sizeof(float));
  188. /* -----------------------------------------------
  189. * pop/process each 10ms segments, push back to output circlebuf */
  190. while (ng->input_buffers[0].size >= segment_size)
  191. process(ng);
  192. /* -----------------------------------------------
  193. * peek front of info circlebuf, check to see if we have enough to
  194. * pop the expected packet size, if not, return null */
  195. memset(&info, 0, sizeof(info));
  196. circlebuf_peek_front(&ng->info_buffer, &info, sizeof(info));
  197. out_size = info.frames * sizeof(float);
  198. if (ng->output_buffers[0].size < out_size)
  199. return NULL;
  200. /* -----------------------------------------------
  201. * if there's enough audio data buffered in the output circlebuf,
  202. * pop and return a packet */
  203. circlebuf_pop_front(&ng->info_buffer, NULL, sizeof(info));
  204. da_resize(ng->output_data, out_size * ng->channels);
  205. for (size_t i = 0; i < ng->channels; i++) {
  206. ng->output_audio.data[i] =
  207. (uint8_t *)&ng->output_data.array[i * out_size];
  208. circlebuf_pop_front(&ng->output_buffers[i],
  209. ng->output_audio.data[i], out_size);
  210. }
  211. ng->output_audio.frames = info.frames;
  212. ng->output_audio.timestamp = info.timestamp;
  213. return &ng->output_audio;
  214. }
  215. static void noise_suppress_defaults(obs_data_t *s)
  216. {
  217. obs_data_set_default_int(s, S_SUPPRESS_LEVEL, -30);
  218. }
  219. static obs_properties_t *noise_suppress_properties(void *data)
  220. {
  221. obs_properties_t *ppts = obs_properties_create();
  222. obs_property_t *p = obs_properties_add_int_slider(ppts,
  223. S_SUPPRESS_LEVEL,
  224. TEXT_SUPPRESS_LEVEL,
  225. SUP_MIN, SUP_MAX, 1);
  226. obs_property_int_set_suffix(p, " dB");
  227. UNUSED_PARAMETER(data);
  228. return ppts;
  229. }
  230. struct obs_source_info noise_suppress_filter = {
  231. .id = "noise_suppress_filter",
  232. .type = OBS_SOURCE_TYPE_FILTER,
  233. .output_flags = OBS_SOURCE_AUDIO,
  234. .get_name = noise_suppress_name,
  235. .create = noise_suppress_create,
  236. .destroy = noise_suppress_destroy,
  237. .update = noise_suppress_update,
  238. .filter_audio = noise_suppress_filter_audio,
  239. .get_defaults = noise_suppress_defaults,
  240. .get_properties = noise_suppress_properties,
  241. };