noise-suppress-filter.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. #include <stdint.h>
  2. #include <inttypes.h>
  3. #include <util/circlebuf.h>
  4. #include <util/threading.h>
  5. #include <obs-module.h>
  6. #ifdef LIBSPEEXDSP_ENABLED
  7. #include <speex/speex_preprocess.h>
  8. #endif
  9. #ifdef LIBRNNOISE_ENABLED
  10. #ifdef _MSC_VER
  11. #define ssize_t intptr_t
  12. #endif
  13. #include <rnnoise.h>
  14. #include <media-io/audio-resampler.h>
  15. #endif
  16. bool nvafx_loaded = false;
  17. #ifdef LIBNVAFX_ENABLED
  18. #include "nvafx-load.h"
  19. #include <pthread.h>
  20. #endif
  21. /* -------------------------------------------------------- */
  22. #define do_log(level, format, ...) \
  23. blog(level, "[noise suppress: '%s'] " format, \
  24. obs_source_get_name(ng->context), ##__VA_ARGS__)
  25. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  26. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  27. #ifdef _DEBUG
  28. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  29. #else
  30. #define debug(format, ...)
  31. #endif
  32. /* -------------------------------------------------------- */
  33. #define S_SUPPRESS_LEVEL "suppress_level"
  34. #define S_NVAFX_INTENSITY "intensity"
  35. #define S_METHOD "method"
  36. #define S_METHOD_SPEEX "speex"
  37. #define S_METHOD_RNN "rnnoise"
  38. #define S_METHOD_NVAFX_DENOISER "denoiser"
  39. #define S_METHOD_NVAFX_DEREVERB "dereverb"
  40. #define S_METHOD_NVAFX_DEREVERB_DENOISER "dereverb_denoiser"
  41. #define MT_ obs_module_text
  42. #define TEXT_SUPPRESS_LEVEL MT_("NoiseSuppress.SuppressLevel")
  43. #define TEXT_NVAFX_INTENSITY MT_("NoiseSuppress.Intensity")
  44. #define TEXT_METHOD MT_("NoiseSuppress.Method")
  45. #define TEXT_METHOD_SPEEX MT_("NoiseSuppress.Method.Speex")
  46. #define TEXT_METHOD_RNN MT_("NoiseSuppress.Method.RNNoise")
  47. #define TEXT_METHOD_NVAFX_DENOISER MT_("NoiseSuppress.Method.Nvafx.Denoiser")
  48. #define TEXT_METHOD_NVAFX_DEREVERB MT_("NoiseSuppress.Method.Nvafx.Dereverb")
  49. #define TEXT_METHOD_NVAFX_DEREVERB_DENOISER \
  50. MT_("NoiseSuppress.Method.Nvafx.DenoiserPlusDereverb")
  51. #define TEXT_METHOD_NVAFX_DEPRECATION \
  52. MT_("NoiseSuppress.Method.Nvafx.Deprecation")
  53. #define MAX_PREPROC_CHANNELS 8
  54. /* RNNoise constants, these can't be changed */
  55. #define RNNOISE_SAMPLE_RATE 48000
  56. #define RNNOISE_FRAME_SIZE 480
  57. /* nvafx constants, these can't be changed */
  58. #define NVAFX_SAMPLE_RATE 48000
  59. #define NVAFX_FRAME_SIZE \
  60. 480 /* the sdk does not explicitly set this as a constant though it relies on it*/
  61. /* If the following constant changes, RNNoise breaks */
  62. #define BUFFER_SIZE_MSEC 10
  63. /* -------------------------------------------------------- */
  64. struct noise_suppress_data {
  65. obs_source_t *context;
  66. int suppress_level;
  67. uint64_t last_timestamp;
  68. uint64_t latency;
  69. size_t frames;
  70. size_t channels;
  71. struct circlebuf info_buffer;
  72. struct circlebuf input_buffers[MAX_PREPROC_CHANNELS];
  73. struct circlebuf output_buffers[MAX_PREPROC_CHANNELS];
  74. bool use_rnnoise;
  75. bool use_nvafx;
  76. bool nvafx_enabled;
  77. bool has_mono_src;
  78. volatile bool reinit_done;
  79. #ifdef LIBSPEEXDSP_ENABLED
  80. /* Speex preprocessor state */
  81. SpeexPreprocessState *spx_states[MAX_PREPROC_CHANNELS];
  82. #endif
  83. #ifdef LIBRNNOISE_ENABLED
  84. /* RNNoise state */
  85. DenoiseState *rnn_states[MAX_PREPROC_CHANNELS];
  86. /* Resampler */
  87. audio_resampler_t *rnn_resampler;
  88. audio_resampler_t *rnn_resampler_back;
  89. #endif
  90. #ifdef LIBNVAFX_ENABLED
  91. /* NVAFX handle, one per audio channel */
  92. NvAFX_Handle handle[MAX_PREPROC_CHANNELS];
  93. uint32_t sample_rate;
  94. float intensity_ratio;
  95. unsigned int num_samples_per_frame, num_channels;
  96. char *model;
  97. bool nvafx_initialized;
  98. const char *fx;
  99. char *sdk_path;
  100. /* Resampler */
  101. audio_resampler_t *nvafx_resampler;
  102. audio_resampler_t *nvafx_resampler_back;
  103. /* Initialization */
  104. bool nvafx_loading;
  105. pthread_t nvafx_thread;
  106. pthread_mutex_t nvafx_mutex;
  107. #endif
  108. /* PCM buffers */
  109. float *copy_buffers[MAX_PREPROC_CHANNELS];
  110. #ifdef LIBSPEEXDSP_ENABLED
  111. spx_int16_t *spx_segment_buffers[MAX_PREPROC_CHANNELS];
  112. #endif
  113. #ifdef LIBRNNOISE_ENABLED
  114. float *rnn_segment_buffers[MAX_PREPROC_CHANNELS];
  115. #endif
  116. #ifdef LIBNVAFX_ENABLED
  117. float *nvafx_segment_buffers[MAX_PREPROC_CHANNELS];
  118. #endif
  119. /* output data */
  120. struct obs_audio_data output_audio;
  121. DARRAY(float) output_data;
  122. };
  123. #ifdef LIBNVAFX_ENABLED
  124. /* global mutex for nvafx load functions since they aren't thread-safe */
  125. bool nvafx_initializer_mutex_initialized;
  126. pthread_mutex_t nvafx_initializer_mutex;
  127. #endif
  128. /* -------------------------------------------------------- */
  129. #define SUP_MIN -60
  130. #define SUP_MAX 0
  131. #ifdef LIBSPEEXDSP_ENABLED
  132. static const float c_32_to_16 = (float)INT16_MAX;
  133. static const float c_16_to_32 = ((float)INT16_MAX + 1.0f);
  134. #endif
  135. /* -------------------------------------------------------- */
  136. static const char *noise_suppress_name(void *unused)
  137. {
  138. UNUSED_PARAMETER(unused);
  139. return obs_module_text("NoiseSuppress");
  140. }
  141. static void noise_suppress_destroy(void *data)
  142. {
  143. struct noise_suppress_data *ng = data;
  144. #ifdef LIBNVAFX_ENABLED
  145. if (ng->nvafx_enabled)
  146. pthread_mutex_lock(&ng->nvafx_mutex);
  147. #endif
  148. for (size_t i = 0; i < ng->channels; i++) {
  149. #ifdef LIBSPEEXDSP_ENABLED
  150. speex_preprocess_state_destroy(ng->spx_states[i]);
  151. #endif
  152. #ifdef LIBRNNOISE_ENABLED
  153. rnnoise_destroy(ng->rnn_states[i]);
  154. #endif
  155. #ifdef LIBNVAFX_ENABLED
  156. if (ng->handle[0]) {
  157. if (NvAFX_DestroyEffect(ng->handle[i]) !=
  158. NVAFX_STATUS_SUCCESS) {
  159. do_log(LOG_ERROR, "NvAFX_Release() failed");
  160. }
  161. }
  162. #endif
  163. circlebuf_free(&ng->input_buffers[i]);
  164. circlebuf_free(&ng->output_buffers[i]);
  165. }
  166. #ifdef LIBSPEEXDSP_ENABLED
  167. bfree(ng->spx_segment_buffers[0]);
  168. #endif
  169. #ifdef LIBRNNOISE_ENABLED
  170. bfree(ng->rnn_segment_buffers[0]);
  171. if (ng->rnn_resampler) {
  172. audio_resampler_destroy(ng->rnn_resampler);
  173. audio_resampler_destroy(ng->rnn_resampler_back);
  174. }
  175. #endif
  176. #ifdef LIBNVAFX_ENABLED
  177. bfree(ng->nvafx_segment_buffers[0]);
  178. if (ng->nvafx_resampler) {
  179. audio_resampler_destroy(ng->nvafx_resampler);
  180. audio_resampler_destroy(ng->nvafx_resampler_back);
  181. }
  182. bfree(ng->model);
  183. bfree(ng->sdk_path);
  184. bfree((void *)ng->fx);
  185. if (ng->nvafx_enabled) {
  186. if (ng->use_nvafx)
  187. pthread_join(ng->nvafx_thread, NULL);
  188. pthread_mutex_unlock(&ng->nvafx_mutex);
  189. pthread_mutex_destroy(&ng->nvafx_mutex);
  190. }
  191. #endif
  192. bfree(ng->copy_buffers[0]);
  193. circlebuf_free(&ng->info_buffer);
  194. da_free(ng->output_data);
  195. bfree(ng);
  196. }
  197. static bool nvafx_initialize_internal(void *data)
  198. {
  199. #ifdef LIBNVAFX_ENABLED
  200. struct noise_suppress_data *ng = data;
  201. NvAFX_Status err;
  202. if (!ng->handle[0]) {
  203. ng->sample_rate = NVAFX_SAMPLE_RATE;
  204. for (size_t i = 0; i < ng->channels; i++) {
  205. // Create FX
  206. CUcontext old = {0};
  207. CUcontext curr = {0};
  208. if (cuCtxGetCurrent(&old) != CUDA_SUCCESS) {
  209. goto failure;
  210. }
  211. // if initialization was with rnnoise or speex
  212. if (strcmp(ng->fx, S_METHOD_NVAFX_DENOISER) != 0 &&
  213. strcmp(ng->fx, S_METHOD_NVAFX_DEREVERB) != 0 &&
  214. strcmp(ng->fx, S_METHOD_NVAFX_DEREVERB_DENOISER) !=
  215. 0) {
  216. ng->fx = bstrdup(S_METHOD_NVAFX_DENOISER);
  217. }
  218. err = NvAFX_CreateEffect(ng->fx, &ng->handle[i]);
  219. if (err != NVAFX_STATUS_SUCCESS) {
  220. do_log(LOG_ERROR,
  221. "%s FX creation failed, error %i",
  222. ng->fx, err);
  223. goto failure;
  224. }
  225. if (cuCtxGetCurrent(&curr) != CUDA_SUCCESS) {
  226. goto failure;
  227. }
  228. if (curr != old) {
  229. cuCtxPopCurrent(NULL);
  230. }
  231. // Set sample rate of FX
  232. err = NvAFX_SetU32(ng->handle[i],
  233. NVAFX_PARAM_INPUT_SAMPLE_RATE,
  234. ng->sample_rate);
  235. if (err != NVAFX_STATUS_SUCCESS) {
  236. do_log(LOG_ERROR,
  237. "NvAFX_SetU32(Sample Rate: %u) failed, error %i",
  238. ng->sample_rate, err);
  239. goto failure;
  240. }
  241. // Set intensity of FX
  242. err = NvAFX_SetFloat(ng->handle[i],
  243. NVAFX_PARAM_INTENSITY_RATIO,
  244. ng->intensity_ratio);
  245. if (err != NVAFX_STATUS_SUCCESS) {
  246. do_log(LOG_ERROR,
  247. "NvAFX_SetFloat(Intensity Ratio: %f) failed, error %i",
  248. ng->intensity_ratio, err);
  249. goto failure;
  250. }
  251. // Set AI models path
  252. err = NvAFX_SetString(ng->handle[i],
  253. NVAFX_PARAM_MODEL_PATH,
  254. ng->model);
  255. if (err != NVAFX_STATUS_SUCCESS) {
  256. do_log(LOG_ERROR,
  257. "NvAFX_SetString() failed, error %i",
  258. err);
  259. goto failure;
  260. }
  261. // Load FX
  262. err = NvAFX_Load(ng->handle[i]);
  263. if (err != NVAFX_STATUS_SUCCESS) {
  264. do_log(LOG_ERROR,
  265. "NvAFX_Load() failed with error %i",
  266. err);
  267. goto failure;
  268. }
  269. os_atomic_set_bool(&ng->reinit_done, true);
  270. }
  271. }
  272. return true;
  273. failure:
  274. ng->use_nvafx = false;
  275. return false;
  276. #else
  277. UNUSED_PARAMETER(data);
  278. return false;
  279. #endif
  280. }
  281. static void *nvafx_initialize(void *data)
  282. {
  283. #ifdef LIBNVAFX_ENABLED
  284. struct noise_suppress_data *ng = data;
  285. NvAFX_Status err;
  286. if (!ng->use_nvafx || !nvafx_loaded) {
  287. return NULL;
  288. }
  289. pthread_mutex_lock(&ng->nvafx_mutex);
  290. pthread_mutex_lock(&nvafx_initializer_mutex);
  291. if (!nvafx_initialize_internal(data)) {
  292. goto failure;
  293. }
  294. if (ng->use_nvafx) {
  295. err = NvAFX_GetU32(ng->handle[0],
  296. NVAFX_PARAM_NUM_INPUT_CHANNELS,
  297. &ng->num_channels);
  298. if (err != NVAFX_STATUS_SUCCESS) {
  299. do_log(LOG_ERROR,
  300. "NvAFX_GetU32() failed to get the number of channels, error %i",
  301. err);
  302. goto failure;
  303. }
  304. if (ng->num_channels != 1) {
  305. do_log(LOG_ERROR,
  306. "The number of channels is not 1 in the sdk any more ==> update code");
  307. goto failure;
  308. }
  309. NvAFX_Status err = NvAFX_GetU32(
  310. ng->handle[0], NVAFX_PARAM_NUM_INPUT_SAMPLES_PER_FRAME,
  311. &ng->num_samples_per_frame);
  312. if (err != NVAFX_STATUS_SUCCESS) {
  313. do_log(LOG_ERROR,
  314. "NvAFX_GetU32() failed to get the number of samples per frame, error %i",
  315. err);
  316. goto failure;
  317. }
  318. if (ng->num_samples_per_frame != NVAFX_FRAME_SIZE) {
  319. do_log(LOG_ERROR,
  320. "The number of samples per frame has changed from 480 (= 10 ms) ==> update code");
  321. goto failure;
  322. }
  323. }
  324. ng->nvafx_initialized = true;
  325. ng->nvafx_loading = false;
  326. pthread_mutex_unlock(&nvafx_initializer_mutex);
  327. pthread_mutex_unlock(&ng->nvafx_mutex);
  328. return NULL;
  329. failure:
  330. ng->use_nvafx = false;
  331. pthread_mutex_unlock(&nvafx_initializer_mutex);
  332. pthread_mutex_unlock(&ng->nvafx_mutex);
  333. return NULL;
  334. #else
  335. UNUSED_PARAMETER(data);
  336. return NULL;
  337. #endif
  338. }
  339. static inline void alloc_channel(struct noise_suppress_data *ng,
  340. uint32_t sample_rate, size_t channel,
  341. size_t frames)
  342. {
  343. #ifdef LIBSPEEXDSP_ENABLED
  344. ng->spx_states[channel] =
  345. speex_preprocess_state_init((int)frames, sample_rate);
  346. #else
  347. UNUSED_PARAMETER(sample_rate);
  348. #endif
  349. #ifdef LIBRNNOISE_ENABLED
  350. ng->rnn_states[channel] = rnnoise_create(NULL);
  351. #endif
  352. circlebuf_reserve(&ng->input_buffers[channel], frames * sizeof(float));
  353. circlebuf_reserve(&ng->output_buffers[channel], frames * sizeof(float));
  354. }
  355. static inline enum speaker_layout convert_speaker_layout(uint8_t channels)
  356. {
  357. switch (channels) {
  358. case 0:
  359. return SPEAKERS_UNKNOWN;
  360. case 1:
  361. return SPEAKERS_MONO;
  362. case 2:
  363. return SPEAKERS_STEREO;
  364. case 3:
  365. return SPEAKERS_2POINT1;
  366. case 4:
  367. return SPEAKERS_4POINT0;
  368. case 5:
  369. return SPEAKERS_4POINT1;
  370. case 6:
  371. return SPEAKERS_5POINT1;
  372. case 8:
  373. return SPEAKERS_7POINT1;
  374. default:
  375. return SPEAKERS_UNKNOWN;
  376. }
  377. }
  378. #ifdef LIBNVAFX_ENABLED
  379. static void set_model(void *data, const char *method)
  380. {
  381. struct noise_suppress_data *ng = data;
  382. const char *file;
  383. if (strcmp(NVAFX_EFFECT_DEREVERB, method) == 0)
  384. file = NVAFX_EFFECT_DEREVERB_MODEL;
  385. else if (strcmp(NVAFX_EFFECT_DEREVERB_DENOISER, method) == 0)
  386. file = NVAFX_EFFECT_DEREVERB_DENOISER_MODEL;
  387. else
  388. file = NVAFX_EFFECT_DENOISER_MODEL;
  389. size_t size = strlen(ng->sdk_path) + strlen(file) + 1;
  390. char *buffer = (char *)bmalloc(size);
  391. strcpy(buffer, ng->sdk_path);
  392. strcat(buffer, file);
  393. ng->model = buffer;
  394. }
  395. #endif
  396. static void noise_suppress_update(void *data, obs_data_t *s)
  397. {
  398. struct noise_suppress_data *ng = data;
  399. uint32_t sample_rate = audio_output_get_sample_rate(obs_get_audio());
  400. size_t channels = audio_output_get_channels(obs_get_audio());
  401. size_t frames = (size_t)sample_rate / (1000 / BUFFER_SIZE_MSEC);
  402. const char *method = obs_data_get_string(s, S_METHOD);
  403. ng->suppress_level = (int)obs_data_get_int(s, S_SUPPRESS_LEVEL);
  404. ng->latency = 1000000000LL / (1000 / BUFFER_SIZE_MSEC);
  405. ng->use_rnnoise = strcmp(method, S_METHOD_RNN) == 0;
  406. bool nvafx_requested =
  407. strcmp(method, S_METHOD_NVAFX_DENOISER) == 0 ||
  408. strcmp(method, S_METHOD_NVAFX_DEREVERB) == 0 ||
  409. strcmp(method, S_METHOD_NVAFX_DEREVERB_DENOISER) == 0;
  410. #ifdef LIBNVAFX_ENABLED
  411. if (nvafx_requested && ng->nvafx_enabled)
  412. set_model(ng, method);
  413. float intensity = (float)obs_data_get_double(s, S_NVAFX_INTENSITY);
  414. if (ng->use_nvafx && ng->nvafx_initialized) {
  415. if (intensity != ng->intensity_ratio &&
  416. (strcmp(ng->fx, method) == 0)) {
  417. NvAFX_Status err;
  418. ng->intensity_ratio = intensity;
  419. pthread_mutex_lock(&ng->nvafx_mutex);
  420. for (size_t i = 0; i < ng->channels; i++) {
  421. err = NvAFX_SetFloat(
  422. ng->handle[i],
  423. NVAFX_PARAM_INTENSITY_RATIO,
  424. ng->intensity_ratio);
  425. if (err != NVAFX_STATUS_SUCCESS) {
  426. do_log(LOG_ERROR,
  427. "NvAFX_SetFloat(Intensity Ratio: %f) failed, error %i",
  428. ng->intensity_ratio, err);
  429. ng->use_nvafx = false;
  430. }
  431. }
  432. pthread_mutex_unlock(&ng->nvafx_mutex);
  433. }
  434. if ((strcmp(ng->fx, method) != 0)) {
  435. pthread_mutex_lock(&ng->nvafx_mutex);
  436. bfree((void *)ng->fx);
  437. ng->fx = bstrdup(method);
  438. ng->intensity_ratio = intensity;
  439. set_model(ng, method);
  440. os_atomic_set_bool(&ng->reinit_done, false);
  441. for (int i = 0; i < (int)ng->channels; i++) {
  442. /* Destroy previous FX */
  443. if (NvAFX_DestroyEffect(ng->handle[i]) !=
  444. NVAFX_STATUS_SUCCESS) {
  445. do_log(LOG_ERROR,
  446. "FX failed to be destroyed.");
  447. ng->use_nvafx = false;
  448. } else
  449. ng->handle[i] = NULL;
  450. }
  451. if (ng->use_nvafx) {
  452. nvafx_initialize_internal(data);
  453. }
  454. pthread_mutex_unlock(&ng->nvafx_mutex);
  455. }
  456. } else {
  457. ng->fx = bstrdup(method);
  458. }
  459. #endif
  460. ng->use_nvafx = ng->nvafx_enabled && nvafx_requested;
  461. /* Process 10 millisecond segments to keep latency low. */
  462. /* Also RNNoise only supports buffers of this exact size. */
  463. /* At 48kHz, NVAFX processes 480 samples which corresponds to 10 ms.*/
  464. ng->frames = frames;
  465. ng->channels = channels;
  466. #ifdef LIBNVAFX_ENABLED
  467. #endif
  468. /* Ignore if already allocated */
  469. #if defined(LIBSPEEXDSP_ENABLED)
  470. if (!ng->use_rnnoise && !ng->use_nvafx && ng->spx_states[0])
  471. return;
  472. #endif
  473. #ifdef LIBNVAFX_ENABLED
  474. if (ng->use_nvafx && (ng->nvafx_initialized || ng->nvafx_loading))
  475. return;
  476. #endif
  477. #ifdef LIBRNNOISE_ENABLED
  478. if (ng->use_rnnoise && ng->rnn_states[0])
  479. return;
  480. #endif
  481. /* One speex/rnnoise state for each channel (limit 2) */
  482. ng->copy_buffers[0] = bmalloc(frames * channels * sizeof(float));
  483. #ifdef LIBSPEEXDSP_ENABLED
  484. ng->spx_segment_buffers[0] =
  485. bmalloc(frames * channels * sizeof(spx_int16_t));
  486. #endif
  487. #ifdef LIBRNNOISE_ENABLED
  488. ng->rnn_segment_buffers[0] =
  489. bmalloc(RNNOISE_FRAME_SIZE * channels * sizeof(float));
  490. #endif
  491. #ifdef LIBNVAFX_ENABLED
  492. ng->nvafx_segment_buffers[0] =
  493. bmalloc(NVAFX_FRAME_SIZE * channels * sizeof(float));
  494. #endif
  495. for (size_t c = 1; c < channels; ++c) {
  496. ng->copy_buffers[c] = ng->copy_buffers[c - 1] + frames;
  497. #ifdef LIBSPEEXDSP_ENABLED
  498. ng->spx_segment_buffers[c] =
  499. ng->spx_segment_buffers[c - 1] + frames;
  500. #endif
  501. #ifdef LIBRNNOISE_ENABLED
  502. ng->rnn_segment_buffers[c] =
  503. ng->rnn_segment_buffers[c - 1] + RNNOISE_FRAME_SIZE;
  504. #endif
  505. #ifdef LIBNVAFX_ENABLED
  506. ng->nvafx_segment_buffers[c] =
  507. ng->nvafx_segment_buffers[c - 1] + NVAFX_FRAME_SIZE;
  508. #endif
  509. }
  510. #ifdef LIBNVAFX_ENABLED
  511. if (!ng->nvafx_initialized && ng->use_nvafx && !ng->nvafx_loading) {
  512. ng->intensity_ratio = intensity;
  513. ng->nvafx_loading = true;
  514. pthread_create(&ng->nvafx_thread, NULL, nvafx_initialize, ng);
  515. }
  516. #endif
  517. for (size_t i = 0; i < channels; i++)
  518. alloc_channel(ng, sample_rate, i, frames);
  519. #ifdef LIBRNNOISE_ENABLED
  520. if (sample_rate == RNNOISE_SAMPLE_RATE) {
  521. ng->rnn_resampler = NULL;
  522. ng->rnn_resampler_back = NULL;
  523. } else {
  524. struct resample_info src, dst;
  525. src.samples_per_sec = sample_rate;
  526. src.format = AUDIO_FORMAT_FLOAT_PLANAR;
  527. src.speakers = convert_speaker_layout((uint8_t)channels);
  528. dst.samples_per_sec = RNNOISE_SAMPLE_RATE;
  529. dst.format = AUDIO_FORMAT_FLOAT_PLANAR;
  530. dst.speakers = convert_speaker_layout((uint8_t)channels);
  531. ng->rnn_resampler = audio_resampler_create(&dst, &src);
  532. ng->rnn_resampler_back = audio_resampler_create(&src, &dst);
  533. }
  534. #endif
  535. #ifdef LIBNVAFX_ENABLED
  536. if (sample_rate == NVAFX_SAMPLE_RATE) {
  537. ng->nvafx_resampler = NULL;
  538. ng->nvafx_resampler_back = NULL;
  539. } else {
  540. struct resample_info src, dst;
  541. src.samples_per_sec = sample_rate;
  542. src.format = AUDIO_FORMAT_FLOAT_PLANAR;
  543. src.speakers = convert_speaker_layout((uint8_t)channels);
  544. dst.samples_per_sec = NVAFX_SAMPLE_RATE;
  545. dst.format = AUDIO_FORMAT_FLOAT_PLANAR;
  546. dst.speakers = convert_speaker_layout((uint8_t)channels);
  547. ng->nvafx_resampler = audio_resampler_create(&dst, &src);
  548. ng->nvafx_resampler_back = audio_resampler_create(&src, &dst);
  549. }
  550. #endif
  551. }
  552. #ifdef _MSC_VER
  553. #pragma warning(push)
  554. #pragma warning(disable : 4706)
  555. #endif
  556. bool load_nvafx(void)
  557. {
  558. #ifdef LIBNVAFX_ENABLED
  559. unsigned int version = get_lib_version();
  560. uint8_t major = (version >> 24) & 0xff;
  561. uint8_t minor = (version >> 16) & 0x00ff;
  562. uint8_t build = (version >> 8) & 0x0000ff;
  563. uint8_t revision = (version >> 0) & 0x000000ff;
  564. if (version) {
  565. blog(LOG_INFO,
  566. "[noise suppress]: NVIDIA AUDIO FX version: %i.%i.%i.%i",
  567. major, minor, build, revision);
  568. if (version < MIN_AFX_SDK_VERSION) {
  569. blog(LOG_INFO,
  570. "[noise suppress]: NVIDIA AUDIO Effects SDK is outdated. Please update both audio & video SDK.");
  571. }
  572. }
  573. if (!load_lib()) {
  574. blog(LOG_INFO,
  575. "[noise suppress]: NVIDIA denoiser disabled, redistributable not found or could not be loaded.");
  576. return false;
  577. }
  578. nvafx_initializer_mutex_initialized =
  579. pthread_mutex_init(&nvafx_initializer_mutex, NULL) == 0;
  580. #define LOAD_SYM_FROM_LIB(sym, lib, dll) \
  581. if (!(sym = (sym##_t)GetProcAddress(lib, #sym))) { \
  582. DWORD err = GetLastError(); \
  583. printf("[noise suppress]: Couldn't load " #sym " from " dll \
  584. ": %lu (0x%lx)", \
  585. err, err); \
  586. goto unload_everything; \
  587. }
  588. #define LOAD_SYM(sym) LOAD_SYM_FROM_LIB(sym, nv_audiofx, "NVAudioEffects.dll")
  589. LOAD_SYM(NvAFX_GetEffectList);
  590. LOAD_SYM(NvAFX_CreateEffect);
  591. LOAD_SYM(NvAFX_CreateChainedEffect);
  592. LOAD_SYM(NvAFX_DestroyEffect);
  593. LOAD_SYM(NvAFX_SetU32);
  594. LOAD_SYM(NvAFX_SetU32List);
  595. LOAD_SYM(NvAFX_SetString);
  596. LOAD_SYM(NvAFX_SetStringList);
  597. LOAD_SYM(NvAFX_SetFloat);
  598. LOAD_SYM(NvAFX_SetFloatList);
  599. LOAD_SYM(NvAFX_GetU32);
  600. LOAD_SYM(NvAFX_GetString);
  601. LOAD_SYM(NvAFX_GetStringList);
  602. LOAD_SYM(NvAFX_GetFloat);
  603. LOAD_SYM(NvAFX_GetFloatList);
  604. LOAD_SYM(NvAFX_Load);
  605. LOAD_SYM(NvAFX_GetSupportedDevices);
  606. LOAD_SYM(NvAFX_Run);
  607. LOAD_SYM(NvAFX_Reset);
  608. #undef LOAD_SYM
  609. #define LOAD_SYM(sym) LOAD_SYM_FROM_LIB(sym, nv_cuda, "nvcuda.dll")
  610. LOAD_SYM(cuCtxGetCurrent);
  611. LOAD_SYM(cuCtxPopCurrent);
  612. LOAD_SYM(cuInit);
  613. #undef LOAD_SYM
  614. NvAFX_Status err;
  615. CUresult cudaerr;
  616. NvAFX_Handle h = NULL;
  617. cudaerr = cuInit(0);
  618. if (cudaerr != CUDA_SUCCESS) {
  619. goto cuda_errors;
  620. }
  621. CUcontext old = {0};
  622. CUcontext curr = {0};
  623. cudaerr = cuCtxGetCurrent(&old);
  624. if (cudaerr != CUDA_SUCCESS) {
  625. goto cuda_errors;
  626. }
  627. err = NvAFX_CreateEffect(NVAFX_EFFECT_DENOISER, &h);
  628. cudaerr = cuCtxGetCurrent(&curr);
  629. if (cudaerr != CUDA_SUCCESS) {
  630. goto cuda_errors;
  631. }
  632. if (curr != old) {
  633. cudaerr = cuCtxPopCurrent(NULL);
  634. if (cudaerr != CUDA_SUCCESS)
  635. goto cuda_errors;
  636. }
  637. if (err != NVAFX_STATUS_SUCCESS) {
  638. if (err == NVAFX_STATUS_GPU_UNSUPPORTED) {
  639. blog(LOG_INFO,
  640. "[noise suppress]: NVIDIA AUDIO FX disabled: unsupported GPU");
  641. } else {
  642. blog(LOG_ERROR,
  643. "[noise suppress]: NVIDIA AUDIO FX disabled, error %i",
  644. err);
  645. }
  646. goto unload_everything;
  647. }
  648. err = NvAFX_DestroyEffect(h);
  649. if (err != NVAFX_STATUS_SUCCESS) {
  650. blog(LOG_ERROR,
  651. "[noise suppress]: NVIDIA AUDIO FX disabled, error %i",
  652. err);
  653. goto unload_everything;
  654. }
  655. nvafx_loaded = true;
  656. blog(LOG_INFO, "[noise suppress]: NVIDIA AUDIO FX enabled");
  657. return true;
  658. cuda_errors:
  659. blog(LOG_ERROR,
  660. "[noise suppress]: NVIDIA AUDIO FX disabled, CUDA error %i",
  661. cudaerr);
  662. unload_everything:
  663. release_lib();
  664. #endif
  665. return false;
  666. }
  667. #ifdef _MSC_VER
  668. #pragma warning(pop)
  669. #endif
  670. #ifdef LIBNVAFX_ENABLED
  671. void unload_nvafx(void)
  672. {
  673. release_lib();
  674. if (nvafx_initializer_mutex_initialized) {
  675. pthread_mutex_destroy(&nvafx_initializer_mutex);
  676. nvafx_initializer_mutex_initialized = false;
  677. }
  678. }
  679. #endif
  680. static void *noise_suppress_create(obs_data_t *settings, obs_source_t *filter)
  681. {
  682. struct noise_suppress_data *ng =
  683. bzalloc(sizeof(struct noise_suppress_data));
  684. ng->context = filter;
  685. #ifdef LIBNVAFX_ENABLED
  686. char sdk_path[MAX_PATH];
  687. if (!nvafx_get_sdk_path(sdk_path, sizeof(sdk_path))) {
  688. ng->nvafx_enabled = false;
  689. do_log(LOG_ERROR, "NVAFX redist is not installed.");
  690. } else {
  691. size_t size = sizeof(sdk_path) + 1;
  692. ng->sdk_path = bmalloc(size);
  693. strcpy(ng->sdk_path, sdk_path);
  694. ng->nvafx_enabled = true;
  695. ng->nvafx_initialized = false;
  696. ng->nvafx_loading = false;
  697. ng->fx = NULL;
  698. pthread_mutex_init(&ng->nvafx_mutex, NULL);
  699. info("NVAFX SDK redist path was found here %s", sdk_path);
  700. }
  701. #endif
  702. noise_suppress_update(ng, settings);
  703. return ng;
  704. }
  705. static inline void process_speexdsp(struct noise_suppress_data *ng)
  706. {
  707. #ifdef LIBSPEEXDSP_ENABLED
  708. /* Set args */
  709. for (size_t i = 0; i < ng->channels; i++)
  710. speex_preprocess_ctl(ng->spx_states[i],
  711. SPEEX_PREPROCESS_SET_NOISE_SUPPRESS,
  712. &ng->suppress_level);
  713. /* Convert to 16bit */
  714. for (size_t i = 0; i < ng->channels; i++)
  715. for (size_t j = 0; j < ng->frames; j++) {
  716. float s = ng->copy_buffers[i][j];
  717. if (s > 1.0f)
  718. s = 1.0f;
  719. else if (s < -1.0f)
  720. s = -1.0f;
  721. ng->spx_segment_buffers[i][j] =
  722. (spx_int16_t)(s * c_32_to_16);
  723. }
  724. /* Execute */
  725. for (size_t i = 0; i < ng->channels; i++)
  726. speex_preprocess_run(ng->spx_states[i],
  727. ng->spx_segment_buffers[i]);
  728. /* Convert back to 32bit */
  729. for (size_t i = 0; i < ng->channels; i++)
  730. for (size_t j = 0; j < ng->frames; j++)
  731. ng->copy_buffers[i][j] =
  732. (float)ng->spx_segment_buffers[i][j] /
  733. c_16_to_32;
  734. #else
  735. UNUSED_PARAMETER(ng);
  736. #endif
  737. }
  738. static inline void process_rnnoise(struct noise_suppress_data *ng)
  739. {
  740. #ifdef LIBRNNOISE_ENABLED
  741. /* Adjust signal level to what RNNoise expects, resample if necessary */
  742. if (ng->rnn_resampler) {
  743. float *output[MAX_PREPROC_CHANNELS];
  744. uint32_t out_frames;
  745. uint64_t ts_offset;
  746. audio_resampler_resample(ng->rnn_resampler, (uint8_t **)output,
  747. &out_frames, &ts_offset,
  748. (const uint8_t **)ng->copy_buffers,
  749. (uint32_t)ng->frames);
  750. for (size_t i = 0; i < ng->channels; i++) {
  751. for (ssize_t j = 0, k = (ssize_t)out_frames -
  752. RNNOISE_FRAME_SIZE;
  753. j < RNNOISE_FRAME_SIZE; ++j, ++k) {
  754. if (k >= 0) {
  755. ng->rnn_segment_buffers[i][j] =
  756. output[i][k] * 32768.0f;
  757. } else {
  758. ng->rnn_segment_buffers[i][j] = 0;
  759. }
  760. }
  761. }
  762. } else {
  763. for (size_t i = 0; i < ng->channels; i++) {
  764. for (size_t j = 0; j < RNNOISE_FRAME_SIZE; ++j) {
  765. ng->rnn_segment_buffers[i][j] =
  766. ng->copy_buffers[i][j] * 32768.0f;
  767. }
  768. }
  769. }
  770. /* Execute */
  771. for (size_t i = 0; i < ng->channels; i++) {
  772. rnnoise_process_frame(ng->rnn_states[i],
  773. ng->rnn_segment_buffers[i],
  774. ng->rnn_segment_buffers[i]);
  775. }
  776. /* Revert signal level adjustment, resample back if necessary */
  777. if (ng->rnn_resampler) {
  778. float *output[MAX_PREPROC_CHANNELS];
  779. uint32_t out_frames;
  780. uint64_t ts_offset;
  781. audio_resampler_resample(
  782. ng->rnn_resampler_back, (uint8_t **)output, &out_frames,
  783. &ts_offset, (const uint8_t **)ng->rnn_segment_buffers,
  784. RNNOISE_FRAME_SIZE);
  785. for (size_t i = 0; i < ng->channels; i++) {
  786. for (ssize_t j = 0,
  787. k = (ssize_t)out_frames - ng->frames;
  788. j < (ssize_t)ng->frames; ++j, ++k) {
  789. if (k >= 0) {
  790. ng->copy_buffers[i][j] =
  791. output[i][k] / 32768.0f;
  792. } else {
  793. ng->copy_buffers[i][j] = 0;
  794. }
  795. }
  796. }
  797. } else {
  798. for (size_t i = 0; i < ng->channels; i++) {
  799. for (size_t j = 0; j < RNNOISE_FRAME_SIZE; ++j) {
  800. ng->copy_buffers[i][j] =
  801. ng->rnn_segment_buffers[i][j] /
  802. 32768.0f;
  803. }
  804. }
  805. }
  806. #else
  807. UNUSED_PARAMETER(ng);
  808. #endif
  809. }
  810. static inline void process_nvafx(struct noise_suppress_data *ng)
  811. {
  812. #ifdef LIBNVAFX_ENABLED
  813. if (nvafx_loaded && ng->use_nvafx && ng->nvafx_initialized) {
  814. /* Resample if necessary */
  815. if (ng->nvafx_resampler) {
  816. float *output[MAX_PREPROC_CHANNELS];
  817. uint32_t out_frames;
  818. uint64_t ts_offset;
  819. audio_resampler_resample(
  820. ng->nvafx_resampler, (uint8_t **)output,
  821. &out_frames, &ts_offset,
  822. (const uint8_t **)ng->copy_buffers,
  823. (uint32_t)ng->frames);
  824. for (size_t i = 0; i < ng->channels; i++) {
  825. for (ssize_t j = 0, k = (ssize_t)out_frames -
  826. NVAFX_FRAME_SIZE;
  827. j < NVAFX_FRAME_SIZE; ++j, ++k) {
  828. if (k >= 0) {
  829. ng->nvafx_segment_buffers[i][j] =
  830. output[i][k];
  831. } else {
  832. ng->nvafx_segment_buffers[i][j] =
  833. 0;
  834. }
  835. }
  836. }
  837. } else {
  838. for (size_t i = 0; i < ng->channels; i++) {
  839. for (size_t j = 0; j < NVAFX_FRAME_SIZE; ++j) {
  840. ng->nvafx_segment_buffers[i][j] =
  841. ng->copy_buffers[i][j];
  842. }
  843. }
  844. }
  845. /* Execute */
  846. size_t runs = ng->has_mono_src ? 1 : ng->channels;
  847. if (ng->reinit_done) {
  848. pthread_mutex_lock(&ng->nvafx_mutex);
  849. for (size_t i = 0; i < runs; i++) {
  850. NvAFX_Status err =
  851. NvAFX_Run(ng->handle[i],
  852. &ng->nvafx_segment_buffers[i],
  853. &ng->nvafx_segment_buffers[i],
  854. ng->num_samples_per_frame,
  855. ng->num_channels);
  856. if (err != NVAFX_STATUS_SUCCESS) {
  857. if (err == NVAFX_STATUS_FAILED) {
  858. do_log(LOG_DEBUG,
  859. "NvAFX_Run() failed, error NVAFX_STATUS_FAILED.\n"
  860. "This can occur when changing the FX and is not consequential.");
  861. os_atomic_set_bool(
  862. &ng->reinit_done,
  863. false); // stop all processing; this will be reset at new init
  864. } else {
  865. do_log(LOG_ERROR,
  866. "NvAFX_Run() failed, error %i.\n",
  867. err);
  868. }
  869. }
  870. }
  871. pthread_mutex_unlock(&ng->nvafx_mutex);
  872. }
  873. if (ng->has_mono_src) {
  874. memcpy(ng->nvafx_segment_buffers[1],
  875. ng->nvafx_segment_buffers[0],
  876. NVAFX_FRAME_SIZE * sizeof(float));
  877. }
  878. /* Revert signal level adjustment, resample back if necessary */
  879. if (ng->nvafx_resampler) {
  880. float *output[MAX_PREPROC_CHANNELS];
  881. uint32_t out_frames;
  882. uint64_t ts_offset;
  883. audio_resampler_resample(
  884. ng->nvafx_resampler_back, (uint8_t **)output,
  885. &out_frames, &ts_offset,
  886. (const uint8_t **)ng->nvafx_segment_buffers,
  887. NVAFX_FRAME_SIZE);
  888. for (size_t i = 0; i < ng->channels; i++) {
  889. for (ssize_t j = 0, k = (ssize_t)out_frames -
  890. ng->frames;
  891. j < (ssize_t)ng->frames; ++j, ++k) {
  892. if (k >= 0) {
  893. ng->copy_buffers[i][j] =
  894. output[i][k];
  895. } else {
  896. ng->copy_buffers[i][j] = 0;
  897. }
  898. }
  899. }
  900. } else {
  901. for (size_t i = 0; i < ng->channels; i++) {
  902. for (size_t j = 0; j < NVAFX_FRAME_SIZE; ++j) {
  903. ng->copy_buffers[i][j] =
  904. ng->nvafx_segment_buffers[i][j];
  905. }
  906. }
  907. }
  908. }
  909. #else
  910. UNUSED_PARAMETER(ng);
  911. #endif
  912. }
  913. static inline void process(struct noise_suppress_data *ng)
  914. {
  915. /* Pop from input circlebuf */
  916. for (size_t i = 0; i < ng->channels; i++)
  917. circlebuf_pop_front(&ng->input_buffers[i], ng->copy_buffers[i],
  918. ng->frames * sizeof(float));
  919. if (ng->use_rnnoise) {
  920. process_rnnoise(ng);
  921. } else if (ng->use_nvafx) {
  922. if (nvafx_loaded) {
  923. process_nvafx(ng);
  924. }
  925. } else {
  926. process_speexdsp(ng);
  927. }
  928. /* Push to output circlebuf */
  929. for (size_t i = 0; i < ng->channels; i++)
  930. circlebuf_push_back(&ng->output_buffers[i], ng->copy_buffers[i],
  931. ng->frames * sizeof(float));
  932. }
  933. struct ng_audio_info {
  934. uint32_t frames;
  935. uint64_t timestamp;
  936. };
  937. static inline void clear_circlebuf(struct circlebuf *buf)
  938. {
  939. circlebuf_pop_front(buf, NULL, buf->size);
  940. }
  941. static void reset_data(struct noise_suppress_data *ng)
  942. {
  943. for (size_t i = 0; i < ng->channels; i++) {
  944. clear_circlebuf(&ng->input_buffers[i]);
  945. clear_circlebuf(&ng->output_buffers[i]);
  946. }
  947. clear_circlebuf(&ng->info_buffer);
  948. }
  949. static struct obs_audio_data *
  950. noise_suppress_filter_audio(void *data, struct obs_audio_data *audio)
  951. {
  952. struct noise_suppress_data *ng = data;
  953. struct ng_audio_info info;
  954. size_t segment_size = ng->frames * sizeof(float);
  955. size_t out_size;
  956. obs_source_t *parent = obs_filter_get_parent(ng->context);
  957. enum speaker_layout layout = obs_source_get_speaker_layout(parent);
  958. ng->has_mono_src = layout == SPEAKERS_MONO && ng->channels == 2;
  959. #ifdef LIBSPEEXDSP_ENABLED
  960. if (!ng->spx_states[0])
  961. return audio;
  962. #endif
  963. #ifdef LIBRNNOISE_ENABLED
  964. if (!ng->rnn_states[0])
  965. return audio;
  966. #endif
  967. /* -----------------------------------------------
  968. * if timestamp has dramatically changed, consider it a new stream of
  969. * audio data. clear all circular buffers to prevent old audio data
  970. * from being processed as part of the new data. */
  971. if (ng->last_timestamp) {
  972. int64_t diff = llabs((int64_t)ng->last_timestamp -
  973. (int64_t)audio->timestamp);
  974. if (diff > 1000000000LL)
  975. reset_data(ng);
  976. }
  977. ng->last_timestamp = audio->timestamp;
  978. /* -----------------------------------------------
  979. * push audio packet info (timestamp/frame count) to info circlebuf */
  980. info.frames = audio->frames;
  981. info.timestamp = audio->timestamp;
  982. circlebuf_push_back(&ng->info_buffer, &info, sizeof(info));
  983. /* -----------------------------------------------
  984. * push back current audio data to input circlebuf */
  985. for (size_t i = 0; i < ng->channels; i++)
  986. circlebuf_push_back(&ng->input_buffers[i], audio->data[i],
  987. audio->frames * sizeof(float));
  988. /* -----------------------------------------------
  989. * pop/process each 10ms segments, push back to output circlebuf */
  990. while (ng->input_buffers[0].size >= segment_size)
  991. process(ng);
  992. /* -----------------------------------------------
  993. * peek front of info circlebuf, check to see if we have enough to
  994. * pop the expected packet size, if not, return null */
  995. memset(&info, 0, sizeof(info));
  996. circlebuf_peek_front(&ng->info_buffer, &info, sizeof(info));
  997. out_size = info.frames * sizeof(float);
  998. if (ng->output_buffers[0].size < out_size)
  999. return NULL;
  1000. /* -----------------------------------------------
  1001. * if there's enough audio data buffered in the output circlebuf,
  1002. * pop and return a packet */
  1003. circlebuf_pop_front(&ng->info_buffer, NULL, sizeof(info));
  1004. da_resize(ng->output_data, out_size * ng->channels);
  1005. for (size_t i = 0; i < ng->channels; i++) {
  1006. ng->output_audio.data[i] =
  1007. (uint8_t *)&ng->output_data.array[i * out_size];
  1008. circlebuf_pop_front(&ng->output_buffers[i],
  1009. ng->output_audio.data[i], out_size);
  1010. }
  1011. ng->output_audio.frames = info.frames;
  1012. ng->output_audio.timestamp = info.timestamp - ng->latency;
  1013. return &ng->output_audio;
  1014. }
  1015. static bool noise_suppress_method_modified(obs_properties_t *props,
  1016. obs_property_t *property,
  1017. obs_data_t *settings)
  1018. {
  1019. obs_property_t *p_suppress_level =
  1020. obs_properties_get(props, S_SUPPRESS_LEVEL);
  1021. obs_property_t *p_navfx_intensity =
  1022. obs_properties_get(props, S_NVAFX_INTENSITY);
  1023. const char *method = obs_data_get_string(settings, S_METHOD);
  1024. bool enable_level = strcmp(method, S_METHOD_SPEEX) == 0;
  1025. bool enable_intensity =
  1026. strcmp(method, S_METHOD_NVAFX_DENOISER) == 0 ||
  1027. strcmp(method, S_METHOD_NVAFX_DEREVERB) == 0 ||
  1028. strcmp(method, S_METHOD_NVAFX_DEREVERB_DENOISER) == 0;
  1029. obs_property_set_visible(p_suppress_level, enable_level);
  1030. obs_property_set_visible(p_navfx_intensity, enable_intensity);
  1031. UNUSED_PARAMETER(property);
  1032. return true;
  1033. }
  1034. static void noise_suppress_defaults_v1(obs_data_t *s)
  1035. {
  1036. obs_data_set_default_int(s, S_SUPPRESS_LEVEL, -30);
  1037. #if defined(LIBRNNOISE_ENABLED) && !defined(LIBSPEEXDSP_ENABLED)
  1038. obs_data_set_default_string(s, S_METHOD, S_METHOD_RNN);
  1039. #else
  1040. obs_data_set_default_string(s, S_METHOD, S_METHOD_SPEEX);
  1041. #endif
  1042. #if defined(LIBNVAFX_ENABLED)
  1043. obs_data_set_default_double(s, S_NVAFX_INTENSITY, 1.0);
  1044. #endif
  1045. }
  1046. static void noise_suppress_defaults_v2(obs_data_t *s)
  1047. {
  1048. obs_data_set_default_int(s, S_SUPPRESS_LEVEL, -30);
  1049. #if defined(LIBRNNOISE_ENABLED)
  1050. obs_data_set_default_string(s, S_METHOD, S_METHOD_RNN);
  1051. #else
  1052. obs_data_set_default_string(s, S_METHOD, S_METHOD_SPEEX);
  1053. #endif
  1054. #if defined(LIBNVAFX_ENABLED)
  1055. obs_data_set_default_double(s, S_NVAFX_INTENSITY, 1.0);
  1056. #endif
  1057. }
  1058. static obs_properties_t *noise_suppress_properties(void *data)
  1059. {
  1060. obs_properties_t *ppts = obs_properties_create();
  1061. #ifdef LIBNVAFX_ENABLED
  1062. struct noise_suppress_data *ng = (struct noise_suppress_data *)data;
  1063. #else
  1064. UNUSED_PARAMETER(data);
  1065. #endif
  1066. #if defined(LIBRNNOISE_ENABLED) && defined(LIBSPEEXDSP_ENABLED)
  1067. obs_property_t *method = obs_properties_add_list(
  1068. ppts, S_METHOD, TEXT_METHOD, OBS_COMBO_TYPE_LIST,
  1069. OBS_COMBO_FORMAT_STRING);
  1070. obs_property_list_add_string(method, TEXT_METHOD_SPEEX, S_METHOD_SPEEX);
  1071. obs_property_list_add_string(method, TEXT_METHOD_RNN, S_METHOD_RNN);
  1072. #ifdef LIBNVAFX_ENABLED
  1073. if (ng->nvafx_enabled) {
  1074. obs_property_list_add_string(method, TEXT_METHOD_NVAFX_DENOISER,
  1075. S_METHOD_NVAFX_DENOISER);
  1076. obs_property_list_add_string(method, TEXT_METHOD_NVAFX_DEREVERB,
  1077. S_METHOD_NVAFX_DEREVERB);
  1078. obs_property_list_add_string(
  1079. method, TEXT_METHOD_NVAFX_DEREVERB_DENOISER,
  1080. S_METHOD_NVAFX_DEREVERB_DENOISER);
  1081. }
  1082. #endif
  1083. obs_property_set_modified_callback(method,
  1084. noise_suppress_method_modified);
  1085. #endif
  1086. #ifdef LIBSPEEXDSP_ENABLED
  1087. obs_property_t *speex_slider = obs_properties_add_int_slider(
  1088. ppts, S_SUPPRESS_LEVEL, TEXT_SUPPRESS_LEVEL, SUP_MIN, SUP_MAX,
  1089. 1);
  1090. obs_property_int_set_suffix(speex_slider, " dB");
  1091. #endif
  1092. #ifdef LIBNVAFX_ENABLED
  1093. if (ng->nvafx_enabled) {
  1094. obs_properties_add_float_slider(ppts, S_NVAFX_INTENSITY,
  1095. TEXT_NVAFX_INTENSITY, 0.0f,
  1096. 1.0f, 0.01f);
  1097. }
  1098. unsigned int version = get_lib_version();
  1099. if (version && version < MIN_AFX_SDK_VERSION) {
  1100. obs_property_t *warning = obs_properties_add_text(
  1101. ppts, "deprecation", NULL, OBS_TEXT_INFO);
  1102. obs_property_text_set_info_type(warning, OBS_TEXT_INFO_WARNING);
  1103. obs_property_set_long_description(
  1104. warning, TEXT_METHOD_NVAFX_DEPRECATION);
  1105. }
  1106. #if defined(LIBRNNOISE_ENABLED) && defined(LIBSPEEXDSP_ENABLED)
  1107. if (!nvafx_loaded) {
  1108. obs_property_list_item_disable(method, 2, true);
  1109. obs_property_list_item_disable(method, 3, true);
  1110. obs_property_list_item_disable(method, 4, true);
  1111. }
  1112. #endif
  1113. #endif
  1114. return ppts;
  1115. }
  1116. struct obs_source_info noise_suppress_filter = {
  1117. .id = "noise_suppress_filter",
  1118. .type = OBS_SOURCE_TYPE_FILTER,
  1119. .output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_CAP_OBSOLETE,
  1120. .get_name = noise_suppress_name,
  1121. .create = noise_suppress_create,
  1122. .destroy = noise_suppress_destroy,
  1123. .update = noise_suppress_update,
  1124. .filter_audio = noise_suppress_filter_audio,
  1125. .get_defaults = noise_suppress_defaults_v1,
  1126. .get_properties = noise_suppress_properties,
  1127. };
  1128. struct obs_source_info noise_suppress_filter_v2 = {
  1129. .id = "noise_suppress_filter",
  1130. .version = 2,
  1131. .type = OBS_SOURCE_TYPE_FILTER,
  1132. .output_flags = OBS_SOURCE_AUDIO,
  1133. .get_name = noise_suppress_name,
  1134. .create = noise_suppress_create,
  1135. .destroy = noise_suppress_destroy,
  1136. .update = noise_suppress_update,
  1137. .filter_audio = noise_suppress_filter_audio,
  1138. .get_defaults = noise_suppress_defaults_v2,
  1139. .get_properties = noise_suppress_properties,
  1140. };