Skip to content

Commit

Permalink
perf: use direct file name matching to identify audio files
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Oct 5, 2023
1 parent f35a15b commit 27612b4
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 111 deletions.
9 changes: 9 additions & 0 deletions radio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ option(RAS "RAS (SWR) enabled" ON)
option(TEMPLATES "Model templates menu" OFF)
option(TRACE_SIMPGMSPACE "Turn on traces in simpgmspace.cpp" ON)
option(TRACE_LUA_INTERNALS "Turn on traces for Lua internals" OFF)
option(TRACE_AUDIO "Traces audio enabled" OFF)
option(DEBUG_SEGGER_RTT "Debug output to Segger RTT" OFF)
option(DEBUG_WINDOWS "Turn on windows traces" OFF)
option(DEBUG_YAML "Turn on YAML traces" OFF)
Expand Down Expand Up @@ -294,6 +295,12 @@ if(TRACE_LUA_INTERNALS)
add_definitions(-DTRACE_LUA_INTERNALS_ENABLED)
endif()

if(TRACE_AUDIO)
add_definitions(-DTRACE_AUDIO)
set(DEBUG ON)
set(DEBUG_TRACE_BUFFER ON)
endif()

if(DEBUG_SEGGER_RTT)
add_definitions(-DDEBUG_SEGGER_RTT)
set(SRC ${SRC} ${THIRDPARTY_DIR}/Segger_RTT/RTT/SEGGER_RTT.c)
Expand Down Expand Up @@ -396,6 +403,8 @@ set(SRC
trainer.cpp
model_init.cpp
serial.cpp
audio.cpp
model_audio.cpp
sbus.cpp
input_mapping.cpp
inactivity_timer.cpp
Expand Down
127 changes: 24 additions & 103 deletions radio/src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
#include "opentx.h"
#include <math.h>

#include "strhelpers.h"
#include "switches.h"

#if defined(LIBOPENUI)
#include "libopenui.h"
#endif

#include "model_audio.h"

extern RTOS_MUTEX_HANDLE audioMutex;

const int16_t sineValues[] =
Expand Down Expand Up @@ -306,82 +309,17 @@ void referenceSystemAudioFiles()
}
}

const char * const suffixes[] = { "-off", "-on" };

char *getModelAudioPath(char *path)
{
strcpy(path, SOUNDS_PATH "/");
strncpy(path + SOUNDS_PATH_LNG_OFS, currentLanguagePack->id, 2);
char *buf = strcat_currentmodelname(path + sizeof(SOUNDS_PATH), ' ');

if (!isFileAvailable(path)) {
buf = strcat_currentmodelname(path + sizeof(SOUNDS_PATH), 0);
}

*buf++ = '/';
*buf = '\0';
return buf;
}

void getFlightmodeAudioFile(char * filename, int index, unsigned int event)
{
char * str = getModelAudioPath(filename);
char * tmp = strcatFlightmodeName(str, index);
strcpy(tmp, suffixes[event]);
strcat(tmp, SOUNDS_EXT);
}

void getSwitchAudioFile(char * filename, swsrc_t index)
{
char * str = getModelAudioPath(filename);

if (index <= SWSRC_LAST_SWITCH) {
div_t swinfo = switchInfo(index);
*str++ = 'S';
*str++ = switchGetLetter(swinfo.quot);
const char * positions[] = { "-up", "-mid", "-down" };
strcpy(str, positions[swinfo.rem]);
}
else {
div_t swinfo = div((int)(index - SWSRC_FIRST_MULTIPOS_SWITCH), XPOTS_MULTIPOS_COUNT);
*str++ = 'S';
*str++ = '1' + swinfo.quot;
*str++ = '1' + swinfo.rem;
*str = '\0';
}
strcat(str, SOUNDS_EXT);
}

void getLogicalSwitchAudioFile(char * filename, int index, unsigned int event)
{
char * str = getModelAudioPath(filename);

*str++ = 'L';
if (index >= 9) {
div_t qr = div(index+1, 10);
*str++ = '0' + qr.quot;
*str++ = '0' + qr.rem;
}
else {
*str++ = '1' + index;
}

strcpy(str, suffixes[event]);
strcat(str, SOUNDS_EXT);
}

