blob: 5fd7823c81ac0882a748da7f58db28548ae3e513 [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
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010059void main_loop()
Richard Burton00553462021-11-10 16:27:14 +000060{
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
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010074 arm::app::Profiler profiler{"noise_reduction"};
Richard Burton00553462021-11-10 16:27:14 +000075 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Richard Burton00553462021-11-10 16:27:14 +000076 caseContext.Set<uint32_t>("numInputFeatures", g_NumInputFeatures);
77 caseContext.Set<uint32_t>("frameLength", g_FrameLength);
78 caseContext.Set<uint32_t>("frameStride", g_FrameStride);
79 caseContext.Set<arm::app::RNNoiseModel&>("model", model);
80 SetAppCtxClipIdx(caseContext, 0);
81
82#if defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM)
83 /* For this use case, for valid targets, we dump contents
84 * of the output tensor to a certain location in memory to
85 * allow offline tools to pick this data up. */
86 constexpr size_t memDumpMaxLen = MEM_DUMP_LEN;
87 uint8_t* memDumpBaseAddr = reinterpret_cast<uint8_t *>(MEM_DUMP_BASE_ADDR);
88 size_t memDumpBytesWritten = 0;
89 caseContext.Set<size_t>("MEM_DUMP_LEN", memDumpMaxLen);
90 caseContext.Set<uint8_t*>("MEM_DUMP_BASE_ADDR", memDumpBaseAddr);
91 caseContext.Set<size_t*>("MEM_DUMP_BYTE_WRITTEN", &memDumpBytesWritten);
92#endif /* defined(MEM_DUMP_BASE_ADDR) && defined(MPS3_PLATFORM) */
93 /* Loop. */
94 do {
95 int menuOption = MENU_OPT_RUN_INF_NEXT;
96
97 if (bUseMenu) {
98 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010099 menuOption = arm::app::ReadUserInputAsInt();
Richard Burton00553462021-11-10 16:27:14 +0000100 printf("\n");
101 }
102 switch (menuOption) {
103 case MENU_OPT_RUN_INF_NEXT:
104 executionSuccessful = NoiseReductionHandler(caseContext, false);
105 break;
106 case MENU_OPT_RUN_INF_CHOSEN: {
107 printf(" Enter the audio clip IFM index [0, %d]: ", NUMBER_OF_FILES-1);
Richard Burton033c9152021-12-07 14:04:44 +0000108 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100109 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Richard Burton00553462021-11-10 16:27:14 +0000110 SetAppCtxClipIdx(caseContext, clipIndex);
111 executionSuccessful = NoiseReductionHandler(caseContext, false);
112 break;
113 }
114 case MENU_OPT_RUN_INF_ALL:
115 executionSuccessful = NoiseReductionHandler(caseContext, true);
116 break;
117 case MENU_OPT_SHOW_MODEL_INFO:
118 executionSuccessful = model.ShowModelInfoHandler();
119 break;
120 case MENU_OPT_LIST_AUDIO_CLIPS:
121 executionSuccessful = ListFilesHandler(caseContext);
122 break;
123 default:
124 printf("Incorrect choice, try again.");
125 break;
126 }
127 } while (executionSuccessful && bUseMenu);
128 info("Main loop terminated.\n");
129}