blob: 257f5cf47d508b37d1cb7063e70a463170c8dfcd [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
Richard Burton00553462021-11-10 16:27:14 +000083 /* Instantiate application context. */
84 arm::app::ApplicationContext caseContext;
85
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010086 arm::app::Profiler profiler{"noise_reduction"};
Richard Burton00553462021-11-10 16:27:14 +000087 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Richard Burton00553462021-11-10 16:27:14 +000088 caseContext.Set<uint32_t>("numInputFeatures", g_NumInputFeatures);
89 caseContext.Set<uint32_t>("frameLength", g_FrameLength);
90 caseContext.Set<uint32_t>("frameStride", g_FrameStride);
91 caseContext.Set<arm::app::RNNoiseModel&>("model", model);
92 SetAppCtxClipIdx(caseContext, 0);
93
94#if defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM)
95 /* For this use case, for valid targets, we dump contents
96 * of the output tensor to a certain location in memory to
97 * allow offline tools to pick this data up. */
98 constexpr size_t memDumpMaxLen = MEM_DUMP_LEN;
99 uint8_t* memDumpBaseAddr = reinterpret_cast<uint8_t *>(MEM_DUMP_BASE_ADDR);
100 size_t memDumpBytesWritten = 0;
101 caseContext.Set<size_t>("MEM_DUMP_LEN", memDumpMaxLen);
102 caseContext.Set<uint8_t*>("MEM_DUMP_BASE_ADDR", memDumpBaseAddr);
103 caseContext.Set<size_t*>("MEM_DUMP_BYTE_WRITTEN", &memDumpBytesWritten);
104#endif /* defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM) */
105 /* Loop. */
106 do {
107 int menuOption = MENU_OPT_RUN_INF_NEXT;
108
109 if (bUseMenu) {
110 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100111 menuOption = arm::app::ReadUserInputAsInt();
Richard Burton00553462021-11-10 16:27:14 +0000112 printf("\n");
113 }
114 switch (menuOption) {
115 case MENU_OPT_RUN_INF_NEXT:
116 executionSuccessful = NoiseReductionHandler(caseContext, false);
117 break;
118 case MENU_OPT_RUN_INF_CHOSEN: {
119 printf(" Enter the audio clip IFM index [0, %d]: ", NUMBER_OF_FILES-1);
Richard Burton033c9152021-12-07 14:04:44 +0000120 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100121 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Richard Burton00553462021-11-10 16:27:14 +0000122 SetAppCtxClipIdx(caseContext, clipIndex);
123 executionSuccessful = NoiseReductionHandler(caseContext, false);
124 break;
125 }
126 case MENU_OPT_RUN_INF_ALL:
127 executionSuccessful = NoiseReductionHandler(caseContext, true);
128 break;
129 case MENU_OPT_SHOW_MODEL_INFO:
130 executionSuccessful = model.ShowModelInfoHandler();
131 break;
132 case MENU_OPT_LIST_AUDIO_CLIPS:
133 executionSuccessful = ListFilesHandler(caseContext);
134 break;
135 default:
136 printf("Incorrect choice, try again.");
137 break;
138 }
139 } while (executionSuccessful && bUseMenu);
140 info("Main loop terminated.\n");
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100141}