blob: 7d814a8387b4d2e959fa0e6ff9c0161b3d481423 [file] [log] [blame]
Richard Burton00553462021-11-10 16:27:14 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * 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 */
17#include "hal.h" /* Brings in platform definitions. */
18#include "UseCaseHandler.hpp" /* Handlers for different user options. */
19#include "UseCaseCommonUtils.hpp" /* Utils functions. */
20#include "RNNoiseModel.hpp" /* Model class for running inference. */
21#include "InputFiles.hpp" /* For input audio clips. */
22#include "RNNoiseProcess.hpp" /* Pre-processing class */
23
24enum opcodes
25{
26 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
27 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
28 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
29 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
30 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clip features. */
31};
32
33static void DisplayMenu()
34{
35 printf("\n\n");
36 printf("User input required\n");
37 printf("Enter option number from:\n\n");
38 printf(" %u. Run noise reduction on the next WAV\n", MENU_OPT_RUN_INF_NEXT);
39 printf(" %u. Run noise reduction on a WAV at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
40 printf(" %u. Run noise reduction on all WAVs\n", MENU_OPT_RUN_INF_ALL);
41 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
42 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
43 printf(" Choice: ");
44 fflush(stdout);
45}
46
47static bool SetAppCtxClipIdx(arm::app::ApplicationContext& ctx, uint32_t idx)
48{
49 if (idx >= NUMBER_OF_FILES) {
50 printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n",
51 idx, NUMBER_OF_FILES);
52 return false;
53 }
54 ctx.Set<uint32_t>("clipIndex", idx);
55 return true;
56}
57
58void main_loop(hal_platform& platform)
59{
60 arm::app::RNNoiseModel model; /* Model wrapper object. */
61
62 bool executionSuccessful = true;
63 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
64
65 /* Load the model. */
66 if (!model.Init()) {
67 printf_err("Failed to initialise model\n");
68 return;
69 }
70 /* Instantiate application context. */
71 arm::app::ApplicationContext caseContext;
72
73 arm::app::Profiler profiler{&platform, "noise_reduction"};
74 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
75
76 caseContext.Set<hal_platform&>("platform", platform);
77 caseContext.Set<uint32_t>("numInputFeatures", g_NumInputFeatures);
78 caseContext.Set<uint32_t>("frameLength", g_FrameLength);
79 caseContext.Set<uint32_t>("frameStride", g_FrameStride);
80 caseContext.Set<arm::app::RNNoiseModel&>("model", model);
81 SetAppCtxClipIdx(caseContext, 0);
82
83#if defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM)
84 /* For this use case, for valid targets, we dump contents
85 * of the output tensor to a certain location in memory to
86 * allow offline tools to pick this data up. */
87 constexpr size_t memDumpMaxLen = MEM_DUMP_LEN;
88 uint8_t* memDumpBaseAddr = reinterpret_cast<uint8_t *>(MEM_DUMP_BASE_ADDR);
89 size_t memDumpBytesWritten = 0;
90 caseContext.Set<size_t>("MEM_DUMP_LEN", memDumpMaxLen);
91 caseContext.Set<uint8_t*>("MEM_DUMP_BASE_ADDR", memDumpBaseAddr);
92 caseContext.Set<size_t*>("MEM_DUMP_BYTE_WRITTEN", &memDumpBytesWritten);
93#endif /* defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM) */
94 /* Loop. */
95 do {
96 int menuOption = MENU_OPT_RUN_INF_NEXT;
97
98 if (bUseMenu) {
99 DisplayMenu();
100 menuOption = arm::app::ReadUserInputAsInt(platform);
101 printf("\n");
102 }
103 switch (menuOption) {
104 case MENU_OPT_RUN_INF_NEXT:
105 executionSuccessful = NoiseReductionHandler(caseContext, false);
106 break;
107 case MENU_OPT_RUN_INF_CHOSEN: {
108 printf(" Enter the audio clip IFM index [0, %d]: ", NUMBER_OF_FILES-1);
Richard Burton033c9152021-12-07 14:04:44 +0000109 fflush(stdout);
Richard Burton00553462021-11-10 16:27:14 +0000110 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt(platform));
111 SetAppCtxClipIdx(caseContext, clipIndex);
112 executionSuccessful = NoiseReductionHandler(caseContext, false);
113 break;
114 }
115 case MENU_OPT_RUN_INF_ALL:
116 executionSuccessful = NoiseReductionHandler(caseContext, true);
117 break;
118 case MENU_OPT_SHOW_MODEL_INFO:
119 executionSuccessful = model.ShowModelInfoHandler();
120 break;
121 case MENU_OPT_LIST_AUDIO_CLIPS:
122 executionSuccessful = ListFilesHandler(caseContext);
123 break;
124 default:
125 printf("Incorrect choice, try again.");
126 break;
127 }
128 } while (executionSuccessful && bUseMenu);
129 info("Main loop terminated.\n");
130}