Prepare new maemo release
[maemo-rb.git] / apps / talk.c
blobb94dcf18eeb74374861d2cb5f2486cc7f95b61a1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Jörg Hohensohn
12 * This module collects the Talkbox and voice UI functions.
13 * (Talkbox reads directory names from mp3 clips called thumbnails,
14 * the voice UI lets menus and screens "talk" from a voicefile in memory.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
26 #include <stdio.h>
27 #include <stddef.h>
28 #include "string-extra.h"
29 #include "file.h"
30 #include "system.h"
31 #include "kernel.h"
32 #include "settings.h"
33 #include "settings_list.h"
34 #if CONFIG_CODEC == SWCODEC
35 #include "voice_thread.h"
36 #else
37 #include "mp3_playback.h"
38 #endif
39 #include "audio.h"
40 #include "lang.h"
41 #include "talk.h"
42 #include "metadata.h"
43 /*#define LOGF_ENABLE*/
44 #include "logf.h"
45 #include "bitswap.h"
46 #include "structec.h"
47 #include "plugin.h" /* plugin_get_buffer() */
48 #include "debug.h"
50 /* Memory layout varies between targets because the
51 Archos (MASCODEC) devices cannot mix voice and audio playback
53 MASCODEC | MASCODEC | SWCODEC
54 (playing) | (stopped) |
55 voicebuf-----------+-----------+------------
56 audio | voice | voice
57 |-----------|------------
58 | thumbnail | thumbnail
59 | |------------
60 | | filebuf
61 | |------------
62 | | audio
63 voicebufend----------+-----------+------------
65 SWCODEC allocates dedicated buffers (except voice and thumbnail are together
66 in the talkbuf), MASCODEC reuses audiobuf. */
69 /***************** Constants *****************/
71 #define QUEUE_SIZE 64 /* must be a power of two */
72 #define QUEUE_MASK (QUEUE_SIZE-1)
73 const char* const dir_thumbnail_name = "_dirname.talk";
74 const char* const file_thumbnail_ext = ".talk";
76 /***************** Functional Macros *****************/
78 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
80 #define LOADED_MASK 0x80000000 /* MSB */
82 /* swcodec: cap p_thumnail to MAX_THUMNAIL_BUFSIZE since audio keeps playing
83 * while voice
84 * hwcodec: just use whatever is left in the audiobuffer, music
85 * playback is impossible => no cap */
86 #if CONFIG_CODEC == SWCODEC
87 #define MAX_THUMBNAIL_BUFSIZE 0x10000
88 #endif
90 /***************** Data types *****************/
92 struct clip_entry /* one entry of the index table */
94 int offset; /* offset from start of voicefile file */
95 int size; /* size of the clip */
98 struct voicefile /* file format of our voice file */
100 int version; /* version of the voicefile */
101 int target_id; /* the rockbox target the file was made for */
102 int table; /* offset to index table, (=header size) */
103 int id1_max; /* number of "normal" clips contained in above index */
104 int id2_max; /* number of "voice only" clips contained in above index */
105 struct clip_entry index[]; /* followed by the index tables */
106 /* and finally the mp3 clips, not visible here, bitswapped
107 for SH based players */
110 struct queue_entry /* one entry of the internal queue */
112 unsigned char* buf;
113 long len;
117 /***************** Globals *****************/
119 #if (CONFIG_CODEC == SWCODEC && MEMORYSIZE <= 2) || defined(ONDIO_SERIES)
120 /* On low memory swcodec targets the entire voice file wouldn't fit in memory
121 * together with codecs, so we load clips each time they are accessed.
122 * The Ondios have slow storage access and loading the entire voice file would
123 * take several seconds, so we use the same mechanism. */
124 #define TALK_PARTIAL_LOAD
125 #endif
127 #ifdef TALK_PARTIAL_LOAD
128 static unsigned char *clip_buffer;
129 static long max_clipsize; /* size of the biggest clip */
130 static long buffered_id[QUEUE_SIZE]; /* IDs of the talk clips */
131 static uint8_t clip_age[QUEUE_SIZE];
132 #if QUEUE_SIZE > 255
133 # error clip_age[] type too small
134 #endif
135 #endif
137 static char* voicebuf; /* root pointer to our buffer */
138 static unsigned char* p_thumbnail = NULL; /* buffer for thumbnails */
139 /* Multiple thumbnails can be loaded back-to-back in this buffer. */
140 static volatile int thumbnail_buf_used SHAREDBSS_ATTR; /* length of data in
141 thumbnail buffer */
142 static long size_for_thumbnail; /* total thumbnail buffer size */
143 static struct voicefile* p_voicefile; /* loaded voicefile */
144 static bool has_voicefile; /* a voicefile file is present */
145 static bool need_shutup; /* is there possibly any voice playing to be shutup */
146 static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
147 static bool force_enqueue_next; /* enqueue next utterance even if enqueue is false */
148 static int queue_write; /* write index of queue, by application */
149 static int queue_read; /* read index of queue, by ISR context */
150 #if CONFIG_CODEC == SWCODEC
151 /* protects queue_read, queue_write and thumbnail_buf_used */
152 static struct mutex queue_mutex SHAREDBSS_ATTR;
153 #define talk_queue_lock() ({ mutex_lock(&queue_mutex); })
154 #define talk_queue_unlock() ({ mutex_unlock(&queue_mutex); })
155 #else
156 #define talk_queue_lock() ({ })
157 #define talk_queue_unlock() ({ })
158 #endif /* CONFIG_CODEC */
159 static int sent; /* how many bytes handed over to playback, owned by ISR */
160 static unsigned char curr_hd[3]; /* current frame header, for re-sync */
161 static int filehandle = -1; /* global, so we can keep the file open if needed */
162 static unsigned char* p_silence; /* VOICE_PAUSE clip, used for termination */
163 static long silence_len; /* length of the VOICE_PAUSE clip */
164 static unsigned char* p_lastclip; /* address of latest clip, for silence add */
165 static unsigned long voicefile_size = 0; /* size of the loaded voice file */
166 static unsigned char last_lang[MAX_FILENAME+1]; /* name of last used lang file (in talk_init) */
167 static bool talk_initialized; /* true if talk_init has been called */
168 static int talk_temp_disable_count; /* if positive, temporarily disable voice UI (not saved) */
171 /***************** Private implementation *****************/
173 static int open_voicefile(void)
175 char buf[64];
176 char* p_lang = "english"; /* default */
178 if ( global_settings.lang_file[0] &&
179 global_settings.lang_file[0] != 0xff )
180 { /* try to open the voice file of the selected language */
181 p_lang = (char *)global_settings.lang_file;
184 snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang);
186 return open(buf, O_RDONLY);
190 /* fetch a clip from the voice file */
191 static unsigned char* get_clip(long id, long* p_size)
193 long clipsize;
194 unsigned char* clipbuf;
196 if (id > VOICEONLY_DELIMITER)
197 { /* voice-only entries use the second part of the table.
198 The first string comes after VOICEONLY_DELIMITER so we need to
199 substract VOICEONLY_DELIMITER + 1 */
200 id -= VOICEONLY_DELIMITER + 1;
201 if (id >= p_voicefile->id2_max)
202 return NULL; /* must be newer than we have */
203 id += p_voicefile->id1_max; /* table 2 is behind table 1 */
205 else
206 { /* normal use of the first table */
207 if (id >= p_voicefile->id1_max)
208 return NULL; /* must be newer than we have */
211 clipsize = p_voicefile->index[id].size;
212 if (clipsize == 0) /* clip not included in voicefile */
213 return NULL;
215 #ifndef TALK_PARTIAL_LOAD
216 clipbuf = (unsigned char *) p_voicefile + p_voicefile->index[id].offset;
217 #endif
219 #ifdef TALK_PARTIAL_LOAD
220 if (!(clipsize & LOADED_MASK))
221 { /* clip needs loading */
222 int idx = 0;
223 if (id == VOICE_PAUSE) {
224 idx = QUEUE_SIZE; /* we keep VOICE_PAUSE loaded */
225 } else {
226 int oldest = 0, i;
227 for(i=0; i<QUEUE_SIZE; i++) {
228 if (buffered_id[i] < 0) {
229 /* found a free entry, that means the buffer isn't
230 * full yet. */
231 idx = i;
232 break;
235 /* find the oldest clip */
236 if(clip_age[i] > oldest) {
237 idx = i;
238 oldest = clip_age[i];
241 /* increment age of each loaded clip */
242 clip_age[i]++;
244 clip_age[idx] = 0; /* reset clip's age */
246 clipbuf = clip_buffer + idx * max_clipsize;
248 lseek(filehandle, p_voicefile->index[id].offset, SEEK_SET);
249 if (read(filehandle, clipbuf, clipsize) != clipsize)
250 return NULL; /* read error */
252 p_voicefile->index[id].size |= LOADED_MASK; /* mark as loaded */
254 if (id != VOICE_PAUSE) {
255 if (buffered_id[idx] >= 0) {
256 /* mark previously loaded clip as unloaded */
257 p_voicefile->index[buffered_id[idx]].size &= ~LOADED_MASK;
259 buffered_id[idx] = id;
262 else
263 { /* clip is in memory already */
264 /* Find where it was loaded */
265 clipbuf = clip_buffer;
266 if (id == VOICE_PAUSE) {
267 clipbuf += QUEUE_SIZE * max_clipsize;
268 } else {
269 int idx;
270 for (idx=0; idx<QUEUE_SIZE; idx++)
271 if (buffered_id[idx] == id) {
272 clipbuf += idx * max_clipsize;
273 clip_age[idx] = 0; /* reset clip's age */
274 break;
277 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
279 #endif /* TALK_PARTIAL_LOAD */
281 *p_size = clipsize;
282 return clipbuf;
286 /* load the voice file into the mp3 buffer */
287 static void load_voicefile(bool probe, char* buf, size_t bufsize)
289 union voicebuf {
290 unsigned char* buf;
291 struct voicefile* file;
293 union voicebuf voicebuf;
295 size_t load_size, alloc_size;
296 ssize_t got_size;
297 #ifdef ROCKBOX_LITTLE_ENDIAN
298 int i;
299 #endif
301 if (!probe)
302 filehandle = open_voicefile();
303 if (filehandle < 0) /* failed to open */
304 goto load_err;
306 voicebuf.buf = buf;
307 if (!voicebuf.buf)
308 goto load_err;
310 #ifdef TALK_PARTIAL_LOAD
311 /* load only the header for now */
312 load_size = sizeof(struct voicefile);
313 #else
314 /* load the entire file */
315 load_size = filesize(filehandle);
316 #endif
317 if (load_size > bufsize) /* won't fit? */
318 goto load_err;
320 got_size = read(filehandle, voicebuf.buf, load_size);
321 if (got_size != (ssize_t)load_size /* failure */)
322 goto load_err;
324 alloc_size = load_size;
326 #ifdef ROCKBOX_LITTLE_ENDIAN
327 logf("Byte swapping voice file");
328 structec_convert(voicebuf.buf, "lllll", 1, true);
329 #endif
331 /* format check */
332 if (voicebuf.file->table == sizeof(struct voicefile))
334 p_voicefile = voicebuf.file;
336 if (p_voicefile->version != VOICE_VERSION ||
337 p_voicefile->target_id != TARGET_ID)
339 logf("Incompatible voice file");
340 goto load_err;
343 else
344 goto load_err;
346 #ifdef TALK_PARTIAL_LOAD
347 /* load the index table, now that we know its size from the header */
348 load_size = (p_voicefile->id1_max + p_voicefile->id2_max)
349 * sizeof(struct clip_entry);
351 if (load_size > bufsize) /* won't fit? */
352 goto load_err;
354 got_size = read(filehandle, &p_voicefile->index[0], load_size);
355 if (got_size != (ssize_t)load_size) /* read error */
356 goto load_err;
358 alloc_size += load_size;
359 #else
360 close(filehandle);
361 filehandle = -1;
362 #endif /* TALK_PARTIAL_LOAD */
364 #ifdef ROCKBOX_LITTLE_ENDIAN
365 for (i = 0; i < p_voicefile->id1_max + p_voicefile->id2_max; i++)
366 structec_convert(&p_voicefile->index[i], "ll", 1, true);
367 #endif
369 #ifdef TALK_PARTIAL_LOAD
370 clip_buffer = (unsigned char *) p_voicefile + p_voicefile->table;
371 unsigned clips = p_voicefile->id1_max + p_voicefile->id2_max;
372 clip_buffer += clips * sizeof(struct clip_entry); /* skip index */
373 #endif
374 if (!probe) {
375 /* make sure to have the silence clip, if available */
376 p_silence = get_clip(VOICE_PAUSE, &silence_len);
379 #ifdef TALK_PARTIAL_LOAD
380 alloc_size += silence_len + QUEUE_SIZE;
381 #endif
383 if (alloc_size > bufsize)
384 goto load_err;
386 /* now move p_thumbnail behind the voice clip buffer */
387 p_thumbnail = voicebuf.buf + alloc_size;
388 p_thumbnail += (long)p_thumbnail % 2; /* 16-bit align */
389 size_for_thumbnail = voicebuf.buf + bufsize - p_thumbnail;
390 #if CONFIG_CODEC == SWCODEC
391 size_for_thumbnail = MIN(size_for_thumbnail, MAX_THUMBNAIL_BUFSIZE);
392 #endif
393 if (size_for_thumbnail <= 0)
394 p_thumbnail = NULL;
396 return;
397 load_err:
398 p_voicefile = NULL;
399 has_voicefile = false; /* don't try again */
400 if (filehandle >= 0)
402 close(filehandle);
403 filehandle = -1;
405 return;
409 /* called in ISR context (on HWCODEC) if mp3 data got consumed */
410 static void mp3_callback(const void** start, size_t* size)
412 queue[queue_read].len -= sent; /* we completed this */
413 queue[queue_read].buf += sent;
415 if (queue[queue_read].len > 0) /* current clip not finished? */
416 { /* feed the next 64K-1 chunk */
417 #if CONFIG_CODEC != SWCODEC
418 sent = MIN(queue[queue_read].len, 0xFFFF);
419 #else
420 sent = queue[queue_read].len;
421 #endif
422 *start = queue[queue_read].buf;
423 *size = sent;
424 return;
426 talk_queue_lock();
427 if(p_thumbnail
428 && queue[queue_read].buf == p_thumbnail +thumbnail_buf_used)
429 thumbnail_buf_used = 0;
430 if (sent > 0) /* go to next entry */
432 queue_read = (queue_read + 1) & QUEUE_MASK;
435 re_check:
437 if (QUEUE_LEVEL != 0) /* queue is not empty? */
438 { /* start next clip */
439 #if CONFIG_CODEC != SWCODEC
440 sent = MIN(queue[queue_read].len, 0xFFFF);
441 #else
442 sent = queue[queue_read].len;
443 #endif
444 *start = p_lastclip = queue[queue_read].buf;
445 *size = sent;
446 curr_hd[0] = p_lastclip[1];
447 curr_hd[1] = p_lastclip[2];
448 curr_hd[2] = p_lastclip[3];
450 else if (p_silence != NULL /* silence clip available */
451 && p_lastclip != p_silence /* previous clip wasn't silence */
452 && !(p_lastclip >= p_thumbnail /* ..or thumbnail */
453 && p_lastclip < p_thumbnail +size_for_thumbnail))
454 { /* add silence clip when queue runs empty playing a voice clip */
455 queue[queue_write].buf = p_silence;
456 queue[queue_write].len = silence_len;
457 queue_write = (queue_write + 1) & QUEUE_MASK;
459 goto re_check;
461 else
463 *size = 0; /* end of data */
465 talk_queue_unlock();
468 /***************** Public routines *****************/
470 /* stop the playback and the pending clips */
471 void talk_force_shutup(void)
473 /* Most of this is MAS only */
474 #if CONFIG_CODEC != SWCODEC
475 #ifdef SIMULATOR
476 return;
477 #endif
478 unsigned char* pos;
479 unsigned char* search;
480 unsigned char* end;
481 if (QUEUE_LEVEL == 0) /* has ended anyway */
482 return;
484 #if CONFIG_CPU == SH7034
485 CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
486 #endif /* CONFIG_CPU == SH7034 */
487 /* search next frame boundary and continue up to there */
488 pos = search = mp3_get_pos();
489 end = queue[queue_read].buf + queue[queue_read].len;
491 if (pos >= queue[queue_read].buf
492 && pos <= end) /* really our clip? */
493 { /* (for strange reasons this isn't nesessarily the case) */
494 /* find the next frame boundary */
495 while (search < end) /* search the remaining data */
497 if (*search++ != 0xFF) /* quick search for frame sync byte */
498 continue; /* (this does the majority of the job) */
500 /* look at the (bitswapped) rest of header candidate */
501 if (search[0] == curr_hd[0] /* do the quicker checks first */
502 && search[2] == curr_hd[2]
503 && (search[1] & 0x30) == (curr_hd[1] & 0x30)) /* sample rate */
505 search--; /* back to the sync byte */
506 break; /* From looking at it, this is our header. */
510 if (search-pos)
511 { /* play old data until the frame end, to keep the MAS in sync */
512 sent = search-pos;
514 queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */
515 queue[queue_read].len = sent; /* current one ends after this */
517 #if CONFIG_CPU == SH7034
518 DTCR3 = sent; /* let the DMA finish this frame */
519 CHCR3 |= 0x0001; /* re-enable DMA */
520 #endif /* CONFIG_CPU == SH7034 */
521 thumbnail_buf_used = 0;
522 return;
525 #endif /* CONFIG_CODEC != SWCODEC */
527 /* Either SWCODEC, or MAS had nothing to do (was frame boundary or not our clip) */
528 mp3_play_stop();
529 talk_queue_lock();
530 queue_write = queue_read = 0; /* reset the queue */
531 thumbnail_buf_used = 0;
532 talk_queue_unlock();
533 need_shutup = false;
536 /* Shutup the voice, except if force_enqueue_next is set. */
537 void talk_shutup(void)
539 if (need_shutup && !force_enqueue_next)
540 talk_force_shutup();
543 /* schedule a clip, at the end or discard the existing queue */
544 static void queue_clip(unsigned char* buf, long size, bool enqueue)
546 int queue_level;
548 if (!enqueue)
549 talk_shutup(); /* cut off all the pending stuff */
550 /* Something is being enqueued, force_enqueue_next override is no
551 longer in effect. */
552 force_enqueue_next = false;
554 if (!size)
555 return; /* safety check */
556 #if CONFIG_CPU == SH7034
557 /* disable the DMA temporarily, to be safe of race condition */
558 CHCR3 &= ~0x0001;
559 #endif
560 talk_queue_lock();
561 queue_level = QUEUE_LEVEL; /* check old level */
563 if (queue_level < QUEUE_SIZE - 1) /* space left? */
565 queue[queue_write].buf = buf; /* populate an entry */
566 queue[queue_write].len = size;
567 queue_write = (queue_write + 1) & QUEUE_MASK;
569 talk_queue_unlock();
571 if (queue_level == 0)
572 { /* queue was empty, we have to do the initial start */
573 p_lastclip = buf;
574 #if CONFIG_CODEC != SWCODEC
575 sent = MIN(size, 0xFFFF); /* DMA can do no more */
576 #else
577 sent = size;
578 #endif
579 mp3_play_data(buf, sent, mp3_callback);
580 curr_hd[0] = buf[1];
581 curr_hd[1] = buf[2];
582 curr_hd[2] = buf[3];
583 mp3_play_pause(true); /* kickoff audio */
585 else
587 #if CONFIG_CPU == SH7034
588 CHCR3 |= 0x0001; /* re-enable DMA */
589 #endif
592 need_shutup = true;
594 return;
597 static void alloc_thumbnail_buf(void)
599 /* use the audio buffer now, need to release before loading a voice */
600 p_thumbnail = voicebuf;
601 #if CONFIG_CODEC == SWCODEC
602 size_for_thumbnail = MAX_THUMBNAIL_BUFSIZE;
603 #endif
604 thumbnail_buf_used = 0;
607 /* common code for talk_init() and talk_buffer_steal() */
608 static void reset_state(void)
610 queue_write = queue_read = 0; /* reset the queue */
611 p_voicefile = NULL; /* indicate no voicefile (trashed) */
612 p_thumbnail = NULL; /* no thumbnails either */
614 #ifdef TALK_PARTIAL_LOAD
615 int i;
616 for(i=0; i<QUEUE_SIZE; i++)
617 buffered_id[i] = -1;
618 #endif
620 p_silence = NULL; /* pause clip not accessible */
621 voicebuf = NULL; /* voice buffer is gone */
624 #if CONFIG_CODEC == SWCODEC
625 static bool restore_state(void)
627 if (!voicebuf)
629 size_t size;
630 audio_restore_playback(AUDIO_WANT_VOICE);
631 voicebuf = audio_get_buffer(true, &size);
632 audio_get_buffer(false, &size);
635 return !!voicebuf;
637 #endif /* CONFIG_CODEC == SWCODEC */
640 /***************** Public implementation *****************/
642 void talk_init(void)
644 talk_temp_disable_count = 0;
645 if (talk_initialized && !strcasecmp(last_lang, global_settings.lang_file))
647 /* not a new file, nothing to do */
648 return;
651 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
652 if (filehandle >= 0)
654 close(filehandle);
655 filehandle = -1;
657 #endif
659 #if CONFIG_CODEC == SWCODEC
660 if(!talk_initialized)
661 mutex_init(&queue_mutex);
662 #endif /* CONFIG_CODEC == SWCODEC */
664 talk_initialized = true;
665 strlcpy((char *)last_lang, (char *)global_settings.lang_file,
666 MAX_FILENAME);
668 filehandle = open_voicefile();
669 if (filehandle < 0) {
670 has_voicefile = false;
671 voicefile_size = 0;
672 return;
675 voicefile_size = filesize(filehandle);
677 audio_get_buffer(false, NULL); /* Must tell audio to reinitialize */
678 reset_state(); /* use this for most of our inits */
680 #ifdef TALK_PARTIAL_LOAD
681 size_t bufsize;
682 char* buf = plugin_get_buffer(&bufsize);
683 /* we won't load the full file, we only need the index */
684 load_voicefile(true, buf, bufsize);
685 if (!p_voicefile)
686 return;
688 unsigned clips = p_voicefile->id1_max + p_voicefile->id2_max;
689 unsigned i;
690 int silence_size = 0;
692 for(i=0; i<clips; i++) {
693 int size = p_voicefile->index[i].size;
694 if (size > max_clipsize)
695 max_clipsize = size;
696 if (i == VOICE_PAUSE)
697 silence_size = size;
700 voicefile_size = p_voicefile->table + clips * sizeof(struct clip_entry);
701 voicefile_size += max_clipsize * QUEUE_SIZE + silence_size;
702 p_voicefile = NULL; /* Don't pretend we can load talk clips just yet */
703 #endif
706 /* test if we can open and if it fits in the audiobuffer */
707 size_t audiobufsz = audio_buffer_available();
708 if (voicefile_size <= audiobufsz) {
709 has_voicefile = true;
710 } else {
711 has_voicefile = false;
712 voicefile_size = 0;
715 close(filehandle); /* close again, this was just to detect presence */
716 filehandle = -1;
718 #if CONFIG_CODEC == SWCODEC
719 /* Safe to init voice playback engine now since we now know if talk is
720 required or not */
721 voice_thread_init();
722 #endif
725 #if CONFIG_CODEC == SWCODEC
726 /* return if a voice codec is required or not */
727 bool talk_voice_required(void)
729 return (voicefile_size != 0) /* Voice file is available */
730 || (global_settings.talk_dir_clip) /* Thumbnail clips are required */
731 || (global_settings.talk_file_clip);
733 #endif
735 /* return size of voice file */
736 static size_t talk_get_buffer_size(void)
738 #if CONFIG_CODEC == SWCODEC
739 return voicefile_size + MAX_THUMBNAIL_BUFSIZE;
740 #else
741 return audio_buffer_available();
742 #endif
745 /* Sets the buffer for the voicefile and returns how many bytes of this
746 * buffer we will use for the voicefile */
747 size_t talkbuf_init(char *bufstart)
749 bool changed = voicebuf != bufstart;
751 if (changed) /* must reload voice file */
752 reset_state();
754 if (bufstart)
755 voicebuf = bufstart;
757 return talk_get_buffer_size();
760 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
761 void talk_buffer_steal(void)
763 #if CONFIG_CODEC != SWCODEC
764 mp3_play_stop();
765 #endif
766 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
767 if (filehandle >= 0)
769 close(filehandle);
770 filehandle = -1;
772 #endif
773 reset_state();
776 /* play a voice ID from voicefile */
777 int talk_id(int32_t id, bool enqueue)
779 long clipsize;
780 unsigned char* clipbuf;
781 int32_t unit;
782 int decimals;
784 if (talk_temp_disable_count > 0)
785 return -1; /* talking has been disabled */
786 #if CONFIG_CODEC == SWCODEC
787 /* If talk buffer was stolen, it must be restored for voicefile's sake */
788 if (!restore_state())
789 return -1; /* cannot get any space */
790 #else
791 if (audio_status()) /* busy, buffer in use */
792 return -1;
793 #endif
795 if (p_voicefile == NULL && has_voicefile) /* reload needed? */
796 load_voicefile(false, voicebuf, talk_get_buffer_size());
798 if (p_voicefile == NULL) /* still no voices? */
799 return -1;
801 if (id == -1) /* -1 is an indication for silence */
802 return -1;
804 decimals = (((uint32_t)id) >> DECIMAL_SHIFT) & 0x7;
806 /* check if this is a special ID, with a value */
807 unit = ((uint32_t)id) >> UNIT_SHIFT;
808 if (unit || decimals)
809 { /* sign-extend the value */
810 id = (uint32_t)id << (32-DECIMAL_SHIFT);
811 id >>= (32-DECIMAL_SHIFT);
813 talk_value_decimal(id, unit, decimals, enqueue); /* speak it */
814 return 0; /* and stop, end of special case */
817 clipbuf = get_clip(id, &clipsize);
818 if (clipbuf == NULL)
819 return -1; /* not present */
821 #ifdef LOGF_ENABLE
822 if (id > VOICEONLY_DELIMITER)
823 logf("\ntalk_id: Say voice clip 0x%x\n", id);
824 else
825 logf("\ntalk_id: Say '%s'\n", str(id));
826 #endif
828 queue_clip(clipbuf, clipsize, enqueue);
830 return 0;
832 /* Speaks zero or more IDs (from an array). */
833 int talk_idarray(const long *ids, bool enqueue)
835 int r;
836 if(!ids)
837 return 0;
838 while(*ids != TALK_FINAL_ID)
840 if((r = talk_id(*ids++, enqueue)) <0)
841 return r;
842 enqueue = true;
844 return 0;
847 /* Make sure the current utterance is not interrupted by the next one. */
848 void talk_force_enqueue_next(void)
850 force_enqueue_next = true;
853 /* play a thumbnail from file */
854 /* Returns size of spoken thumbnail, so >0 means something is spoken,
855 <=0 means something went wrong. */
856 static int _talk_file(const char* filename,
857 const long *prefix_ids, bool enqueue)
859 int fd;
860 int size;
861 int thumb_used;
862 #if CONFIG_CODEC != SWCODEC
863 struct mp3entry info;
864 #endif
866 if (talk_temp_disable_count > 0)
867 return -1; /* talking has been disabled */
868 #if CONFIG_CODEC == SWCODEC
869 /* If talk buffer was stolen, it must be restored for thumbnail's sake */
870 if (!restore_state())
871 return -1; /* cannot get any space */
872 #else
873 if (audio_status()) /* busy, buffer in use */
874 return -1;
875 #endif
877 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
878 alloc_thumbnail_buf();
880 #if CONFIG_CODEC != SWCODEC
881 if(mp3info(&info, filename)) /* use this to find real start */
883 return 0; /* failed to open, or invalid */
885 #endif
887 if (!enqueue)
888 /* shutup now to free the thumbnail buffer */
889 talk_shutup();
891 fd = open(filename, O_RDONLY);
892 if (fd < 0) /* failed to open */
894 return 0;
897 thumb_used = thumbnail_buf_used;
898 if(filesize(fd) > size_for_thumbnail -thumb_used)
899 { /* Don't play truncated clips */
900 close(fd);
901 return 0;
904 #if CONFIG_CODEC != SWCODEC
905 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
906 #endif
908 size = read(fd, p_thumbnail +thumb_used,
909 size_for_thumbnail -thumb_used);
910 close(fd);
912 /* ToDo: find audio, skip ID headers and trailers */
914 if (size > 0) /* Don't play missing clips */
916 #if CONFIG_CODEC != SWCODEC && !defined(SIMULATOR)
917 bitswap(p_thumbnail, size);
918 #endif
919 if(prefix_ids)
920 /* prefix thumbnail by speaking these ids, but only now
921 that we know there's actually a thumbnail to be
922 spoken. */
923 talk_idarray(prefix_ids, true);
924 talk_queue_lock();
925 thumbnail_buf_used = thumb_used +size;
926 talk_queue_unlock();
927 queue_clip(p_thumbnail +thumb_used, size, true);
930 return size;
933 int talk_file(const char *root, const char *dir, const char *file,
934 const char *ext, const long *prefix_ids, bool enqueue)
935 /* Play a thumbnail file */
937 char buf[MAX_PATH];
938 /* Does root end with a slash */
939 char *slash = (root && root[0]
940 && root[strlen(root)-1] != '/') ? "/" : "";
941 snprintf(buf, MAX_PATH, "%s%s%s%s%s%s",
942 root ? root : "", slash,
943 dir ? dir : "", dir ? "/" : "",
944 file ? file : "",
945 ext ? ext : "");
946 return _talk_file(buf, prefix_ids, enqueue);
949 static int talk_spell_basename(const char *path,
950 const long *prefix_ids, bool enqueue)
952 if(prefix_ids)
954 talk_idarray(prefix_ids, enqueue);
955 enqueue = true;
957 char buf[MAX_PATH];
958 /* Spell only the path component after the last slash */
959 strlcpy(buf, path, sizeof(buf));
960 if(strlen(buf) >1 && buf[strlen(buf)-1] == '/')
961 /* strip trailing slash */
962 buf[strlen(buf)-1] = '\0';
963 char *ptr = strrchr(buf, '/');
964 if(ptr && strlen(buf) >1)
965 ++ptr;
966 else ptr = buf;
967 return talk_spell(ptr, enqueue);
970 /* Play a file's .talk thumbnail, fallback to spelling the filename, or
971 go straight to spelling depending on settings. */
972 int talk_file_or_spell(const char *dirname, const char *filename,
973 const long *prefix_ids, bool enqueue)
975 if (global_settings.talk_file_clip)
976 { /* .talk clips enabled */
977 if(talk_file(dirname, NULL, filename, file_thumbnail_ext,
978 prefix_ids, enqueue) >0)
979 return 0;
981 if (global_settings.talk_file == 2)
982 /* Either .talk clips are disabled, or as a fallback */
983 return talk_spell_basename(filename, prefix_ids, enqueue);
984 return 0;
987 #if CONFIG_CODEC == SWCODEC
988 /* Play a directory's .talk thumbnail, fallback to spelling the filename, or
989 go straight to spelling depending on settings. */
990 int talk_dir_or_spell(const char* dirname,
991 const long *prefix_ids, bool enqueue)
993 if (global_settings.talk_dir_clip)
994 { /* .talk clips enabled */
995 if(talk_file(dirname, NULL, dir_thumbnail_name, NULL,
996 prefix_ids, enqueue) >0)
997 return 0;
999 if (global_settings.talk_dir == 2)
1000 /* Either .talk clips disabled or as a fallback */
1001 return talk_spell_basename(dirname, prefix_ids, enqueue);
1002 return 0;
1004 #endif
1006 /* say a numeric value, this word ordering works for english,
1007 but not necessarily for other languages (e.g. german) */
1008 int talk_number(long n, bool enqueue)
1010 int level = 2; /* mille count */
1011 long mil = 1000000000; /* highest possible "-illion" */
1013 if (talk_temp_disable_count > 0)
1014 return -1; /* talking has been disabled */
1015 #if CONFIG_CODEC != SWCODEC
1016 if (audio_status()) /* busy, buffer in use */
1017 return -1;
1018 #endif
1020 if (!enqueue)
1021 talk_shutup(); /* cut off all the pending stuff */
1023 if (n==0)
1024 { /* special case */
1025 talk_id(VOICE_ZERO, true);
1026 return 0;
1029 if (n<0)
1031 talk_id(VOICE_MINUS, true);
1032 n = -n;
1035 while (n)
1037 int segment = n / mil; /* extract in groups of 3 digits */
1038 n -= segment * mil; /* remove the used digits from number */
1039 mil /= 1000; /* digit place for next round */
1041 if (segment)
1043 int hundreds = segment / 100;
1044 int ones = segment % 100;
1046 if (hundreds)
1048 talk_id(VOICE_ZERO + hundreds, true);
1049 talk_id(VOICE_HUNDRED, true);
1052 /* combination indexing */
1053 if (ones > 20)
1055 int tens = ones/10 + 18;
1056 talk_id(VOICE_ZERO + tens, true);
1057 ones %= 10;
1060 /* direct indexing */
1061 if (ones)
1062 talk_id(VOICE_ZERO + ones, true);
1064 /* add billion, million, thousand */
1065 if (mil)
1066 talk_id(VOICE_THOUSAND + level, true);
1068 level--;
1071 return 0;
1074 /* Say time duration/interval. Input is time in seconds,
1075 say hours,minutes,seconds. */
1076 static int talk_time_unit(long secs, bool enqueue)
1078 int hours, mins;
1079 if (!enqueue)
1080 talk_shutup();
1081 if((hours = secs/3600)) {
1082 secs %= 3600;
1083 talk_value(hours, UNIT_HOUR, true);
1085 if((mins = secs/60)) {
1086 secs %= 60;
1087 talk_value(mins, UNIT_MIN, true);
1089 if((secs) || (!hours && !mins))
1090 talk_value(secs, UNIT_SEC, true);
1091 else if(!hours && secs)
1092 talk_number(secs, true);
1093 return 0;
1096 void talk_fractional(char *tbuf, int value, int unit)
1098 int i;
1099 /* strip trailing zeros from the fraction */
1100 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
1101 tbuf[i] = '\0';
1103 talk_number(value, true);
1104 if (tbuf[0] != 0)
1106 talk_id(LANG_POINT, true);
1107 talk_spell(tbuf, true);
1109 talk_id(unit, true);
1112 int talk_value(long n, int unit, bool enqueue)
1114 return talk_value_decimal(n, unit, 0, enqueue);
1117 /* singular/plural aware saying of a value */
1118 int talk_value_decimal(long n, int unit, int decimals, bool enqueue)
1120 int unit_id;
1121 static const int unit_voiced[] =
1122 { /* lookup table for the voice ID of the units */
1123 [0 ... UNIT_LAST-1] = -1, /* regular ID, int, signed */
1124 [UNIT_MS]
1125 = VOICE_MILLISECONDS, /* here come the "real" units */
1126 [UNIT_SEC]
1127 = VOICE_SECONDS,
1128 [UNIT_MIN]
1129 = VOICE_MINUTES,
1130 [UNIT_HOUR]
1131 = VOICE_HOURS,
1132 [UNIT_KHZ]
1133 = VOICE_KHZ,
1134 [UNIT_DB]
1135 = VOICE_DB,
1136 [UNIT_PERCENT]
1137 = VOICE_PERCENT,
1138 [UNIT_MAH]
1139 = VOICE_MILLIAMPHOURS,
1140 [UNIT_PIXEL]
1141 = VOICE_PIXEL,
1142 [UNIT_PER_SEC]
1143 = VOICE_PER_SEC,
1144 [UNIT_HERTZ]
1145 = VOICE_HERTZ,
1146 [UNIT_MB]
1147 = LANG_MEGABYTE,
1148 [UNIT_KBIT]
1149 = VOICE_KBIT_PER_SEC,
1150 [UNIT_PM_TICK]
1151 = VOICE_PM_UNITS_PER_TICK,
1154 static const int pow10[] = { /* 10^0 - 10^7 */
1155 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
1158 char tbuf[8];
1159 char fmt[] = "%0nd";
1161 if (talk_temp_disable_count > 0)
1162 return -1; /* talking has been disabled */
1163 #if CONFIG_CODEC != SWCODEC
1164 if (audio_status()) /* busy, buffer in use */
1165 return -1;
1166 #endif
1168 /* special case for time duration */
1169 if (unit == UNIT_TIME)
1170 return talk_time_unit(n, enqueue);
1172 if (unit < 0 || unit >= UNIT_LAST)
1173 unit_id = -1;
1174 else
1175 unit_id = unit_voiced[unit];
1177 if ((n==1 || n==-1) /* singular? */
1178 && unit_id >= VOICE_SECONDS && unit_id <= VOICE_HOURS)
1180 unit_id--; /* use the singular for those units which have */
1183 /* special case with a "plus" before */
1184 if (n > 0 && (unit == UNIT_SIGNED || unit == UNIT_DB))
1186 talk_id(VOICE_PLUS, enqueue);
1187 enqueue = true;
1190 if (decimals)
1192 /* needed for the "-0.5" corner case */
1193 if (n < 0)
1195 talk_id(VOICE_MINUS, enqueue);
1196 n = -n;
1199 fmt[2] = '0' + decimals;
1201 snprintf(tbuf, sizeof(tbuf), fmt, n % pow10[decimals]);
1202 talk_fractional(tbuf, n / pow10[decimals], unit_id);
1204 return 0;
1207 talk_number(n, enqueue); /* say the number */
1208 talk_id(unit_id, true); /* say the unit, if any */
1210 return 0;
1213 /* spell a string */
1214 int talk_spell(const char* spell, bool enqueue)
1216 char c; /* currently processed char */
1218 if (talk_temp_disable_count > 0)
1219 return -1; /* talking has been disabled */
1220 #if CONFIG_CODEC != SWCODEC
1221 if (audio_status()) /* busy, buffer in use */
1222 return -1;
1223 #endif
1225 if (!enqueue)
1226 talk_shutup(); /* cut off all the pending stuff */
1228 while ((c = *spell++) != '\0')
1230 /* if this grows into too many cases, I should use a table */
1231 if (c >= 'A' && c <= 'Z')
1232 talk_id(VOICE_CHAR_A + c - 'A', true);
1233 else if (c >= 'a' && c <= 'z')
1234 talk_id(VOICE_CHAR_A + c - 'a', true);
1235 else if (c >= '0' && c <= '9')
1236 talk_id(VOICE_ZERO + c - '0', true);
1237 else if (c == '-')
1238 talk_id(VOICE_MINUS, true);
1239 else if (c == '+')
1240 talk_id(VOICE_PLUS, true);
1241 else if (c == '.')
1242 talk_id(VOICE_DOT, true);
1243 else if (c == ' ')
1244 talk_id(VOICE_PAUSE, true);
1245 else if (c == '/')
1246 talk_id(VOICE_CHAR_SLASH, true);
1249 return 0;
1252 void talk_disable(bool disable)
1254 if (disable)
1255 talk_temp_disable_count++;
1256 else
1257 talk_temp_disable_count--;
1260 void talk_setting(const void *global_settings_variable)
1262 const struct settings_list *setting;
1263 if (!global_settings.talk_menu)
1264 return;
1265 setting = find_setting(global_settings_variable, NULL);
1266 if (setting == NULL)
1267 return;
1268 if (setting->lang_id)
1269 talk_id(setting->lang_id,false);
1273 #if CONFIG_RTC
1274 void talk_date(const struct tm *tm, bool enqueue)
1276 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, enqueue);
1277 talk_number(tm->tm_mday, true);
1278 talk_number(1900 + tm->tm_year, true);
1281 void talk_time(const struct tm *tm, bool enqueue)
1283 if (global_settings.timeformat == 1)
1285 /* Voice the hour */
1286 long am_pm_id = VOICE_AM;
1287 int hour = tm->tm_hour;
1288 if (hour >= 12)
1290 am_pm_id = VOICE_PM;
1291 hour -= 12;
1293 if (hour == 0)
1294 hour = 12;
1295 talk_number(hour, enqueue);
1297 /* Voice the minutes */
1298 if (tm->tm_min == 0)
1300 /* Say o'clock if the minute is 0. */
1301 talk_id(VOICE_OCLOCK, true);
1303 else
1305 /* Pronounce the leading 0 */
1306 if(tm->tm_min < 10)
1307 talk_id(VOICE_OH, true);
1308 talk_number(tm->tm_min, true);
1310 talk_id(am_pm_id, true);
1312 else
1314 /* Voice the time in 24 hour format */
1315 talk_number(tm->tm_hour, enqueue);
1316 if (tm->tm_min == 0)
1318 talk_id(VOICE_HUNDRED, true);
1319 talk_id(VOICE_HOUR, true);
1321 else
1323 /* Pronounce the leading 0 */
1324 if(tm->tm_min < 10)
1325 talk_id(VOICE_OH, true);
1326 talk_number(tm->tm_min, true);
1331 #endif /* CONFIG_RTC */