blob: f6a3ec428613b961c9c51d89ce70bd9758a81916 [file] [log] [blame]
Richard Burton4e002792022-05-04 09:45:02 +01001/*
2 * Copyright (c) 2022 Arm Limited. All rights reserved.
3 * 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#include "RNNoiseProcessing.hpp"
18#include "log_macros.h"
19
20namespace arm {
21namespace app {
22
23 RNNoisePreProcess::RNNoisePreProcess(TfLiteTensor* inputTensor,
24 std::shared_ptr<rnn::RNNoiseFeatureProcessor> featureProcessor, std::shared_ptr<rnn::FrameFeatures> frameFeatures)
25 : m_inputTensor{inputTensor},
26 m_featureProcessor{featureProcessor},
27 m_frameFeatures{frameFeatures}
28 {}
29
30 bool RNNoisePreProcess::DoPreProcess(const void* data, size_t inputSize)
31 {
32 if (data == nullptr) {
33 printf_err("Data pointer is null");
34 return false;
35 }
36
37 auto input = static_cast<const int16_t*>(data);
38 this->m_audioFrame = rnn::vec1D32F(input, input + inputSize);
39 m_featureProcessor->PreprocessFrame(this->m_audioFrame.data(), inputSize, *this->m_frameFeatures);
40
41 QuantizeAndPopulateInput(this->m_frameFeatures->m_featuresVec,
42 this->m_inputTensor->params.scale, this->m_inputTensor->params.zero_point,
43 this->m_inputTensor);
44
45 debug("Input tensor populated \n");
46
47 return true;
48 }
49
50 void RNNoisePreProcess::QuantizeAndPopulateInput(rnn::vec1D32F& inputFeatures,
51 const float quantScale, const int quantOffset,
52 TfLiteTensor* inputTensor)
53 {
54 const float minVal = std::numeric_limits<int8_t>::min();
55 const float maxVal = std::numeric_limits<int8_t>::max();
56
57 auto* inputTensorData = tflite::GetTensorData<int8_t>(inputTensor);
58
59 for (size_t i=0; i < inputFeatures.size(); ++i) {
60 float quantValue = ((inputFeatures[i] / quantScale) + quantOffset);
61 inputTensorData[i] = static_cast<int8_t>(std::min<float>(std::max<float>(quantValue, minVal), maxVal));
62 }
63 }
64
65 RNNoisePostProcess::RNNoisePostProcess(TfLiteTensor* outputTensor,
66 std::vector<int16_t>& denoisedAudioFrame,
67 std::shared_ptr<rnn::RNNoiseFeatureProcessor> featureProcessor,
68 std::shared_ptr<rnn::FrameFeatures> frameFeatures)
69 : m_outputTensor{outputTensor},
70 m_denoisedAudioFrame{denoisedAudioFrame},
71 m_featureProcessor{featureProcessor},
72 m_frameFeatures{frameFeatures}
73 {
74 this->m_denoisedAudioFrameFloat.reserve(denoisedAudioFrame.size());
75 this->m_modelOutputFloat.resize(outputTensor->bytes);
76 }
77
78 bool RNNoisePostProcess::DoPostProcess()
79 {
80 const auto* outputData = tflite::GetTensorData<int8_t>(this->m_outputTensor);
81 auto outputQuantParams = GetTensorQuantParams(this->m_outputTensor);
82
83 for (size_t i = 0; i < this->m_outputTensor->bytes; ++i) {
84 this->m_modelOutputFloat[i] = (static_cast<float>(outputData[i]) - outputQuantParams.offset)
85 * outputQuantParams.scale;
86 }
87
88 this->m_featureProcessor->PostProcessFrame(this->m_modelOutputFloat,
89 *this->m_frameFeatures, this->m_denoisedAudioFrameFloat);
90
91 for (size_t i = 0; i < this->m_denoisedAudioFrame.size(); ++i) {
92 this->m_denoisedAudioFrame[i] = static_cast<int16_t>(
93 std::roundf(this->m_denoisedAudioFrameFloat[i]));
94 }
95
96 return true;
97 }
98
99} /* namespace app */
100} /* namespace arm */