void referenceModelAudioFiles()
{
char path[AUDIO_FILENAME_MAXLEN+1];
FILINFO fno;
DIR dir;
FILINFO fno;
char path[AUDIO_FILENAME_MAXLEN+1];

sdAvailableFlightmodeAudioFiles.reset();
sdAvailableSwitchAudioFiles.reset();
sdAvailableLogicalSwitchAudioFiles.reset();

char * filename = getModelAudioPath(path);
*(filename-1) = '\0';
getModelAudioPath(path);

FRESULT res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
Expand All @@ -392,46 +330,29 @@ void referenceModelAudioFiles()
bool found = false;

// Eliminates directories / non wav files
if (len < 5 || strcasecmp(fno.fname+len-4, SOUNDS_EXT) || (fno.fattrib & AM_DIR)) continue;
if (fno.fattrib & AM_DIR) continue;
if (len < sizeof(SOUNDS_EXT)) continue;

char* ext = fno.fname + len - (sizeof(SOUNDS_EXT) - 1);
if (strcasecmp(ext, SOUNDS_EXT)) continue;

TRACE("referenceModelAudioFiles(): using file: %s", fno.fname);

// Flight modes Audio Files <flightmodename>-[on|off].wav
for (int i=0; i<MAX_FLIGHT_MODES && !found; i++) {
for (int event=0; event<2; event++) {
getFlightmodeAudioFile(path, i, event);
// TRACE("referenceModelAudioFiles(): searching for %s in %s", filename, fno.fname);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableFlightmodeAudioFiles.setBit(INDEX_PHASE_AUDIO_FILE(i, event));
found = true;
TRACE("\tfound: %s", filename);
break;
}
}
int idx, event;
if (matchModeAudioFile(fno.fname, idx, event)) {
sdAvailableFlightmodeAudioFiles.setBit(INDEX_PHASE_AUDIO_FILE(idx, event));
continue;
}

// Switches Audio Files <switchname>-[up|mid|down].wav
for (unsigned i = 0; i <= MAX_SWITCH_POSITIONS && !found; i++) {
getSwitchAudioFile(path, SWSRC_FIRST_SWITCH+i);
// TRACE("referenceModelAudioFiles(): searching for %s in %s (%d)", path, fno.fname, i);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableSwitchAudioFiles.setBit(i);
found = true;
TRACE("\tfound: %s", filename);
}
if (matchSwitchAudioFile(fno.fname, idx)) {
sdAvailableSwitchAudioFiles.setBit(idx);
continue;
}

// Logical Switches Audio Files <switchname>-[on|off].wav
for (int i=0; i<MAX_LOGICAL_SWITCHES && !found; i++) {
for (int event=0; event<2; event++) {
getLogicalSwitchAudioFile(path, i, event);
// TRACE("referenceModelAudioFiles(): searching for %s in %s", filename, fno.fname);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableLogicalSwitchAudioFiles.setBit(INDEX_LOGICAL_SWITCH_AUDIO_FILE(i, event));
found = true;
TRACE("\tfound: %s", filename);
break;
}
}
if (matchLogicalSwitchAudioFile(fno.fname, idx, event)) {
sdAvailableLogicalSwitchAudioFiles.setBit(
INDEX_LOGICAL_SWITCH_AUDIO_FILE(idx, event));
continue;
}
}
f_closedir(&dir);
Expand Down Expand Up @@ -460,7 +381,7 @@ bool isAudioFileReferenced(uint32_t i, char * filename)
}
else if (category == SWITCH_AUDIO_CATEGORY) {
if (sdAvailableSwitchAudioFiles.getBit(index)) {
getSwitchAudioFile(filename, SWSRC_FIRST_SWITCH+index);
getSwitchAudioFile(filename, SWSRC_FIRST_SWITCH + index);
return true;
}
}
Expand Down
184 changes: 184 additions & 0 deletions radio/src/model_audio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

#include "model_audio.h"

#include "opentx.h"
#include "switches.h"

static const char * const _suffixes[] = { "-off", "-on" };

char* getModelAudioPath(char* path)
{
strcpy(path, SOUNDS_PATH "/");
strncpy(path + SOUNDS_PATH_LNG_OFS, currentLanguagePack->id, 2);
char *buf = strcat_currentmodelname(path + sizeof(SOUNDS_PATH), ' ');

if (!isFileAvailable(path)) {
buf = strcat_currentmodelname(path + sizeof(SOUNDS_PATH), 0);
}

*buf++ = '/';
*buf = '\0';
return buf;
}

void getFlightmodeAudioFile(char* path, int index, unsigned int event)
{
char* str = getModelAudioPath(path);
char* tmp = strcatFlightmodeName(str, index);
tmp = strAppend(tmp, _suffixes[event]);
strAppend(tmp, SOUNDS_EXT);
}

