Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 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 | */ |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 17 | #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. */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 21 | #include "log_macros.h" |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 22 | |
| 23 | enum 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 | |
| 32 | static 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 | |
| 46 | static 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 Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 57 | void main_loop() |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 58 | { |
| 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 Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 72 | arm::app::Profiler profiler{"noise_reduction"}; |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 73 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 74 | 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 Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 97 | menuOption = arm::app::ReadUserInputAsInt(); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 98 | 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 Burton | 033c915 | 2021-12-07 14:04:44 +0000 | [diff] [blame] | 106 | fflush(stdout); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 107 | auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt()); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 108 | 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 | } |