noise-suppress-filter.c 34 KB

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