static const char * const _sw_positions[] = {
"-up", "-mid", "-down"
};

bool getSwitchAudioFile(char* path, swsrc_t index)
{
char* str = getModelAudioPath(path);

if (index <= SWSRC_LAST_SWITCH) {
div_t swinfo = switchInfo(index);
auto sw_name = switchGetName(swinfo.quot);
if (!sw_name) return false;
str = strAppend(str, sw_name);
str = strAppend(str, _sw_positions[swinfo.rem]);
}
else {
div_t swinfo = div((int)(index - SWSRC_FIRST_MULTIPOS_SWITCH), XPOTS_MULTIPOS_COUNT);
*str++ = 'S';
*str++ = '1' + swinfo.quot;
*str++ = '1' + swinfo.rem;
*str = '\0';
}
strAppend(str, SOUNDS_EXT);
return true;
}

void getLogicalSwitchAudioFile(char * filename, int index, unsigned int event)
{
char * str = getModelAudioPath(filename);

*str++ = 'L';
if (index >= 9) {
div_t qr = div(index + 1, 10);
*str++ = '0' + qr.quot;
*str++ = '0' + qr.rem;
}
else {
*str++ = '1' + index;
}

strcpy(str, _suffixes[event]);
strcat(str, SOUNDS_EXT);
}

// Flight modes Audio Files <flightmodename>-[on|off].wav
bool matchModeAudioFile(const char* filename, int& index, int& event)
{
for (int i = 0; i < MAX_FLIGHT_MODES; i++) {
auto *c = filename;
auto *fm_name = g_model.flightModeData[i].name;
auto fm_name_len = strnlen(fm_name, LEN_FLIGHT_MODE_NAME);
if (strncasecmp(c, fm_name, fm_name_len) != 0) continue;
c += fm_name_len;
for (int e = 0; e < DIM(_suffixes); e++) {
auto suffix_len = strlen(_suffixes[e]);
if (strncasecmp(c, _suffixes[e], suffix_len) != 0) continue;
c += suffix_len;
if (*c != '.') continue;
index = i;
event = e;
return true;
}
}
return false;
}

bool matchSwitchAudioFile(const char* filename, int& sw_pos)
{
// Switches Audio Files <switchname>-[up|mid|down].wav
for (int i = 0; i < switchGetMaxSwitches(); i++) {
auto *c = filename;
auto sw_name = switchGetName(i);
auto sw_name_len = strlen(sw_name);
if (strncasecmp(c, sw_name, sw_name_len) != 0) continue;
c += sw_name_len;
for (int pos = 0; pos < DIM(_sw_positions); pos++) {
auto pos_len = strlen(_sw_positions[pos]);
if (strncasecmp(c, _sw_positions[pos], pos_len) != 0) continue;
c += pos_len;
if (*c != '.') continue;
sw_pos = i * 3 + pos;
return true;
}
}

// match multipos switches
{
auto* c = filename;
if (*c != 'S' && *c != 's') return false;
c += 1;
if (*c < '1' || *c > '9') return false;
uint8_t xpot = uint8_t(*c++ - '1');
if (*c < '1' || *c > '9') return false;
uint8_t pos = uint8_t(*c++ - '1');
if (pos >= XPOTS_MULTIPOS_COUNT) return false;
if (*c != '.') return false;

for (int i = 0; i < MAX_POTS; i++) {
if (i != xpot) continue;
if (!IS_POT_MULTIPOS(i)) continue;
sw_pos = (MAX_SWITCHES * 3) + XPOTS_MULTIPOS_COUNT * xpot + pos;
return true;
}
}

return false;
}

// Logical Switches Audio Files <switchname>-[on|off].wav
bool matchLogicalSwitchAudioFile(const char* filename, int& index, int& event)
{
auto* c = filename;
if (*c != 'L' && *c != 'l') return false;
c += 1;
if (*c < '1' && *c > '9') return false;
int lsw = *c++ - '0';
if (*c >= '0' && *c <= '9') {
lsw = (lsw * 10) + (*c++ - '0');
}
if (*c != '-' || lsw < 1) return false;

for (int e = 0; e < DIM(_suffixes); e++) {
auto* s = c;
auto suffix_len = strlen(_suffixes[e]);
if (strncasecmp(s, _suffixes[e], suffix_len) != 0) continue;
s += suffix_len;
if (*s != '.') continue;
index = lsw - 1;
event = e;
return true;
}
return false;
}
Loading

0 comments on commit 27612b4

Please sign in to comment.