1
0

noise-suppress-filter.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. #include <stdint.h>
  2. #include <inttypes.h>
  3. #include <util/circlebuf.h>
  4. #include <obs-module.h>
  5. #ifdef LIBSPEEXDSP_ENABLED
  6. #include <speex/speex_preprocess.h>
  7. #endif
  8. #ifdef LIBRNNOISE_ENABLED
  9. #ifdef _MSC_VER
  10. #define ssize_t intptr_t
  11. #endif
  12. #include <rnnoise.h>
  13. #include <media-io/audio-resampler.h>
  14. #endif
  15. /* -------------------------------------------------------- */
  16. #define do_log(level, format, ...) \
  17. blog(level, "[noise suppress: '%s'] " format, \
  18. obs_source_get_name(ng->context), ##__VA_ARGS__)
  19. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  20. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  21. #ifdef _DEBUG
  22. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  23. #else
  24. #define debug(format, ...)
  25. #endif
  26. /* -------------------------------------------------------- */
  27. #define S_SUPPRESS_LEVEL "suppress_level"
  28. #define S_METHOD "method"
  29. #define S_METHOD_SPEEX "speex"
  30. #define S_METHOD_RNN "rnnoise"
  31. #define MT_ obs_module_text
  32. #define TEXT_SUPPRESS_LEVEL MT_("NoiseSuppress.SuppressLevel")
  33. #define TEXT_METHOD MT_("NoiseSuppress.Method")
  34. #define TEXT_METHOD_SPEEX MT_("NoiseSuppress.Method.Speex")
  35. #define TEXT_METHOD_RNN MT_("NoiseSuppress.Method.RNNoise")
  36. #define MAX_PREPROC_CHANNELS 8
  37. /* RNNoise constants, these can't be changed */
  38. #define RNNOISE_SAMPLE_RATE 48000
  39. #define RNNOISE_FRAME_SIZE 480
  40. /* If the following constant changes, RNNoise breaks */
  41. #define BUFFER_SIZE_MSEC 10
  42. /* -------------------------------------------------------- */
  43. struct noise_suppress_data {
  44. obs_source_t *context;
  45. int suppress_level;
  46. uint64_t last_timestamp;
  47. uint64_t latency;
  48. size_t frames;
  49. size_t channels;
  50. struct circlebuf info_buffer;
  51. struct circlebuf input_buffers[MAX_PREPROC_CHANNELS];
  52. struct circlebuf output_buffers[MAX_PREPROC_CHANNELS];
  53. bool use_rnnoise;
  54. #ifdef LIBSPEEXDSP_ENABLED
  55. /* Speex preprocessor state */
  56. SpeexPreprocessState *spx_states[MAX_PREPROC_CHANNELS];
  57. #endif
  58. #ifdef LIBRNNOISE_ENABLED
  59. /* RNNoise state */
  60. DenoiseState *rnn_states[MAX_PREPROC_CHANNELS];
  61. /* Resampler */
  62. audio_resampler_t *rnn_resampler;
  63. audio_resampler_t *rnn_resampler_back;
  64. #endif
  65. /* PCM buffers */
  66. float *copy_buffers[MAX_PREPROC_CHANNELS];
  67. #ifdef LIBSPEEXDSP_ENABLED
  68. spx_int16_t *spx_segment_buffers[MAX_PREPROC_CHANNELS];
  69. #endif
  70. #ifdef LIBRNNOISE_ENABLED
  71. float *rnn_segment_buffers[MAX_PREPROC_CHANNELS];
  72. #endif
  73. /* output data */
  74. struct obs_audio_data output_audio;
  75. DARRAY(float) output_data;
  76. };
  77. /* -------------------------------------------------------- */
  78. #define SUP_MIN -60
  79. #define SUP_MAX 0
  80. static const float c_32_to_16 = (float)INT16_MAX;
  81. static const float c_16_to_32 = ((float)INT16_MAX + 1.0f);
  82. /* -------------------------------------------------------- */
  83. static const char *noise_suppress_name(void *unused)
  84. {
  85. UNUSED_PARAMETER(unused);
  86. return obs_module_text("NoiseSuppress");
  87. }
  88. static void noise_suppress_destroy(void *data)
  89. {
  90. struct noise_suppress_data *ng = data;
  91. for (size_t i = 0; i < ng->channels; i++) {
  92. #ifdef LIBSPEEXDSP_ENABLED
  93. speex_preprocess_state_destroy(ng->spx_states[i]);
  94. #endif
  95. #ifdef LIBRNNOISE_ENABLED
  96. rnnoise_destroy(ng->rnn_states[i]);
  97. #endif
  98. circlebuf_free(&ng->input_buffers[i]);
  99. circlebuf_free(&ng->output_buffers[i]);
  100. }
  101. #ifdef LIBSPEEXDSP_ENABLED
  102. bfree(ng->spx_segment_buffers[0]);
  103. #endif
  104. #ifdef LIBRNNOISE_ENABLED
  105. bfree(ng->rnn_segment_buffers[0]);
  106. if (ng->rnn_resampler) {
  107. audio_resampler_destroy(ng->rnn_resampler);
  108. audio_resampler_destroy(ng->rnn_resampler_back);
  109. }
  110. #endif
  111. bfree(ng->copy_buffers[0]);
  112. circlebuf_free(&ng->info_buffer);
  113. da_free(ng->output_data);
  114. bfree(ng);
  115. }
  116. static inline void alloc_channel(struct noise_suppress_data *ng,
  117. uint32_t sample_rate, size_t channel,
  118. size_t frames)
  119. {
  120. #ifdef LIBSPEEXDSP_ENABLED
  121. ng->spx_states[channel] =
  122. speex_preprocess_state_init((int)frames, sample_rate);
  123. #endif
  124. #ifdef LIBRNNOISE_ENABLED
  125. ng->rnn_states[channel] = rnnoise_create(NULL);
  126. #endif
  127. circlebuf_reserve(&ng->input_buffers[channel], frames * sizeof(float));
  128. circlebuf_reserve(&ng->output_buffers[channel], frames * sizeof(float));
  129. }
  130. static inline enum speaker_layout convert_speaker_layout(uint8_t channels)
  131. {
  132. switch (channels) {
  133. case 0:
  134. return SPEAKERS_UNKNOWN;
  135. case 1:
  136. return SPEAKERS_MONO;
  137. case 2:
  138. return SPEAKERS_STEREO;
  139. case 3:
  140. return SPEAKERS_2POINT1;
  141. case 4:
  142. return SPEAKERS_4POINT0;
  143. case 5:
  144. return SPEAKERS_4POINT1;
  145. case 6:
  146. return SPEAKERS_5POINT1;
  147. case 8:
  148. return SPEAKERS_7POINT1;
  149. default:
  150. return SPEAKERS_UNKNOWN;
  151. }
  152. }
  153. static void noise_suppress_update(void *data, obs_data_t *s)
  154. {
  155. struct noise_suppress_data *ng = data;
  156. uint32_t sample_rate = audio_output_get_sample_rate(obs_get_audio());
  157. size_t channels = audio_output_get_channels(obs_get_audio());
  158. size_t frames = (size_t)sample_rate / (1000 / BUFFER_SIZE_MSEC);
  159. const char *method = obs_data_get_string(s, S_METHOD);
  160. ng->suppress_level = (int)obs_data_get_int(s, S_SUPPRESS_LEVEL);
  161. ng->latency = 1000000000LL / (1000 / BUFFER_SIZE_MSEC);
  162. ng->use_rnnoise = strcmp(method, S_METHOD_RNN) == 0;
  163. /* Process 10 millisecond segments to keep latency low */
  164. /* Also RNNoise only supports buffers of this exact size. */
  165. ng->frames = frames;
  166. ng->channels = channels;
  167. /* Ignore if already allocated */
  168. #if defined(LIBSPEEXDSP_ENABLED)
  169. if (ng->spx_states[0])
  170. return;
  171. #else
  172. if (ng->rnn_states[0])
  173. return;
  174. #endif
  175. /* One speex/rnnoise state for each channel (limit 2) */
  176. ng->copy_buffers[0] = bmalloc(frames * channels * sizeof(float));
  177. #ifdef LIBSPEEXDSP_ENABLED
  178. ng->spx_segment_buffers[0] =
  179. bmalloc(frames * channels * sizeof(spx_int16_t));
  180. #endif
  181. #ifdef LIBRNNOISE_ENABLED
  182. ng->rnn_segment_buffers[0] =
  183. bmalloc(RNNOISE_FRAME_SIZE * channels * sizeof(float));
  184. #endif
  185. for (size_t c = 1; c < channels; ++c) {
  186. ng->copy_buffers[c] = ng->copy_buffers[c - 1] + frames;
  187. #ifdef LIBSPEEXDSP_ENABLED
  188. ng->spx_segment_buffers[c] =
  189. ng->spx_segment_buffers[c - 1] + frames;
  190. #endif
  191. #ifdef LIBRNNOISE_ENABLED
  192. ng->rnn_segment_buffers[c] =
  193. ng->rnn_segment_buffers[c - 1] + RNNOISE_FRAME_SIZE;
  194. #endif
  195. }
  196. for (size_t i = 0; i < channels; i++)
  197. alloc_channel(ng, sample_rate, i, frames);
  198. #ifdef LIBRNNOISE_ENABLED
  199. if (sample_rate == RNNOISE_SAMPLE_RATE) {
  200. ng->rnn_resampler = NULL;
  201. ng->rnn_resampler_back = NULL;
  202. } else {
  203. struct resample_info src, dst;
  204. src.samples_per_sec = sample_rate;
  205. src.format = AUDIO_FORMAT_FLOAT_PLANAR;
  206. src.speakers = convert_speaker_layout((uint8_t)channels);
  207. dst.samples_per_sec = RNNOISE_SAMPLE_RATE;
  208. dst.format = AUDIO_FORMAT_FLOAT_PLANAR;
  209. dst.speakers = convert_speaker_layout((uint8_t)channels);
  210. ng->rnn_resampler = audio_resampler_create(&dst, &src);
  211. ng->rnn_resampler_back = audio_resampler_create(&src, &dst);
  212. }
  213. #endif
  214. }
  215. static void *noise_suppress_create(obs_data_t *settings, obs_source_t *filter)
  216. {
  217. struct noise_suppress_data *ng =
  218. bzalloc(sizeof(struct noise_suppress_data));
  219. ng->context = filter;
  220. noise_suppress_update(ng, settings);
  221. return ng;
  222. }
  223. static inline void process_speexdsp(struct noise_suppress_data *ng)
  224. {
  225. #ifdef LIBSPEEXDSP_ENABLED
  226. /* Set args */
  227. for (size_t i = 0; i < ng->channels; i++)
  228. speex_preprocess_ctl(ng->spx_states[i],
  229. SPEEX_PREPROCESS_SET_NOISE_SUPPRESS,
  230. &ng->suppress_level);
  231. /* Convert to 16bit */
  232. for (size_t i = 0; i < ng->channels; i++)
  233. for (size_t j = 0; j < ng->frames; j++) {
  234. float s = ng->copy_buffers[i][j];
  235. if (s > 1.0f)
  236. s = 1.0f;
  237. else if (s < -1.0f)
  238. s = -1.0f;
  239. ng->spx_segment_buffers[i][j] =
  240. (spx_int16_t)(s * c_32_to_16);
  241. }
  242. /* Execute */
  243. for (size_t i = 0; i < ng->channels; i++)
  244. speex_preprocess_run(ng->spx_states[i],
  245. ng->spx_segment_buffers[i]);
  246. /* Convert back to 32bit */
  247. for (size_t i = 0; i < ng->channels; i++)
  248. for (size_t j = 0; j < ng->frames; j++)
  249. ng->copy_buffers[i][j] =
  250. (float)ng->spx_segment_buffers[i][j] /
  251. c_16_to_32;
  252. #endif
  253. }
  254. static inline void process_rnnoise(struct noise_suppress_data *ng)
  255. {
  256. #ifdef LIBRNNOISE_ENABLED
  257. /* Adjust signal level to what RNNoise expects, resample if necessary */
  258. if (ng->rnn_resampler) {
  259. float *output[MAX_PREPROC_CHANNELS];
  260. uint32_t out_frames;
  261. uint64_t ts_offset;
  262. audio_resampler_resample(ng->rnn_resampler, (uint8_t **)output,
  263. &out_frames, &ts_offset,
  264. (const uint8_t **)ng->copy_buffers,
  265. (uint32_t)ng->frames);
  266. for (size_t i = 0; i < ng->channels; i++) {
  267. for (ssize_t j = 0, k = (ssize_t)out_frames -
  268. RNNOISE_FRAME_SIZE;
  269. j < RNNOISE_FRAME_SIZE; ++j, ++k) {
  270. if (k >= 0) {
  271. ng->rnn_segment_buffers[i][j] =
  272. output[i][k] * 32768.0f;
  273. } else {
  274. ng->rnn_segment_buffers[i][j] = 0;
  275. }
  276. }
  277. }
  278. } else {
  279. for (size_t i = 0; i < ng->channels; i++) {
  280. for (size_t j = 0; j < RNNOISE_FRAME_SIZE; ++j) {
  281. ng->rnn_segment_buffers[i][j] =
  282. ng->copy_buffers[i][j] * 32768.0f;
  283. }
  284. }
  285. }
  286. /* Execute */
  287. for (size_t i = 0; i < ng->channels; i++) {
  288. rnnoise_process_frame(ng->rnn_states[i],
  289. ng->rnn_segment_buffers[i],
  290. ng->rnn_segment_buffers[i]);
  291. }
  292. /* Revert signal level adjustment, resample back if necessary */
  293. if (ng->rnn_resampler) {
  294. float *output[MAX_PREPROC_CHANNELS];
  295. uint32_t out_frames;
  296. uint64_t ts_offset;
  297. audio_resampler_resample(
  298. ng->rnn_resampler_back, (uint8_t **)output, &out_frames,
  299. &ts_offset, (const uint8_t **)ng->rnn_segment_buffers,
  300. RNNOISE_FRAME_SIZE);
  301. for (size_t i = 0; i < ng->channels; i++) {
  302. for (ssize_t j = 0,
  303. k = (ssize_t)out_frames - ng->frames;
  304. j < (ssize_t)ng->frames; ++j, ++k) {
  305. if (k >= 0) {
  306. ng->copy_buffers[i][j] =
  307. output[i][k] / 32768.0f;
  308. } else {
  309. ng->copy_buffers[i][j] = 0;
  310. }
  311. }
  312. }
  313. } else {
  314. for (size_t i = 0; i < ng->channels; i++) {
  315. for (size_t j = 0; j < RNNOISE_FRAME_SIZE; ++j) {
  316. ng->copy_buffers[i][j] =
  317. ng->rnn_segment_buffers[i][j] /
  318. 32768.0f;
  319. }
  320. }
  321. }
  322. #else
  323. UNUSED_PARAMETER(ng);
  324. #endif
  325. }
  326. static inline void process(struct noise_suppress_data *ng)
  327. {
  328. /* Pop from input circlebuf */
  329. for (size_t i = 0; i < ng->channels; i++)
  330. circlebuf_pop_front(&ng->input_buffers[i], ng->copy_buffers[i],
  331. ng->frames * sizeof(float));
  332. if (ng->use_rnnoise) {
  333. process_rnnoise(ng);
  334. } else {
  335. process_speexdsp(ng);
  336. }
  337. /* Push to output circlebuf */
  338. for (size_t i = 0; i < ng->channels; i++)
  339. circlebuf_push_back(&ng->output_buffers[i], ng->copy_buffers[i],
  340. ng->frames * sizeof(float));
  341. }
  342. struct ng_audio_info {
  343. uint32_t frames;
  344. uint64_t timestamp;
  345. };
  346. static inline void clear_circlebuf(struct circlebuf *buf)
  347. {
  348. circlebuf_pop_front(buf, NULL, buf->size);
  349. }
  350. static void reset_data(struct noise_suppress_data *ng)
  351. {
  352. for (size_t i = 0; i < ng->channels; i++) {
  353. clear_circlebuf(&ng->input_buffers[i]);
  354. clear_circlebuf(&ng->output_buffers[i]);
  355. }
  356. clear_circlebuf(&ng->info_buffer);
  357. }
  358. static struct obs_audio_data *
  359. noise_suppress_filter_audio(void *data, struct obs_audio_data *audio)
  360. {
  361. struct noise_suppress_data *ng = data;
  362. struct ng_audio_info info;
  363. size_t segment_size = ng->frames * sizeof(float);
  364. size_t out_size;
  365. #ifdef LIBSPEEXDSP_ENABLED
  366. if (!ng->spx_states[0])
  367. return audio;
  368. #else
  369. if (!ng->rnn_states[0])
  370. return audio;
  371. #endif
  372. /* -----------------------------------------------
  373. * if timestamp has dramatically changed, consider it a new stream of
  374. * audio data. clear all circular buffers to prevent old audio data
  375. * from being processed as part of the new data. */
  376. if (ng->last_timestamp) {
  377. int64_t diff = llabs((int64_t)ng->last_timestamp -
  378. (int64_t)audio->timestamp);
  379. if (diff > 1000000000LL)
  380. reset_data(ng);
  381. }
  382. ng->last_timestamp = audio->timestamp;
  383. /* -----------------------------------------------
  384. * push audio packet info (timestamp/frame count) to info circlebuf */
  385. info.frames = audio->frames;
  386. info.timestamp = audio->timestamp;
  387. circlebuf_push_back(&ng->info_buffer, &info, sizeof(info));
  388. /* -----------------------------------------------
  389. * push back current audio data to input circlebuf */
  390. for (size_t i = 0; i < ng->channels; i++)
  391. circlebuf_push_back(&ng->input_buffers[i], audio->data[i],
  392. audio->frames * sizeof(float));
  393. /* -----------------------------------------------
  394. * pop/process each 10ms segments, push back to output circlebuf */
  395. while (ng->input_buffers[0].size >= segment_size)
  396. process(ng);
  397. /* -----------------------------------------------
  398. * peek front of info circlebuf, check to see if we have enough to
  399. * pop the expected packet size, if not, return null */
  400. memset(&info, 0, sizeof(info));
  401. circlebuf_peek_front(&ng->info_buffer, &info, sizeof(info));
  402. out_size = info.frames * sizeof(float);
  403. if (ng->output_buffers[0].size < out_size)
  404. return NULL;
  405. /* -----------------------------------------------
  406. * if there's enough audio data buffered in the output circlebuf,
  407. * pop and return a packet */
  408. circlebuf_pop_front(&ng->info_buffer, NULL, sizeof(info));
  409. da_resize(ng->output_data, out_size * ng->channels);
  410. for (size_t i = 0; i < ng->channels; i++) {
  411. ng->output_audio.data[i] =
  412. (uint8_t *)&ng->output_data.array[i * out_size];
  413. circlebuf_pop_front(&ng->output_buffers[i],
  414. ng->output_audio.data[i], out_size);
  415. }
  416. ng->output_audio.frames = info.frames;
  417. ng->output_audio.timestamp = info.timestamp - ng->latency;
  418. return &ng->output_audio;
  419. }
  420. static bool noise_suppress_method_modified(obs_properties_t *props,
  421. obs_property_t *property,
  422. obs_data_t *settings)
  423. {
  424. obs_property_t *p_suppress_level =
  425. obs_properties_get(props, S_SUPPRESS_LEVEL);
  426. const char *method = obs_data_get_string(settings, S_METHOD);
  427. bool enable_level = strcmp(method, S_METHOD_SPEEX) == 0;
  428. obs_property_set_visible(p_suppress_level, enable_level);
  429. UNUSED_PARAMETER(property);
  430. return true;
  431. }
  432. static void noise_suppress_defaults_v1(obs_data_t *s)
  433. {
  434. obs_data_set_default_int(s, S_SUPPRESS_LEVEL, -30);
  435. #if defined(LIBRNNOISE_ENABLED) && !defined(LIBSPEEXDSP_ENABLED)
  436. obs_data_set_default_string(s, S_METHOD, S_METHOD_RNN);
  437. #else
  438. obs_data_set_default_string(s, S_METHOD, S_METHOD_SPEEX);
  439. #endif
  440. }
  441. static void noise_suppress_defaults_v2(obs_data_t *s)
  442. {
  443. obs_data_set_default_int(s, S_SUPPRESS_LEVEL, -30);
  444. #if defined(LIBRNNOISE_ENABLED)
  445. obs_data_set_default_string(s, S_METHOD, S_METHOD_RNN);
  446. #else
  447. obs_data_set_default_string(s, S_METHOD, S_METHOD_SPEEX);
  448. #endif
  449. }
  450. static obs_properties_t *noise_suppress_properties(void *data)
  451. {
  452. obs_properties_t *ppts = obs_properties_create();
  453. #if defined(LIBRNNOISE_ENABLED) && defined(LIBSPEEXDSP_ENABLED)
  454. obs_property_t *method = obs_properties_add_list(
  455. ppts, S_METHOD, TEXT_METHOD, OBS_COMBO_TYPE_LIST,
  456. OBS_COMBO_FORMAT_STRING);
  457. obs_property_list_add_string(method, TEXT_METHOD_SPEEX, S_METHOD_SPEEX);
  458. obs_property_list_add_string(method, TEXT_METHOD_RNN, S_METHOD_RNN);
  459. obs_property_set_modified_callback(method,
  460. noise_suppress_method_modified);
  461. #endif
  462. #ifdef LIBSPEEXDSP_ENABLED
  463. obs_property_t *speex_slider = obs_properties_add_int_slider(
  464. ppts, S_SUPPRESS_LEVEL, TEXT_SUPPRESS_LEVEL, SUP_MIN, SUP_MAX,
  465. 1);
  466. obs_property_int_set_suffix(speex_slider, " dB");
  467. #endif
  468. UNUSED_PARAMETER(data);
  469. return ppts;
  470. }
  471. struct obs_source_info noise_suppress_filter = {
  472. .id = "noise_suppress_filter",
  473. .type = OBS_SOURCE_TYPE_FILTER,
  474. .output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_CAP_OBSOLETE,
  475. .get_name = noise_suppress_name,
  476. .create = noise_suppress_create,
  477. .destroy = noise_suppress_destroy,
  478. .update = noise_suppress_update,
  479. .filter_audio = noise_suppress_filter_audio,
  480. .get_defaults = noise_suppress_defaults_v1,
  481. .get_properties = noise_suppress_properties,
  482. };
  483. struct obs_source_info noise_suppress_filter_v2 = {
  484. .id = "noise_suppress_filter",
  485. .version = 2,
  486. .type = OBS_SOURCE_TYPE_FILTER,
  487. .output_flags = OBS_SOURCE_AUDIO,
  488. .get_name = noise_suppress_name,
  489. .create = noise_suppress_create,
  490. .destroy = noise_suppress_destroy,
  491. .update = noise_suppress_update,
  492. .filter_audio = noise_suppress_filter_audio,
  493. .get_defaults = noise_suppress_defaults_v2,
  494. .get_properties = noise_suppress_properties,
  495. };