Skip to content

Commit

Permalink
voice:
Browse files Browse the repository at this point in the history
adjust to properly detect voice availability (previous disabled config if MUSIC_KEY_PRESSING had zero length)
add a voice ini parser mode that allows reading (not writing) of what the old voice system would've read for an id in models
  • Loading branch information
somewhatlurker committed Sep 20, 2020
1 parent 6dfc826 commit d34f6f4
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/config/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,8 @@ int assign_int(void* ptr, const struct struct_map *map, int map_size)

if (MATCH_SECTION(SECTION_VOICE)) {
u16 val = atoi(value);
CONFIG_VoiceParse(val);
CONFIG_VoiceParse_WithMode(val, VOICE_PARSE_MATCH_CUSTOM_LINENUM);
val = current_voice_mapping.id;
if (current_voice_mapping.duration == 0 || val < CUSTOM_ALARM_ID) {
printf("%s: Music %s not found in voice.ini or below ID %d\n", section, value, CUSTOM_ALARM_ID);
return 0;
Expand Down
75 changes: 54 additions & 21 deletions src/config/voice.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,68 @@
const char SECTION_VOICE_GLOBAL[] = "global";
const char SECTION_VOICE_CUSTOM[] = "custom";

typedef struct { u16 id; voice_parse_mode mode; } ini_parse_data;

u16 custom_linenum;

static int ini_handler(void* user, const char* section, const char* name, const char* value)
{
(void) section;
u16 req_id = *((long*)user);
ini_parse_data* userdata = (ini_parse_data*)user;
u16 req_id = userdata->id;
u16 id = atoi(name);

// find end of label (k) and get duration (text after k) as u16
const char* ptr = value;
u8 k = 0;
u16 duration = 0;

while (*ptr && *ptr != ',') {
ptr++;
k++;
}
if (*ptr == ',') {
duration = atoi(ptr + 1);
}

if ( k && (req_id != MAX_VOICEMAP_ENTRIES) && (req_id == id) ) {
current_voice_mapping.duration = duration;
if (HAS_MUSIC_CONFIG)
strlcpy(tempstring, value, k+1); // return a requested mp3 label passed at *user to tempstring
return 1;
}
if (HAS_MUSIC_CONFIG) {
if ( req_id == MAX_VOICEMAP_ENTRIES ) {
if (MATCH_SECTION(SECTION_VOICE_GLOBAL)) {
Transmitter.voice_ini_entries = VOICE_INI_GLOBAL_ONLY;

if (userdata->mode == VOICE_PARSE_MATCH_CUSTOM_LINENUM) {
// match the line number of a custom alert to simulate old behaviour of forming an unordered array from all entries
if (MATCH_SECTION(SECTION_VOICE_CUSTOM)) {
if (custom_linenum == req_id) {
// old behaviour didn't bother to check the entry had duration set, so don't replicate that here
current_voice_mapping.id = id;
current_voice_mapping.duration = duration;
if (HAS_MUSIC_CONFIG)
strlcpy(tempstring, value, k+1); // return a requested mp3 label passed at *user to tempstring
}
if (MATCH_SECTION(SECTION_VOICE_CUSTOM)) {
// Initial count of custom voicemap entries
Transmitter.voice_ini_entries++;
return 1;
custom_linenum++;
}
return 1;
} else {
if ( k && (req_id != MAX_VOICEMAP_ENTRIES) && (req_id == id) ) {
current_voice_mapping.id = id;
current_voice_mapping.duration = duration;
if (HAS_MUSIC_CONFIG)
strlcpy(tempstring, value, k+1); // return a requested mp3 label passed at *user to tempstring
return 1;
}
if (HAS_MUSIC_CONFIG) {
if ( req_id == MAX_VOICEMAP_ENTRIES ) {
if (MATCH_SECTION(SECTION_VOICE_GLOBAL)) {
Transmitter.voice_ini_entries = VOICE_INI_GLOBAL_ONLY;
}
if (MATCH_SECTION(SECTION_VOICE_CUSTOM)) {
// Initial count of custom voicemap entries
Transmitter.voice_ini_entries++;
return 1;
}
return 0;
}
return 0;
}
}
return 1; // voice label ignored
}

const char* CONFIG_VoiceParse(unsigned id)
const char* CONFIG_VoiceParse_WithMode(unsigned id, voice_parse_mode mode)
{
#ifdef _DEVO12_TARGET_H_
static char filename[] = "media/voice.ini\0\0\0"; // placeholder for longer folder name
Expand All @@ -86,19 +108,30 @@ const char* CONFIG_VoiceParse(unsigned id)
#else
char filename[] = "media/voice.ini";
#endif

ini_parse_data userdata = { .id = id, .mode = mode };
if (id == MAX_VOICEMAP_ENTRIES) { // initial parse of voice.ini
if (CONFIG_IniParse(filename, ini_handler, &id))
userdata.mode = VOICE_PARSE_MATCH_ID; // VOICE_PARSE_MATCH_CUSTOM_LINENUM not supported for initial parse
if (CONFIG_IniParse(filename, ini_handler, &userdata))
tempstring[0] = '\0';
if (Transmitter.voice_ini_entries == VOICE_INI_EMPTY) {
printf("Failed to parse voice.ini\n");
Transmitter.audio_player = AUDIO_NONE; // disable external voice when no global voices are found in voice.ini
}
}
if ( (id < MAX_VOICEMAP_ENTRIES) ) {
if (CONFIG_IniParse(filename, ini_handler, &id)) {
// reset vars
current_voice_mapping.id = 0;
current_voice_mapping.duration = 0;
custom_linenum = 200; // reset this (not neat but works)
if (CONFIG_IniParse(filename, ini_handler, &userdata)) {
// ini handler will return tempstring with label of id and fill current_voice_mapping
}
}
return tempstring;
}

const char* CONFIG_VoiceParse(unsigned id) {
return CONFIG_VoiceParse_WithMode(id, VOICE_PARSE_MATCH_ID);
}
#endif
4 changes: 4 additions & 0 deletions src/config/voice.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef _VOICE_H_
#define _VOICE_H_

// VOICE_PARSE_MATCH_CUSTOM_LINENUM looks up what the old voice system would have in a given voice_map entry
typedef enum { VOICE_PARSE_MATCH_ID, VOICE_PARSE_MATCH_CUSTOM_LINENUM } voice_parse_mode;

const char* CONFIG_VoiceParse_WithMode(unsigned id, voice_parse_mode mode);
const char* CONFIG_VoiceParse(unsigned id);

#endif
2 changes: 1 addition & 1 deletion src/extended_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void AUDIO_CheckQueue() {
}
}

static int AUDIO_VoiceAvailable() {
int AUDIO_VoiceAvailable() {
#if defined BUILDTYPE_DEV
#if HAS_AUDIO_UART
if (!Transmitter.audio_uart)
Expand Down
1 change: 1 addition & 0 deletions src/extended_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void AUDIO_Init();
void AUDIO_SetVolume();
void AUDIO_CheckQueue();
int AUDIO_AddQueue(u16 music);
int AUDIO_VoiceAvailable();

extern struct QueueEntry audio_queue[AUDIO_QUEUE_LENGTH];
extern struct QueueEntry current_voice_mapping;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/128x64x1/voiceconfig_page.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void PAGE_VoiceconfigInit(int page)
{
(void)page;
PAGE_SetModal(0);
if ( !AUDIO_AddQueue(MUSIC_KEY_PRESSING) ) { // MUSIC_KEY_PRESSING is empty by default
if ( !AUDIO_VoiceAvailable() ) {
GUI_CreateLabelBox(&gui->msg, MSG_X, MSG_Y, 0, 0, &LABEL_FONT, GUI_Localize, NULL,
_tr_noop("External voice\ncurrently not\navailable"));
return;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/320x240x16/voiceconfig_page.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void PAGE_VoiceconfigInit(int page)
PAGE_SetModal(0);
PAGE_ShowHeader(PAGE_GetName(PAGEID_VOICECFG));

if ( !AUDIO_AddQueue(MUSIC_KEY_PRESSING) ) { // MUSIC_KEY_PRESSING is empty by default
if ( !AUDIO_VoiceAvailable() ) {
GUI_CreateLabelBox(&gui->msg, 20, 80, 280, 100, &NARROW_FONT, GUI_Localize, NULL,
_tr_noop("External voice\ncurrently not\navailable"));
return;
Expand Down

0 comments on commit d34f6f4

Please sign in to comment.