Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates |
| 3 | * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0 |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 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 | */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 17 | #include "UseCaseHandler.hpp" |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 18 | #include "AudioUtils.hpp" |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 19 | #include "ImageUtils.hpp" |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 20 | #include "InputFiles.hpp" |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 21 | #include "RNNoiseFeatureProcessor.hpp" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 22 | #include "RNNoiseModel.hpp" |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 23 | #include "RNNoiseProcessing.hpp" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 24 | #include "UseCaseCommonUtils.hpp" |
| 25 | #include "hal.h" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 26 | #include "log_macros.h" |
| 27 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 28 | namespace arm { |
| 29 | namespace app { |
| 30 | |
| 31 | /** |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 32 | * @brief Helper function to increment current audio clip features index. |
| 33 | * @param[in,out] ctx Pointer to the application context object. |
| 34 | **/ |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 35 | static void IncrementAppCtxClipIdx(ApplicationContext& ctx); |
| 36 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 37 | /* Noise reduction inference handler. */ |
| 38 | bool NoiseReductionHandler(ApplicationContext& ctx, bool runAll) |
| 39 | { |
| 40 | constexpr uint32_t dataPsnTxtInfStartX = 20; |
| 41 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 42 | |
| 43 | /* Variables used for memory dumping. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 44 | size_t memDumpMaxLen = 0; |
| 45 | uint8_t* memDumpBaseAddr = nullptr; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 46 | size_t undefMemDumpBytesWritten = 0; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 47 | size_t* pMemDumpBytesWritten = &undefMemDumpBytesWritten; |
| 48 | if (ctx.Has("MEM_DUMP_LEN") && ctx.Has("MEM_DUMP_BASE_ADDR") && |
| 49 | ctx.Has("MEM_DUMP_BYTE_WRITTEN")) { |
| 50 | memDumpMaxLen = ctx.Get<size_t>("MEM_DUMP_LEN"); |
| 51 | memDumpBaseAddr = ctx.Get<uint8_t*>("MEM_DUMP_BASE_ADDR"); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 52 | pMemDumpBytesWritten = ctx.Get<size_t*>("MEM_DUMP_BYTE_WRITTEN"); |
| 53 | } |
| 54 | std::reference_wrapper<size_t> memDumpBytesWritten = std::ref(*pMemDumpBytesWritten); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 55 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 56 | |
| 57 | /* Get model reference. */ |
| 58 | auto& model = ctx.Get<RNNoiseModel&>("model"); |
| 59 | if (!model.IsInited()) { |
| 60 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | /* Populate Pre-Processing related parameters. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 65 | auto audioFrameLen = ctx.Get<uint32_t>("frameLength"); |
| 66 | auto audioFrameStride = ctx.Get<uint32_t>("frameStride"); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 67 | auto nrNumInputFeatures = ctx.Get<uint32_t>("numInputFeatures"); |
| 68 | |
| 69 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 70 | if (nrNumInputFeatures != inputTensor->bytes) { |
| 71 | printf_err("Input features size must be equal to input tensor size." |
| 72 | " Feature size = %" PRIu32 ", Tensor size = %zu.\n", |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 73 | nrNumInputFeatures, |
| 74 | inputTensor->bytes); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
| 78 | TfLiteTensor* outputTensor = model.GetOutputTensor(model.m_indexForModelOutput); |
| 79 | |
| 80 | /* Initial choice of index for WAV file. */ |
| 81 | auto startClipIdx = ctx.Get<uint32_t>("clipIndex"); |
| 82 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 83 | std::function<const int16_t*(const uint32_t)> audioAccessorFunc = GetAudioArray; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 84 | if (ctx.Has("features")) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 85 | audioAccessorFunc = ctx.Get<std::function<const int16_t*(const uint32_t)>>("features"); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 86 | } |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 87 | std::function<uint32_t(const uint32_t)> audioSizeAccessorFunc = GetAudioArraySize; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 88 | if (ctx.Has("featureSizes")) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 89 | audioSizeAccessorFunc = |
| 90 | ctx.Get<std::function<uint32_t(const uint32_t)>>("featureSizes"); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 91 | } |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 92 | std::function<const char*(const uint32_t)> audioFileAccessorFunc = GetFilename; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 93 | if (ctx.Has("featureFileNames")) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 94 | audioFileAccessorFunc = |
| 95 | ctx.Get<std::function<const char*(const uint32_t)>>("featureFileNames"); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 96 | } |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 97 | do { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 98 | hal_lcd_clear(COLOR_BLACK); |
Richard Burton | 9b8d67a | 2021-12-10 12:32:51 +0000 | [diff] [blame] | 99 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 100 | auto startDumpAddress = memDumpBaseAddr + memDumpBytesWritten; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 101 | auto currentIndex = ctx.Get<uint32_t>("clipIndex"); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 102 | |
| 103 | /* Creating a sliding window through the audio. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 104 | auto audioDataSlider = |
| 105 | audio::SlidingWindow<const int16_t>(audioAccessorFunc(currentIndex), |
| 106 | audioSizeAccessorFunc(currentIndex), |
| 107 | audioFrameLen, |
| 108 | audioFrameStride); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 109 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 110 | info("Running inference on input feature map %" PRIu32 " => %s\n", |
| 111 | currentIndex, |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 112 | audioFileAccessorFunc(currentIndex)); |
| 113 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 114 | memDumpBytesWritten += |
| 115 | DumpDenoisedAudioHeader(audioFileAccessorFunc(currentIndex), |
| 116 | (audioDataSlider.TotalStrides() + 1) * audioFrameLen, |
| 117 | memDumpBaseAddr + memDumpBytesWritten, |
| 118 | memDumpMaxLen - memDumpBytesWritten); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 119 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 120 | /* Set up pre and post-processing. */ |
| 121 | std::shared_ptr<rnn::RNNoiseFeatureProcessor> featureProcessor = |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 122 | std::make_shared<rnn::RNNoiseFeatureProcessor>(); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 123 | std::shared_ptr<rnn::FrameFeatures> frameFeatures = |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 124 | std::make_shared<rnn::FrameFeatures>(); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 125 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 126 | RNNoisePreProcess preProcess = |
| 127 | RNNoisePreProcess(inputTensor, featureProcessor, frameFeatures); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 128 | |
| 129 | std::vector<int16_t> denoisedAudioFrame(audioFrameLen); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 130 | RNNoisePostProcess postProcess = RNNoisePostProcess( |
| 131 | outputTensor, denoisedAudioFrame, featureProcessor, frameFeatures); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 132 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 133 | bool resetGRU = true; |
| 134 | |
| 135 | while (audioDataSlider.HasNext()) { |
| 136 | const int16_t* inferenceWindow = audioDataSlider.Next(); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 137 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 138 | if (!preProcess.DoPreProcess(inferenceWindow, audioFrameLen)) { |
| 139 | printf_err("Pre-processing failed."); |
| 140 | return false; |
| 141 | } |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 142 | |
| 143 | /* Reset or copy over GRU states first to avoid TFLu memory overlap issues. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 144 | if (resetGRU) { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 145 | model.ResetGruState(); |
| 146 | } else { |
| 147 | /* Copying gru state outputs to gru state inputs. |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 148 | * Call ResetGruState in between the sequence of inferences on unrelated input |
| 149 | * data. */ |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 150 | model.CopyGruStates(); |
| 151 | } |
| 152 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 153 | /* Strings for presentation/logging. */ |
| 154 | std::string str_inf{"Running inference... "}; |
| 155 | |
| 156 | /* Display message on the LCD - inference running. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 157 | hal_lcd_display_text(str_inf.c_str(), |
| 158 | str_inf.size(), |
| 159 | dataPsnTxtInfStartX, |
| 160 | dataPsnTxtInfStartY, |
| 161 | false); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 162 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 163 | info("Inference %zu/%zu\n", |
| 164 | audioDataSlider.Index() + 1, |
| 165 | audioDataSlider.TotalStrides() + 1); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 166 | |
| 167 | /* Run inference over this feature sliding window. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 168 | if (!RunInference(model, profiler)) { |
| 169 | printf_err("Inference failed."); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 170 | return false; |
| 171 | } |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 172 | resetGRU = false; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 173 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 174 | /* Carry out post-processing. */ |
| 175 | if (!postProcess.DoPostProcess()) { |
| 176 | printf_err("Post-processing failed."); |
| 177 | return false; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* Erase. */ |
| 181 | str_inf = std::string(str_inf.size(), ' '); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 182 | hal_lcd_display_text(str_inf.c_str(), |
| 183 | str_inf.size(), |
| 184 | dataPsnTxtInfStartX, |
| 185 | dataPsnTxtInfStartY, |
| 186 | false); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 187 | |
| 188 | if (memDumpMaxLen > 0) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 189 | /* Dump final post processed output to memory. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 190 | memDumpBytesWritten += |
| 191 | DumpOutputDenoisedAudioFrame(denoisedAudioFrame, |
| 192 | memDumpBaseAddr + memDumpBytesWritten, |
| 193 | memDumpMaxLen - memDumpBytesWritten); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | if (memDumpMaxLen > 0) { |
| 198 | /* Needed to not let the compiler complain about type mismatch. */ |
| 199 | size_t valMemDumpBytesWritten = memDumpBytesWritten; |
| 200 | info("Output memory dump of %zu bytes written at address 0x%p\n", |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 201 | valMemDumpBytesWritten, |
| 202 | startDumpAddress); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 205 | /* Finish by dumping the footer. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 206 | DumpDenoisedAudioFooter(memDumpBaseAddr + memDumpBytesWritten, |
| 207 | memDumpMaxLen - memDumpBytesWritten); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 208 | |
Richard Burton | 9b8d67a | 2021-12-10 12:32:51 +0000 | [diff] [blame] | 209 | info("All inferences for audio clip complete.\n"); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 210 | profiler.PrintProfilingResult(); |
| 211 | IncrementAppCtxClipIdx(ctx); |
| 212 | |
Ayaan Masood | 233cec0 | 2021-12-09 17:22:22 +0000 | [diff] [blame] | 213 | std::string clearString{' '}; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 214 | hal_lcd_display_text(clearString.c_str(), |
| 215 | clearString.size(), |
| 216 | dataPsnTxtInfStartX, |
| 217 | dataPsnTxtInfStartY, |
| 218 | false); |
Ayaan Masood | 233cec0 | 2021-12-09 17:22:22 +0000 | [diff] [blame] | 219 | |
| 220 | std::string completeMsg{"Inference complete!"}; |
| 221 | |
| 222 | /* Display message on the LCD - inference complete. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 223 | hal_lcd_display_text(completeMsg.c_str(), |
| 224 | completeMsg.size(), |
| 225 | dataPsnTxtInfStartX, |
| 226 | dataPsnTxtInfStartY, |
| 227 | false); |
Ayaan Masood | 233cec0 | 2021-12-09 17:22:22 +0000 | [diff] [blame] | 228 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 229 | } while (runAll && ctx.Get<uint32_t>("clipIndex") != startClipIdx); |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 234 | size_t DumpDenoisedAudioHeader(const char* filename, |
| 235 | size_t dumpSize, |
| 236 | uint8_t* memAddress, |
| 237 | size_t memSize) |
| 238 | { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 239 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 240 | if (memAddress == nullptr) { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | int32_t filenameLength = strlen(filename); |
| 245 | size_t numBytesWritten = 0; |
| 246 | size_t numBytesToWrite = 0; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 247 | int32_t dumpSizeByte = dumpSize * sizeof(int16_t); |
| 248 | bool overflow = false; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 249 | |
| 250 | /* Write the filename length */ |
| 251 | numBytesToWrite = sizeof(filenameLength); |
| 252 | if (memSize - numBytesToWrite > 0) { |
| 253 | std::memcpy(memAddress, &filenameLength, numBytesToWrite); |
| 254 | numBytesWritten += numBytesToWrite; |
| 255 | memSize -= numBytesWritten; |
| 256 | } else { |
| 257 | overflow = true; |
| 258 | } |
| 259 | |
| 260 | /* Write file name */ |
| 261 | numBytesToWrite = filenameLength; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 262 | if (memSize - numBytesToWrite > 0) { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 263 | std::memcpy(memAddress + numBytesWritten, filename, numBytesToWrite); |
| 264 | numBytesWritten += numBytesToWrite; |
| 265 | memSize -= numBytesWritten; |
| 266 | } else { |
| 267 | overflow = true; |
| 268 | } |
| 269 | |
| 270 | /* Write dumpSize in byte */ |
| 271 | numBytesToWrite = sizeof(dumpSizeByte); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 272 | if (memSize - numBytesToWrite > 0) { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 273 | std::memcpy(memAddress + numBytesWritten, &(dumpSizeByte), numBytesToWrite); |
| 274 | numBytesWritten += numBytesToWrite; |
| 275 | memSize -= numBytesWritten; |
| 276 | } else { |
| 277 | overflow = true; |
| 278 | } |
| 279 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 280 | if (false == overflow) { |
| 281 | info("Audio Clip dump header info (%zu bytes) written to %p\n", |
| 282 | numBytesWritten, |
| 283 | memAddress); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 284 | } else { |
| 285 | printf_err("Not enough memory to dump Audio Clip header.\n"); |
| 286 | } |
| 287 | |
| 288 | return numBytesWritten; |
| 289 | } |
| 290 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 291 | size_t DumpDenoisedAudioFooter(uint8_t* memAddress, size_t memSize) |
| 292 | { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 293 | if ((memAddress == nullptr) || (memSize < 4)) { |
| 294 | return 0; |
| 295 | } |
| 296 | const int32_t eofMarker = -1; |
| 297 | std::memcpy(memAddress, &eofMarker, sizeof(int32_t)); |
| 298 | |
| 299 | return sizeof(int32_t); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 300 | } |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 301 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 302 | size_t DumpOutputDenoisedAudioFrame(const std::vector<int16_t>& audioFrame, |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 303 | uint8_t* memAddress, |
| 304 | size_t memSize) |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 305 | { |
| 306 | if (memAddress == nullptr) { |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | size_t numByteToBeWritten = audioFrame.size() * sizeof(int16_t); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 311 | if (numByteToBeWritten > memSize) { |
| 312 | printf_err("Overflow error: Writing %zu of %zu bytes to memory @ 0x%p.\n", |
| 313 | memSize, |
| 314 | numByteToBeWritten, |
| 315 | memAddress); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 316 | numByteToBeWritten = memSize; |
| 317 | } |
| 318 | |
| 319 | std::memcpy(memAddress, audioFrame.data(), numByteToBeWritten); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 320 | info("Copied %zu bytes to %p\n", numByteToBeWritten, memAddress); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 321 | |
| 322 | return numByteToBeWritten; |
| 323 | } |
| 324 | |
| 325 | size_t DumpOutputTensorsToMemory(Model& model, uint8_t* memAddress, const size_t memSize) |
| 326 | { |
| 327 | const size_t numOutputs = model.GetNumOutputs(); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 328 | size_t numBytesWritten = 0; |
| 329 | uint8_t* ptr = memAddress; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 330 | |
| 331 | /* Iterate over all output tensors. */ |
| 332 | for (size_t i = 0; i < numOutputs; ++i) { |
| 333 | const TfLiteTensor* tensor = model.GetOutputTensor(i); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 334 | const auto* tData = tflite::GetTensorData<uint8_t>(tensor); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 335 | #if VERIFY_TEST_OUTPUT |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 336 | DumpTensor(tensor); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 337 | #endif /* VERIFY_TEST_OUTPUT */ |
| 338 | /* Ensure that we don't overflow the allowed limit. */ |
| 339 | if (numBytesWritten + tensor->bytes <= memSize) { |
| 340 | if (tensor->bytes > 0) { |
| 341 | std::memcpy(ptr, tData, tensor->bytes); |
| 342 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 343 | info("Copied %zu bytes for tensor %zu to 0x%p\n", tensor->bytes, i, ptr); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 344 | |
| 345 | numBytesWritten += tensor->bytes; |
| 346 | ptr += tensor->bytes; |
| 347 | } |
| 348 | } else { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 349 | printf_err("Error writing tensor %zu to memory @ 0x%p\n", i, memAddress); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | info("%zu bytes written to memory @ 0x%p\n", numBytesWritten, memAddress); |
| 355 | |
| 356 | return numBytesWritten; |
| 357 | } |
| 358 | |
| 359 | static void IncrementAppCtxClipIdx(ApplicationContext& ctx) |
| 360 | { |
| 361 | auto curClipIdx = ctx.Get<uint32_t>("clipIndex"); |
| 362 | if (curClipIdx + 1 >= NUMBER_OF_FILES) { |
| 363 | ctx.Set<uint32_t>("clipIndex", 0); |
| 364 | return; |
| 365 | } |
| 366 | ++curClipIdx; |
| 367 | ctx.Set<uint32_t>("clipIndex", curClipIdx); |
| 368 | } |
| 369 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 370 | } /* namespace app */ |
| 371 | } /* namespace arm */ |