2004-03-19 22:15:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-05-05 10:32:46 +00:00
|
|
|
* Copyright (C) 2004 Jörg Hohensohn
|
2004-03-19 22:15:53 +00:00
|
|
|
*
|
|
|
|
* This module collects the Talkbox and voice UI functions.
|
|
|
|
* (Talkbox reads directory names from mp3 clips called thumbnails,
|
2004-05-09 09:41:23 +00:00
|
|
|
* the voice UI lets menus and screens "talk" from a voicefile in memory.
|
2004-03-19 22:15:53 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2004-03-19 22:15:53 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
2010-05-06 21:04:40 +00:00
|
|
|
#include "string-extra.h"
|
2004-03-19 22:15:53 +00:00
|
|
|
#include "file.h"
|
|
|
|
#include "system.h"
|
2007-03-09 09:21:11 +00:00
|
|
|
#include "kernel.h"
|
2004-04-06 07:06:59 +00:00
|
|
|
#include "settings.h"
|
2008-03-25 15:24:03 +00:00
|
|
|
#include "settings_list.h"
|
2020-05-22 12:52:01 +00:00
|
|
|
#include "splash.h"
|
2012-05-02 21:22:28 +00:00
|
|
|
#include "voice_thread.h"
|
2005-04-04 12:06:29 +00:00
|
|
|
#include "audio.h"
|
2004-03-19 22:15:53 +00:00
|
|
|
#include "lang.h"
|
|
|
|
#include "talk.h"
|
2008-10-15 06:38:51 +00:00
|
|
|
#include "metadata.h"
|
2010-05-26 03:14:30 +00:00
|
|
|
/*#define LOGF_ENABLE*/
|
2005-08-20 11:13:19 +00:00
|
|
|
#include "logf.h"
|
2004-09-26 09:25:59 +00:00
|
|
|
#include "bitswap.h"
|
2007-02-14 19:20:13 +00:00
|
|
|
#include "structec.h"
|
2011-08-14 15:37:05 +00:00
|
|
|
#include "plugin.h" /* plugin_get_buffer() */
|
2006-08-23 08:21:15 +00:00
|
|
|
#include "debug.h"
|
2014-02-02 13:48:32 +00:00
|
|
|
#include "panic.h"
|
Auto-Ranging Time Formatting For Menus (hh:mm:ss:mss)
Unifies time formatting in settings_list.c allows time format to
display as HH:MM:SS.MSS or any consecutive combination thereof
(hh:mm:ss, mm:ss, mm:ss.mss, ss.mss, hh, mm, ss ,mss)
works in INT and TABLE settings with the addition of flag 'F_TIME_SETTING'
Time is auto-ranged dependent on value
Adds talk_time_intervals to allow time values to be spoken similar to
display format: x Hours, x Minutes, x Seconds, x Milliseconds
Table lookups merged or removed from recording, clip meter and lcd timeout
-String_Choice replaced with TABLE_SETTING or INT_SETTING for these
functions as well, cleaned-up cfg_vals that get saved to cfgfile
RTL Languages ARE supported
Negative values ARE supported
Backlight on/off are now Always and Never to share formatter with LCD
Timeout
Added flag to allow ranged units to be locked to a minimum index
Added flag to allow leading zero to be supressed from the largest unit
merged talk_time_unit() and talk_time_intervals()
optimized time_split()
optimized format_time_auto()
Backlight time-out list same as original
Change-Id: I59027c62d3f2956bd16fdcc1a48b2ac32c084abd
2018-12-18 04:27:55 +00:00
|
|
|
#include "misc.h" /* time_split_units() */
|
2004-03-19 22:15:53 +00:00
|
|
|
|
|
|
|
/***************** Constants *****************/
|
|
|
|
|
2004-05-09 09:41:23 +00:00
|
|
|
#define QUEUE_SIZE 64 /* must be a power of two */
|
|
|
|
#define QUEUE_MASK (QUEUE_SIZE-1)
|
2004-08-01 23:34:44 +00:00
|
|
|
const char* const dir_thumbnail_name = "_dirname.talk";
|
2004-10-21 18:34:48 +00:00
|
|
|
const char* const file_thumbnail_ext = ".talk";
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2004-05-09 09:41:23 +00:00
|
|
|
/***************** Functional Macros *****************/
|
|
|
|
|
|
|
|
#define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
|
|
|
|
|
2004-10-12 16:43:22 +00:00
|
|
|
#define LOADED_MASK 0x80000000 /* MSB */
|
|
|
|
|
2020-07-21 12:54:14 +00:00
|
|
|
#ifndef DEFAULT_VOICE_LANG
|
2013-11-21 10:44:04 +00:00
|
|
|
#define DEFAULT_VOICE_LANG "english"
|
2020-07-21 12:54:14 +00:00
|
|
|
#endif
|
2013-11-21 10:44:04 +00:00
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
/***************** Data types *****************/
|
|
|
|
|
|
|
|
struct clip_entry /* one entry of the index table */
|
|
|
|
{
|
2004-05-09 09:41:23 +00:00
|
|
|
int offset; /* offset from start of voicefile file */
|
2004-03-19 22:15:53 +00:00
|
|
|
int size; /* size of the clip */
|
|
|
|
};
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
struct voicefile_header /* file format of our voice file */
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2004-05-09 09:41:23 +00:00
|
|
|
int version; /* version of the voicefile */
|
2007-08-05 19:19:39 +00:00
|
|
|
int target_id; /* the rockbox target the file was made for */
|
2004-04-03 20:52:24 +00:00
|
|
|
int table; /* offset to index table, (=header size) */
|
|
|
|
int id1_max; /* number of "normal" clips contained in above index */
|
|
|
|
int id2_max; /* number of "voice only" clips contained in above index */
|
2013-05-30 09:24:16 +00:00
|
|
|
/* The header is folled by the index tables (n*struct clip_entry),
|
|
|
|
* which is followed by the mp3/speex encoded clip data */
|
2004-03-19 22:15:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***************** Globals *****************/
|
|
|
|
|
2020-07-17 04:01:32 +00:00
|
|
|
#if MEMORYSIZE <= 2
|
2011-11-06 13:58:17 +00:00
|
|
|
/* On low memory swcodec targets the entire voice file wouldn't fit in memory
|
2020-07-15 23:40:55 +00:00
|
|
|
* together with codecs, so we load clips each time they are accessed. */
|
2014-02-02 13:48:32 +00:00
|
|
|
#define TALK_PROGRESSIVE_LOAD
|
|
|
|
/* 70+ clips should fit into 100k */
|
|
|
|
#define MAX_CLIP_BUFFER_SIZE (100000)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAX_CLIP_BUFFER_SIZE
|
2014-02-02 15:58:28 +00:00
|
|
|
/* 1GB should be enough for everybody; will be capped to voicefile size */
|
|
|
|
#define MAX_CLIP_BUFFER_SIZE (1<<30)
|
2010-09-01 00:08:50 +00:00
|
|
|
#endif
|
2014-02-02 15:58:28 +00:00
|
|
|
#define THUMBNAIL_RESERVE (50000)
|
2010-09-01 00:08:50 +00:00
|
|
|
|
2008-07-15 14:06:11 +00:00
|
|
|
/* Multiple thumbnails can be loaded back-to-back in this buffer. */
|
|
|
|
static volatile int thumbnail_buf_used SHAREDBSS_ATTR; /* length of data in
|
|
|
|
thumbnail buffer */
|
2013-05-30 09:24:16 +00:00
|
|
|
static struct voicefile_header voicefile; /* loaded voicefile */
|
2004-05-09 09:41:23 +00:00
|
|
|
static bool has_voicefile; /* a voicefile file is present */
|
2008-07-15 14:06:11 +00:00
|
|
|
static bool need_shutup; /* is there possibly any voice playing to be shutup */
|
2007-10-19 15:31:42 +00:00
|
|
|
static bool force_enqueue_next; /* enqueue next utterance even if enqueue is false */
|
2004-03-19 22:15:53 +00:00
|
|
|
static int queue_write; /* write index of queue, by application */
|
|
|
|
static int queue_read; /* read index of queue, by ISR context */
|
2020-05-21 17:55:32 +00:00
|
|
|
static enum talk_status talk_status = TALK_STATUS_OK;
|
2010-02-24 19:40:45 +00:00
|
|
|
/* protects queue_read, queue_write and thumbnail_buf_used */
|
2020-09-20 14:36:25 +00:00
|
|
|
static struct mutex queue_mutex SHAREDBSS_ATTR;
|
2008-07-15 14:06:11 +00:00
|
|
|
#define talk_queue_lock() ({ mutex_lock(&queue_mutex); })
|
|
|
|
#define talk_queue_unlock() ({ mutex_unlock(&queue_mutex); })
|
2004-05-09 09:41:23 +00:00
|
|
|
static int sent; /* how many bytes handed over to playback, owned by ISR */
|
2004-04-20 21:47:07 +00:00
|
|
|
static unsigned char curr_hd[3]; /* current frame header, for re-sync */
|
2006-02-23 21:29:29 +00:00
|
|
|
static unsigned char last_lang[MAX_FILENAME+1]; /* name of last used lang file (in talk_init) */
|
2006-02-26 16:07:34 +00:00
|
|
|
static bool talk_initialized; /* true if talk_init has been called */
|
2013-05-30 09:24:16 +00:00
|
|
|
static bool give_buffer_away; /* true if we should give the buffers away in shrink_callback if requested */
|
2007-10-19 15:31:42 +00:00
|
|
|
static int talk_temp_disable_count; /* if positive, temporarily disable voice UI (not saved) */
|
2014-02-02 13:48:32 +00:00
|
|
|
/* size of the voice data in the voice file and the actually allocated buffer
|
|
|
|
* for it. voicebuf_size is always smaller or equal to voicefile_size */
|
|
|
|
static unsigned long voicefile_size, voicebuf_size;
|
2013-05-30 09:24:16 +00:00
|
|
|
|
|
|
|
struct queue_entry /* one entry of the internal queue */
|
|
|
|
{
|
2014-02-02 15:58:28 +00:00
|
|
|
int handle; /* buflib handle to the clip data */
|
|
|
|
int length; /* total length of the clip */
|
2013-11-21 10:44:04 +00:00
|
|
|
int remaining; /* bytes that still need to be deoded */
|
2013-05-30 09:24:16 +00:00
|
|
|
};
|
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
static struct buflib_context clip_ctx;
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
struct clip_cache_metadata {
|
|
|
|
long tick;
|
|
|
|
int handle, voice_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int metadata_table_handle;
|
|
|
|
static unsigned max_clips;
|
|
|
|
static int cache_hits, cache_misses;
|
|
|
|
|
|
|
|
static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
|
|
|
|
static struct queue_entry silence, *last_clip;
|
2004-03-19 22:15:53 +00:00
|
|
|
|
|
|
|
/***************** Private implementation *****************/
|
|
|
|
|
2014-02-02 15:58:28 +00:00
|
|
|
static int index_handle, talk_handle;
|
2013-11-21 10:44:04 +00:00
|
|
|
|
|
|
|
static int move_callback(int handle, void *current, void *new)
|
|
|
|
{
|
2019-08-05 02:46:09 +00:00
|
|
|
(void)current;
|
2014-02-02 13:48:32 +00:00
|
|
|
if (handle == talk_handle && !buflib_context_relocate(&clip_ctx, new))
|
|
|
|
return BUFLIB_CB_CANNOT_MOVE;
|
2013-11-21 10:44:04 +00:00
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct mutex read_buffer_mutex;
|
2013-05-30 09:24:16 +00:00
|
|
|
|
2020-07-17 04:01:32 +00:00
|
|
|
static inline bool check_audio_status(void)
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:36:25 +00:00
|
|
|
/* ISR (voice_callback()) must not run during moving of the clip buffer,
|
2013-05-30 09:24:16 +00:00
|
|
|
* because the MAS may get out-of-sync */
|
|
|
|
static void sync_callback(int handle, bool sync_on)
|
|
|
|
{
|
|
|
|
(void) handle;
|
|
|
|
if (sync_on)
|
2013-11-21 10:44:04 +00:00
|
|
|
mutex_lock(&read_buffer_mutex);
|
2013-05-30 09:24:16 +00:00
|
|
|
else
|
2013-11-21 10:44:04 +00:00
|
|
|
mutex_unlock(&read_buffer_mutex);
|
2013-05-30 09:24:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
static ssize_t read_to_handle_ex(int fd, struct buflib_context *ctx, int handle,
|
|
|
|
int handle_offset, size_t count)
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
ssize_t ret;
|
|
|
|
mutex_lock(&read_buffer_mutex);
|
|
|
|
|
|
|
|
if (!ctx)
|
|
|
|
buf = core_get_data(handle);
|
|
|
|
else
|
|
|
|
buf = buflib_get_data(ctx, handle);
|
|
|
|
|
|
|
|
buf += handle_offset;
|
|
|
|
ret = read(fd, buf, count);
|
|
|
|
|
|
|
|
mutex_unlock(&read_buffer_mutex);
|
|
|
|
|
|
|
|
return ret;
|
2013-05-30 09:24:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
static ssize_t read_to_handle(int fd, int handle, int handle_offset, size_t count)
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
return read_to_handle_ex(fd, NULL, handle, handle_offset, count);
|
|
|
|
}
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
static int shrink_callback(int handle, unsigned hints, void *start, size_t old_size)
|
|
|
|
{
|
2019-08-05 02:46:09 +00:00
|
|
|
(void)start;(void)old_size;
|
2013-11-21 10:44:04 +00:00
|
|
|
int *h;
|
|
|
|
#if (MAX_CLIP_BUFFER_SIZE < (MEMORYSIZE<<20) || (MEMORYSIZE > 2))
|
2014-01-29 06:08:42 +00:00
|
|
|
/* on low-mem and when the voice buffer size is not limited (i.e.
|
|
|
|
* on 2MB HWCODEC) we effectively own the entire buffer because
|
|
|
|
* the voicefile takes up all RAM. This blocks other Rockbox parts
|
|
|
|
* from allocating, especially during bootup. Therefore always give
|
|
|
|
* up the buffer and reload when clips are played back. On high-mem
|
|
|
|
* or when the clip buffer is limited to a few 100K this provision is
|
|
|
|
* not necessary. */
|
2013-11-21 10:44:04 +00:00
|
|
|
if (give_buffer_away
|
2013-05-30 09:24:16 +00:00
|
|
|
&& (hints & BUFLIB_SHRINK_POS_MASK) == BUFLIB_SHRINK_POS_MASK)
|
2019-08-05 02:46:09 +00:00
|
|
|
#else
|
|
|
|
(void)hints;
|
2014-01-29 06:08:42 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
if (handle == talk_handle)
|
|
|
|
h = &talk_handle;
|
2019-08-05 02:46:09 +00:00
|
|
|
else if (handle == index_handle)
|
2013-11-21 10:44:04 +00:00
|
|
|
h = &index_handle;
|
2019-08-05 02:46:09 +00:00
|
|
|
else h = NULL;
|
2013-11-21 10:44:04 +00:00
|
|
|
|
|
|
|
mutex_lock(&read_buffer_mutex);
|
|
|
|
/* the clip buffer isn't usable without index table */
|
|
|
|
if (handle == index_handle && talk_handle > 0)
|
|
|
|
talk_handle = core_free(talk_handle);
|
2019-08-05 02:46:09 +00:00
|
|
|
if (h)
|
|
|
|
*h = core_free(handle);
|
2013-11-21 10:44:04 +00:00
|
|
|
mutex_unlock(&read_buffer_mutex);
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
return BUFLIB_CB_CANNOT_SHRINK;
|
|
|
|
}
|
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
static struct buflib_callbacks talk_ops = {
|
2013-05-30 09:24:16 +00:00
|
|
|
.move_callback = move_callback,
|
|
|
|
.sync_callback = sync_callback,
|
2013-11-21 10:44:04 +00:00
|
|
|
.shrink_callback = shrink_callback,
|
2014-01-26 18:14:47 +00:00
|
|
|
};
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
|
2004-04-06 07:06:59 +00:00
|
|
|
static int open_voicefile(void)
|
|
|
|
{
|
|
|
|
char buf[64];
|
2013-07-02 06:24:00 +00:00
|
|
|
char* p_lang = DEFAULT_VOICE_LANG; /* default */
|
2004-04-06 07:06:59 +00:00
|
|
|
|
|
|
|
if ( global_settings.lang_file[0] &&
|
2020-09-20 14:36:25 +00:00
|
|
|
global_settings.lang_file[0] != 0xff )
|
2004-04-06 07:06:59 +00:00
|
|
|
{ /* try to open the voice file of the selected language */
|
2005-12-05 22:44:42 +00:00
|
|
|
p_lang = (char *)global_settings.lang_file;
|
2004-04-06 07:06:59 +00:00
|
|
|
}
|
|
|
|
|
2006-10-03 10:38:27 +00:00
|
|
|
snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang);
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2004-04-06 07:06:59 +00:00
|
|
|
return open(buf, O_RDONLY);
|
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
static int id2index(int id)
|
2007-10-19 15:31:42 +00:00
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
int index = id;
|
2007-10-19 15:31:42 +00:00
|
|
|
if (id > VOICEONLY_DELIMITER)
|
2012-06-05 20:46:16 +00:00
|
|
|
{ /* voice-only entries use the second part of the table.
|
|
|
|
The first string comes after VOICEONLY_DELIMITER so we need to
|
|
|
|
substract VOICEONLY_DELIMITER + 1 */
|
2013-11-21 10:44:04 +00:00
|
|
|
index -= VOICEONLY_DELIMITER + 1;
|
|
|
|
if (index >= voicefile.id2_max)
|
2013-05-30 09:24:16 +00:00
|
|
|
return -1; /* must be newer than we have */
|
2013-11-21 10:44:04 +00:00
|
|
|
index += voicefile.id1_max; /* table 2 is behind table 1 */
|
2007-10-19 15:31:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* normal use of the first table */
|
2013-05-30 09:24:16 +00:00
|
|
|
if (id >= voicefile.id1_max)
|
|
|
|
return -1; /* must be newer than we have */
|
2007-10-19 15:31:42 +00:00
|
|
|
}
|
2013-11-21 10:44:04 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2014-02-03 11:33:33 +00:00
|
|
|
#ifndef TALK_PROGRESSIVE_LOAD
|
2014-02-02 13:48:32 +00:00
|
|
|
static int index2id(int index)
|
|
|
|
{
|
|
|
|
int id = index;
|
|
|
|
|
|
|
|
if (index >= voicefile.id2_max + voicefile.id1_max)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (index >= voicefile.id1_max)
|
|
|
|
{ /* must be voice-only if it exceeds table 1 */
|
|
|
|
id -= voicefile.id1_max;
|
|
|
|
/* The first string comes after VOICEONLY_DELIMITER so we need to
|
|
|
|
add VOICEONLY_DELIMITER + 1 */
|
|
|
|
id += VOICEONLY_DELIMITER + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
2014-02-03 11:33:33 +00:00
|
|
|
#endif
|
2014-02-02 13:48:32 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
static int free_oldest_clip(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
int oldest = 0;
|
2019-07-21 13:03:38 +00:00
|
|
|
bool thumb = false;
|
2013-11-21 10:44:04 +00:00
|
|
|
long age, now;
|
|
|
|
struct clip_entry* clipbuf;
|
|
|
|
struct clip_cache_metadata *cc = buflib_get_data(&clip_ctx, metadata_table_handle);
|
|
|
|
for(age = i = 0, now = current_tick; i < max_clips; i++)
|
|
|
|
{
|
2014-02-02 15:58:28 +00:00
|
|
|
if (cc[i].handle)
|
2013-11-21 10:44:04 +00:00
|
|
|
{
|
2019-07-21 13:03:38 +00:00
|
|
|
if (thumb && cc[i].voice_id == VOICEONLY_DELIMITER && (now - cc[i].tick) > age)
|
2014-02-02 15:58:28 +00:00
|
|
|
{
|
2019-07-21 13:03:38 +00:00
|
|
|
/* thumb clips are freed first */
|
2014-02-02 15:58:28 +00:00
|
|
|
age = now - cc[i].tick;
|
|
|
|
oldest = i;
|
|
|
|
}
|
2019-07-21 13:03:38 +00:00
|
|
|
else if (!thumb)
|
2014-02-02 15:58:28 +00:00
|
|
|
{
|
2019-07-21 13:03:38 +00:00
|
|
|
if (cc[i].voice_id == VOICEONLY_DELIMITER)
|
|
|
|
{
|
|
|
|
age = now - cc[i].tick;
|
|
|
|
oldest = i;
|
|
|
|
thumb = true;
|
|
|
|
}
|
|
|
|
else if ((now - cc[i].tick) > age && cc[i].voice_id != VOICE_PAUSE)
|
|
|
|
{
|
|
|
|
/* find the last-used clip but never consider silence */
|
|
|
|
age = now - cc[i].tick;
|
|
|
|
oldest = i;
|
|
|
|
}
|
2014-02-02 15:58:28 +00:00
|
|
|
}
|
2013-11-21 10:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-02 15:58:28 +00:00
|
|
|
/* free the last one if no oldest one could be determined */
|
2013-11-21 10:44:04 +00:00
|
|
|
cc = &cc[oldest];
|
|
|
|
cc->handle = buflib_free(&clip_ctx, cc->handle);
|
2014-02-02 15:58:28 +00:00
|
|
|
/* need to clear the LOADED bit too (not for thumb clips) */
|
|
|
|
if (cc->voice_id != VOICEONLY_DELIMITER)
|
|
|
|
{
|
|
|
|
clipbuf = core_get_data(index_handle);
|
|
|
|
clipbuf[id2index(cc->voice_id)].size &= ~LOADED_MASK;
|
|
|
|
}
|
2013-11-21 10:44:04 +00:00
|
|
|
return oldest;
|
|
|
|
}
|
2014-02-02 13:48:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* common code for load_initial_clips() and get_clip() */
|
|
|
|
static void add_cache_entry(int clip_handle, int table_index, int id)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
struct clip_cache_metadata *cc = buflib_get_data(&clip_ctx, metadata_table_handle);
|
|
|
|
|
|
|
|
if (table_index != -1)
|
|
|
|
{
|
|
|
|
/* explicit slot; use that */
|
|
|
|
cc = &cc[table_index];
|
|
|
|
if (cc->handle > 0) panicf("%s(): Slot already used", __func__);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* find an empty slot */
|
|
|
|
for(i = 0; cc[i].handle && i < max_clips; i++) ;
|
|
|
|
if (i == max_clips) /* no free slot in the cache table? */
|
|
|
|
i = free_oldest_clip();
|
|
|
|
cc = &cc[i];
|
|
|
|
}
|
|
|
|
cc->handle = clip_handle;
|
|
|
|
cc->tick = current_tick;
|
|
|
|
cc->voice_id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t read_clip_data(int fd, int index, int clip_handle)
|
|
|
|
{
|
|
|
|
struct clip_entry* clipbuf;
|
|
|
|
size_t clipsize;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
buflib_free(&clip_ctx, clip_handle);
|
|
|
|
return -1; /* open error */
|
|
|
|
}
|
|
|
|
|
|
|
|
clipbuf = core_get_data(index_handle);
|
|
|
|
/* this must not be called with LOADED_MASK set in clipsize */
|
|
|
|
clipsize = clipbuf[index].size;
|
|
|
|
lseek(fd, clipbuf[index].offset, SEEK_SET);
|
|
|
|
ret = read_to_handle_ex(fd, &clip_ctx, clip_handle, 0, clipsize);
|
|
|
|
|
|
|
|
if (ret < 0 || clipsize != (size_t)ret)
|
|
|
|
{
|
|
|
|
buflib_free(&clip_ctx, clip_handle);
|
|
|
|
return -2; /* read error */
|
|
|
|
}
|
|
|
|
|
|
|
|
clipbuf = core_get_data(index_handle);
|
|
|
|
clipbuf[index].size |= LOADED_MASK; /* mark as loaded */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_initial_clips(int fd)
|
|
|
|
{
|
2019-08-05 02:46:09 +00:00
|
|
|
#if defined(TALK_PROGRESSIVE_LOAD)
|
2014-02-02 13:48:32 +00:00
|
|
|
(void) fd;
|
2019-08-05 02:46:09 +00:00
|
|
|
#else
|
2014-02-02 13:48:32 +00:00
|
|
|
unsigned index, i;
|
|
|
|
unsigned num_clips = voicefile.id1_max + voicefile.id2_max;
|
|
|
|
|
|
|
|
for(index = i = 0; index < num_clips && i < max_clips; index++)
|
|
|
|
{
|
|
|
|
int handle;
|
|
|
|
struct clip_entry* clipbuf = core_get_data(index_handle);
|
|
|
|
size_t clipsize = clipbuf[index].size;
|
2014-02-02 15:58:28 +00:00
|
|
|
ssize_t ret;
|
|
|
|
|
2014-02-02 13:48:32 +00:00
|
|
|
if (clipsize == 0) /* clip not included in voicefile */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
handle = buflib_alloc(&clip_ctx, clipsize);
|
|
|
|
if (handle < 0)
|
|
|
|
break;
|
|
|
|
|
2014-02-02 15:58:28 +00:00
|
|
|
ret = read_clip_data(fd, index, handle);
|
|
|
|
if (ret < 0)
|
2014-02-02 13:48:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
add_cache_entry(handle, i++, index2id(index));
|
|
|
|
}
|
2013-11-21 10:44:04 +00:00
|
|
|
#endif
|
2014-02-02 13:48:32 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
/* fetch a clip from the voice file */
|
|
|
|
static int get_clip(long id, struct queue_entry *q)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int retval = -1;
|
|
|
|
struct clip_entry* clipbuf;
|
|
|
|
size_t clipsize;
|
|
|
|
|
|
|
|
index = id2index(id);
|
|
|
|
if (index == -1)
|
|
|
|
return -1;
|
2013-05-30 09:24:16 +00:00
|
|
|
|
|
|
|
clipbuf = core_get_data(index_handle);
|
2013-11-21 10:44:04 +00:00
|
|
|
clipsize = clipbuf[index].size;
|
2007-10-19 15:31:42 +00:00
|
|
|
if (clipsize == 0) /* clip not included in voicefile */
|
2013-05-30 09:24:16 +00:00
|
|
|
return -1;
|
2010-09-01 00:08:50 +00:00
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
if (!(clipsize & LOADED_MASK))
|
2010-09-01 00:08:50 +00:00
|
|
|
{ /* clip needs loading */
|
2013-11-21 10:44:04 +00:00
|
|
|
int fd, handle, oldest = -1;
|
2013-05-30 09:24:16 +00:00
|
|
|
ssize_t ret;
|
2013-07-02 06:24:00 +00:00
|
|
|
cache_misses++;
|
2013-11-21 10:44:04 +00:00
|
|
|
/* free clips from cache until this one succeeds to allocate */
|
|
|
|
while ((handle = buflib_alloc(&clip_ctx, clipsize)) < 0)
|
|
|
|
oldest = free_oldest_clip();
|
|
|
|
/* handle should now hold a valid alloc. Load from disk
|
|
|
|
* and insert into cache */
|
2013-05-30 09:24:16 +00:00
|
|
|
fd = open_voicefile();
|
2014-02-02 13:48:32 +00:00
|
|
|
ret = read_clip_data(fd, index, handle);
|
2013-05-30 09:24:16 +00:00
|
|
|
close(fd);
|
2014-02-02 13:48:32 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-11-21 10:44:04 +00:00
|
|
|
/* finally insert into metadata table */
|
2014-02-02 13:48:32 +00:00
|
|
|
add_cache_entry(handle, oldest, id);
|
2013-11-21 10:44:04 +00:00
|
|
|
retval = handle;
|
2007-10-19 15:31:42 +00:00
|
|
|
}
|
|
|
|
else
|
2013-11-21 10:44:04 +00:00
|
|
|
{ /* clip is in memory already; find where it was loaded */
|
2013-07-02 06:24:00 +00:00
|
|
|
cache_hits++;
|
2013-11-21 10:44:04 +00:00
|
|
|
struct clip_cache_metadata *cc;
|
|
|
|
static int i;
|
|
|
|
cc = buflib_get_data(&clip_ctx, metadata_table_handle);
|
|
|
|
for (i = 0; cc[i].voice_id != id || !cc[i].handle; i++) ;
|
|
|
|
cc[i].tick = current_tick; /* reset age */
|
2007-10-19 15:31:42 +00:00
|
|
|
clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
|
2013-11-21 10:44:04 +00:00
|
|
|
retval = cc[i].handle;
|
2007-10-19 15:31:42 +00:00
|
|
|
}
|
|
|
|
|
2014-02-02 15:58:28 +00:00
|
|
|
q->handle = retval;
|
2013-11-21 10:44:04 +00:00
|
|
|
q->length = clipsize;
|
|
|
|
q->remaining = clipsize;
|
|
|
|
return 0;
|
2007-10-19 15:31:42 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
static bool load_index_table(int fd, const struct voicefile_header *hdr)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
ssize_t ret;
|
2011-08-14 15:13:00 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (index_handle > 0) /* nothing to do? */
|
|
|
|
return true;
|
2004-10-12 16:43:22 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
ssize_t alloc_size = (hdr->id1_max + hdr->id2_max) * sizeof(struct clip_entry);
|
2013-11-21 10:44:04 +00:00
|
|
|
index_handle = core_alloc_ex("voice index", alloc_size, &talk_ops);
|
2013-05-30 09:24:16 +00:00
|
|
|
if (index_handle < 0)
|
|
|
|
return false;
|
2004-10-12 16:43:22 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
ret = read_to_handle(fd, index_handle, 0, alloc_size);
|
2011-08-14 15:13:00 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (ret == alloc_size)
|
2013-11-21 10:44:04 +00:00
|
|
|
{
|
2020-10-13 17:20:56 +00:00
|
|
|
#ifdef ROCKBOX_LITTLE_ENDIAN
|
|
|
|
struct clip_entry *buf;
|
2013-11-21 10:44:04 +00:00
|
|
|
buf = core_get_data(index_handle);
|
2013-05-30 09:24:16 +00:00
|
|
|
for (int i = 0; i < hdr->id1_max + hdr->id2_max; i++)
|
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
/* doesn't yield() */
|
2013-05-30 09:24:16 +00:00
|
|
|
structec_convert(&buf[i], "ll", 1, true);
|
|
|
|
}
|
2020-10-13 17:20:56 +00:00
|
|
|
#endif
|
2013-11-21 10:44:04 +00:00
|
|
|
}
|
|
|
|
else
|
2013-05-30 09:24:16 +00:00
|
|
|
index_handle = core_free(index_handle);
|
|
|
|
|
|
|
|
return ret == alloc_size;
|
|
|
|
}
|
2007-08-08 21:12:43 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
static bool load_header(int fd, struct voicefile_header *hdr)
|
|
|
|
{
|
|
|
|
ssize_t got_size = read(fd, hdr, sizeof(*hdr));
|
|
|
|
if (got_size != sizeof(*hdr))
|
|
|
|
return false;
|
2011-08-14 15:13:00 +00:00
|
|
|
|
2006-03-19 16:09:41 +00:00
|
|
|
#ifdef ROCKBOX_LITTLE_ENDIAN
|
2005-08-20 11:13:19 +00:00
|
|
|
logf("Byte swapping voice file");
|
2013-05-30 09:24:16 +00:00
|
|
|
structec_convert(&voicefile, "lllll", 1, true);
|
2005-08-20 11:13:19 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2014-02-02 13:48:32 +00:00
|
|
|
static bool create_clip_buffer(size_t max_size)
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
2014-02-02 13:48:32 +00:00
|
|
|
/* just allocate, populate on an as-needed basis later */
|
|
|
|
talk_handle = core_alloc_ex("voice data", max_size, &talk_ops);
|
2013-05-30 09:24:16 +00:00
|
|
|
if (talk_handle < 0)
|
2014-02-02 13:48:32 +00:00
|
|
|
goto alloc_err;
|
2010-09-01 00:08:50 +00:00
|
|
|
|
2014-02-02 13:48:32 +00:00
|
|
|
buflib_init(&clip_ctx, core_get_data(talk_handle), max_size);
|
2011-08-14 15:13:00 +00:00
|
|
|
|
2014-02-02 13:48:32 +00:00
|
|
|
return true;
|
2004-10-12 16:43:22 +00:00
|
|
|
|
2014-02-02 13:48:32 +00:00
|
|
|
alloc_err:
|
2020-05-21 17:55:32 +00:00
|
|
|
talk_status = TALK_STATUS_ERR_ALLOC;
|
2020-09-20 17:29:02 +00:00
|
|
|
if (index_handle > 0)
|
|
|
|
index_handle = core_free(index_handle);
|
2014-02-02 13:48:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-30 09:24:16 +00:00
|
|
|
|
|
|
|
/* load the voice file into the mp3 buffer */
|
|
|
|
static bool load_voicefile_index(int fd)
|
|
|
|
{
|
|
|
|
if (fd < 0) /* failed to open */
|
2020-05-21 17:55:32 +00:00
|
|
|
{
|
|
|
|
talk_status = TALK_STATUS_ERR_NOFILE;
|
2013-05-30 09:24:16 +00:00
|
|
|
return false;
|
2020-05-21 17:55:32 +00:00
|
|
|
}
|
2013-05-30 09:24:16 +00:00
|
|
|
|
|
|
|
/* load the header first */
|
|
|
|
if (!load_header(fd, &voicefile))
|
2020-05-21 17:55:32 +00:00
|
|
|
{
|
|
|
|
talk_status = TALK_STATUS_ERR_INCOMPATIBLE;
|
2013-05-30 09:24:16 +00:00
|
|
|
return false;
|
2020-05-21 17:55:32 +00:00
|
|
|
}
|
2013-05-30 09:24:16 +00:00
|
|
|
|
|
|
|
/* format check */
|
|
|
|
if (voicefile.table == sizeof(struct voicefile_header))
|
|
|
|
{
|
|
|
|
if (voicefile.version == VOICE_VERSION &&
|
2019-10-15 02:42:19 +00:00
|
|
|
voicefile.target_id == TARGET_ID &&
|
|
|
|
voicefile.id1_max == TALK_FINAL_ID &&
|
2020-07-21 04:34:37 +00:00
|
|
|
voicefile.id2_max == TALK_FINAL_ID_VOICEONLY - VOICEONLY_DELIMITER - 1)
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
|
|
|
if (load_index_table(fd, &voicefile))
|
|
|
|
return true;
|
|
|
|
}
|
2010-09-01 00:08:50 +00:00
|
|
|
}
|
2004-11-17 22:30:38 +00:00
|
|
|
|
2020-05-21 17:55:32 +00:00
|
|
|
talk_status = TALK_STATUS_ERR_INCOMPATIBLE;
|
2013-05-30 09:24:16 +00:00
|
|
|
logf("Incompatible voice file");
|
2019-10-15 02:42:19 +00:00
|
|
|
logf("version %d expected %d", voicefile.version, VOICE_VERSION);
|
|
|
|
logf("target_id %d expected %d", voicefile.target_id, TARGET_ID);
|
|
|
|
logf("id1_max %d expected %d", voicefile.id1_max, TALK_FINAL_ID);
|
|
|
|
logf("id2_max %d expected %d",
|
|
|
|
voicefile.id2_max, TALK_FINAL_ID_VOICEONLY - VOICEONLY_DELIMITER);
|
2013-05-30 09:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-02 13:48:32 +00:00
|
|
|
/* this function caps the voicefile buffer and allocates it. It can
|
|
|
|
* be called after talk_init(), e.g. when the voice was temporarily disabled.
|
|
|
|
* The buffer size has to be capped again each time because the available
|
|
|
|
* audio buffer changes over time */
|
|
|
|
static bool load_voicefile_data(int fd)
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
2014-02-02 13:48:32 +00:00
|
|
|
voicebuf_size = voicefile_size;
|
|
|
|
/* cap to the max. number of clips or the size of the available audio
|
|
|
|
* buffer which we grab. We leave some to the rest of the system.
|
|
|
|
* While that reduces our buffer size it improves the chance that
|
|
|
|
* other allocs succeed without disabling voice which would require
|
|
|
|
* reloading the voice from disk (as we do not shrink our buffer when
|
|
|
|
* other code attempts new allocs these would fail) */
|
2020-09-20 17:29:02 +00:00
|
|
|
size_t metadata_alloc_size;
|
2014-02-02 13:48:32 +00:00
|
|
|
ssize_t cap = MIN(MAX_CLIP_BUFFER_SIZE, audio_buffer_available() - (64<<10));
|
|
|
|
if (UNLIKELY(cap < 0))
|
|
|
|
{
|
|
|
|
logf("Not enough memory for voice. Disabling...\n");
|
2020-05-21 17:55:32 +00:00
|
|
|
talk_status = TALK_STATUS_ERR_OOM;
|
2014-02-02 13:48:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (voicebuf_size > (size_t)cap)
|
|
|
|
voicebuf_size = cap;
|
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
/* just allocate, populate on an as-needed basis later
|
|
|
|
* re-create the clip buffer to ensure clip_ctx is up-to-date */
|
|
|
|
if (talk_handle > 0)
|
|
|
|
talk_handle = core_free(talk_handle);
|
2014-02-02 13:48:32 +00:00
|
|
|
if (!create_clip_buffer(voicebuf_size))
|
|
|
|
return false;
|
2011-08-22 10:23:11 +00:00
|
|
|
|
2020-09-20 17:29:02 +00:00
|
|
|
/* the first alloc is the clip metadata table */
|
|
|
|
metadata_alloc_size = max_clips * sizeof(struct clip_cache_metadata);
|
|
|
|
metadata_table_handle = buflib_alloc(&clip_ctx, metadata_alloc_size);
|
|
|
|
memset(buflib_get_data(&clip_ctx, metadata_table_handle), 0, metadata_alloc_size);
|
|
|
|
|
2014-02-02 13:48:32 +00:00
|
|
|
load_initial_clips(fd);
|
|
|
|
/* make sure to have the silence clip, if available return value can
|
|
|
|
* be cached globally even for TALK_PROGRESSIVE_LOAD because the
|
|
|
|
* VOICE_PAUSE clip is specially handled */
|
|
|
|
get_clip(VOICE_PAUSE, &silence);
|
2004-10-12 16:43:22 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
return true;
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 04:01:32 +00:00
|
|
|
/* Use a static buffer to avoid difficulties with buflib during
|
|
|
|
* buffer passing to the voice_thread (swcodec). Clips can be played
|
|
|
|
in chunks so the size is not that important */
|
2014-02-03 11:43:05 +00:00
|
|
|
static unsigned char commit_buffer[2<<10];
|
2013-11-21 10:44:04 +00:00
|
|
|
|
|
|
|
static void* commit_transfer(struct queue_entry *qe, size_t *size)
|
|
|
|
{
|
|
|
|
void *buf = NULL; /* shut up gcc */
|
|
|
|
static unsigned char *bufpos = commit_buffer;
|
|
|
|
sent = qe->remaining;
|
|
|
|
sent = MIN((size_t)sent, sizeof(commit_buffer));
|
2014-02-02 15:58:28 +00:00
|
|
|
buf = buflib_get_data(&clip_ctx, qe->handle);
|
2013-11-21 10:44:04 +00:00
|
|
|
/* adjust buffer position to what has been played already */
|
|
|
|
buf += (qe->length - qe->remaining);
|
|
|
|
memcpy(bufpos, buf, sent);
|
|
|
|
*size = sent;
|
|
|
|
|
|
|
|
return commit_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool is_silence(struct queue_entry *qe)
|
|
|
|
{
|
2014-02-02 15:58:28 +00:00
|
|
|
if (silence.handle > 0) /* silence clip available? */
|
|
|
|
return (qe->handle == silence.handle);
|
2013-11-21 10:44:04 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-04 19:44:43 +00:00
|
|
|
/* called in ISR context (on HWCODEC) if mp3 data got consumed */
|
|
|
|
static void mp3_callback(const void** start, size_t* size)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
struct queue_entry *qe = &queue[queue_read];
|
2014-01-21 22:22:37 +00:00
|
|
|
/* voice_thread.c hints us how many of the buffer we provided it actually
|
|
|
|
* consumed. Because buffers have to be frame-aligned for speex
|
|
|
|
* it might be less than what we presented */
|
|
|
|
if (*size)
|
|
|
|
sent = *size;
|
2013-11-21 10:44:04 +00:00
|
|
|
qe->remaining -= sent; /* we completed this */
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
if (qe->remaining > 0) /* current clip not finished? */
|
2004-03-19 22:15:53 +00:00
|
|
|
{ /* feed the next 64K-1 chunk */
|
2013-11-21 10:44:04 +00:00
|
|
|
*start = commit_transfer(qe, size);
|
2004-03-19 22:15:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-21 10:44:04 +00:00
|
|
|
|
2008-07-15 14:06:11 +00:00
|
|
|
talk_queue_lock();
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
/* increment read position for the just played clip */
|
|
|
|
queue_read = (queue_read + 1) & QUEUE_MASK;
|
2004-11-18 22:59:20 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
if (QUEUE_LEVEL == 0)
|
|
|
|
{
|
2014-02-02 15:58:28 +00:00
|
|
|
if (!is_silence(last_clip))
|
2013-11-21 10:44:04 +00:00
|
|
|
{ /* add silence clip when queue runs empty playing a voice clip,
|
2014-02-02 15:58:28 +00:00
|
|
|
* only if the previous clip wasn't already silence */
|
2013-11-21 10:44:04 +00:00
|
|
|
queue[queue_write] = silence;
|
|
|
|
queue_write = (queue_write + 1) & QUEUE_MASK;
|
|
|
|
}
|
2013-05-30 09:24:16 +00:00
|
|
|
else
|
2013-11-21 10:44:04 +00:00
|
|
|
{
|
|
|
|
*size = 0; /* end of data */
|
|
|
|
}
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
2004-11-18 22:59:20 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
if (QUEUE_LEVEL != 0) /* queue is not empty? */
|
|
|
|
{ /* start next clip */
|
|
|
|
last_clip = &queue[queue_read];
|
|
|
|
*start = commit_transfer(last_clip, size);
|
|
|
|
curr_hd[0] = commit_buffer[1];
|
|
|
|
curr_hd[1] = commit_buffer[2];
|
|
|
|
curr_hd[2] = commit_buffer[3];
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2008-07-15 14:06:11 +00:00
|
|
|
talk_queue_unlock();
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
/***************** Public routines *****************/
|
|
|
|
|
2006-10-25 08:54:25 +00:00
|
|
|
/* stop the playback and the pending clips */
|
2007-10-19 15:31:42 +00:00
|
|
|
void talk_force_shutup(void)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2020-07-17 04:01:32 +00:00
|
|
|
/* Had nothing to do (was frame boundary or not our clip) */
|
2020-09-20 14:36:25 +00:00
|
|
|
voice_play_stop();
|
2008-07-15 14:06:11 +00:00
|
|
|
talk_queue_lock();
|
2004-05-09 09:41:23 +00:00
|
|
|
queue_write = queue_read = 0; /* reset the queue */
|
2008-07-15 14:06:11 +00:00
|
|
|
thumbnail_buf_used = 0;
|
|
|
|
talk_queue_unlock();
|
|
|
|
need_shutup = false;
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 13:08:36 +00:00
|
|
|
/* Shutup the voice, except if force_enqueue_next is set. */
|
2007-10-19 15:31:42 +00:00
|
|
|
void talk_shutup(void)
|
2007-08-06 13:08:36 +00:00
|
|
|
{
|
2008-07-15 14:06:11 +00:00
|
|
|
if (need_shutup && !force_enqueue_next)
|
2007-10-19 15:31:42 +00:00
|
|
|
talk_force_shutup();
|
2007-08-06 13:08:36 +00:00
|
|
|
}
|
2004-03-19 22:15:53 +00:00
|
|
|
|
|
|
|
/* schedule a clip, at the end or discard the existing queue */
|
2013-11-21 10:44:04 +00:00
|
|
|
static void queue_clip(struct queue_entry *clip, bool enqueue)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
struct queue_entry *qe;
|
2004-05-09 09:41:23 +00:00
|
|
|
int queue_level;
|
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
if (!enqueue)
|
2007-10-19 15:31:42 +00:00
|
|
|
talk_shutup(); /* cut off all the pending stuff */
|
2007-08-06 13:08:36 +00:00
|
|
|
/* Something is being enqueued, force_enqueue_next override is no
|
|
|
|
longer in effect. */
|
|
|
|
force_enqueue_next = false;
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
if (!clip->length)
|
2007-10-19 15:31:42 +00:00
|
|
|
return; /* safety check */
|
2008-07-15 14:06:11 +00:00
|
|
|
talk_queue_lock();
|
2004-05-09 09:41:23 +00:00
|
|
|
queue_level = QUEUE_LEVEL; /* check old level */
|
2013-11-21 10:44:04 +00:00
|
|
|
qe = &queue[queue_write];
|
2004-05-09 09:41:23 +00:00
|
|
|
|
|
|
|
if (queue_level < QUEUE_SIZE - 1) /* space left? */
|
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
queue[queue_write] = *clip;
|
2004-11-18 22:59:20 +00:00
|
|
|
queue_write = (queue_write + 1) & QUEUE_MASK;
|
2004-05-09 09:41:23 +00:00
|
|
|
}
|
2008-07-15 14:06:11 +00:00
|
|
|
talk_queue_unlock();
|
|
|
|
|
2004-05-09 09:41:23 +00:00
|
|
|
if (queue_level == 0)
|
|
|
|
{ /* queue was empty, we have to do the initial start */
|
2013-11-21 10:44:04 +00:00
|
|
|
size_t size;
|
|
|
|
void *buf = commit_transfer(qe, &size);
|
|
|
|
last_clip = qe;
|
2020-09-20 14:36:25 +00:00
|
|
|
voice_play_data(buf, size, mp3_callback);
|
2013-11-21 10:44:04 +00:00
|
|
|
curr_hd[0] = commit_buffer[1];
|
|
|
|
curr_hd[1] = commit_buffer[2];
|
|
|
|
curr_hd[2] = commit_buffer[3];
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
2008-07-15 14:06:11 +00:00
|
|
|
need_shutup = true;
|
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
return;
|
2004-11-17 22:30:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 06:14:58 +00:00
|
|
|
/* return if a voice codec is required or not */
|
|
|
|
static bool talk_voice_required(void)
|
|
|
|
{
|
|
|
|
return (has_voicefile) /* Voice file is available */
|
|
|
|
|| (global_settings.talk_dir_clip) /* Thumbnail clips are required */
|
|
|
|
|| (global_settings.talk_file_clip);
|
|
|
|
}
|
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
/***************** Public implementation *****************/
|
|
|
|
|
|
|
|
void talk_init(void)
|
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
int filehandle;
|
2007-10-19 15:31:42 +00:00
|
|
|
talk_temp_disable_count = 0;
|
2006-02-26 16:07:34 +00:00
|
|
|
if (talk_initialized && !strcasecmp(last_lang, global_settings.lang_file))
|
2006-02-23 21:29:29 +00:00
|
|
|
{
|
|
|
|
/* not a new file, nothing to do */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-15 14:06:11 +00:00
|
|
|
if(!talk_initialized)
|
2013-11-21 10:44:04 +00:00
|
|
|
{
|
2008-07-15 14:06:11 +00:00
|
|
|
mutex_init(&queue_mutex);
|
2013-11-21 10:44:04 +00:00
|
|
|
mutex_init(&read_buffer_mutex);
|
|
|
|
}
|
2020-07-24 03:27:49 +00:00
|
|
|
|
|
|
|
talk_force_shutup(); /* In case we have something speaking! */
|
|
|
|
|
2006-02-26 16:07:34 +00:00
|
|
|
talk_initialized = true;
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy((char *)last_lang, (char *)global_settings.lang_file,
|
2006-02-23 21:29:29 +00:00
|
|
|
MAX_FILENAME);
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
/* reset some states */
|
|
|
|
queue_write = queue_read = 0; /* reset the queue */
|
|
|
|
memset(&voicefile, 0, sizeof(voicefile));
|
|
|
|
|
2014-02-02 15:58:28 +00:00
|
|
|
silence.handle = -1; /* pause clip not accessible */
|
2013-05-30 09:24:16 +00:00
|
|
|
voicefile_size = has_voicefile = 0;
|
|
|
|
/* need to free these as their size depends on the voice file, and
|
|
|
|
* this function is called when the talk voice file changes */
|
|
|
|
if (index_handle > 0) index_handle = core_free(index_handle);
|
|
|
|
if (talk_handle > 0) talk_handle = core_free(talk_handle);
|
|
|
|
/* don't free thumb handle, it doesn't depend on the actual voice file
|
|
|
|
* and so we can re-use it if it's already allocated in any event */
|
|
|
|
|
2004-10-12 16:43:22 +00:00
|
|
|
filehandle = open_voicefile();
|
2017-02-18 22:39:48 +00:00
|
|
|
if (filehandle > -1)
|
2013-11-21 10:44:04 +00:00
|
|
|
{
|
2014-02-02 15:58:28 +00:00
|
|
|
if (!load_voicefile_index(filehandle))
|
2020-05-22 12:52:01 +00:00
|
|
|
{
|
2020-07-15 03:00:37 +00:00
|
|
|
if (global_settings.talk_menu)
|
|
|
|
splashf(HZ, str(LANG_READ_FAILED), ".voice");
|
2014-02-02 15:58:28 +00:00
|
|
|
goto out;
|
2020-05-22 12:52:01 +00:00
|
|
|
}
|
2014-02-02 15:58:28 +00:00
|
|
|
/* Now determine the maximum buffer size needed for the voicefile.
|
|
|
|
* The below pretends the entire voicefile would be loaded. The buffer
|
|
|
|
* size is eventually capped later on in load_voicefile_data() */
|
|
|
|
int num_clips = voicefile.id1_max + voicefile.id2_max;
|
|
|
|
int non_empty = num_clips;
|
|
|
|
int total_size = 0, avg_size;
|
|
|
|
struct clip_entry *clips = core_get_data(index_handle);
|
|
|
|
/* check for the average clip size to estimate the maximum number of
|
|
|
|
* clips the buffer can hold */
|
|
|
|
for (int i = 0; i<num_clips; i++)
|
|
|
|
{
|
|
|
|
if (clips[i].size)
|
|
|
|
total_size += ALIGN_UP(clips[i].size, sizeof(void *));
|
|
|
|
else
|
|
|
|
non_empty -= 1;
|
|
|
|
}
|
|
|
|
avg_size = total_size / non_empty;
|
|
|
|
max_clips = MIN((int)(MAX_CLIP_BUFFER_SIZE/avg_size) + 1, non_empty);
|
2020-09-20 14:36:25 +00:00
|
|
|
/* account for possible thumb clips */
|
2014-02-02 15:58:28 +00:00
|
|
|
total_size += THUMBNAIL_RESERVE;
|
|
|
|
max_clips += 16;
|
|
|
|
voicefile_size = total_size;
|
|
|
|
has_voicefile = true;
|
2004-03-20 16:49:58 +00:00
|
|
|
}
|
2014-02-02 15:58:28 +00:00
|
|
|
else if (talk_voice_required())
|
|
|
|
{
|
|
|
|
/* create buffer just for thumb clips */
|
|
|
|
max_clips = 16;
|
|
|
|
voicefile_size = THUMBNAIL_RESERVE;
|
|
|
|
}
|
|
|
|
/* additionally to the clip we need a table to record the age of the clips
|
2013-11-21 10:44:04 +00:00
|
|
|
* so that, when memory is tight, only the most recently used ones are kept */
|
|
|
|
voicefile_size += sizeof(struct clip_cache_metadata) * max_clips;
|
2014-02-02 13:48:32 +00:00
|
|
|
/* compensate a bit for buflib alloc overhead. */
|
|
|
|
voicefile_size += BUFLIB_ALLOC_OVERHEAD * max_clips + 64;
|
2013-11-21 10:44:04 +00:00
|
|
|
|
2014-02-02 13:48:32 +00:00
|
|
|
load_voicefile_data(filehandle);
|
|
|
|
|
2014-01-29 06:14:58 +00:00
|
|
|
/* Initialize the actual voice clip playback engine as well */
|
|
|
|
if (talk_voice_required())
|
|
|
|
voice_thread_init();
|
2013-05-30 09:24:16 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
close(filehandle); /* close again, this was just to detect presence */
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* somebody else claims the mp3 buffer, e.g. for regular play/record */
|
2013-05-30 09:24:16 +00:00
|
|
|
void talk_buffer_set_policy(int policy)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
switch(policy)
|
2004-10-12 16:43:22 +00:00
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
case TALK_BUFFER_DEFAULT:
|
|
|
|
case TALK_BUFFER_HOLD: give_buffer_away = false; break;
|
|
|
|
case TALK_BUFFER_LOOSE: give_buffer_away = true; break;
|
|
|
|
default: DEBUGF("Ignoring unknown policy\n"); break;
|
2004-10-12 16:43:22 +00:00
|
|
|
}
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
2004-05-09 09:41:23 +00:00
|
|
|
/* play a voice ID from voicefile */
|
2007-11-20 19:50:52 +00:00
|
|
|
int talk_id(int32_t id, bool enqueue)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2007-11-20 19:50:52 +00:00
|
|
|
int32_t unit;
|
2008-04-19 13:19:04 +00:00
|
|
|
int decimals;
|
2013-11-21 10:44:04 +00:00
|
|
|
struct queue_entry clip;
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2014-01-27 09:05:48 +00:00
|
|
|
if (!has_voicefile)
|
|
|
|
return 0; /* no voicefile loaded, not an error -> pretent success */
|
2007-10-19 15:31:42 +00:00
|
|
|
if (talk_temp_disable_count > 0)
|
|
|
|
return -1; /* talking has been disabled */
|
2013-05-30 09:24:16 +00:00
|
|
|
if (!check_audio_status())
|
2007-11-20 19:50:52 +00:00
|
|
|
return -1;
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2014-01-27 09:05:48 +00:00
|
|
|
if (talk_handle <= 0 || index_handle <= 0) /* reload needed? */
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
|
|
|
int fd = open_voicefile();
|
2014-02-02 15:58:28 +00:00
|
|
|
if (fd < 0 || !load_voicefile_index(fd))
|
2013-05-30 09:24:16 +00:00
|
|
|
return -1;
|
2014-02-02 15:58:28 +00:00
|
|
|
load_voicefile_data(fd);
|
|
|
|
close(fd);
|
2013-05-30 09:24:16 +00:00
|
|
|
}
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2004-04-03 20:52:24 +00:00
|
|
|
if (id == -1) /* -1 is an indication for silence */
|
|
|
|
return -1;
|
|
|
|
|
2008-04-19 13:19:04 +00:00
|
|
|
decimals = (((uint32_t)id) >> DECIMAL_SHIFT) & 0x7;
|
2008-04-19 13:35:23 +00:00
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
/* check if this is a special ID, with a value */
|
2007-11-20 19:50:52 +00:00
|
|
|
unit = ((uint32_t)id) >> UNIT_SHIFT;
|
2008-04-19 13:19:04 +00:00
|
|
|
if (unit || decimals)
|
2004-03-19 22:15:53 +00:00
|
|
|
{ /* sign-extend the value */
|
2008-04-19 13:19:04 +00:00
|
|
|
id = (uint32_t)id << (32-DECIMAL_SHIFT);
|
|
|
|
id >>= (32-DECIMAL_SHIFT);
|
|
|
|
|
|
|
|
talk_value_decimal(id, unit, decimals, enqueue); /* speak it */
|
2004-03-19 22:15:53 +00:00
|
|
|
return 0; /* and stop, end of special case */
|
|
|
|
}
|
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
if (get_clip(id, &clip) < 0)
|
2004-11-17 22:30:38 +00:00
|
|
|
return -1; /* not present */
|
2004-10-12 16:43:22 +00:00
|
|
|
|
2010-05-26 03:11:00 +00:00
|
|
|
#ifdef LOGF_ENABLE
|
|
|
|
if (id > VOICEONLY_DELIMITER)
|
2010-05-26 03:51:13 +00:00
|
|
|
logf("\ntalk_id: Say voice clip 0x%x\n", id);
|
2010-05-26 03:11:00 +00:00
|
|
|
else
|
|
|
|
logf("\ntalk_id: Say '%s'\n", str(id));
|
|
|
|
#endif
|
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
queue_clip(&clip, enqueue);
|
2004-03-19 22:15:53 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-06 13:08:36 +00:00
|
|
|
/* Speaks zero or more IDs (from an array). */
|
2008-09-29 16:29:51 +00:00
|
|
|
int talk_idarray(const long *ids, bool enqueue)
|
2007-08-06 13:08:36 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
if(!ids)
|
|
|
|
return 0;
|
|
|
|
while(*ids != TALK_FINAL_ID)
|
|
|
|
{
|
|
|
|
if((r = talk_id(*ids++, enqueue)) <0)
|
|
|
|
return r;
|
|
|
|
enqueue = true;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the current utterance is not interrupted by the next one. */
|
|
|
|
void talk_force_enqueue_next(void)
|
|
|
|
{
|
|
|
|
force_enqueue_next = true;
|
|
|
|
}
|
2004-03-19 22:15:53 +00:00
|
|
|
|
|
|
|
/* play a thumbnail from file */
|
2008-07-15 14:55:31 +00:00
|
|
|
/* Returns size of spoken thumbnail, so >0 means something is spoken,
|
|
|
|
<=0 means something went wrong. */
|
2008-09-29 16:29:51 +00:00
|
|
|
static int _talk_file(const char* filename,
|
|
|
|
const long *prefix_ids, bool enqueue)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int size;
|
2014-02-02 15:58:28 +00:00
|
|
|
int handle, oldest = -1;
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2013-11-21 10:44:04 +00:00
|
|
|
/* reload needed? */
|
2007-10-19 15:31:42 +00:00
|
|
|
if (talk_temp_disable_count > 0)
|
|
|
|
return -1; /* talking has been disabled */
|
2013-05-30 09:24:16 +00:00
|
|
|
if (!check_audio_status())
|
|
|
|
return -1;
|
2019-08-05 02:46:09 +00:00
|
|
|
if (talk_handle <= 0 || index_handle <= 0)
|
|
|
|
{
|
|
|
|
int fd = open_voicefile();
|
|
|
|
if (fd < 0 || !load_voicefile_index(fd))
|
|
|
|
return -1;
|
|
|
|
load_voicefile_data(fd);
|
|
|
|
close(fd);
|
|
|
|
}
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2008-07-15 14:06:11 +00:00
|
|
|
if (!enqueue)
|
|
|
|
/* shutup now to free the thumbnail buffer */
|
|
|
|
talk_shutup();
|
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) /* failed to open */
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-02 15:58:28 +00:00
|
|
|
size = filesize(fd);
|
2008-07-15 14:06:11 +00:00
|
|
|
|
2014-02-02 15:58:28 +00:00
|
|
|
/* free clips from cache until this one succeeds to allocate */
|
|
|
|
while ((handle = buflib_alloc(&clip_ctx, size)) < 0)
|
|
|
|
oldest = free_oldest_clip();
|
|
|
|
|
|
|
|
size = read_to_handle_ex(fd, &clip_ctx, handle, 0, size);
|
2004-03-19 22:15:53 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* ToDo: find audio, skip ID headers and trailers */
|
|
|
|
|
2008-07-15 14:06:11 +00:00
|
|
|
if (size > 0) /* Don't play missing clips */
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2013-11-21 10:44:04 +00:00
|
|
|
struct queue_entry clip;
|
2014-02-02 15:58:28 +00:00
|
|
|
clip.handle = handle;
|
|
|
|
clip.length = clip.remaining = size;
|
2008-07-15 14:55:31 +00:00
|
|
|
if(prefix_ids)
|
|
|
|
/* prefix thumbnail by speaking these ids, but only now
|
|
|
|
that we know there's actually a thumbnail to be
|
|
|
|
spoken. */
|
|
|
|
talk_idarray(prefix_ids, true);
|
2014-02-02 15:58:28 +00:00
|
|
|
/* finally insert into metadata table. thumb clips go under the
|
|
|
|
* VOICEONLY_DELIMITER id so the cache can distinguish them from
|
|
|
|
* normal clips */
|
|
|
|
add_cache_entry(handle, oldest, VOICEONLY_DELIMITER);
|
2013-11-21 10:44:04 +00:00
|
|
|
queue_clip(&clip, true);
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
2014-02-02 15:58:28 +00:00
|
|
|
else
|
|
|
|
buflib_free(&clip_ctx, handle);
|
2004-03-19 22:15:53 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2008-09-29 16:29:51 +00:00
|
|
|
int talk_file(const char *root, const char *dir, const char *file,
|
|
|
|
const char *ext, const long *prefix_ids, bool enqueue)
|
2008-07-15 14:55:31 +00:00
|
|
|
/* Play a thumbnail file */
|
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
/* Does root end with a slash */
|
|
|
|
char *slash = (root && root[0]
|
|
|
|
&& root[strlen(root)-1] != '/') ? "/" : "";
|
|
|
|
snprintf(buf, MAX_PATH, "%s%s%s%s%s%s",
|
|
|
|
root ? root : "", slash,
|
|
|
|
dir ? dir : "", dir ? "/" : "",
|
|
|
|
file ? file : "",
|
|
|
|
ext ? ext : "");
|
|
|
|
return _talk_file(buf, prefix_ids, enqueue);
|
|
|
|
}
|
|
|
|
|
2008-09-29 16:29:51 +00:00
|
|
|
static int talk_spell_basename(const char *path,
|
|
|
|
const long *prefix_ids, bool enqueue)
|
2008-07-15 14:55:31 +00:00
|
|
|
{
|
|
|
|
if(prefix_ids)
|
|
|
|
{
|
|
|
|
talk_idarray(prefix_ids, enqueue);
|
|
|
|
enqueue = true;
|
|
|
|
}
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
/* Spell only the path component after the last slash */
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(buf, path, sizeof(buf));
|
2008-07-15 14:55:31 +00:00
|
|
|
if(strlen(buf) >1 && buf[strlen(buf)-1] == '/')
|
|
|
|
/* strip trailing slash */
|
|
|
|
buf[strlen(buf)-1] = '\0';
|
|
|
|
char *ptr = strrchr(buf, '/');
|
|
|
|
if(ptr && strlen(buf) >1)
|
|
|
|
++ptr;
|
|
|
|
else ptr = buf;
|
|
|
|
return talk_spell(ptr, enqueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Play a file's .talk thumbnail, fallback to spelling the filename, or
|
|
|
|
go straight to spelling depending on settings. */
|
|
|
|
int talk_file_or_spell(const char *dirname, const char *filename,
|
2008-09-29 16:29:51 +00:00
|
|
|
const long *prefix_ids, bool enqueue)
|
2008-07-15 14:55:31 +00:00
|
|
|
{
|
|
|
|
if (global_settings.talk_file_clip)
|
|
|
|
{ /* .talk clips enabled */
|
|
|
|
if(talk_file(dirname, NULL, filename, file_thumbnail_ext,
|
|
|
|
prefix_ids, enqueue) >0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (global_settings.talk_file == 2)
|
|
|
|
/* Either .talk clips are disabled, or as a fallback */
|
|
|
|
return talk_spell_basename(filename, prefix_ids, enqueue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Play a directory's .talk thumbnail, fallback to spelling the filename, or
|
|
|
|
go straight to spelling depending on settings. */
|
|
|
|
int talk_dir_or_spell(const char* dirname,
|
2008-09-29 16:29:51 +00:00
|
|
|
const long *prefix_ids, bool enqueue)
|
2008-07-15 14:55:31 +00:00
|
|
|
{
|
|
|
|
if (global_settings.talk_dir_clip)
|
|
|
|
{ /* .talk clips enabled */
|
|
|
|
if(talk_file(dirname, NULL, dir_thumbnail_name, NULL,
|
|
|
|
prefix_ids, enqueue) >0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (global_settings.talk_dir == 2)
|
|
|
|
/* Either .talk clips disabled or as a fallback */
|
|
|
|
return talk_spell_basename(dirname, prefix_ids, enqueue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-30 13:45:26 +00:00
|
|
|
/* Speak thumbnail for each component of a full path, again falling
|
|
|
|
back or going straight to spelling depending on settings. */
|
|
|
|
int talk_fullpath(const char* path, bool enqueue)
|
|
|
|
{
|
|
|
|
if (!enqueue)
|
|
|
|
talk_shutup();
|
|
|
|
if(path[0] != '/')
|
|
|
|
/* path ought to start with /... */
|
|
|
|
return talk_spell(path, true);
|
|
|
|
talk_id(VOICE_CHAR_SLASH, true);
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
strlcpy(buf, path, MAX_PATH);
|
|
|
|
char *start = buf+1; /* start of current component */
|
|
|
|
char *ptr = strchr(start, '/'); /* end of current component */
|
|
|
|
while(ptr) { /* There are more slashes ahead */
|
|
|
|
/* temporarily poke a NULL at end of component to truncate string */
|
|
|
|
*ptr = '\0';
|
|
|
|
talk_dir_or_spell(buf, NULL, true);
|
|
|
|
*ptr = '/'; /* restore string */
|
|
|
|
talk_id(VOICE_CHAR_SLASH, true);
|
|
|
|
start = ptr+1; /* setup for next component */
|
|
|
|
ptr = strchr(start, '/');
|
|
|
|
}
|
|
|
|
/* no more slashes, final component is a filename */
|
|
|
|
return talk_file_or_spell(NULL, buf, NULL, true);
|
|
|
|
}
|
|
|
|
|
2004-03-20 16:49:58 +00:00
|
|
|
/* say a numeric value, this word ordering works for english,
|
|
|
|
but not necessarily for other languages (e.g. german) */
|
2005-02-12 11:26:36 +00:00
|
|
|
int talk_number(long n, bool enqueue)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2006-05-25 12:45:57 +00:00
|
|
|
int level = 2; /* mille count */
|
2005-02-12 11:26:36 +00:00
|
|
|
long mil = 1000000000; /* highest possible "-illion" */
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
if (talk_temp_disable_count > 0)
|
|
|
|
return -1; /* talking has been disabled */
|
2013-05-30 09:24:16 +00:00
|
|
|
if (!check_audio_status())
|
|
|
|
return -1;
|
2004-03-22 12:34:06 +00:00
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
if (!enqueue)
|
2007-10-19 15:31:42 +00:00
|
|
|
talk_shutup(); /* cut off all the pending stuff */
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
if (n==0)
|
2004-03-20 16:49:58 +00:00
|
|
|
{ /* special case */
|
2004-03-19 22:15:53 +00:00
|
|
|
talk_id(VOICE_ZERO, true);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
if (n<0)
|
|
|
|
{
|
|
|
|
talk_id(VOICE_MINUS, true);
|
|
|
|
n = -n;
|
|
|
|
}
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
while (n)
|
|
|
|
{
|
2004-03-20 16:49:58 +00:00
|
|
|
int segment = n / mil; /* extract in groups of 3 digits */
|
|
|
|
n -= segment * mil; /* remove the used digits from number */
|
|
|
|
mil /= 1000; /* digit place for next round */
|
2004-03-19 22:15:53 +00:00
|
|
|
|
|
|
|
if (segment)
|
|
|
|
{
|
|
|
|
int hundreds = segment / 100;
|
|
|
|
int ones = segment % 100;
|
|
|
|
|
|
|
|
if (hundreds)
|
|
|
|
{
|
|
|
|
talk_id(VOICE_ZERO + hundreds, true);
|
|
|
|
talk_id(VOICE_HUNDRED, true);
|
|
|
|
}
|
|
|
|
|
2004-03-20 16:49:58 +00:00
|
|
|
/* combination indexing */
|
2004-03-19 22:15:53 +00:00
|
|
|
if (ones > 20)
|
|
|
|
{
|
|
|
|
int tens = ones/10 + 18;
|
|
|
|
talk_id(VOICE_ZERO + tens, true);
|
|
|
|
ones %= 10;
|
|
|
|
}
|
|
|
|
|
2004-03-20 16:49:58 +00:00
|
|
|
/* direct indexing */
|
2004-03-19 22:15:53 +00:00
|
|
|
if (ones)
|
|
|
|
talk_id(VOICE_ZERO + ones, true);
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2004-03-20 16:49:58 +00:00
|
|
|
/* add billion, million, thousand */
|
2004-03-19 22:15:53 +00:00
|
|
|
if (mil)
|
2006-05-25 12:45:57 +00:00
|
|
|
talk_id(VOICE_THOUSAND + level, true);
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
2006-05-25 12:45:57 +00:00
|
|
|
level--;
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-30 13:45:26 +00:00
|
|
|
/* Say year like "nineteen ninety nine" instead of "one thousand 9
|
|
|
|
hundred ninety nine". */
|
|
|
|
static int talk_year(long year, bool enqueue)
|
|
|
|
{
|
|
|
|
int rem;
|
|
|
|
if(year < 1100 || year >=2000)
|
|
|
|
/* just say it as a regular number */
|
|
|
|
return talk_number(year, enqueue);
|
|
|
|
/* Say century */
|
|
|
|
talk_number(year/100, enqueue);
|
|
|
|
rem = year%100;
|
|
|
|
if(rem == 0)
|
|
|
|
/* as in 1900 */
|
|
|
|
return talk_id(VOICE_HUNDRED, true);
|
|
|
|
if(rem <10)
|
|
|
|
/* as in 1905 */
|
|
|
|
talk_id(VOICE_ZERO, true);
|
|
|
|
/* sub-century year */
|
|
|
|
return talk_number(rem, true);
|
|
|
|
}
|
|
|
|
|
2007-11-03 03:43:12 +00:00
|
|
|
/* Say time duration/interval. Input is time in seconds,
|
|
|
|
say hours,minutes,seconds. */
|
2011-02-20 15:23:18 +00:00
|
|
|
static int talk_time_unit(long secs, bool enqueue)
|
2007-11-03 03:43:12 +00:00
|
|
|
{
|
Auto-Ranging Time Formatting For Menus (hh:mm:ss:mss)
Unifies time formatting in settings_list.c allows time format to
display as HH:MM:SS.MSS or any consecutive combination thereof
(hh:mm:ss, mm:ss, mm:ss.mss, ss.mss, hh, mm, ss ,mss)
works in INT and TABLE settings with the addition of flag 'F_TIME_SETTING'
Time is auto-ranged dependent on value
Adds talk_time_intervals to allow time values to be spoken similar to
display format: x Hours, x Minutes, x Seconds, x Milliseconds
Table lookups merged or removed from recording, clip meter and lcd timeout
-String_Choice replaced with TABLE_SETTING or INT_SETTING for these
functions as well, cleaned-up cfg_vals that get saved to cfgfile
RTL Languages ARE supported
Negative values ARE supported
Backlight on/off are now Always and Never to share formatter with LCD
Timeout
Added flag to allow ranged units to be locked to a minimum index
Added flag to allow leading zero to be supressed from the largest unit
merged talk_time_unit() and talk_time_intervals()
optimized time_split()
optimized format_time_auto()
Backlight time-out list same as original
Change-Id: I59027c62d3f2956bd16fdcc1a48b2ac32c084abd
2018-12-18 04:27:55 +00:00
|
|
|
return talk_time_intervals(secs, UNIT_SEC, enqueue);
|
2007-11-03 03:43:12 +00:00
|
|
|
}
|
|
|
|
|
2008-04-20 11:02:42 +00:00
|
|
|
void talk_fractional(char *tbuf, int value, int unit)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/* strip trailing zeros from the fraction */
|
|
|
|
for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
|
|
|
|
tbuf[i] = '\0';
|
|
|
|
|
|
|
|
talk_number(value, true);
|
|
|
|
if (tbuf[0] != 0)
|
|
|
|
{
|
|
|
|
talk_id(LANG_POINT, true);
|
|
|
|
talk_spell(tbuf, true);
|
|
|
|
}
|
|
|
|
talk_id(unit, true);
|
|
|
|
}
|
|
|
|
|
2005-02-12 11:26:36 +00:00
|
|
|
int talk_value(long n, int unit, bool enqueue)
|
2008-04-19 13:19:04 +00:00
|
|
|
{
|
|
|
|
return talk_value_decimal(n, unit, 0, enqueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* singular/plural aware saying of a value */
|
|
|
|
int talk_value_decimal(long n, int unit, int decimals, bool enqueue)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
|
|
|
int unit_id;
|
2020-09-20 14:36:25 +00:00
|
|
|
static const int unit_voiced[] =
|
2004-03-19 22:15:53 +00:00
|
|
|
{ /* lookup table for the voice ID of the units */
|
2006-09-26 17:44:43 +00:00
|
|
|
[0 ... UNIT_LAST-1] = -1, /* regular ID, int, signed */
|
|
|
|
[UNIT_MS]
|
|
|
|
= VOICE_MILLISECONDS, /* here come the "real" units */
|
|
|
|
[UNIT_SEC]
|
2020-09-20 14:36:25 +00:00
|
|
|
= VOICE_SECONDS,
|
2006-09-26 17:44:43 +00:00
|
|
|
[UNIT_MIN]
|
2020-09-20 14:36:25 +00:00
|
|
|
= VOICE_MINUTES,
|
2006-09-26 17:44:43 +00:00
|
|
|
[UNIT_HOUR]
|
2020-09-20 14:36:25 +00:00
|
|
|
= VOICE_HOURS,
|
2006-09-26 17:44:43 +00:00
|
|
|
[UNIT_KHZ]
|
2020-09-20 14:36:25 +00:00
|
|
|
= VOICE_KHZ,
|
2006-09-26 17:44:43 +00:00
|
|
|
[UNIT_DB]
|
2020-09-20 14:36:25 +00:00
|
|
|
= VOICE_DB,
|
2006-09-26 17:44:43 +00:00
|
|
|
[UNIT_PERCENT]
|
|
|
|
= VOICE_PERCENT,
|
|
|
|
[UNIT_MAH]
|
|
|
|
= VOICE_MILLIAMPHOURS,
|
|
|
|
[UNIT_PIXEL]
|
|
|
|
= VOICE_PIXEL,
|
|
|
|
[UNIT_PER_SEC]
|
|
|
|
= VOICE_PER_SEC,
|
|
|
|
[UNIT_HERTZ]
|
|
|
|
= VOICE_HERTZ,
|
|
|
|
[UNIT_MB]
|
2018-12-09 18:09:40 +00:00
|
|
|
= LANG_MEBIBYTE,
|
2006-09-26 17:44:43 +00:00
|
|
|
[UNIT_KBIT]
|
|
|
|
= VOICE_KBIT_PER_SEC,
|
2007-08-18 23:03:03 +00:00
|
|
|
[UNIT_PM_TICK]
|
|
|
|
= VOICE_PM_UNITS_PER_TICK,
|
2004-03-19 22:15:53 +00:00
|
|
|
};
|
|
|
|
|
2008-04-20 11:02:42 +00:00
|
|
|
static const int pow10[] = { /* 10^0 - 10^7 */
|
|
|
|
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
|
|
|
|
};
|
|
|
|
|
|
|
|
char tbuf[8];
|
2008-04-20 14:07:21 +00:00
|
|
|
char fmt[] = "%0nd";
|
2008-04-20 11:02:42 +00:00
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
if (talk_temp_disable_count > 0)
|
|
|
|
return -1; /* talking has been disabled */
|
2013-05-30 09:24:16 +00:00
|
|
|
if (!check_audio_status())
|
|
|
|
return -1;
|
2004-03-22 12:34:06 +00:00
|
|
|
|
2018-10-30 13:45:26 +00:00
|
|
|
/* special pronounciation for year number */
|
|
|
|
if (unit == UNIT_DATEYEAR)
|
|
|
|
return talk_year(n, enqueue);
|
2007-11-03 03:43:12 +00:00
|
|
|
/* special case for time duration */
|
2011-02-20 15:23:18 +00:00
|
|
|
if (unit == UNIT_TIME)
|
|
|
|
return talk_time_unit(n, enqueue);
|
2007-11-03 03:43:12 +00:00
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
if (unit < 0 || unit >= UNIT_LAST)
|
|
|
|
unit_id = -1;
|
|
|
|
else
|
|
|
|
unit_id = unit_voiced[unit];
|
|
|
|
|
2004-03-20 16:49:58 +00:00
|
|
|
if ((n==1 || n==-1) /* singular? */
|
2004-03-19 22:15:53 +00:00
|
|
|
&& unit_id >= VOICE_SECONDS && unit_id <= VOICE_HOURS)
|
|
|
|
{
|
|
|
|
unit_id--; /* use the singular for those units which have */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* special case with a "plus" before */
|
|
|
|
if (n > 0 && (unit == UNIT_SIGNED || unit == UNIT_DB))
|
|
|
|
{
|
|
|
|
talk_id(VOICE_PLUS, enqueue);
|
|
|
|
enqueue = true;
|
2008-04-19 13:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (decimals)
|
|
|
|
{
|
|
|
|
/* needed for the "-0.5" corner case */
|
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
talk_id(VOICE_MINUS, enqueue);
|
|
|
|
n = -n;
|
|
|
|
}
|
|
|
|
|
2008-04-20 14:07:21 +00:00
|
|
|
fmt[2] = '0' + decimals;
|
|
|
|
|
|
|
|
snprintf(tbuf, sizeof(tbuf), fmt, n % pow10[decimals]);
|
2008-04-20 11:02:42 +00:00
|
|
|
talk_fractional(tbuf, n / pow10[decimals], unit_id);
|
|
|
|
|
|
|
|
return 0;
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
talk_number(n, enqueue); /* say the number */
|
|
|
|
talk_id(unit_id, true); /* say the unit, if any */
|
2008-04-20 11:02:42 +00:00
|
|
|
|
2004-03-19 22:15:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Auto-Ranging Time Formatting For Menus (hh:mm:ss:mss)
Unifies time formatting in settings_list.c allows time format to
display as HH:MM:SS.MSS or any consecutive combination thereof
(hh:mm:ss, mm:ss, mm:ss.mss, ss.mss, hh, mm, ss ,mss)
works in INT and TABLE settings with the addition of flag 'F_TIME_SETTING'
Time is auto-ranged dependent on value
Adds talk_time_intervals to allow time values to be spoken similar to
display format: x Hours, x Minutes, x Seconds, x Milliseconds
Table lookups merged or removed from recording, clip meter and lcd timeout
-String_Choice replaced with TABLE_SETTING or INT_SETTING for these
functions as well, cleaned-up cfg_vals that get saved to cfgfile
RTL Languages ARE supported
Negative values ARE supported
Backlight on/off are now Always and Never to share formatter with LCD
Timeout
Added flag to allow ranged units to be locked to a minimum index
Added flag to allow leading zero to be supressed from the largest unit
merged talk_time_unit() and talk_time_intervals()
optimized time_split()
optimized format_time_auto()
Backlight time-out list same as original
Change-Id: I59027c62d3f2956bd16fdcc1a48b2ac32c084abd
2018-12-18 04:27:55 +00:00
|
|
|
/* Say time duration/interval. Input is time unit specifies base unit,
|
|
|
|
say hours,minutes,seconds, milliseconds. or any combination thereof */
|
|
|
|
int talk_time_intervals(long time, int unit_idx, bool enqueue)
|
|
|
|
{
|
|
|
|
unsigned long units_in[UNIT_IDX_TIME_COUNT];
|
|
|
|
|
|
|
|
if (talk_temp_disable_count > 0)
|
|
|
|
return -1; /* talking has been disabled */
|
|
|
|
if (!check_audio_status())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (talk_handle <= 0 || index_handle <= 0) /* reload needed? */
|
|
|
|
{
|
|
|
|
int fd = open_voicefile();
|
|
|
|
if (fd < 0 || !load_voicefile_index(fd))
|
|
|
|
return -1;
|
|
|
|
load_voicefile_data(fd);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enqueue)
|
|
|
|
talk_shutup(); /* cut off all the pending stuff */
|
|
|
|
|
|
|
|
time_split_units(unit_idx, labs(time), &units_in);
|
|
|
|
|
|
|
|
if (time < 0)
|
|
|
|
talk_id(VOICE_MINUS, true);
|
|
|
|
|
|
|
|
if (time == 0)
|
|
|
|
talk_value(0, unit_idx, true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (units_in[UNIT_IDX_HR] != 0)
|
|
|
|
{
|
|
|
|
talk_value(units_in[UNIT_IDX_HR], UNIT_HOUR, true);
|
|
|
|
}
|
|
|
|
if (units_in[UNIT_IDX_MIN] != 0)
|
|
|
|
{
|
|
|
|
talk_value(units_in[UNIT_IDX_MIN], UNIT_MIN, true);
|
|
|
|
}
|
|
|
|
if (units_in[UNIT_IDX_SEC] != 0)
|
|
|
|
{
|
|
|
|
talk_value(units_in[UNIT_IDX_SEC], UNIT_SEC, true);
|
|
|
|
}
|
|
|
|
if (units_in[UNIT_IDX_MS] != 0)
|
|
|
|
{
|
|
|
|
talk_value(units_in[UNIT_IDX_MS], UNIT_MS, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-04 19:08:44 +00:00
|
|
|
/* spell a string */
|
2004-08-26 20:30:22 +00:00
|
|
|
int talk_spell(const char* spell, bool enqueue)
|
2004-04-04 19:08:44 +00:00
|
|
|
{
|
|
|
|
char c; /* currently processed char */
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
if (talk_temp_disable_count > 0)
|
|
|
|
return -1; /* talking has been disabled */
|
2013-05-30 09:24:16 +00:00
|
|
|
if (!check_audio_status())
|
|
|
|
return -1;
|
2004-04-04 19:08:44 +00:00
|
|
|
|
|
|
|
if (!enqueue)
|
2007-10-19 15:31:42 +00:00
|
|
|
talk_shutup(); /* cut off all the pending stuff */
|
2020-09-20 14:36:25 +00:00
|
|
|
|
2004-04-04 19:08:44 +00:00
|
|
|
while ((c = *spell++) != '\0')
|
|
|
|
{
|
|
|
|
/* if this grows into too many cases, I should use a table */
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
talk_id(VOICE_CHAR_A + c - 'A', true);
|
|
|
|
else if (c >= 'a' && c <= 'z')
|
|
|
|
talk_id(VOICE_CHAR_A + c - 'a', true);
|
|
|
|
else if (c >= '0' && c <= '9')
|
|
|
|
talk_id(VOICE_ZERO + c - '0', true);
|
|
|
|
else if (c == '-')
|
|
|
|
talk_id(VOICE_MINUS, true);
|
|
|
|
else if (c == '+')
|
|
|
|
talk_id(VOICE_PLUS, true);
|
|
|
|
else if (c == '.')
|
2020-09-20 14:36:25 +00:00
|
|
|
talk_id(VOICE_DOT, true);
|
2004-10-23 17:58:38 +00:00
|
|
|
else if (c == ' ')
|
|
|
|
talk_id(VOICE_PAUSE, true);
|
2008-07-15 14:55:31 +00:00
|
|
|
else if (c == '/')
|
|
|
|
talk_id(VOICE_CHAR_SLASH, true);
|
2004-04-04 19:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-06-11 08:28:38 +00:00
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
void talk_disable(bool disable)
|
2007-06-11 08:28:38 +00:00
|
|
|
{
|
2007-10-19 15:31:42 +00:00
|
|
|
if (disable)
|
|
|
|
talk_temp_disable_count++;
|
2020-09-20 14:36:25 +00:00
|
|
|
else
|
2007-10-19 15:31:42 +00:00
|
|
|
talk_temp_disable_count--;
|
2007-06-11 08:28:38 +00:00
|
|
|
}
|
2007-10-07 08:12:01 +00:00
|
|
|
|
2008-03-25 15:24:03 +00:00
|
|
|
void talk_setting(const void *global_settings_variable)
|
|
|
|
{
|
|
|
|
const struct settings_list *setting;
|
|
|
|
if (!global_settings.talk_menu)
|
|
|
|
return;
|
|
|
|
setting = find_setting(global_settings_variable, NULL);
|
|
|
|
if (setting == NULL)
|
|
|
|
return;
|
|
|
|
if (setting->lang_id)
|
|
|
|
talk_id(setting->lang_id,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-29 16:29:51 +00:00
|
|
|
void talk_date(const struct tm *tm, bool enqueue)
|
2007-10-07 08:12:01 +00:00
|
|
|
{
|
2007-10-24 12:32:12 +00:00
|
|
|
talk_id(LANG_MONTH_JANUARY + tm->tm_mon, enqueue);
|
|
|
|
talk_number(tm->tm_mday, true);
|
|
|
|
talk_number(1900 + tm->tm_year, true);
|
|
|
|
}
|
|
|
|
|
2008-09-29 16:29:51 +00:00
|
|
|
void talk_time(const struct tm *tm, bool enqueue)
|
2007-10-24 12:32:12 +00:00
|
|
|
{
|
|
|
|
if (global_settings.timeformat == 1)
|
2007-10-07 08:12:01 +00:00
|
|
|
{
|
2007-10-24 12:32:12 +00:00
|
|
|
/* Voice the hour */
|
|
|
|
long am_pm_id = VOICE_AM;
|
|
|
|
int hour = tm->tm_hour;
|
|
|
|
if (hour >= 12)
|
2007-10-07 08:12:01 +00:00
|
|
|
{
|
2007-10-24 12:32:12 +00:00
|
|
|
am_pm_id = VOICE_PM;
|
|
|
|
hour -= 12;
|
|
|
|
}
|
|
|
|
if (hour == 0)
|
|
|
|
hour = 12;
|
|
|
|
talk_number(hour, enqueue);
|
2007-10-07 08:12:01 +00:00
|
|
|
|
2007-10-24 12:32:12 +00:00
|
|
|
/* Voice the minutes */
|
|
|
|
if (tm->tm_min == 0)
|
|
|
|
{
|
|
|
|
/* Say o'clock if the minute is 0. */
|
|
|
|
talk_id(VOICE_OCLOCK, true);
|
2007-10-07 08:12:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-24 12:32:12 +00:00
|
|
|
/* Pronounce the leading 0 */
|
|
|
|
if(tm->tm_min < 10)
|
|
|
|
talk_id(VOICE_OH, true);
|
|
|
|
talk_number(tm->tm_min, true);
|
|
|
|
}
|
|
|
|
talk_id(am_pm_id, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Voice the time in 24 hour format */
|
|
|
|
talk_number(tm->tm_hour, enqueue);
|
|
|
|
if (tm->tm_min == 0)
|
2018-10-30 13:45:26 +00:00
|
|
|
talk_ids(true, VOICE_HUNDRED, VOICE_HOUR);
|
2007-10-24 12:32:12 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Pronounce the leading 0 */
|
|
|
|
if(tm->tm_min < 10)
|
|
|
|
talk_id(VOICE_OH, true);
|
|
|
|
talk_number(tm->tm_min, true);
|
2007-10-07 08:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-24 12:32:12 +00:00
|
|
|
|
2020-09-20 17:29:02 +00:00
|
|
|
void talk_announce_voice_invalid(void)
|
|
|
|
{
|
|
|
|
int voice_fd;
|
|
|
|
int voice_sz;
|
|
|
|
int buf_handle;
|
|
|
|
struct queue_entry qe;
|
|
|
|
|
|
|
|
const char talkfile[] =
|
|
|
|
LANG_DIR "/InvalidVoice_" DEFAULT_VOICE_LANG ".talk";
|
|
|
|
|
2020-09-20 20:44:24 +00:00
|
|
|
if (global_settings.talk_menu && talk_status != TALK_STATUS_OK)
|
2020-09-20 17:29:02 +00:00
|
|
|
{
|
|
|
|
talk_temp_disable_count = 0xFF; /* don't let anyone else use voice sys */
|
|
|
|
|
|
|
|
voice_fd = open(talkfile, O_RDONLY);
|
|
|
|
if (voice_fd < 0)
|
|
|
|
return; /* can't open */
|
|
|
|
|
|
|
|
voice_sz= lseek(voice_fd, 0, SEEK_END);
|
|
|
|
if (voice_sz == 0 || voice_sz > (64<<10))
|
|
|
|
return; /* nothing here or too big */
|
|
|
|
|
|
|
|
lseek(voice_fd, 0, SEEK_SET);
|
|
|
|
/* add a bit extra for buflib overhead (2K) */
|
|
|
|
if (!create_clip_buffer(ALIGN_UP(voice_sz, sizeof(long)) + (2<<10)))
|
|
|
|
return;
|
|
|
|
mutex_lock(&read_buffer_mutex);
|
|
|
|
buf_handle = buflib_alloc(&clip_ctx, ALIGN_UP(voice_sz, sizeof(long)));
|
|
|
|
|
|
|
|
if (buf_handle < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (read_to_handle_ex(voice_fd, &clip_ctx, buf_handle, 0, voice_sz) > 0)
|
|
|
|
{
|
|
|
|
voice_thread_init();
|
|
|
|
qe.handle = buf_handle;
|
|
|
|
qe.length = qe.remaining = voice_sz;
|
|
|
|
queue_clip(&qe, false);
|
|
|
|
voice_wait();
|
|
|
|
voice_thread_kill();
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&read_buffer_mutex);
|
|
|
|
close(voice_fd);
|
|
|
|
|
|
|
|
buf_handle = buflib_free(&clip_ctx, buf_handle);
|
|
|
|
talk_handle = core_free(talk_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 06:24:00 +00:00
|
|
|
bool talk_get_debug_data(struct talk_debug_data *data)
|
|
|
|
{
|
|
|
|
char* p_lang = DEFAULT_VOICE_LANG; /* default */
|
2014-02-02 15:58:28 +00:00
|
|
|
struct clip_cache_metadata *cc;
|
2013-07-02 06:24:00 +00:00
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
|
2020-05-21 17:55:32 +00:00
|
|
|
data->status = talk_status;
|
2013-07-02 06:24:00 +00:00
|
|
|
|
|
|
|
if (global_settings.lang_file[0] && global_settings.lang_file[0] != 0xff)
|
|
|
|
p_lang = (char *)global_settings.lang_file;
|
|
|
|
|
2020-05-21 17:55:32 +00:00
|
|
|
strlcpy(data->voicefile, p_lang, sizeof(data->voicefile));
|
|
|
|
|
|
|
|
if (!has_voicefile || index_handle <= 0)
|
|
|
|
{
|
|
|
|
if (data->status == TALK_STATUS_OK)
|
|
|
|
data->status = TALK_STATUS_ERR_NOFILE;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-02 06:24:00 +00:00
|
|
|
struct clip_entry *clips = core_get_data(index_handle);
|
|
|
|
int cached = 0;
|
|
|
|
int real_clips = 0;
|
|
|
|
|
|
|
|
data->num_clips = voicefile.id1_max + voicefile.id2_max;
|
|
|
|
data->avg_clipsize = data->max_clipsize = 0;
|
|
|
|
data->min_clipsize = INT_MAX;
|
|
|
|
for(int i = 0; i < data->num_clips; i++)
|
|
|
|
{
|
|
|
|
int size = clips[i].size & (~LOADED_MASK);
|
|
|
|
if (!size) continue;
|
|
|
|
real_clips += 1;
|
|
|
|
if (size < data->min_clipsize)
|
|
|
|
data->min_clipsize = size;
|
|
|
|
if (size > data->max_clipsize)
|
|
|
|
data->max_clipsize = size;
|
|
|
|
data->avg_clipsize += size;
|
2014-02-02 15:58:28 +00:00
|
|
|
}
|
|
|
|
cc = buflib_get_data(&clip_ctx, metadata_table_handle);
|
|
|
|
for (int i = 0; i < (int) max_clips; i++)
|
|
|
|
{
|
|
|
|
if (cc[i].handle > 0)
|
|
|
|
cached += 1;
|
2013-07-02 06:24:00 +00:00
|
|
|
}
|
|
|
|
data->avg_clipsize /= real_clips;
|
|
|
|
data->num_empty_clips = data->num_clips - real_clips;
|
2013-11-21 10:44:04 +00:00
|
|
|
data->memory_allocated = sizeof(commit_buffer) + sizeof(voicefile)
|
|
|
|
+ data->num_clips * sizeof(struct clip_entry)
|
2014-02-02 15:58:28 +00:00
|
|
|
+ voicebuf_size;
|
|
|
|
data->memory_used = 0;
|
2013-11-21 10:44:04 +00:00
|
|
|
if (talk_handle > 0)
|
2014-02-02 15:58:28 +00:00
|
|
|
data->memory_used = data->memory_allocated - buflib_available(&clip_ctx);
|
2013-07-02 06:24:00 +00:00
|
|
|
data->cached_clips = cached;
|
|
|
|
data->cache_hits = cache_hits;
|
|
|
|
data->cache_misses = cache_misses;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|