noise-suppress-filter.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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, size_t frames)
  68. {
  69. ng->states[channel] = speex_preprocess_state_init((int)frames,
  70. sample_rate);
  71. circlebuf_reserve(&ng->input_buffers[channel], frames * sizeof(float));
  72. circlebuf_reserve(&ng->output_buffers[channel], frames * sizeof(float));
  73. }
  74. static void noise_suppress_update(void *data, obs_data_t *s)
  75. {
  76. struct noise_suppress_data *ng = data;
  77. uint32_t sample_rate = audio_output_get_sample_rate(obs_get_audio());
  78. size_t channels = audio_output_get_channels(obs_get_audio());
  79. size_t frames = (size_t)sample_rate / 100;
  80. ng->suppress_level = (int)obs_data_get_int(s, S_SUPPRESS_LEVEL);
  81. /* Process 10 millisecond segments to keep latency low */
  82. ng->frames = frames;
  83. ng->channels = channels;
  84. /* Ignore if already allocated */
  85. if (ng->states[0])
  86. return;
  87. /* One speex state for each channel (limit 2) */
  88. ng->copy_buffers[0] = bmalloc(frames * channels * sizeof(float));
  89. ng->segment_buffers[0] = bmalloc(frames * channels * sizeof(spx_int16_t));
  90. for (size_t c = 1; c < channels; ++c) {
  91. ng->copy_buffers[c] = ng->copy_buffers[c-1] + frames;
  92. ng->segment_buffers[c] = ng->segment_buffers[c-1] + frames;
  93. }
  94. for (size_t i = 0; i < channels; i++)
  95. alloc_channel(ng, sample_rate, i, frames);
  96. }
  97. static void *noise_suppress_create(obs_data_t *settings, obs_source_t *filter)
  98. {
  99. struct noise_suppress_data *ng =
  100. bzalloc(sizeof(struct noise_suppress_data));
  101. ng->context = filter;
  102. noise_suppress_update(ng, settings);
  103. return ng;
  104. }
  105. static inline void process(struct noise_suppress_data *ng)
  106. {
  107. /* Pop from input circlebuf */
  108. for (size_t i = 0; i < ng->channels; i++)
  109. circlebuf_pop_front(&ng->input_buffers[i], ng->copy_buffers[i],
  110. ng->frames * sizeof(float));
  111. /* Set args */
  112. for (size_t i = 0; i < ng->channels; i++)
  113. speex_preprocess_ctl(ng->states[i],
  114. SPEEX_PREPROCESS_SET_NOISE_SUPPRESS,
  115. &ng->suppress_level);
  116. /* Convert to 16bit */
  117. for (size_t i = 0; i < ng->channels; i++)
  118. for (size_t j = 0; j < ng->frames; j++) {
  119. float s = ng->copy_buffers[i][j];
  120. if (s > 1.0f) s = 1.0f;
  121. else if (s < -1.0f) s = -1.0f;
  122. ng->segment_buffers[i][j] = (spx_int16_t)
  123. (s * c_32_to_16);
  124. }
  125. /* Execute */
  126. for (size_t i = 0; i < ng->channels; i++)
  127. speex_preprocess_run(ng->states[i], ng->segment_buffers[i]);
  128. /* Convert back to 32bit */
  129. for (size_t i = 0; i < ng->channels; i++)
  130. for (size_t j = 0; j < ng->frames; j++)
  131. ng->copy_buffers[i][j] =
  132. (float)ng->segment_buffers[i][j] / c_16_to_32;
  133. /* Push to output circlebuf */
  134. for (size_t i = 0; i < ng->channels; i++)
  135. circlebuf_push_back(&ng->output_buffers[i], ng->copy_buffers[i],
  136. ng->frames * sizeof(float));
  137. }
  138. struct ng_audio_info {
  139. uint32_t frames;
  140. uint64_t timestamp;
  141. };
  142. static inline void clear_circlebuf(struct circlebuf *buf)
  143. {
  144. circlebuf_pop_front(buf, NULL, buf->size);
  145. }
  146. static void reset_data(struct noise_suppress_data *ng)
  147. {
  148. for (size_t i = 0; i < ng->channels; i++) {
  149. clear_circlebuf(&ng->input_buffers[i]);
  150. clear_circlebuf(&ng->output_buffers[i]);
  151. }
  152. clear_circlebuf(&ng->info_buffer);
  153. }
  154. static struct obs_audio_data *noise_suppress_filter_audio(void *data,
  155. struct obs_audio_data *audio)
  156. {
  157. struct noise_suppress_data *ng = data;
  158. struct ng_audio_info info;
  159. size_t segment_size = ng->frames * sizeof(float);
  160. size_t out_size;
  161. if (!ng->states[0])
  162. return audio;
  163. /* -----------------------------------------------
  164. * if timestamp has dramatically changed, consider it a new stream of
  165. * audio data. clear all circular buffers to prevent old audio data
  166. * from being processed as part of the new data. */
  167. if (ng->last_timestamp) {
  168. int64_t diff = llabs((int64_t)ng->last_timestamp -
  169. (int64_t)audio->timestamp);
  170. if (diff > 1000000000LL)
  171. reset_data(ng);
  172. }
  173. ng->last_timestamp = audio->timestamp;
  174. /* -----------------------------------------------
  175. * push audio packet info (timestamp/frame count) to info circlebuf */
  176. info.frames = audio->frames;
  177. info.timestamp = audio->timestamp;
  178. circlebuf_push_back(&ng->info_buffer, &info, sizeof(info));
  179. /* -----------------------------------------------
  180. * push back current audio data to input circlebuf */
  181. for (size_t i = 0; i < ng->channels; i++)
  182. circlebuf_push_back(&ng->input_buffers[i], audio->data[i],
  183. audio->frames * sizeof(float));
  184. /* -----------------------------------------------
  185. * pop/process each 10ms segments, push back to output circlebuf */
  186. while (ng->input_buffers[0].size >= segment_size)
  187. process(ng);
  188. /* -----------------------------------------------
  189. * peek front of info circlebuf, check to see if we have enough to
  190. * pop the expected packet size, if not, return null */
  191. memset(&info, 0, sizeof(info));
  192. circlebuf_peek_front(&ng->info_buffer, &info, sizeof(info));
  193. out_size = info.frames * sizeof(float);
  194. if (ng->output_buffers[0].size < out_size)
  195. return NULL;
  196. /* -----------------------------------------------
  197. * if there's enough audio data buffered in the output circlebuf,
  198. * pop and return a packet */
  199. circlebuf_pop_front(&ng->info_buffer, NULL, sizeof(info));
  200. da_resize(ng->output_data, out_size * ng->channels);
  201. for (size_t i = 0; i < ng->channels; i++) {
  202. ng->output_audio.data[i] =
  203. (uint8_t*)&ng->output_data.array[i * out_size];
  204. circlebuf_pop_front(&ng->output_buffers[i],
  205. ng->output_audio.data[i],
  206. out_size);
  207. }
  208. ng->output_audio.frames = info.frames;
  209. ng->output_audio.timestamp = info.timestamp;
  210. return &ng->output_audio;
  211. }
  212. static void noise_suppress_defaults(obs_data_t *s)
  213. {
  214. obs_data_set_default_int(s, S_SUPPRESS_LEVEL, -30);
  215. }
  216. static obs_properties_t *noise_suppress_properties(void *data)
  217. {
  218. obs_properties_t *ppts = obs_properties_create();
  219. obs_properties_add_int_slider(ppts, S_SUPPRESS_LEVEL,
  220. TEXT_SUPPRESS_LEVEL, SUP_MIN, SUP_MAX, 1);
  221. UNUSED_PARAMETER(data);
  222. return ppts;
  223. }
  224. struct obs_source_info noise_suppress_filter = {
  225. .id = "noise_suppress_filter",
  226. .type = OBS_SOURCE_TYPE_FILTER,
  227. .output_flags = OBS_SOURCE_AUDIO,
  228. .get_name = noise_suppress_name,
  229. .create = noise_suppress_create,
  230. .destroy = noise_suppress_destroy,
  231. .update = noise_suppress_update,
  232. .filter_audio = noise_suppress_filter_audio,
  233. .get_defaults = noise_suppress_defaults,
  234. .get_properties = noise_suppress_properties,
  235. };