2 * Copyright (c) 2022-2023 Samsung Electronics Co., Ltd.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Samsung Electronics Co., Ltd. nor the names
16 * of its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <NoiseSuppression.h>
33 #include <noise-suppression.h>
36 #include <ns_define.h>
38 CNoiseSuppression::CNoiseSuppression() : _modelFrameSize(0), _inputFrameSize(0)
39 #if !defined ENABLE_AUDIO_RESAMPLING
40 , _modelSR(0), _inputSR(0)
42 , isInitialized(false)
46 CNoiseSuppression::~CNoiseSuppression()
51 int CNoiseSuppression::init(unsigned int inputSR,
52 unsigned int maxDelayMSec, unsigned int *frameSize)
54 unsigned int modelSR = 0;
55 unsigned int modelFrameSize = 0;
58 _I("Already initialized, deinit and init again");
63 int ret = dnnManager.init(inputSR, &modelSR);
64 if (NOISE_SUPPRESSION_ERROR_NONE != ret)
65 throw std::runtime_error("dnnManager Initialization failed");
67 _I("Input Sampling Rate: %u, Model Sampling Rate: %u",
69 #if defined ENABLE_AUDIO_RESAMPLING
70 if (NOISE_SUPPRESSION_ERROR_NONE != audioResamplerIn.init(inputSR, modelSR))
71 throw std::runtime_error("audioResamplerIn.init() fail");
73 if (NOISE_SUPPRESSION_ERROR_NONE != audioResamplerOut.init(modelSR, inputSR))
74 throw std::runtime_error("audioResamplerIn.init() fail");
80 ret = audioProcessor.init(modelSR, maxDelayMSec, &modelFrameSize);
81 if (NOISE_SUPPRESSION_ERROR_NONE != ret)
82 throw std::runtime_error("AudioProcessor initialization failed");
84 *frameSize = (unsigned int)((modelFrameSize * inputSR) / modelSR);
86 _modelFrameSize = modelFrameSize;
87 _inputFrameSize = *frameSize;
89 } catch (const std::runtime_error& e) {
92 return NOISE_SUPPRESSION_ERROR_OPERATION_FAILED;
95 return NOISE_SUPPRESSION_ERROR_NONE;
98 void CNoiseSuppression::deinit()
101 #if defined ENABLE_AUDIO_RESAMPLING
102 audioResamplerIn.deinit();
103 audioResamplerOut.deinit();
105 audioProcessor.deinit();
107 isInitialized = false;
110 void CNoiseSuppression::setNSLevel(int level)
112 audioProcessor.setNSLevel(level);
113 audioProcessor.setMinMaxAtteunation(0.000001);
114 float bestAtteunation = audioProcessor.getMinMaxAtteunation();
117 case NOISE_SUPPRESSION_LEVEL_VERY_LOW:
118 bestAtteunation = 0.630957; // pow(10, (-2/10)) for 2dB
120 case NOISE_SUPPRESSION_LEVEL_LOW:
121 bestAtteunation = 0.501187; // pow(10, (-3/10))
123 case NOISE_SUPPRESSION_LEVEL_MID:
124 bestAtteunation = 0.316227; // pow(10, (-5/10))
126 case NOISE_SUPPRESSION_LEVEL_HIGH:
127 bestAtteunation = 0.158489; // pow(10, (-8/10)) for 9dB
129 case NOISE_SUPPRESSION_LEVEL_VERY_HIGH:
130 bestAtteunation = 0.000001;
133 bestAtteunation = 0.000001; // This should not hit
136 audioProcessor.setMaxAtteunation(bestAtteunation);
139 int CNoiseSuppression::processAudio(const float *in, float *out)
141 retv_if(!isInitialized, NOISE_SUPPRESSION_ERROR_NOT_INITIALIZED);
143 float buf[_modelFrameSize] = {};
144 float *features = NULL;
147 float vadProbability = 0;
148 bool isSilence = false;
150 #if defined ENABLE_AUDIO_RESAMPLING
151 audioResamplerIn.resampleAudio(in, _inputFrameSize, buf, _modelFrameSize);
153 std::copy(in, (in + _inputFrameSize), buf);
156 audioProcessor.audioToFeatures(&features, &featureLen, buf, &isSilence);
158 int dnnRet = NOISE_SUPPRESSION_ERROR_NONE;
160 dnnRet = dnnManager.processFrame(features, featureLen, &gain,
164 if (NOISE_SUPPRESSION_ERROR_NONE != dnnRet) {
165 _E("processFrame() failed with error Code: %d", dnnRet);
168 audioProcessor.featuresToAudio(buf, gain, isSilence);
170 #if defined ENABLE_AUDIO_RESAMPLING
171 audioResamplerOut.resampleAudio(buf, _modelFrameSize, out, _inputFrameSize);
173 std::copy(buf, (buf + _modelFrameSize), out);
176 delete_arr(features);
178 return NOISE_SUPPRESSION_ERROR_NONE;