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