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