1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
28 #include "string-extra.h"
33 #include "settings_list.h"
34 #if CONFIG_CODEC == SWCODEC
35 #include "voice_thread.h"
37 #include "mp3_playback.h"
43 /*#define LOGF_ENABLE*/
47 #include "plugin.h" /* plugin_get_buffer() */
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-----------+-----------+------------
57 |-----------|------------
58 | thumbnail | thumbnail
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
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
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 */
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
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
];
133 # error clip_age[] type too small
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
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); })
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)
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
)
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 */
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 */
215 #ifndef TALK_PARTIAL_LOAD
216 clipbuf
= (unsigned char *) p_voicefile
+ p_voicefile
->index
[id
].offset
;
219 #ifdef TALK_PARTIAL_LOAD
220 if (!(clipsize
& LOADED_MASK
))
221 { /* clip needs loading */
223 if (id
== VOICE_PAUSE
) {
224 idx
= QUEUE_SIZE
; /* we keep VOICE_PAUSE loaded */
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
235 /* find the oldest clip */
236 if(clip_age
[i
] > oldest
) {
238 oldest
= clip_age
[i
];
241 /* increment age of each loaded clip */
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
;
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
;
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 */
277 clipsize
&= ~LOADED_MASK
; /* without the extra bit gives true size */
279 #endif /* TALK_PARTIAL_LOAD */
286 /* load the voice file into the mp3 buffer */
287 static void load_voicefile(bool probe
, char* buf
, size_t bufsize
)
291 struct voicefile
* file
;
293 union voicebuf voicebuf
;
295 size_t load_size
, alloc_size
;
297 #ifdef ROCKBOX_LITTLE_ENDIAN
302 filehandle
= open_voicefile();
303 if (filehandle
< 0) /* failed to open */
310 #ifdef TALK_PARTIAL_LOAD
311 /* load only the header for now */
312 load_size
= sizeof(struct voicefile
);
314 /* load the entire file */
315 load_size
= filesize(filehandle
);
317 if (load_size
> bufsize
) /* won't fit? */
320 got_size
= read(filehandle
, voicebuf
.buf
, load_size
);
321 if (got_size
!= (ssize_t
)load_size
/* failure */)
324 alloc_size
= load_size
;
326 #ifdef ROCKBOX_LITTLE_ENDIAN
327 logf("Byte swapping voice file");
328 structec_convert(voicebuf
.buf
, "lllll", 1, true);
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");
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? */
354 got_size
= read(filehandle
, &p_voicefile
->index
[0], load_size
);
355 if (got_size
!= (ssize_t
)load_size
) /* read error */
358 alloc_size
+= load_size
;
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);
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 */
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
;
383 if (alloc_size
> bufsize
)
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
);
393 if (size_for_thumbnail
<= 0)
399 has_voicefile
= false; /* don't try again */
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);
420 sent
= queue
[queue_read
].len
;
422 *start
= queue
[queue_read
].buf
;
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
;
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);
442 sent
= queue
[queue_read
].len
;
444 *start
= p_lastclip
= queue
[queue_read
].buf
;
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
;
463 *size
= 0; /* end of data */
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
479 unsigned char* search
;
481 if (QUEUE_LEVEL
== 0) /* has ended anyway */
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. */
511 { /* play old data until the frame end, to keep the MAS in sync */
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;
525 #endif /* CONFIG_CODEC != SWCODEC */
527 /* Either SWCODEC, or MAS had nothing to do (was frame boundary or not our clip) */
530 queue_write
= queue_read
= 0; /* reset the queue */
531 thumbnail_buf_used
= 0;
536 /* Shutup the voice, except if force_enqueue_next is set. */
537 void talk_shutup(void)
539 if (need_shutup
&& !force_enqueue_next
)
543 /* schedule a clip, at the end or discard the existing queue */
544 static void queue_clip(unsigned char* buf
, long size
, bool enqueue
)
549 talk_shutup(); /* cut off all the pending stuff */
550 /* Something is being enqueued, force_enqueue_next override is no
552 force_enqueue_next
= false;
555 return; /* safety check */
556 #if CONFIG_CPU == SH7034
557 /* disable the DMA temporarily, to be safe of race condition */
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
;
571 if (queue_level
== 0)
572 { /* queue was empty, we have to do the initial start */
574 #if CONFIG_CODEC != SWCODEC
575 sent
= MIN(size
, 0xFFFF); /* DMA can do no more */
579 mp3_play_data(buf
, sent
, mp3_callback
);
583 mp3_play_pause(true); /* kickoff audio */
587 #if CONFIG_CPU == SH7034
588 CHCR3
|= 0x0001; /* re-enable DMA */
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
;
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
616 for(i
=0; i
<QUEUE_SIZE
; i
++)
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)
630 audio_restore_playback(AUDIO_WANT_VOICE
);
631 voicebuf
= audio_get_buffer(true, &size
);
632 audio_get_buffer(false, &size
);
637 #endif /* CONFIG_CODEC == SWCODEC */
640 /***************** Public implementation *****************/
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 */
651 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
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
,
668 filehandle
= open_voicefile();
669 if (filehandle
< 0) {
670 has_voicefile
= false;
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
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
);
688 unsigned clips
= p_voicefile
->id1_max
+ p_voicefile
->id2_max
;
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
)
696 if (i
== VOICE_PAUSE
)
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 */
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;
711 has_voicefile
= false;
715 close(filehandle
); /* close again, this was just to detect presence */
718 #if CONFIG_CODEC == SWCODEC
719 /* Safe to init voice playback engine now since we now know if talk is
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
);
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
;
741 return audio_buffer_available();
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 */
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
766 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
776 /* play a voice ID from voicefile */
777 int talk_id(int32_t id
, bool enqueue
)
780 unsigned char* clipbuf
;
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 */
791 if (audio_status()) /* busy, buffer in use */
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? */
801 if (id
== -1) /* -1 is an indication for silence */
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
);
819 return -1; /* not present */
822 if (id
> VOICEONLY_DELIMITER
)
823 logf("\ntalk_id: Say voice clip 0x%x\n", id
);
825 logf("\ntalk_id: Say '%s'\n", str(id
));
828 queue_clip(clipbuf
, clipsize
, enqueue
);
832 /* Speaks zero or more IDs (from an array). */
833 int talk_idarray(const long *ids
, bool enqueue
)
838 while(*ids
!= TALK_FINAL_ID
)
840 if((r
= talk_id(*ids
++, enqueue
)) <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
)
862 #if CONFIG_CODEC != SWCODEC
863 struct mp3entry info
;
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 */
873 if (audio_status()) /* busy, buffer in use */
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 */
888 /* shutup now to free the thumbnail buffer */
891 fd
= open(filename
, O_RDONLY
);
892 if (fd
< 0) /* failed to open */
897 thumb_used
= thumbnail_buf_used
;
898 if(filesize(fd
) > size_for_thumbnail
-thumb_used
)
899 { /* Don't play truncated clips */
904 #if CONFIG_CODEC != SWCODEC
905 lseek(fd
, info
.first_frame_offset
, SEEK_SET
); /* behind ID data */
908 size
= read(fd
, p_thumbnail
+thumb_used
,
909 size_for_thumbnail
-thumb_used
);
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
);
920 /* prefix thumbnail by speaking these ids, but only now
921 that we know there's actually a thumbnail to be
923 talk_idarray(prefix_ids
, true);
925 thumbnail_buf_used
= thumb_used
+size
;
927 queue_clip(p_thumbnail
+thumb_used
, size
, true);
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 */
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
? "/" : "",
946 return _talk_file(buf
, prefix_ids
, enqueue
);
949 static int talk_spell_basename(const char *path
,
950 const long *prefix_ids
, bool enqueue
)
954 talk_idarray(prefix_ids
, enqueue
);
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)
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)
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
);
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)
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
);
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 */
1021 talk_shutup(); /* cut off all the pending stuff */
1024 { /* special case */
1025 talk_id(VOICE_ZERO
, true);
1031 talk_id(VOICE_MINUS
, true);
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 */
1043 int hundreds
= segment
/ 100;
1044 int ones
= segment
% 100;
1048 talk_id(VOICE_ZERO
+ hundreds
, true);
1049 talk_id(VOICE_HUNDRED
, true);
1052 /* combination indexing */
1055 int tens
= ones
/10 + 18;
1056 talk_id(VOICE_ZERO
+ tens
, true);
1060 /* direct indexing */
1062 talk_id(VOICE_ZERO
+ ones
, true);
1064 /* add billion, million, thousand */
1066 talk_id(VOICE_THOUSAND
+ level
, true);
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
)
1081 if((hours
= secs
/3600)) {
1083 talk_value(hours
, UNIT_HOUR
, true);
1085 if((mins
= 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);
1096 void talk_fractional(char *tbuf
, int value
, int unit
)
1099 /* strip trailing zeros from the fraction */
1100 for (i
= strlen(tbuf
) - 1; (i
>= 0) && (tbuf
[i
] == '0'); i
--)
1103 talk_number(value
, true);
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
)
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 */
1125 = VOICE_MILLISECONDS
, /* here come the "real" units */
1139 = VOICE_MILLIAMPHOURS
,
1149 = VOICE_KBIT_PER_SEC
,
1151 = VOICE_PM_UNITS_PER_TICK
,
1154 static const int pow10
[] = { /* 10^0 - 10^7 */
1155 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
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 */
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
)
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
);
1192 /* needed for the "-0.5" corner case */
1195 talk_id(VOICE_MINUS
, enqueue
);
1199 fmt
[2] = '0' + decimals
;
1201 snprintf(tbuf
, sizeof(tbuf
), fmt
, n
% pow10
[decimals
]);
1202 talk_fractional(tbuf
, n
/ pow10
[decimals
], unit_id
);
1207 talk_number(n
, enqueue
); /* say the number */
1208 talk_id(unit_id
, true); /* say the unit, if any */
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 */
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);
1238 talk_id(VOICE_MINUS
, true);
1240 talk_id(VOICE_PLUS
, true);
1242 talk_id(VOICE_DOT
, true);
1244 talk_id(VOICE_PAUSE
, true);
1246 talk_id(VOICE_CHAR_SLASH
, true);
1252 void talk_disable(bool disable
)
1255 talk_temp_disable_count
++;
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
)
1265 setting
= find_setting(global_settings_variable
, NULL
);
1266 if (setting
== NULL
)
1268 if (setting
->lang_id
)
1269 talk_id(setting
->lang_id
,false);
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
;
1290 am_pm_id
= VOICE_PM
;
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);
1305 /* Pronounce the leading 0 */
1307 talk_id(VOICE_OH
, true);
1308 talk_number(tm
->tm_min
, true);
1310 talk_id(am_pm_id
, true);
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);
1323 /* Pronounce the leading 0 */
1325 talk_id(VOICE_OH
, true);
1326 talk_number(tm
->tm_min
, true);
1331 #endif /* CONFIG_RTC */