blob: b7f17bf59e286cb40057fef530d39dcd350daa59 [file] [log] [blame]
Richard Burton4e002792022-05-04 09:45:02 +01001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Richard Burton4e002792022-05-04 09:45:02 +01003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef RNNOISE_PROCESSING_HPP
18#define RNNOISE_PROCESSING_HPP
19
20#include "BaseProcessing.hpp"
21#include "Model.hpp"
22#include "RNNoiseFeatureProcessor.hpp"
23
24namespace arm {
25namespace app {
26
27 /**
28 * @brief Pre-processing class for Noise Reduction use case.
29 * Implements methods declared by BasePreProcess and anything else needed
30 * to populate input tensors ready for inference.
31 */
32 class RNNoisePreProcess : public BasePreProcess {
33
34 public:
35 /**
36 * @brief Constructor
37 * @param[in] inputTensor Pointer to the TFLite Micro input Tensor.
38 * @param[in/out] featureProcessor RNNoise specific feature extractor object.
39 * @param[in/out] frameFeatures RNNoise specific features shared between pre & post-processing.
40 *
41 **/
42 explicit RNNoisePreProcess(TfLiteTensor* inputTensor,
43 std::shared_ptr<rnn::RNNoiseFeatureProcessor> featureProcessor,
44 std::shared_ptr<rnn::FrameFeatures> frameFeatures);
45
46 /**
47 * @brief Should perform pre-processing of 'raw' input audio data and load it into
48 * TFLite Micro input tensors ready for inference
49 * @param[in] input Pointer to the data that pre-processing will work on.
50 * @param[in] inputSize Size of the input data.
51 * @return true if successful, false otherwise.
52 **/
53 bool DoPreProcess(const void* input, size_t inputSize) override;
54
55 private:
56 TfLiteTensor* m_inputTensor; /* Model input tensor. */
57 std::shared_ptr<rnn::RNNoiseFeatureProcessor> m_featureProcessor; /* RNNoise feature processor shared between pre & post-processing. */
58 std::shared_ptr<rnn::FrameFeatures> m_frameFeatures; /* RNNoise features shared between pre & post-processing. */
59 rnn::vec1D32F m_audioFrame; /* Audio frame cast to FP32 */
60
61 /**
62 * @brief Quantize the given features and populate the input Tensor.
63 * @param[in] inputFeatures Vector of floating point features to quantize.
64 * @param[in] quantScale Quantization scale for the inputTensor.
65 * @param[in] quantOffset Quantization offset for the inputTensor.
66 * @param[in,out] inputTensor TFLite micro tensor to populate.
67 **/
68 static void QuantizeAndPopulateInput(rnn::vec1D32F& inputFeatures,
69 float quantScale, int quantOffset,
70 TfLiteTensor* inputTensor);
71 };
72
73 /**
74 * @brief Post-processing class for Noise Reduction use case.
75 * Implements methods declared by BasePostProcess and anything else needed
76 * to populate result vector.
77 */
78 class RNNoisePostProcess : public BasePostProcess {
79
80 public:
81 /**
82 * @brief Constructor
83 * @param[in] outputTensor Pointer to the TFLite Micro output Tensor.
84 * @param[out] denoisedAudioFrame Vector to store the final denoised audio frame.
85 * @param[in/out] featureProcessor RNNoise specific feature extractor object.
86 * @param[in/out] frameFeatures RNNoise specific features shared between pre & post-processing.
87 **/
88 RNNoisePostProcess(TfLiteTensor* outputTensor,
89 std::vector<int16_t>& denoisedAudioFrame,
90 std::shared_ptr<rnn::RNNoiseFeatureProcessor> featureProcessor,
91 std::shared_ptr<rnn::FrameFeatures> frameFeatures);
92
93 /**
94 * @brief Should perform post-processing of the result of inference then
95 * populate result data for any later use.
96 * @return true if successful, false otherwise.
97 **/
98 bool DoPostProcess() override;
99
100 private:
101 TfLiteTensor* m_outputTensor; /* Model output tensor. */
102 std::vector<int16_t>& m_denoisedAudioFrame; /* Vector to store the final denoised frame. */
103 rnn::vec1D32F m_denoisedAudioFrameFloat; /* Internal vector to store the final denoised frame (FP32). */
104 std::shared_ptr<rnn::RNNoiseFeatureProcessor> m_featureProcessor; /* RNNoise feature processor shared between pre & post-processing. */
105 std::shared_ptr<rnn::FrameFeatures> m_frameFeatures; /* RNNoise features shared between pre & post-processing. */
106 std::vector<float> m_modelOutputFloat; /* Internal vector to store de-quantized model output. */
107
108 };
109
110} /* namespace app */
111} /* namespace arm */
112
113#endif /* RNNOISE_PROCESSING_HPP */