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"
|
2012-05-02 21:22:28 +00:00
|
|
|
#if CONFIG_CODEC == SWCODEC
|
|
|
|
#include "voice_thread.h"
|
|
|
|
#else
|
2004-03-19 22:15:53 +00:00
|
|
|
#include "mp3_playback.h"
|
2012-05-02 21:22:28 +00:00
|
|
|
#endif
|
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"
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2006-08-01 22:02:47 +00:00
|
|
|
/* Memory layout varies between targets because the
|
|
|
|
Archos (MASCODEC) devices cannot mix voice and audio playback
|
|
|
|
|
|
|
|
MASCODEC | MASCODEC | SWCODEC
|
|
|
|
(playing) | (stopped) |
|
2011-08-14 15:13:00 +00:00
|
|
|
voicebuf-----------+-----------+------------
|
2011-08-21 15:05:57 +00:00
|
|
|
audio | voice | voice
|
2006-11-06 18:07:30 +00:00
|
|
|
|-----------|------------
|
2011-08-21 15:05:57 +00:00
|
|
|
| thumbnail | thumbnail
|
2006-11-06 18:07:30 +00:00
|
|
|
| |------------
|
|
|
|
| | filebuf
|
|
|
|
| |------------
|
2006-08-01 22:02:47 +00:00
|
|
|
| | audio
|
2011-08-14 15:13:00 +00:00
|
|
|
voicebufend----------+-----------+------------
|
2006-08-01 22:02:47 +00:00
|
|
|
|
2011-08-21 15:05:57 +00:00
|
|
|
SWCODEC allocates dedicated buffers (except voice and thumbnail are together
|
|
|
|
in the talkbuf), MASCODEC reuses audiobuf. */
|
2006-08-01 22:02:47 +00:00
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
/* swcodec: cap thumbnail buffer to MAX_THUMNAIL_BUFSIZE since audio keeps
|
|
|
|
* playing while voice
|
2011-08-22 09:56:58 +00:00
|
|
|
* hwcodec: just use whatever is left in the audiobuffer, music
|
|
|
|
* playback is impossible => no cap */
|
2006-08-01 22:02:47 +00:00
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2007-10-05 08:14:09 +00:00
|
|
|
#define MAX_THUMBNAIL_BUFSIZE 0x10000
|
2006-08-01 22:02:47 +00:00
|
|
|
#endif
|
|
|
|
|
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 *****************/
|
|
|
|
|
2011-11-06 14:25:33 +00:00
|
|
|
#if (CONFIG_CODEC == SWCODEC && MEMORYSIZE <= 2) || defined(ONDIO_SERIES)
|
2011-11-06 13:58:17 +00:00
|
|
|
/* On low memory swcodec targets the entire voice file wouldn't fit in memory
|
|
|
|
* together with codecs, so we load clips each time they are accessed.
|
|
|
|
* The Ondios have slow storage access and loading the entire voice file would
|
|
|
|
* take several seconds, so we use the same mechanism. */
|
|
|
|
#define TALK_PARTIAL_LOAD
|
2010-09-01 00:08:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TALK_PARTIAL_LOAD
|
|
|
|
static long max_clipsize; /* size of the biggest clip */
|
|
|
|
static long buffered_id[QUEUE_SIZE]; /* IDs of the talk clips */
|
|
|
|
static uint8_t clip_age[QUEUE_SIZE];
|
|
|
|
#if QUEUE_SIZE > 255
|
|
|
|
# error clip_age[] type too small
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
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 */
|
|
|
|
static long size_for_thumbnail; /* total thumbnail buffer size */
|
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 */
|
2008-07-15 14:06:11 +00:00
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2010-02-24 19:40:45 +00:00
|
|
|
/* protects queue_read, queue_write and thumbnail_buf_used */
|
|
|
|
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); })
|
|
|
|
#else
|
|
|
|
#define talk_queue_lock() ({ })
|
|
|
|
#define talk_queue_unlock() ({ })
|
|
|
|
#endif /* CONFIG_CODEC */
|
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 */
|
2013-05-30 09:24:16 +00:00
|
|
|
static int silence_offset; /* VOICE_PAUSE clip, used for termination */
|
|
|
|
static long silence_length; /* length of the VOICE_PAUSE clip */
|
|
|
|
static unsigned long lastclip_offset; /* address of latest clip, for silence add */
|
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) */
|
2013-05-30 09:24:16 +00:00
|
|
|
/* size of the loaded voice file
|
|
|
|
* offsets smaller than this denote a clip from teh voice file,
|
|
|
|
* offsets larger than this denote a thumbnail clip */
|
|
|
|
static unsigned long voicefile_size;
|
|
|
|
|
|
|
|
struct queue_entry /* one entry of the internal queue */
|
|
|
|
{
|
|
|
|
int offset, length;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
|
2004-03-19 22:15:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***************** Private implementation *****************/
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
static int thumb_handle;
|
|
|
|
static int talk_handle, talk_handle_locked;
|
|
|
|
|
|
|
|
#if CONFIG_CODEC != SWCODEC
|
|
|
|
|
|
|
|
/* on HWCODEC only voice xor audio can be active at a time */
|
|
|
|
static bool check_audio_status(void)
|
|
|
|
{
|
|
|
|
if (audio_status()) /* busy, buffer in use */
|
|
|
|
return false;
|
|
|
|
/* ensure playback is given up on the buffer */
|
|
|
|
audio_hard_stop();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ISR (mp3_callback()) must not run during moving of the clip buffer,
|
|
|
|
* because the MAS may get out-of-sync */
|
|
|
|
static void sync_callback(int handle, bool sync_on)
|
|
|
|
{
|
|
|
|
(void) handle;
|
|
|
|
(void) sync_on;
|
|
|
|
#if CONFIG_CPU == SH7034
|
|
|
|
if (sync_on)
|
|
|
|
CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
|
|
|
|
else
|
|
|
|
CHCR3 |= 0x0001; /* re-enable the DMA */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define check_audio_status() (true)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int move_callback(int handle, void *current, void *new)
|
|
|
|
{
|
|
|
|
(void)handle;(void)current;(void)new;
|
|
|
|
if (UNLIKELY(talk_handle_locked))
|
|
|
|
return BUFLIB_CB_CANNOT_MOVE;
|
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-26 18:14:47 +00:00
|
|
|
static int clip_shrink_callback(int handle, unsigned hints, void *start, size_t old_size)
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
2014-01-29 06:08:42 +00:00
|
|
|
(void)start;(void)old_size;(void)hints;
|
2013-05-30 09:24:16 +00:00
|
|
|
|
2014-01-29 06:08:42 +00:00
|
|
|
#if (!defined(TALK_PARTIAL_LOAD) || (MEMORYSIZE > 2))
|
|
|
|
/* 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-05-30 09:24:16 +00:00
|
|
|
if (LIKELY(!talk_handle_locked)
|
|
|
|
&& give_buffer_away
|
|
|
|
&& (hints & BUFLIB_SHRINK_POS_MASK) == BUFLIB_SHRINK_POS_MASK)
|
2014-01-29 06:08:42 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
{
|
2014-01-26 18:14:47 +00:00
|
|
|
talk_handle = core_free(handle);
|
2013-05-30 09:24:16 +00:00
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
return BUFLIB_CB_CANNOT_SHRINK;
|
|
|
|
}
|
|
|
|
|
2014-01-26 18:14:47 +00:00
|
|
|
static int thumb_shrink_callback(int handle, unsigned hints, void *start, size_t old_size)
|
|
|
|
{
|
|
|
|
(void)start;(void)old_size;(void)hints;
|
|
|
|
|
|
|
|
/* be generous about the thumbnail buffer unless currently used */
|
|
|
|
if (LIKELY(!talk_handle_locked) && thumbnail_buf_used == 0)
|
|
|
|
{
|
|
|
|
thumb_handle = core_free(handle);
|
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
return BUFLIB_CB_CANNOT_SHRINK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct buflib_callbacks clip_ops = {
|
2013-05-30 09:24:16 +00:00
|
|
|
.move_callback = move_callback,
|
|
|
|
#if CONFIG_CODEC != SWCODEC
|
|
|
|
.sync_callback = sync_callback,
|
|
|
|
#endif
|
2014-01-26 18:14:47 +00:00
|
|
|
.shrink_callback = clip_shrink_callback,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct buflib_callbacks thumb_ops = {
|
|
|
|
.move_callback = move_callback,
|
|
|
|
#if CONFIG_CODEC != SWCODEC
|
|
|
|
.sync_callback = sync_callback,
|
|
|
|
#endif
|
|
|
|
.shrink_callback = thumb_shrink_callback,
|
2013-05-30 09:24:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int index_handle, index_handle_locked;
|
|
|
|
static int index_move_callback(int handle, void *current, void *new)
|
|
|
|
{
|
|
|
|
(void)handle;(void)current;(void)new;
|
|
|
|
if (UNLIKELY(index_handle_locked))
|
|
|
|
return BUFLIB_CB_CANNOT_MOVE;
|
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int index_shrink_callback(int handle, unsigned hints, void *start, size_t old_size)
|
|
|
|
{
|
|
|
|
(void)start;(void)old_size;
|
|
|
|
if (LIKELY(!index_handle_locked)
|
|
|
|
&& give_buffer_away
|
|
|
|
&& (hints & BUFLIB_SHRINK_POS_MASK) == BUFLIB_SHRINK_POS_MASK)
|
|
|
|
{
|
|
|
|
index_handle = core_free(handle);
|
|
|
|
/* the clip buffer isn't usable without index table */
|
|
|
|
if (LIKELY(!talk_handle_locked))
|
|
|
|
talk_handle = core_free(talk_handle);
|
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
return BUFLIB_CB_CANNOT_SHRINK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct buflib_callbacks index_ops = {
|
|
|
|
.move_callback = index_move_callback,
|
|
|
|
.shrink_callback = index_shrink_callback,
|
|
|
|
};
|
|
|
|
|
2004-04-06 07:06:59 +00:00
|
|
|
static int open_voicefile(void)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
char* p_lang = "english"; /* default */
|
|
|
|
|
|
|
|
if ( global_settings.lang_file[0] &&
|
|
|
|
global_settings.lang_file[0] != 0xff )
|
|
|
|
{ /* 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);
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2004-04-06 07:06:59 +00:00
|
|
|
return open(buf, O_RDONLY);
|
|
|
|
}
|
|
|
|
|
2005-08-20 11:13:19 +00:00
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
/* fetch a clip from the voice file */
|
2013-05-30 09:24:16 +00:00
|
|
|
static int get_clip(long id, long* p_size)
|
2007-10-19 15:31:42 +00:00
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
int retval = -1;
|
|
|
|
struct clip_entry* clipbuf;
|
|
|
|
size_t clipsize;
|
|
|
|
|
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 */
|
2007-10-19 15:31:42 +00:00
|
|
|
id -= VOICEONLY_DELIMITER + 1;
|
2013-05-30 09:24:16 +00:00
|
|
|
if (id >= voicefile.id2_max)
|
|
|
|
return -1; /* must be newer than we have */
|
|
|
|
id += 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-05-30 09:24:16 +00:00
|
|
|
|
|
|
|
clipbuf = core_get_data(index_handle);
|
|
|
|
clipsize = clipbuf[id].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
|
|
|
|
|
|
|
#ifndef TALK_PARTIAL_LOAD
|
2013-05-30 09:24:16 +00:00
|
|
|
retval = clipbuf[id].offset;
|
2007-10-19 15:31:42 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
#else
|
2007-10-19 15:31:42 +00:00
|
|
|
if (!(clipsize & LOADED_MASK))
|
2010-09-01 00:08:50 +00:00
|
|
|
{ /* clip needs loading */
|
2013-05-30 09:24:16 +00:00
|
|
|
ssize_t ret;
|
|
|
|
int fd, idx = 0;
|
|
|
|
unsigned char *voicebuf;
|
2010-09-01 00:08:50 +00:00
|
|
|
if (id == VOICE_PAUSE) {
|
|
|
|
idx = QUEUE_SIZE; /* we keep VOICE_PAUSE loaded */
|
|
|
|
} else {
|
|
|
|
int oldest = 0, i;
|
|
|
|
for(i=0; i<QUEUE_SIZE; i++) {
|
|
|
|
if (buffered_id[i] < 0) {
|
|
|
|
/* found a free entry, that means the buffer isn't
|
|
|
|
* full yet. */
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the oldest clip */
|
|
|
|
if(clip_age[i] > oldest) {
|
|
|
|
idx = i;
|
|
|
|
oldest = clip_age[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* increment age of each loaded clip */
|
|
|
|
clip_age[i]++;
|
|
|
|
}
|
|
|
|
clip_age[idx] = 0; /* reset clip's age */
|
|
|
|
}
|
2013-05-30 09:24:16 +00:00
|
|
|
retval = idx * max_clipsize;
|
|
|
|
fd = open_voicefile();
|
|
|
|
if (fd < 0)
|
|
|
|
return -1; /* open error */
|
|
|
|
|
|
|
|
talk_handle_locked++;
|
|
|
|
voicebuf = core_get_data(talk_handle);
|
|
|
|
clipbuf = core_get_data(index_handle);
|
|
|
|
lseek(fd, clipbuf[id].offset, SEEK_SET);
|
|
|
|
ret = read(fd, &voicebuf[retval], clipsize);
|
|
|
|
close(fd);
|
|
|
|
talk_handle_locked--;
|
2010-09-01 00:08:50 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (ret < 0 || clipsize != (size_t)ret)
|
|
|
|
return -1; /* read error */
|
2007-10-19 15:31:42 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
clipbuf = core_get_data(index_handle);
|
|
|
|
clipbuf[id].size |= LOADED_MASK; /* mark as loaded */
|
2010-09-01 00:08:50 +00:00
|
|
|
|
|
|
|
if (id != VOICE_PAUSE) {
|
|
|
|
if (buffered_id[idx] >= 0) {
|
|
|
|
/* mark previously loaded clip as unloaded */
|
2013-05-30 09:24:16 +00:00
|
|
|
clipbuf[buffered_id[idx]].size &= ~LOADED_MASK;
|
2010-09-01 00:08:50 +00:00
|
|
|
}
|
|
|
|
buffered_id[idx] = id;
|
|
|
|
}
|
2007-10-19 15:31:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* clip is in memory already */
|
2010-09-01 00:08:50 +00:00
|
|
|
/* Find where it was loaded */
|
|
|
|
if (id == VOICE_PAUSE) {
|
2013-05-30 09:24:16 +00:00
|
|
|
retval = QUEUE_SIZE * max_clipsize;
|
2010-09-01 00:08:50 +00:00
|
|
|
} else {
|
|
|
|
int idx;
|
|
|
|
for (idx=0; idx<QUEUE_SIZE; idx++)
|
|
|
|
if (buffered_id[idx] == id) {
|
2013-05-30 09:24:16 +00:00
|
|
|
retval = idx * max_clipsize;
|
2010-09-01 00:08:50 +00:00
|
|
|
clip_age[idx] = 0; /* reset clip's age */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 15:31:42 +00:00
|
|
|
clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
|
|
|
|
}
|
2011-11-06 13:58:17 +00:00
|
|
|
#endif /* TALK_PARTIAL_LOAD */
|
2007-10-19 15:31:42 +00:00
|
|
|
|
|
|
|
*p_size = clipsize;
|
2013-05-30 09:24:16 +00:00
|
|
|
return retval;
|
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;
|
|
|
|
struct clip_entry *buf;
|
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);
|
|
|
|
index_handle = core_alloc_ex("voice index", alloc_size, &index_ops);
|
|
|
|
if (index_handle < 0)
|
|
|
|
return false;
|
2004-10-12 16:43:22 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
index_handle_locked++;
|
|
|
|
buf = core_get_data(index_handle);
|
|
|
|
ret = read(fd, buf, alloc_size);
|
2011-08-14 15:13:00 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
#ifndef TALK_PARTIAL_LOAD
|
|
|
|
int clips_offset, num_clips;
|
|
|
|
/* adjust the offsets of the clips, they are relative to the file
|
|
|
|
* TALK_PARTUAL_LOAD needs the file offset instead as it loads
|
|
|
|
* the clips later */
|
|
|
|
clips_offset = hdr->table;
|
|
|
|
num_clips = hdr->id1_max + hdr->id2_max;
|
|
|
|
clips_offset += num_clips * sizeof(struct clip_entry); /* skip index */
|
|
|
|
#endif
|
|
|
|
if (ret == alloc_size)
|
|
|
|
for (int i = 0; i < hdr->id1_max + hdr->id2_max; i++)
|
|
|
|
{
|
|
|
|
#ifdef ROCKBOX_LITTLE_ENDIAN
|
|
|
|
structec_convert(&buf[i], "ll", 1, true);
|
|
|
|
#endif
|
|
|
|
#ifndef TALK_PARTIAL_LOAD
|
|
|
|
buf[i].offset -= clips_offset;
|
2004-10-12 16:43:22 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
}
|
2010-09-01 00:08:50 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
index_handle_locked--;
|
|
|
|
|
|
|
|
if (ret != alloc_size)
|
|
|
|
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
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
#ifndef TALK_PARTIAL_LOAD
|
|
|
|
static bool load_data(int fd, ssize_t size_to_read)
|
|
|
|
{
|
|
|
|
unsigned char *buf;
|
|
|
|
ssize_t ret;
|
2004-10-12 16:43:22 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (size_to_read < 0)
|
|
|
|
return false;
|
2010-09-01 00:08:50 +00:00
|
|
|
|
2014-01-26 18:14:47 +00:00
|
|
|
talk_handle = core_alloc_ex("voice data", size_to_read, &clip_ops);
|
2013-05-30 09:24:16 +00:00
|
|
|
if (talk_handle < 0)
|
|
|
|
return false;
|
2010-09-01 00:08:50 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
talk_handle_locked++;
|
|
|
|
buf = core_get_data(talk_handle);
|
|
|
|
ret = read(fd, buf, size_to_read);
|
|
|
|
talk_handle_locked--;
|
2011-08-14 15:13:00 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (ret != size_to_read)
|
|
|
|
talk_handle = core_free(talk_handle);
|
2010-09-01 00:08:50 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
return ret == size_to_read;
|
|
|
|
}
|
2004-10-12 16:43:22 +00:00
|
|
|
#endif
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
static bool alloc_thumbnail_buf(void)
|
|
|
|
{
|
|
|
|
int handle;
|
|
|
|
size_t size;
|
|
|
|
if (thumb_handle > 0)
|
|
|
|
return true; /* nothing to do? */
|
|
|
|
#if CONFIG_CODEC == SWCODEC
|
|
|
|
/* try to allocate the max. first, and take whatever we can get if that
|
|
|
|
* fails */
|
|
|
|
size = MAX_THUMBNAIL_BUFSIZE;
|
2014-01-26 18:14:47 +00:00
|
|
|
handle = core_alloc_ex("voice thumb", MAX_THUMBNAIL_BUFSIZE, &thumb_ops);
|
2013-05-30 09:24:16 +00:00
|
|
|
if (handle < 0)
|
|
|
|
{
|
|
|
|
size = core_allocatable();
|
2014-01-26 18:14:47 +00:00
|
|
|
handle = core_alloc_ex("voice thumb", size, &thumb_ops);
|
2013-05-30 09:24:16 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* on HWCODEC, just use the rest of the remaining buffer,
|
|
|
|
* normal playback cannot happen anyway */
|
2014-01-26 18:14:47 +00:00
|
|
|
handle = core_alloc_maximum("voice thumb", &size, &thumb_ops);
|
2010-09-01 00:08:50 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
thumb_handle = handle;
|
|
|
|
size_for_thumbnail = (handle > 0) ? size : 0;
|
|
|
|
return handle > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load the voice file into the mp3 buffer */
|
|
|
|
static bool load_voicefile_index(int fd)
|
|
|
|
{
|
|
|
|
if (fd < 0) /* failed to open */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* load the header first */
|
|
|
|
if (!load_header(fd, &voicefile))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* format check */
|
|
|
|
if (voicefile.table == sizeof(struct voicefile_header))
|
|
|
|
{
|
|
|
|
if (voicefile.version == VOICE_VERSION &&
|
|
|
|
voicefile.target_id == TARGET_ID)
|
|
|
|
{
|
|
|
|
if (load_index_table(fd, &voicefile))
|
|
|
|
return true;
|
|
|
|
}
|
2010-09-01 00:08:50 +00:00
|
|
|
}
|
2004-11-17 22:30:38 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
logf("Incompatible voice file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool load_voicefile_data(int fd, size_t max_size)
|
|
|
|
{
|
2011-08-14 15:13:00 +00:00
|
|
|
#ifdef TALK_PARTIAL_LOAD
|
2013-12-23 11:54:36 +00:00
|
|
|
(void)fd;
|
2013-05-30 09:24:16 +00:00
|
|
|
/* just allocate, populate on an as-needed basis later */
|
2014-01-26 18:14:47 +00:00
|
|
|
talk_handle = core_alloc_ex("voice data", max_size, &clip_ops);
|
2013-05-30 09:24:16 +00:00
|
|
|
if (talk_handle < 0)
|
|
|
|
goto load_err_free;
|
|
|
|
#else
|
|
|
|
size_t load_size, clips_size;
|
|
|
|
/* load the entire file into memory */
|
|
|
|
clips_size = (voicefile.id1_max+voicefile.id2_max) * sizeof(struct clip_entry);
|
|
|
|
load_size = max_size - voicefile.table - clips_size;
|
|
|
|
if (!load_data(fd, load_size))
|
|
|
|
goto load_err_free;
|
2011-08-14 15:13:00 +00:00
|
|
|
#endif
|
2011-08-22 10:23:11 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
/* make sure to have the silence clip, if available
|
|
|
|
* return value can be cached globally even for TALK_PARTIAL_LOAD because
|
|
|
|
* the VOICE_PAUSE clip is specially handled */
|
|
|
|
silence_offset = get_clip(VOICE_PAUSE, &silence_length);
|
2004-10-12 16:43:22 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
/* not an error if this fails here, might try again when the
|
|
|
|
* actual thumbnails are attempted to be played back */
|
|
|
|
alloc_thumbnail_buf();
|
2011-08-22 10:23:11 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
load_err_free:
|
|
|
|
index_handle = core_free(index_handle);
|
|
|
|
return false;
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-05-30 09:24:16 +00:00
|
|
|
queue[queue_read].length -= sent; /* we completed this */
|
|
|
|
queue[queue_read].offset += sent;
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (queue[queue_read].length > 0) /* current clip not finished? */
|
2004-03-19 22:15:53 +00:00
|
|
|
{ /* feed the next 64K-1 chunk */
|
2013-05-30 09:24:16 +00:00
|
|
|
int offset;
|
2005-08-29 21:15:27 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2013-05-30 09:24:16 +00:00
|
|
|
sent = MIN(queue[queue_read].length, 0xFFFF);
|
2005-08-20 11:13:19 +00:00
|
|
|
#else
|
2013-05-30 09:24:16 +00:00
|
|
|
sent = queue[queue_read].length;
|
2005-08-20 11:13:19 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
offset = queue[queue_read].offset;
|
|
|
|
if ((unsigned long)offset >= voicefile_size)
|
|
|
|
*start = core_get_data(thumb_handle) + offset - voicefile_size;
|
|
|
|
else
|
|
|
|
*start = core_get_data(talk_handle) + offset;
|
2004-05-09 09:41:23 +00:00
|
|
|
*size = sent;
|
2004-03-19 22:15:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-07-15 14:06:11 +00:00
|
|
|
talk_queue_lock();
|
2013-05-30 09:24:16 +00:00
|
|
|
if(thumb_handle && (unsigned long)queue[queue_read].offset == voicefile_size+thumbnail_buf_used)
|
2008-07-15 14:06:11 +00:00
|
|
|
thumbnail_buf_used = 0;
|
|
|
|
if (sent > 0) /* go to next entry */
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2004-11-18 22:59:20 +00:00
|
|
|
queue_read = (queue_read + 1) & QUEUE_MASK;
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
2004-11-18 22:59:20 +00:00
|
|
|
re_check:
|
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
if (QUEUE_LEVEL != 0) /* queue is not empty? */
|
2004-03-19 22:15:53 +00:00
|
|
|
{ /* start next clip */
|
2013-05-30 09:24:16 +00:00
|
|
|
unsigned char *buf;
|
2005-08-29 21:15:27 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2013-05-30 09:24:16 +00:00
|
|
|
sent = MIN(queue[queue_read].length, 0xFFFF);
|
2005-08-20 11:13:19 +00:00
|
|
|
#else
|
2013-05-30 09:24:16 +00:00
|
|
|
sent = queue[queue_read].length;
|
2005-08-20 11:13:19 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
lastclip_offset = queue[queue_read].offset;
|
|
|
|
/* offsets larger than voicefile_size denote thumbnail clips */
|
|
|
|
if (lastclip_offset >= voicefile_size)
|
|
|
|
buf = core_get_data(thumb_handle) + lastclip_offset - voicefile_size;
|
|
|
|
else
|
|
|
|
buf = core_get_data(talk_handle) + lastclip_offset;
|
|
|
|
*start = buf;
|
2004-05-09 09:41:23 +00:00
|
|
|
*size = sent;
|
2013-05-30 09:24:16 +00:00
|
|
|
curr_hd[0] = buf[1];
|
|
|
|
curr_hd[1] = buf[2];
|
|
|
|
curr_hd[2] = buf[3];
|
2004-03-19 22:15:53 +00:00
|
|
|
}
|
2013-05-30 09:24:16 +00:00
|
|
|
else if (silence_offset > 0 /* silence clip available */
|
|
|
|
&& lastclip_offset != (unsigned long)silence_offset /* previous clip wasn't silence */
|
|
|
|
&& !(lastclip_offset >= voicefile_size /* ..or thumbnail */
|
|
|
|
&& lastclip_offset < voicefile_size +size_for_thumbnail))
|
2004-11-17 22:30:38 +00:00
|
|
|
{ /* add silence clip when queue runs empty playing a voice clip */
|
2013-05-30 09:24:16 +00:00
|
|
|
queue[queue_write].offset = silence_offset;
|
|
|
|
queue[queue_write].length = silence_length;
|
2004-11-18 22:59:20 +00:00
|
|
|
queue_write = (queue_write + 1) & QUEUE_MASK;
|
|
|
|
|
|
|
|
goto re_check;
|
2004-11-17 22:30:38 +00:00
|
|
|
}
|
2004-03-19 22:15:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*size = 0; /* end of data */
|
2013-05-30 09:24:16 +00:00
|
|
|
talk_handle_locked--;
|
2004-03-19 22:15:53 +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
|
|
|
{
|
2007-10-19 15:31:42 +00:00
|
|
|
/* Most of this is MAS only */
|
2006-10-25 06:19:27 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2007-11-07 09:28:07 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
return;
|
|
|
|
#endif
|
2004-03-21 17:45:45 +00:00
|
|
|
unsigned char* pos;
|
|
|
|
unsigned char* search;
|
|
|
|
unsigned char* end;
|
2013-05-30 09:24:16 +00:00
|
|
|
int len;
|
|
|
|
unsigned clip_offset;
|
2004-05-09 09:41:23 +00:00
|
|
|
if (QUEUE_LEVEL == 0) /* has ended anyway */
|
2007-10-19 15:31:42 +00:00
|
|
|
return;
|
|
|
|
|
2005-02-02 21:50:24 +00:00
|
|
|
#if CONFIG_CPU == SH7034
|
2004-05-09 09:41:23 +00:00
|
|
|
CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
|
2007-10-19 15:31:42 +00:00
|
|
|
#endif /* CONFIG_CPU == SH7034 */
|
2004-03-21 17:45:45 +00:00
|
|
|
/* search next frame boundary and continue up to there */
|
|
|
|
pos = search = mp3_get_pos();
|
2013-05-30 09:24:16 +00:00
|
|
|
clip_offset = queue[queue_read].offset;
|
|
|
|
if (clip_offset >= voicefile_size)
|
|
|
|
end = core_get_data(thumb_handle) + clip_offset - voicefile_size;
|
|
|
|
else
|
|
|
|
end = core_get_data(talk_handle) + clip_offset;
|
|
|
|
len = queue[queue_read].length;
|
2004-03-21 17:45:45 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (pos >= end && pos <= (end+len)) /* really our clip? */
|
2004-05-09 09:41:23 +00:00
|
|
|
{ /* (for strange reasons this isn't nesessarily the case) */
|
|
|
|
/* find the next frame boundary */
|
2013-05-30 09:24:16 +00:00
|
|
|
while (search < (end+len)) /* search the remaining data */
|
2004-03-21 17:45:45 +00:00
|
|
|
{
|
2004-05-09 09:41:23 +00:00
|
|
|
if (*search++ != 0xFF) /* quick search for frame sync byte */
|
|
|
|
continue; /* (this does the majority of the job) */
|
|
|
|
|
|
|
|
/* look at the (bitswapped) rest of header candidate */
|
|
|
|
if (search[0] == curr_hd[0] /* do the quicker checks first */
|
|
|
|
&& search[2] == curr_hd[2]
|
|
|
|
&& (search[1] & 0x30) == (curr_hd[1] & 0x30)) /* sample rate */
|
|
|
|
{
|
|
|
|
search--; /* back to the sync byte */
|
|
|
|
break; /* From looking at it, this is our header. */
|
|
|
|
}
|
2004-03-21 17:45:45 +00:00
|
|
|
}
|
2004-04-20 22:11:20 +00:00
|
|
|
|
2004-05-09 09:41:23 +00:00
|
|
|
if (search-pos)
|
|
|
|
{ /* play old data until the frame end, to keep the MAS in sync */
|
|
|
|
sent = search-pos;
|
|
|
|
|
2004-11-18 22:59:20 +00:00
|
|
|
queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */
|
2013-05-30 09:24:16 +00:00
|
|
|
queue[queue_read].length = sent; /* current one ends after this */
|
2004-05-09 09:41:23 +00:00
|
|
|
|
2005-02-02 21:50:24 +00:00
|
|
|
#if CONFIG_CPU == SH7034
|
2004-05-09 09:41:23 +00:00
|
|
|
DTCR3 = sent; /* let the DMA finish this frame */
|
|
|
|
CHCR3 |= 0x0001; /* re-enable DMA */
|
2007-10-19 15:31:42 +00:00
|
|
|
#endif /* CONFIG_CPU == SH7034 */
|
2008-07-15 14:06:11 +00:00
|
|
|
thumbnail_buf_used = 0;
|
2007-10-19 15:31:42 +00:00
|
|
|
return;
|
2004-05-09 09:41:23 +00:00
|
|
|
}
|
2004-04-20 22:11:20 +00:00
|
|
|
}
|
2007-10-19 15:31:42 +00:00
|
|
|
#endif /* CONFIG_CODEC != SWCODEC */
|
2004-03-21 17:45:45 +00:00
|
|
|
|
2007-10-19 15:31:42 +00:00
|
|
|
/* Either SWCODEC, or MAS had nothing to do (was frame boundary or not our clip) */
|
2006-10-25 07:08:00 +00:00
|
|
|
mp3_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 */
|
2013-05-30 09:24:16 +00:00
|
|
|
talk_handle_locked = MAX(talk_handle_locked-1, 0);
|
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-05-30 09:24:16 +00:00
|
|
|
static void queue_clip(unsigned long clip_offset, long size, bool enqueue)
|
2004-03-19 22:15:53 +00:00
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
unsigned char *buf;
|
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;
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2004-05-09 09:41:23 +00:00
|
|
|
if (!size)
|
2007-10-19 15:31:42 +00:00
|
|
|
return; /* safety check */
|
2005-02-02 21:50:24 +00:00
|
|
|
#if CONFIG_CPU == SH7034
|
2004-05-09 09:41:23 +00:00
|
|
|
/* disable the DMA temporarily, to be safe of race condition */
|
|
|
|
CHCR3 &= ~0x0001;
|
2005-02-02 21:50:24 +00:00
|
|
|
#endif
|
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 */
|
|
|
|
|
|
|
|
if (queue_level < QUEUE_SIZE - 1) /* space left? */
|
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
queue[queue_write].offset = clip_offset; /* populate an entry */
|
|
|
|
queue[queue_write].length = size;
|
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-05-30 09:24:16 +00:00
|
|
|
lastclip_offset = clip_offset;
|
2005-08-29 21:15:27 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2004-05-09 09:41:23 +00:00
|
|
|
sent = MIN(size, 0xFFFF); /* DMA can do no more */
|
2005-08-20 11:13:19 +00:00
|
|
|
#else
|
|
|
|
sent = size;
|
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
talk_handle_locked++;
|
|
|
|
if (clip_offset >= voicefile_size)
|
|
|
|
buf = core_get_data(thumb_handle) + clip_offset - voicefile_size;
|
|
|
|
else
|
|
|
|
buf = core_get_data(talk_handle) + clip_offset;
|
2004-05-09 09:41:23 +00:00
|
|
|
mp3_play_data(buf, sent, mp3_callback);
|
2004-04-20 21:47:07 +00:00
|
|
|
curr_hd[0] = buf[1];
|
|
|
|
curr_hd[1] = buf[2];
|
|
|
|
curr_hd[2] = buf[3];
|
2004-03-19 22:15:53 +00:00
|
|
|
mp3_play_pause(true); /* kickoff audio */
|
|
|
|
}
|
2004-05-09 09:41:23 +00:00
|
|
|
else
|
|
|
|
{
|
2005-02-02 21:50:24 +00:00
|
|
|
#if CONFIG_CPU == SH7034
|
2004-05-09 09:41:23 +00:00
|
|
|
CHCR3 |= 0x0001; /* re-enable DMA */
|
2005-02-02 21:50:24 +00:00
|
|
|
#endif
|
2004-05-09 09:41:23 +00:00
|
|
|
}
|
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
|
|
|
#if CONFIG_CODEC == SWCODEC
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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 CONFIG_CODEC == SWCODEC
|
|
|
|
if(!talk_initialized)
|
|
|
|
mutex_init(&queue_mutex);
|
|
|
|
#endif /* CONFIG_CODEC == SWCODEC */
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
#ifdef TALK_PARTIAL_LOAD
|
|
|
|
for(int i=0; i<QUEUE_SIZE; i++)
|
|
|
|
buffered_id[i] = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
silence_offset = -1; /* pause clip not accessible */
|
|
|
|
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();
|
2013-05-30 09:24:16 +00:00
|
|
|
if (filehandle < 0)
|
2010-09-01 00:08:50 +00:00
|
|
|
return;
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (!load_voicefile_index(filehandle))
|
|
|
|
goto out;
|
2010-09-01 00:08:50 +00:00
|
|
|
|
|
|
|
#ifdef TALK_PARTIAL_LOAD
|
2013-05-30 09:24:16 +00:00
|
|
|
/* TALK_PARTIAL_LOAD loads the actual clip data later, and not all
|
|
|
|
* at once */
|
|
|
|
unsigned num_clips = voicefile.id1_max + voicefile.id2_max;
|
|
|
|
struct clip_entry *clips = core_get_data(index_handle);
|
2010-09-01 00:08:50 +00:00
|
|
|
int silence_size = 0;
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
for(unsigned i=0; i<num_clips; i++) {
|
|
|
|
int size = clips[i].size;
|
2010-09-01 00:08:50 +00:00
|
|
|
if (size > max_clipsize)
|
|
|
|
max_clipsize = size;
|
|
|
|
if (i == VOICE_PAUSE)
|
|
|
|
silence_size = size;
|
2004-03-20 16:49:58 +00:00
|
|
|
}
|
2006-08-23 08:21:15 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
voicefile_size = voicefile.table + num_clips * sizeof(struct clip_entry);
|
2010-09-01 00:08:50 +00:00
|
|
|
voicefile_size += max_clipsize * QUEUE_SIZE + silence_size;
|
2011-08-14 15:13:00 +00:00
|
|
|
|
|
|
|
/* test if we can open and if it fits in the audiobuffer */
|
2011-08-30 14:01:33 +00:00
|
|
|
size_t audiobufsz = audio_buffer_available();
|
2013-05-30 09:24:16 +00:00
|
|
|
has_voicefile = audiobufsz >= voicefile_size;
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* load the compressed clip data into memory, in its entirety */
|
|
|
|
voicefile_size = filesize(filehandle);
|
|
|
|
if (!load_voicefile_data(filehandle, voicefile_size))
|
|
|
|
{
|
2010-09-01 00:08:50 +00:00
|
|
|
voicefile_size = 0;
|
2013-05-30 09:24:16 +00:00
|
|
|
goto out;
|
2010-09-01 00:08:50 +00:00
|
|
|
}
|
2013-05-30 09:24:16 +00:00
|
|
|
has_voicefile = true;
|
|
|
|
#endif
|
2012-05-02 21:22:28 +00:00
|
|
|
|
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2014-01-29 06:14:58 +00:00
|
|
|
/* Initialize the actual voice clip playback engine as well */
|
|
|
|
if (talk_voice_required())
|
|
|
|
voice_thread_init();
|
2012-05-02 21:22:28 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
close(filehandle); /* close again, this was just to detect presence */
|
|
|
|
filehandle = -1;
|
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
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
int clip;
|
2005-02-12 11:26:36 +00:00
|
|
|
long clipsize;
|
2007-11-20 19:50:52 +00:00
|
|
|
int32_t unit;
|
2008-04-19 13:19:04 +00:00
|
|
|
int decimals;
|
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();
|
|
|
|
if (fd < 0
|
|
|
|
|| !load_voicefile_index(fd)
|
|
|
|
|| !load_voicefile_data(fd, voicefile_size))
|
|
|
|
return -1;
|
|
|
|
}
|
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-05-30 09:24:16 +00:00
|
|
|
clip = get_clip(id, &clipsize);
|
|
|
|
if (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-05-30 09:24:16 +00:00
|
|
|
queue_clip(clip, clipsize, 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;
|
2008-07-15 14:06:11 +00:00
|
|
|
int thumb_used;
|
2013-05-30 09:24:16 +00:00
|
|
|
char *buf;
|
2007-11-18 17:12:19 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2004-03-27 00:11:01 +00:00
|
|
|
struct mp3entry info;
|
2007-11-18 17:12:19 +00:00
|
|
|
#endif
|
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-19 22:15:53 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
if (!alloc_thumbnail_buf())
|
|
|
|
return -1;
|
2004-03-19 22:15:53 +00:00
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2007-09-19 10:40:55 +00:00
|
|
|
if(mp3info(&info, filename)) /* use this to find real start */
|
2004-03-27 00:11:01 +00:00
|
|
|
{
|
|
|
|
return 0; /* failed to open, or invalid */
|
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
#endif
|
2004-03-27 00:11:01 +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;
|
|
|
|
}
|
|
|
|
|
2008-07-15 14:06:11 +00:00
|
|
|
thumb_used = thumbnail_buf_used;
|
|
|
|
if(filesize(fd) > size_for_thumbnail -thumb_used)
|
|
|
|
{ /* Don't play truncated clips */
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2004-03-27 00:11:01 +00:00
|
|
|
lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
|
2007-11-18 17:12:19 +00:00
|
|
|
#endif
|
2004-03-27 00:11:01 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
talk_handle_locked++;
|
|
|
|
buf = core_get_data(thumb_handle);
|
|
|
|
size = read(fd, buf+thumb_used, size_for_thumbnail - thumb_used);
|
|
|
|
talk_handle_locked--;
|
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
|
|
|
{
|
2007-11-07 09:28:07 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC && !defined(SIMULATOR)
|
2013-05-30 09:24:16 +00:00
|
|
|
/* bitswap doesnt yield() */
|
|
|
|
bitswap(core_get_data(thumb_handle), size);
|
2005-06-06 00:34:07 +00:00
|
|
|
#endif
|
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);
|
2008-07-15 14:06:11 +00:00
|
|
|
talk_queue_lock();
|
2013-05-30 09:24:16 +00:00
|
|
|
thumbnail_buf_used = thumb_used + size;
|
2008-07-15 14:06:11 +00:00
|
|
|
talk_queue_unlock();
|
2013-05-30 09:24:16 +00:00
|
|
|
queue_clip(voicefile_size + thumb_used, size, true);
|
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;
|
|
|
|
}
|
|
|
|
|
2011-12-15 20:58:14 +00:00
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2008-07-15 14:55:31 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2011-12-15 20:58:14 +00:00
|
|
|
#endif
|
2008-07-15 14:55:31 +00:00
|
|
|
|
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 */
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n<0)
|
|
|
|
{
|
|
|
|
talk_id(VOICE_MINUS, true);
|
|
|
|
n = -n;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int hours, mins;
|
|
|
|
if (!enqueue)
|
|
|
|
talk_shutup();
|
|
|
|
if((hours = secs/3600)) {
|
|
|
|
secs %= 3600;
|
|
|
|
talk_value(hours, UNIT_HOUR, true);
|
|
|
|
}
|
|
|
|
if((mins = secs/60)) {
|
|
|
|
secs %= 60;
|
2011-02-20 15:23:18 +00:00
|
|
|
talk_value(mins, UNIT_MIN, true);
|
2007-11-03 03:43:12 +00:00
|
|
|
}
|
2011-02-20 15:23:18 +00:00
|
|
|
if((secs) || (!hours && !mins))
|
2007-11-03 03:43:12 +00:00
|
|
|
talk_value(secs, UNIT_SEC, true);
|
|
|
|
else if(!hours && secs)
|
|
|
|
talk_number(secs, true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2004-08-26 20:30:22 +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]
|
|
|
|
= VOICE_SECONDS,
|
|
|
|
[UNIT_MIN]
|
|
|
|
= VOICE_MINUTES,
|
|
|
|
[UNIT_HOUR]
|
|
|
|
= VOICE_HOURS,
|
|
|
|
[UNIT_KHZ]
|
|
|
|
= VOICE_KHZ,
|
|
|
|
[UNIT_DB]
|
|
|
|
= VOICE_DB,
|
|
|
|
[UNIT_PERCENT]
|
|
|
|
= VOICE_PERCENT,
|
|
|
|
[UNIT_MAH]
|
|
|
|
= VOICE_MILLIAMPHOURS,
|
|
|
|
[UNIT_PIXEL]
|
|
|
|
= VOICE_PIXEL,
|
|
|
|
[UNIT_PER_SEC]
|
|
|
|
= VOICE_PER_SEC,
|
|
|
|
[UNIT_HERTZ]
|
|
|
|
= VOICE_HERTZ,
|
|
|
|
[UNIT_MB]
|
|
|
|
= LANG_MEGABYTE,
|
|
|
|
[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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
|
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 */
|
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 == '.')
|
2004-10-25 20:44:37 +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++;
|
|
|
|
else
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 08:12:01 +00:00
|
|
|
#if CONFIG_RTC
|
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)
|
|
|
|
{
|
|
|
|
talk_id(VOICE_HUNDRED, true);
|
|
|
|
talk_id(VOICE_HOUR, true);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2010-09-01 00:08:50 +00:00
|
|
|
#endif /* CONFIG_RTC */
|