noise-suppress-filter.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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. bfree((void *)ng->fx);
  436. ng->fx = bstrdup(method);
  437. ng->intensity_ratio = intensity;
  438. set_model(ng, method);
  439. os_atomic_set_bool(&ng->reinit_done, false);
  440. pthread_mutex_lock(&ng->nvafx_mutex);
  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. for (size_t i = 0; i < runs; i++) {
  849. NvAFX_Status err =
  850. NvAFX_Run(ng->handle[i],
  851. &ng->nvafx_segment_buffers[i],
  852. &ng->nvafx_segment_buffers[i],
  853. ng->num_samples_per_frame,
  854. ng->num_channels);
  855. if (err != NVAFX_STATUS_SUCCESS) {
  856. if (err == NVAFX_STATUS_FAILED) {
  857. do_log(LOG_DEBUG,
  858. "NvAFX_Run() failed, error NVAFX_STATUS_FAILED.\n"
  859. "This can occur when changing the FX and is not consequential.");
  860. os_atomic_set_bool(
  861. &ng->reinit_done,
  862. false); // stop all processing; this will be reset at new init
  863. } else {
  864. do_log(LOG_ERROR,
  865. "NvAFX_Run() failed, error %i.\n",
  866. err);
  867. }
  868. }
  869. }
  870. }
  871. if (ng->has_mono_src) {
  872. memcpy(ng->nvafx_segment_buffers[1],
  873. ng->nvafx_segment_buffers[0],
  874. NVAFX_FRAME_SIZE * sizeof(float));
  875. }
  876. /* Revert signal level adjustment, resample back if necessary */
  877. if (ng->nvafx_resampler) {
  878. float *output[MAX_PREPROC_CHANNELS];
  879. uint32_t out_frames;
  880. uint64_t ts_offset;
  881. audio_resampler_resample(
  882. ng->nvafx_resampler_back, (uint8_t **)output,
  883. &out_frames, &ts_offset,
  884. (const uint8_t **)ng->nvafx_segment_buffers,
  885. NVAFX_FRAME_SIZE);
  886. for (size_t i = 0; i < ng->channels; i++) {
  887. for (ssize_t j = 0, k = (ssize_t)out_frames -
  888. ng->frames;
  889. j < (ssize_t)ng->frames; ++j, ++k) {
  890. if (k >= 0) {
  891. ng->copy_buffers[i][j] =
  892. output[i][k];
  893. } else {
  894. ng->copy_buffers[i][j] = 0;
  895. }
  896. }
  897. }
  898. } else {
  899. for (size_t i = 0; i < ng->channels; i++) {
  900. for (size_t j = 0; j < NVAFX_FRAME_SIZE; ++j) {
  901. ng->copy_buffers[i][j] =
  902. ng->nvafx_segment_buffers[i][j];
  903. }
  904. }
  905. }
  906. }
  907. #else
  908. UNUSED_PARAMETER(ng);
  909. #endif
  910. }
  911. static inline void process(struct noise_suppress_data *ng)
  912. {
  913. /* Pop from input circlebuf */
  914. for (size_t i = 0; i < ng->channels; i++)
  915. circlebuf_pop_front(&ng->input_buffers[i], ng->copy_buffers[i],
  916. ng->frames * sizeof(float));
  917. if (ng->use_rnnoise) {
  918. process_rnnoise(ng);
  919. } else if (ng->use_nvafx) {
  920. if (nvafx_loaded) {
  921. process_nvafx(ng);
  922. }
  923. } else {
  924. process_speexdsp(ng);
  925. }
  926. /* Push to output circlebuf */
  927. for (size_t i = 0; i < ng->channels; i++)
  928. circlebuf_push_back(&ng->output_buffers[i], ng->copy_buffers[i],
  929. ng->frames * sizeof(float));
  930. }
  931. struct ng_audio_info {
  932. uint32_t frames;
  933. uint64_t timestamp;
  934. };
  935. static inline void clear_circlebuf(struct circlebuf *buf)
  936. {
  937. circlebuf_pop_front(buf, NULL, buf->size);
  938. }
  939. static void reset_data(struct noise_suppress_data *ng)
  940. {
  941. for (size_t i = 0; i < ng->channels; i++) {
  942. clear_circlebuf(&ng->input_buffers[i]);
  943. clear_circlebuf(&ng->output_buffers[i]);
  944. }
  945. clear_circlebuf(&ng->info_buffer);
  946. }
  947. static struct obs_audio_data *
  948. noise_suppress_filter_audio(void *data, struct obs_audio_data *audio)
  949. {
  950. struct noise_suppress_data *ng = data;
  951. struct ng_audio_info info;
  952. size_t segment_size = ng->frames * sizeof(float);
  953. size_t out_size;
  954. obs_source_t *parent = obs_filter_get_parent(ng->context);
  955. enum speaker_layout layout = obs_source_get_speaker_layout(parent);
  956. ng->has_mono_src = layout == SPEAKERS_MONO && ng->channels == 2;
  957. #ifdef LIBSPEEXDSP_ENABLED
  958. if (!ng->spx_states[0])
  959. return audio;
  960. #endif
  961. #ifdef LIBRNNOISE_ENABLED
  962. if (!ng->rnn_states[0])
  963. return audio;
  964. #endif
  965. /* -----------------------------------------------
  966. * if timestamp has dramatically changed, consider it a new stream of
  967. * audio data. clear all circular buffers to prevent old audio data
  968. * from being processed as part of the new data. */
  969. if (ng->last_timestamp) {
  970. int64_t diff = llabs((int64_t)ng->last_timestamp -
  971. (int64_t)audio->timestamp);
  972. if (diff > 1000000000LL)
  973. reset_data(ng);
  974. }
  975. ng->last_timestamp = audio->timestamp;
  976. /* -----------------------------------------------
  977. * push audio packet info (timestamp/frame count) to info circlebuf */
  978. info.frames = audio->frames;
  979. info.timestamp = audio->timestamp;
  980. circlebuf_push_back(&ng->info_buffer, &info, sizeof(info));
  981. /* -----------------------------------------------
  982. * push back current audio data to input circlebuf */
  983. for (size_t i = 0; i < ng->channels; i++)
  984. circlebuf_push_back(&ng->input_buffers[i], audio->data[i],
  985. audio->frames * sizeof(float));
  986. /* -----------------------------------------------
  987. * pop/process each 10ms segments, push back to output circlebuf */
  988. while (ng->input_buffers[0].size >= segment_size)
  989. process(ng);
  990. /* -----------------------------------------------
  991. * peek front of info circlebuf, check to see if we have enough to
  992. * pop the expected packet size, if not, return null */
  993. memset(&info, 0, sizeof(info));
  994. circlebuf_peek_front(&ng->info_buffer, &info, sizeof(info));
  995. out_size = info.frames * sizeof(float);
  996. if (ng->output_buffers[0].size < out_size)
  997. return NULL;
  998. /* -----------------------------------------------
  999. * if there's enough audio data buffered in the output circlebuf,
  1000. * pop and return a packet */
  1001. circlebuf_pop_front(&ng->info_buffer, NULL, sizeof(info));
  1002. da_resize(ng->output_data, out_size * ng->channels);
  1003. for (size_t i = 0; i < ng->channels; i++) {
  1004. ng->output_audio.data[i] =
  1005. (uint8_t *)&ng->output_data.array[i * out_size];
  1006. circlebuf_pop_front(&ng->output_buffers[i],
  1007. ng->output_audio.data[i], out_size);
  1008. }
  1009. ng->output_audio.frames = info.frames;
  1010. ng->output_audio.timestamp = info.timestamp - ng->latency;
  1011. return &ng->output_audio;
  1012. }
  1013. static bool noise_suppress_method_modified(obs_properties_t *props,
  1014. obs_property_t *property,
  1015. obs_data_t *settings)
  1016. {
  1017. obs_property_t *p_suppress_level =
  1018. obs_properties_get(props, S_SUPPRESS_LEVEL);
  1019. obs_property_t *p_navfx_intensity =
  1020. obs_properties_get(props, S_NVAFX_INTENSITY);
  1021. const char *method = obs_data_get_string(settings, S_METHOD);
  1022. bool enable_level = strcmp(method, S_METHOD_SPEEX) == 0;
  1023. bool enable_intensity =
  1024. strcmp(method, S_METHOD_NVAFX_DENOISER) == 0 ||
  1025. strcmp(method, S_METHOD_NVAFX_DEREVERB) == 0 ||
  1026. strcmp(method, S_METHOD_NVAFX_DEREVERB_DENOISER) == 0;
  1027. obs_property_set_visible(p_suppress_level, enable_level);
  1028. obs_property_set_visible(p_navfx_intensity, enable_intensity);
  1029. UNUSED_PARAMETER(property);
  1030. return true;
  1031. }
  1032. static void noise_suppress_defaults_v1(obs_data_t *s)
  1033. {
  1034. obs_data_set_default_int(s, S_SUPPRESS_LEVEL, -30);
  1035. #if defined(LIBRNNOISE_ENABLED) && !defined(LIBSPEEXDSP_ENABLED)
  1036. obs_data_set_default_string(s, S_METHOD, S_METHOD_RNN);
  1037. #else
  1038. obs_data_set_default_string(s, S_METHOD, S_METHOD_SPEEX);
  1039. #endif
  1040. #if defined(LIBNVAFX_ENABLED)
  1041. obs_data_set_default_double(s, S_NVAFX_INTENSITY, 1.0);
  1042. #endif
  1043. }
  1044. static void noise_suppress_defaults_v2(obs_data_t *s)
  1045. {
  1046. obs_data_set_default_int(s, S_SUPPRESS_LEVEL, -30);
  1047. #if defined(LIBRNNOISE_ENABLED)
  1048. obs_data_set_default_string(s, S_METHOD, S_METHOD_RNN);
  1049. #else
  1050. obs_data_set_default_string(s, S_METHOD, S_METHOD_SPEEX);
  1051. #endif
  1052. #if defined(LIBNVAFX_ENABLED)
  1053. obs_data_set_default_double(s, S_NVAFX_INTENSITY, 1.0);
  1054. #endif
  1055. }
  1056. static obs_properties_t *noise_suppress_properties(void *data)
  1057. {
  1058. obs_properties_t *ppts = obs_properties_create();
  1059. #ifdef LIBNVAFX_ENABLED
  1060. struct noise_suppress_data *ng = (struct noise_suppress_data *)data;
  1061. #else
  1062. UNUSED_PARAMETER(data);
  1063. #endif
  1064. #if defined(LIBRNNOISE_ENABLED) && defined(LIBSPEEXDSP_ENABLED)
  1065. obs_property_t *method = obs_properties_add_list(
  1066. ppts, S_METHOD, TEXT_METHOD, OBS_COMBO_TYPE_LIST,
  1067. OBS_COMBO_FORMAT_STRING);
  1068. obs_property_list_add_string(method, TEXT_METHOD_SPEEX, S_METHOD_SPEEX);
  1069. obs_property_list_add_string(method, TEXT_METHOD_RNN, S_METHOD_RNN);
  1070. #ifdef LIBNVAFX_ENABLED
  1071. if (ng->nvafx_enabled) {
  1072. obs_property_list_add_string(method, TEXT_METHOD_NVAFX_DENOISER,
  1073. S_METHOD_NVAFX_DENOISER);
  1074. obs_property_list_add_string(method, TEXT_METHOD_NVAFX_DEREVERB,
  1075. S_METHOD_NVAFX_DEREVERB);
  1076. obs_property_list_add_string(
  1077. method, TEXT_METHOD_NVAFX_DEREVERB_DENOISER,
  1078. S_METHOD_NVAFX_DEREVERB_DENOISER);
  1079. }
  1080. #endif
  1081. obs_property_set_modified_callback(method,
  1082. noise_suppress_method_modified);
  1083. #endif
  1084. #ifdef LIBSPEEXDSP_ENABLED
  1085. obs_property_t *speex_slider = obs_properties_add_int_slider(
  1086. ppts, S_SUPPRESS_LEVEL, TEXT_SUPPRESS_LEVEL, SUP_MIN, SUP_MAX,
  1087. 1);
  1088. obs_property_int_set_suffix(speex_slider, " dB");
  1089. #endif
  1090. #ifdef LIBNVAFX_ENABLED
  1091. if (ng->nvafx_enabled) {
  1092. obs_properties_add_float_slider(ppts, S_NVAFX_INTENSITY,
  1093. TEXT_NVAFX_INTENSITY, 0.0f,
  1094. 1.0f, 0.01f);
  1095. }
  1096. unsigned int version = get_lib_version();
  1097. if (version && version < MIN_AFX_SDK_VERSION) {
  1098. obs_property_t *warning = obs_properties_add_text(
  1099. ppts, "deprecation", NULL, OBS_TEXT_INFO);
  1100. obs_property_text_set_info_type(warning, OBS_TEXT_INFO_WARNING);
  1101. obs_property_set_long_description(
  1102. warning, TEXT_METHOD_NVAFX_DEPRECATION);
  1103. }
  1104. #if defined(LIBRNNOISE_ENABLED) && defined(LIBSPEEXDSP_ENABLED)
  1105. if (!nvafx_loaded) {
  1106. obs_property_list_item_disable(method, 2, true);
  1107. obs_property_list_item_disable(method, 3, true);
  1108. obs_property_list_item_disable(method, 4, true);
  1109. }
  1110. #endif
  1111. #endif
  1112. return ppts;
  1113. }
  1114. struct obs_source_info noise_suppress_filter = {
  1115. .id = "noise_suppress_filter",
  1116. .type = OBS_SOURCE_TYPE_FILTER,
  1117. .output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_CAP_OBSOLETE,
  1118. .get_name = noise_suppress_name,
  1119. .create = noise_suppress_create,
  1120. .destroy = noise_suppress_destroy,
  1121. .update = noise_suppress_update,
  1122. .filter_audio = noise_suppress_filter_audio,
  1123. .get_defaults = noise_suppress_defaults_v1,
  1124. .get_properties = noise_suppress_properties,
  1125. };
  1126. struct obs_source_info noise_suppress_filter_v2 = {
  1127. .id = "noise_suppress_filter",
  1128. .version = 2,
  1129. .type = OBS_SOURCE_TYPE_FILTER,
  1130. .output_flags = OBS_SOURCE_AUDIO,
  1131. .get_name = noise_suppress_name,
  1132. .create = noise_suppress_create,
  1133. .destroy = noise_suppress_destroy,
  1134. .update = noise_suppress_update,
  1135. .filter_audio = noise_suppress_filter_audio,
  1136. .get_defaults = noise_suppress_defaults_v2,
  1137. .get_properties = noise_suppress_properties,
  1138. };