Use full form rather than abbreviations
[platform/core/multimedia/noise-suppression.git] / src / NoiseSuppression.cpp
1 /**
2  * Copyright (c) 2022-2023 Samsung Electronics Co., Ltd.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
30  */
31
32 #include <NoiseSuppression.h>
33 #include <noise-suppression.h>
34 #include <log.h>
35 #include <stdexcept>
36 #include <ns_define.h>
37
38 CNoiseSuppression::CNoiseSuppression() : _modelFrameSize(0), _inputFrameSize(0)
39 #if !defined ENABLE_AUDIO_RESAMPLING
40                                          , _modelSR(0), _inputSR(0)
41 #endif
42 , isInitialized(false)
43 {
44 }
45
46 CNoiseSuppression::~CNoiseSuppression()
47 {
48         deinit();
49 }
50
51 int CNoiseSuppression::init(unsigned int inputSR,
52                 unsigned int maxDelayMSec, unsigned int *frameSize)
53 {
54         unsigned int modelSR = 0;
55         unsigned int modelFrameSize = 0;
56
57         if (isInitialized) {
58                 _I("Already initialized, deinit and init again");
59                 deinit();
60         }
61
62         try {
63                 int ret = dnnManager.init(inputSR, &modelSR);
64                 if (NOISE_SUPPRESSION_ERROR_NONE != ret)
65                         throw std::runtime_error("dnnManager Initialization failed");
66
67                 _I("Input Sampling Rate: %u, Model Sampling Rate: %u",
68                                 inputSR, modelSR);
69 #if defined ENABLE_AUDIO_RESAMPLING
70                 if (NOISE_SUPPRESSION_ERROR_NONE != audioResamplerIn.init(inputSR, modelSR))
71                         throw std::runtime_error("audioResamplerIn.init() fail");
72
73                 if (NOISE_SUPPRESSION_ERROR_NONE != audioResamplerOut.init(modelSR, inputSR))
74                         throw std::runtime_error("audioResamplerIn.init() fail");
75 #else
76                 _modelSR = modelSR;
77                 _inputSR = inputSR;
78 #endif
79
80                 ret = audioProcessor.init(modelSR, maxDelayMSec, &modelFrameSize);
81                 if (NOISE_SUPPRESSION_ERROR_NONE != ret)
82                         throw std::runtime_error("AudioProcessor initialization failed");
83
84                 *frameSize = (unsigned int)((modelFrameSize * inputSR) / modelSR);
85
86                 _modelFrameSize = modelFrameSize;
87                 _inputFrameSize = *frameSize;
88                 isInitialized = true;
89         } catch (const std::runtime_error& e) {
90                 _E("%s", e.what());
91                 deinit();
92                 return NOISE_SUPPRESSION_ERROR_OPERATION_FAILED;
93         }
94
95         return NOISE_SUPPRESSION_ERROR_NONE;
96 }
97
98 void CNoiseSuppression::deinit()
99 {
100         dnnManager.deinit();
101 #if defined ENABLE_AUDIO_RESAMPLING
102         audioResamplerIn.deinit();
103         audioResamplerOut.deinit();
104 #endif
105         audioProcessor.deinit();
106
107         isInitialized = false;
108 }
109
110 void CNoiseSuppression::setNSLevel(int level)
111 {
112         audioProcessor.setNSLevel(level);
113         audioProcessor.setMinMaxAtteunation(0.000001);
114         float bestAtteunation = audioProcessor.getMinMaxAtteunation();
115
116         switch (level) {
117                 case NOISE_SUPPRESSION_LEVEL_VERY_LOW:
118                         bestAtteunation = 0.630957; // pow(10, (-2/10)) for 2dB
119                         break;
120                 case NOISE_SUPPRESSION_LEVEL_LOW:
121                         bestAtteunation = 0.501187; // pow(10, (-3/10))
122                         break;
123                 case NOISE_SUPPRESSION_LEVEL_MID:
124                         bestAtteunation = 0.316227; // pow(10, (-5/10))
125                         break;
126                 case NOISE_SUPPRESSION_LEVEL_HIGH:
127                         bestAtteunation = 0.158489; // pow(10, (-8/10)) for 9dB
128                         break;
129                 case NOISE_SUPPRESSION_LEVEL_VERY_HIGH:
130                         bestAtteunation = 0.000001;
131                         break;
132                 default:
133                         bestAtteunation = 0.000001; // This should not hit
134         }
135
136         audioProcessor.setMaxAtteunation(bestAtteunation);
137 }
138
139 int CNoiseSuppression::processAudio(const float *in, float *out)
140 {
141         retv_if(!isInitialized, NOISE_SUPPRESSION_ERROR_NOT_INITIALIZED);
142
143         float buf[_modelFrameSize] = {};
144         float *features = NULL;
145         int featureLen = 0;
146         float *gain = NULL;
147         float vadProbability = 0;
148         bool isSilence = false;
149
150 #if defined ENABLE_AUDIO_RESAMPLING
151         audioResamplerIn.resampleAudio(in, _inputFrameSize, buf, _modelFrameSize);
152 #else
153         std::copy(in, (in + _inputFrameSize), buf);
154 #endif
155
156         audioProcessor.audioToFeatures(&features, &featureLen, buf, &isSilence);
157
158         int dnnRet = NOISE_SUPPRESSION_ERROR_NONE;
159         if (!isSilence) {
160                 dnnRet = dnnManager.processFrame(features, featureLen, &gain,
161                                 &vadProbability);
162         }
163
164         if (NOISE_SUPPRESSION_ERROR_NONE != dnnRet) {
165                 _E("processFrame() failed with error Code: %d", dnnRet);
166         }
167         else
168                 audioProcessor.featuresToAudio(buf, gain, isSilence);
169
170 #if defined ENABLE_AUDIO_RESAMPLING
171         audioResamplerOut.resampleAudio(buf, _modelFrameSize, out, _inputFrameSize);
172 #else
173         std::copy(buf, (buf + _modelFrameSize), out);
174 #endif
175
176         delete_arr(features);
177         delete_arr(gain);
178         return NOISE_SUPPRESSION_ERROR_NONE;
179 }