1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup feature_processor
18  * @{
19  *
20  * @brief Defines the basic functions for FeatureProcessor, including the supported data types
21  * and other related configuration parameters.
22  *
23  * @since 2.2
24  * @version 1.0
25  */
26 
27 /**
28  * @file noise_reduction_processor.h
29  *
30  * @brief Defines NoiseReductionProcessor that reduces noise after estimating the noise based on the
31  * frequency-domain data.
32  *
33  * @since 2.2
34  * @version 1.0
35  */
36 
37 #ifndef AUDIO_PREPROCESS_NOISE_REDUCTION_PROCESSOR_H
38 #define AUDIO_PREPROCESS_NOISE_REDUCTION_PROCESSOR_H
39 
40 #include <cstdint>
41 #include <memory>
42 
43 #include "feature_processor.h"
44 
45 namespace OHOS {
46 namespace AI {
47 namespace Feature {
48 /**
49  * @brief Specifies the structure for the NoiseReductionProcessor configuration.
50  *
51  * @since 2.2
52  * @version 1.0
53  */
54 struct NoiseReductionConfig : FeatureProcessorConfig {
55     /** Indicates whether to execute the energy gain after noise reduction. */
56     bool enablePcanGain;
57     /** Number of smooth bits. Before signal processing, the value is multiplied by 2 to the power of
58      * <b>smoothingBits</b> for signal smoothing. */
59     int16_t smoothingBits;
60     /** Number of gain bits. The value is multiplied by 2 to the power of <b>gainBits</b> to achieve the gain effect. */
61     int16_t gainBits;
62     /** Number of signal shifts. This parameter is used to reduce the semaphore difference.
63      * The value ranges from <b>0</b> to <b>32</b>. */
64     uint16_t correctionBits;
65     /** Number of frequency-domain channels for noise reduction. The maximum value is {@link MAX_NUM_CHANNELS}. */
66     size_t numChannels;
67     /** Smoothing coefficient for even channels. The value ranges from <b>0.0</b> to <b>1.0</b>. */
68     float evenSmoothing;
69     /** Smoothing coefficient for odd channels. The value ranges from <b>0.0</b> to <b>1.0</b>. */
70     float oddSmoothing;
71     /** Signal reservation ratio. The value ranges from <b>0.0</b> to <b>1.0</b>.
72      * Value <b>1.0</b> indicates that signals are not filtered,
73      * and value <b>0.0</b> indicates that signals are all filtered. */
74     float minSignalRemaining;
75     /** Gain normalization index. The value ranges from <b>0.0</b> to <b>1.0</b>.
76      * Value <b>0.0</b> indicates no gain, and value <b>1.0</b> indicates full gain. */
77     float strength;
78     /** Normalization offset. The value must be greater than <b>0.0</b>. */
79     float offset;
80 };
81 
82 /**
83  * @brief Defines the functions for NoiseReductionProcessor.
84  *
85  * @since 2.2
86  * @version 1.0
87  */
88 class NoiseReductionProcessor : public FeatureProcessor {
89 public:
90     /**
91      * @brief Defines the constructor for NoiseReductionProcessor.
92      *
93      * @since 2.2
94      * @version 1.0
95      */
96     NoiseReductionProcessor();
97 
98     /**
99      * @brief Defines deconstructor for NoiseReductionProcessor.
100      *
101      * @since 2.2
102      * @version 1.0
103      */
104     virtual ~NoiseReductionProcessor();
105 
106     /**
107      * @brief Initializes NoiseReductionProcessor.
108      *
109      * @param config Indicates the pointer to the basic configuration of FeatureProcessor.
110      * The caller needs to pass in a pointer address defined by {@link NoiseReductionConfig}
111      * and release the pointer after using it.
112      * @return Returns {@link RETCODE_SUCCESS} if the operation is successful;
113      * returns {@link RETCODE_FAILURE} otherwise.
114      *
115      * @since 2.2
116      * @version 1.0
117      */
118     int32_t Init(const FeatureProcessorConfig *config) override;
119 
120     /**
121      * @brief Performs feature processing.
122      *
123      * @param input Indicates the input data for FeatureProcessor.
124      * The caller must pass in FeatureData of the UINT32 type defined by {@link DataType},
125      * besides the address and data length must meet the configuration requirements.
126      * @param output Indicates the output data for FeatureProcessor.
127      * The caller must pass in FeatureData of the UINT32 type defined by {@link DataType}.
128      * If and only if its address is empty and the data length is <b>0</b>,
129      * data will be filled by the FeatureProcessor.
130      * @return Returns {@link RETCODE_SUCCESS} if the operation is successful;
131      * returns {@link RETCODE_FAILURE} otherwise.
132      *
133      * @since 2.2
134      * @version 1.0
135      */
136     int32_t Process(const FeatureData &input, FeatureData &output) override;
137 
138     /**
139      * @brief Releases resources.
140      *
141      * @since 2.2
142      * @version 1.0
143      */
144     void Release() override;
145 
146 private:
147     class NoiseReductionImpl;
148     std::unique_ptr<NoiseReductionImpl> impl_;
149 };
150 } // namespace Feature
151 } // namespace AI
152 } // namespace OHOS
153 #endif // AUDIO_PREPROCESS_NOISE_REDUCTION_PROCESSOR_H
154 /** @} */