From 005534664e192cf909a11435c4bc4696b1f4c51f Mon Sep 17 00:00:00 2001 From: Richard Burton Date: Wed, 10 Nov 2021 16:27:14 +0000 Subject: MLECO-2354 MLECO-2355 MLECO-2356: Moving noise reduction to public repository * Use RNNoise model from PMZ * Add Noise reduction use-case Signed-off-by: Richard burton Change-Id: Ia8cc7ef102e22a5ff8bfbd3833594a4905a66057 --- source/use_case/noise_reduction/src/MainLoop.cc | 129 ++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 source/use_case/noise_reduction/src/MainLoop.cc (limited to 'source/use_case/noise_reduction/src/MainLoop.cc') diff --git a/source/use_case/noise_reduction/src/MainLoop.cc b/source/use_case/noise_reduction/src/MainLoop.cc new file mode 100644 index 0000000..ee0a61b --- /dev/null +++ b/source/use_case/noise_reduction/src/MainLoop.cc @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "hal.h" /* Brings in platform definitions. */ +#include "UseCaseHandler.hpp" /* Handlers for different user options. */ +#include "UseCaseCommonUtils.hpp" /* Utils functions. */ +#include "RNNoiseModel.hpp" /* Model class for running inference. */ +#include "InputFiles.hpp" /* For input audio clips. */ +#include "RNNoiseProcess.hpp" /* Pre-processing class */ + +enum opcodes +{ + MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ + MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ + MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ + MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ + MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clip features. */ +}; + +static void DisplayMenu() +{ + printf("\n\n"); + printf("User input required\n"); + printf("Enter option number from:\n\n"); + printf(" %u. Run noise reduction on the next WAV\n", MENU_OPT_RUN_INF_NEXT); + printf(" %u. Run noise reduction on a WAV at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); + printf(" %u. Run noise reduction on all WAVs\n", MENU_OPT_RUN_INF_ALL); + printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); + printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS); + printf(" Choice: "); + fflush(stdout); +} + +static bool SetAppCtxClipIdx(arm::app::ApplicationContext& ctx, uint32_t idx) +{ + if (idx >= NUMBER_OF_FILES) { + printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n", + idx, NUMBER_OF_FILES); + return false; + } + ctx.Set("clipIndex", idx); + return true; +} + +void main_loop(hal_platform& platform) +{ + arm::app::RNNoiseModel model; /* Model wrapper object. */ + + bool executionSuccessful = true; + constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; + + /* Load the model. */ + if (!model.Init()) { + printf_err("Failed to initialise model\n"); + return; + } + /* Instantiate application context. */ + arm::app::ApplicationContext caseContext; + + arm::app::Profiler profiler{&platform, "noise_reduction"}; + caseContext.Set("profiler", profiler); + + caseContext.Set("platform", platform); + caseContext.Set("numInputFeatures", g_NumInputFeatures); + caseContext.Set("frameLength", g_FrameLength); + caseContext.Set("frameStride", g_FrameStride); + caseContext.Set("model", model); + SetAppCtxClipIdx(caseContext, 0); + +#if defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM) + /* For this use case, for valid targets, we dump contents + * of the output tensor to a certain location in memory to + * allow offline tools to pick this data up. */ + constexpr size_t memDumpMaxLen = MEM_DUMP_LEN; + uint8_t* memDumpBaseAddr = reinterpret_cast(MEM_DUMP_BASE_ADDR); + size_t memDumpBytesWritten = 0; + caseContext.Set("MEM_DUMP_LEN", memDumpMaxLen); + caseContext.Set("MEM_DUMP_BASE_ADDR", memDumpBaseAddr); + caseContext.Set("MEM_DUMP_BYTE_WRITTEN", &memDumpBytesWritten); +#endif /* defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM) */ + /* Loop. */ + do { + int menuOption = MENU_OPT_RUN_INF_NEXT; + + if (bUseMenu) { + DisplayMenu(); + menuOption = arm::app::ReadUserInputAsInt(platform); + printf("\n"); + } + switch (menuOption) { + case MENU_OPT_RUN_INF_NEXT: + executionSuccessful = NoiseReductionHandler(caseContext, false); + break; + case MENU_OPT_RUN_INF_CHOSEN: { + printf(" Enter the audio clip IFM index [0, %d]: ", NUMBER_OF_FILES-1); + auto clipIndex = static_cast(arm::app::ReadUserInputAsInt(platform)); + SetAppCtxClipIdx(caseContext, clipIndex); + executionSuccessful = NoiseReductionHandler(caseContext, false); + break; + } + case MENU_OPT_RUN_INF_ALL: + executionSuccessful = NoiseReductionHandler(caseContext, true); + break; + case MENU_OPT_SHOW_MODEL_INFO: + executionSuccessful = model.ShowModelInfoHandler(); + break; + case MENU_OPT_LIST_AUDIO_CLIPS: + executionSuccessful = ListFilesHandler(caseContext); + break; + default: + printf("Incorrect choice, try again."); + break; + } + } while (executionSuccessful && bUseMenu); + info("Main loop terminated.\n"); +} \ No newline at end of file -- cgit v1.2.1