blob: 140359beffdd03c986b0e3f61c917b7f5d82f9cd [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burton4e002792022-05-04 09:45:02 +01002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +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 */
alexander3c798932021-03-26 21:42:19 +000017#include "InputFiles.hpp" /* For input data */
18#include "AdModel.hpp" /* Model class for running inference */
19#include "UseCaseCommonUtils.hpp" /* Utils functions */
20#include "UseCaseHandler.hpp" /* Handlers for different user options */
alexander31ae9f02022-02-10 16:15:54 +000021#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +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 signals */
30};
31
32static void DisplayMenu()
33{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010034 printf("\n");
35 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000036 printf("Enter option number from:\n\n");
37 printf(" %u. Classify next audio signal\n", MENU_OPT_RUN_INF_NEXT);
38 printf(" %u. Classify audio signal at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
39 printf(" %u. Run classification on all audio signals\n", MENU_OPT_RUN_INF_ALL);
40 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
41 printf(" %u. List audio signals\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
42 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010043 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000044}
45
46
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010047void main_loop()
alexander3c798932021-03-26 21:42:19 +000048{
49 arm::app::AdModel model; /* Model wrapper object. */
50
51 /* Load the model. */
52 if (!model.Init())
53 {
54 printf_err("failed to initialise model\n");
55 return;
56 }
57
58 /* Instantiate application context. */
59 arm::app::ApplicationContext caseContext;
60
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010061 arm::app::Profiler profiler{"ad"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010062 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000063 caseContext.Set<arm::app::Model&>("model", model);
64 caseContext.Set<uint32_t>("clipIndex", 0);
Richard Burton4e002792022-05-04 09:45:02 +010065 caseContext.Set<uint32_t>("frameLength", g_FrameLength);
66 caseContext.Set<uint32_t>("frameStride", g_FrameStride);
alexander3c798932021-03-26 21:42:19 +000067 caseContext.Set<float>("scoreThreshold", g_ScoreThreshold);
68 caseContext.Set<float>("trainingMean", g_TrainingMean);
69
70 /* Main program loop. */
71 bool executionSuccessful = true;
72 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
73
74 /* Loop. */
75 do {
76 int menuOption = MENU_OPT_RUN_INF_NEXT;
77 if (bUseMenu) {
78 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010079 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +000080 printf("\n");
81 }
82 switch (menuOption) {
83 case MENU_OPT_RUN_INF_NEXT:
84 executionSuccessful = ClassifyVibrationHandler(
85 caseContext,
86 caseContext.Get<uint32_t>("clipIndex"),
87 false);
88 break;
89 case MENU_OPT_RUN_INF_CHOSEN: {
90 printf(" Enter the data index [0, %d]: ",
91 NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010092 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000093 auto audioIndex = static_cast<uint32_t>(
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010094 arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +000095 executionSuccessful = ClassifyVibrationHandler(caseContext,
96 audioIndex,
97 false);
98 break;
99 }
100 case MENU_OPT_RUN_INF_ALL:
101 executionSuccessful = ClassifyVibrationHandler(
102 caseContext,
103 caseContext.Get<uint32_t>("clipIndex"),
104 true);
105 break;
106 case MENU_OPT_SHOW_MODEL_INFO:
107 executionSuccessful = model.ShowModelInfoHandler();
108 break;
109 case MENU_OPT_LIST_AUDIO_CLIPS:
110 executionSuccessful = ListFilesHandler(caseContext);
111 break;
112 default:
113 printf("Incorrect choice, try again.");
114 break;
115 }
116 } while (executionSuccessful && bUseMenu);
117 info("Main loop terminated.\n");
118}