blob: 4c74a48126f1385083d522affd7db486e8385c4c [file] [log] [blame]
Richard Burton00553462021-11-10 16:27:14 +00001/*
Richard Burton4e002792022-05-04 09:45:02 +01002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
Richard Burton00553462021-11-10 16:27:14 +00003 * 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 */
Richard Burton00553462021-11-10 16:27:14 +000017#include "UseCaseHandler.hpp" /* Handlers for different user options. */
18#include "UseCaseCommonUtils.hpp" /* Utils functions. */
19#include "RNNoiseModel.hpp" /* Model class for running inference. */
20#include "InputFiles.hpp" /* For input audio clips. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010021#include "log_macros.h" /* Logging functions */
22#include "BufAttributes.hpp" /* Buffer attributes to be applied */
23
24namespace arm {
25 namespace app {
26 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
27 } /* namespace app */
28} /* namespace arm */
29
30extern uint8_t* GetModelPointer();
31extern size_t GetModelLen();
Richard Burton00553462021-11-10 16:27:14 +000032
33enum opcodes
34{
35 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
36 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
37 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
38 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
39 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clip features. */
40};
41
42static void DisplayMenu()
43{
44 printf("\n\n");
45 printf("User input required\n");
46 printf("Enter option number from:\n\n");
47 printf(" %u. Run noise reduction on the next WAV\n", MENU_OPT_RUN_INF_NEXT);
48 printf(" %u. Run noise reduction on a WAV at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
49 printf(" %u. Run noise reduction on all WAVs\n", MENU_OPT_RUN_INF_ALL);
50 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
51 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
52 printf(" Choice: ");
53 fflush(stdout);
54}
55
56static bool SetAppCtxClipIdx(arm::app::ApplicationContext& ctx, uint32_t idx)
57{
58 if (idx >= NUMBER_OF_FILES) {
59 printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n",
60 idx, NUMBER_OF_FILES);
61 return false;
62 }
63 ctx.Set<uint32_t>("clipIndex", idx);
64 return true;
65}
66
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010067void main_loop()
Richard Burton00553462021-11-10 16:27:14 +000068{
69 arm::app::RNNoiseModel model; /* Model wrapper object. */
70
71 bool executionSuccessful = true;
72 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
73
74 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010075 if (!model.Init(arm::app::tensorArena,
76 sizeof(arm::app::tensorArena),
77 GetModelPointer(),
78 GetModelLen())) {
Richard Burton00553462021-11-10 16:27:14 +000079 printf_err("Failed to initialise model\n");
80 return;
81 }
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010082
83#if !defined(ARM_NPU)
84 /* If it is not a NPU build check if the model contains a NPU operator */
85 if (model.ContainsEthosUOperator()) {
86 printf_err("No driver support for Ethos-U operator found in the model.\n");
87 return;
88 }
89#endif /* ARM_NPU */
90
Richard Burton00553462021-11-10 16:27:14 +000091 /* Instantiate application context. */
92 arm::app::ApplicationContext caseContext;
93
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010094 arm::app::Profiler profiler{"noise_reduction"};
Richard Burton00553462021-11-10 16:27:14 +000095 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Richard Burton00553462021-11-10 16:27:14 +000096 caseContext.Set<uint32_t>("numInputFeatures", g_NumInputFeatures);
97 caseContext.Set<uint32_t>("frameLength", g_FrameLength);
98 caseContext.Set<uint32_t>("frameStride", g_FrameStride);
99 caseContext.Set<arm::app::RNNoiseModel&>("model", model);
100 SetAppCtxClipIdx(caseContext, 0);
101
102#if defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM)
103 /* For this use case, for valid targets, we dump contents
104 * of the output tensor to a certain location in memory to
105 * allow offline tools to pick this data up. */
106 constexpr size_t memDumpMaxLen = MEM_DUMP_LEN;
107 uint8_t* memDumpBaseAddr = reinterpret_cast<uint8_t *>(MEM_DUMP_BASE_ADDR);
108 size_t memDumpBytesWritten = 0;
109 caseContext.Set<size_t>("MEM_DUMP_LEN", memDumpMaxLen);
110 caseContext.Set<uint8_t*>("MEM_DUMP_BASE_ADDR", memDumpBaseAddr);
111 caseContext.Set<size_t*>("MEM_DUMP_BYTE_WRITTEN", &memDumpBytesWritten);
112#endif /* defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM) */
113 /* Loop. */
114 do {
115 int menuOption = MENU_OPT_RUN_INF_NEXT;
116
117 if (bUseMenu) {
118 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100119 menuOption = arm::app::ReadUserInputAsInt();
Richard Burton00553462021-11-10 16:27:14 +0000120 printf("\n");
121 }
122 switch (menuOption) {
123 case MENU_OPT_RUN_INF_NEXT:
124 executionSuccessful = NoiseReductionHandler(caseContext, false);
125 break;
126 case MENU_OPT_RUN_INF_CHOSEN: {
127 printf(" Enter the audio clip IFM index [0, %d]: ", NUMBER_OF_FILES-1);
Richard Burton033c9152021-12-07 14:04:44 +0000128 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100129 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Richard Burton00553462021-11-10 16:27:14 +0000130 SetAppCtxClipIdx(caseContext, clipIndex);
131 executionSuccessful = NoiseReductionHandler(caseContext, false);
132 break;
133 }
134 case MENU_OPT_RUN_INF_ALL:
135 executionSuccessful = NoiseReductionHandler(caseContext, true);
136 break;
137 case MENU_OPT_SHOW_MODEL_INFO:
138 executionSuccessful = model.ShowModelInfoHandler();
139 break;
140 case MENU_OPT_LIST_AUDIO_CLIPS:
141 executionSuccessful = ListFilesHandler(caseContext);
142 break;
143 default:
144 printf("Incorrect choice, try again.");
145 break;
146 }
147 } while (executionSuccessful && bUseMenu);
148 info("Main loop terminated.\n");
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100149}