blob: bc277da27a9812932cb2fc6a84ea020e9522fa81 [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 {
Liam Barry213a5432022-05-09 17:06:19 +010025namespace app {
26 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
27 namespace rnn {
28 extern uint8_t* GetModelPointer();
29 extern size_t GetModelLen();
30 } /* namespace rnn */
31} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010032} /* namespace arm */
33
Richard Burton00553462021-11-10 16:27:14 +000034enum opcodes
35{
36 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
37 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
38 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
39 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
40 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clip features. */
41};
42
43static void DisplayMenu()
44{
45 printf("\n\n");
46 printf("User input required\n");
47 printf("Enter option number from:\n\n");
48 printf(" %u. Run noise reduction on the next WAV\n", MENU_OPT_RUN_INF_NEXT);
49 printf(" %u. Run noise reduction on a WAV at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
50 printf(" %u. Run noise reduction on all WAVs\n", MENU_OPT_RUN_INF_ALL);
51 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
52 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
53 printf(" Choice: ");
54 fflush(stdout);
55}
56
57static bool SetAppCtxClipIdx(arm::app::ApplicationContext& ctx, uint32_t idx)
58{
59 if (idx >= NUMBER_OF_FILES) {
60 printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n",
61 idx, NUMBER_OF_FILES);
62 return false;
63 }
64 ctx.Set<uint32_t>("clipIndex", idx);
65 return true;
66}
67
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010068void main_loop()
Richard Burton00553462021-11-10 16:27:14 +000069{
70 arm::app::RNNoiseModel model; /* Model wrapper object. */
71
72 bool executionSuccessful = true;
73 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
74
75 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010076 if (!model.Init(arm::app::tensorArena,
77 sizeof(arm::app::tensorArena),
Liam Barry213a5432022-05-09 17:06:19 +010078 arm::app::rnn::GetModelPointer(),
79 arm::app::rnn::GetModelLen())) {
Richard Burton00553462021-11-10 16:27:14 +000080 printf_err("Failed to initialise model\n");
81 return;
82 }
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010083
Richard Burton00553462021-11-10 16:27:14 +000084 /* Instantiate application context. */
85 arm::app::ApplicationContext caseContext;
86
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010087 arm::app::Profiler profiler{"noise_reduction"};
Richard Burton00553462021-11-10 16:27:14 +000088 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Liam Barry213a5432022-05-09 17:06:19 +010089 caseContext.Set<uint32_t>("numInputFeatures", arm::app::rnn::g_NumInputFeatures);
90 caseContext.Set<uint32_t>("frameLength", arm::app::rnn::g_FrameLength);
91 caseContext.Set<uint32_t>("frameStride", arm::app::rnn::g_FrameStride);
Richard Burton00553462021-11-10 16:27:14 +000092 caseContext.Set<arm::app::RNNoiseModel&>("model", model);
93 SetAppCtxClipIdx(caseContext, 0);
94
95#if defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM)
96 /* For this use case, for valid targets, we dump contents
97 * of the output tensor to a certain location in memory to
98 * allow offline tools to pick this data up. */
99 constexpr size_t memDumpMaxLen = MEM_DUMP_LEN;
100 uint8_t* memDumpBaseAddr = reinterpret_cast<uint8_t *>(MEM_DUMP_BASE_ADDR);
101 size_t memDumpBytesWritten = 0;
102 caseContext.Set<size_t>("MEM_DUMP_LEN", memDumpMaxLen);
103 caseContext.Set<uint8_t*>("MEM_DUMP_BASE_ADDR", memDumpBaseAddr);
104 caseContext.Set<size_t*>("MEM_DUMP_BYTE_WRITTEN", &memDumpBytesWritten);
105#endif /* defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM) */
106 /* Loop. */
107 do {
108 int menuOption = MENU_OPT_RUN_INF_NEXT;
109
110 if (bUseMenu) {
111 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100112 menuOption = arm::app::ReadUserInputAsInt();
Richard Burton00553462021-11-10 16:27:14 +0000113 printf("\n");
114 }
115 switch (menuOption) {
116 case MENU_OPT_RUN_INF_NEXT:
117 executionSuccessful = NoiseReductionHandler(caseContext, false);
118 break;
119 case MENU_OPT_RUN_INF_CHOSEN: {
120 printf(" Enter the audio clip IFM index [0, %d]: ", NUMBER_OF_FILES-1);
Richard Burton033c9152021-12-07 14:04:44 +0000121 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100122 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Richard Burton00553462021-11-10 16:27:14 +0000123 SetAppCtxClipIdx(caseContext, clipIndex);
124 executionSuccessful = NoiseReductionHandler(caseContext, false);
125 break;
126 }
127 case MENU_OPT_RUN_INF_ALL:
128 executionSuccessful = NoiseReductionHandler(caseContext, true);
129 break;
130 case MENU_OPT_SHOW_MODEL_INFO:
131 executionSuccessful = model.ShowModelInfoHandler();
132 break;
133 case MENU_OPT_LIST_AUDIO_CLIPS:
134 executionSuccessful = ListFilesHandler(caseContext);
135 break;
136 default:
137 printf("Incorrect choice, try again.");
138 break;
139 }
140 } while (executionSuccessful && bUseMenu);
141 info("Main loop terminated.\n");
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100142}