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"
34 #include "settings_list.h"
35 #include "mp3_playback.h"
40 /*#define LOGF_ENABLE*/
44 #include "plugin.h" /* plugin_get_buffer() */
48 /* Memory layout varies between targets because the
49 Archos (MASCODEC) devices cannot mix voice and audio playback
51 MASCODEC | MASCODEC | SWCODEC
52 (playing) | (stopped) |
53 voicebuf-----------+-----------+------------
55 |-----------|------------
56 | thumbnail | thumbnail
61 voicebufend----------+-----------+------------
63 SWCODEC allocates dedicated buffers (except voice and thumbnail are together
64 in the talkbuf), MASCODEC reuses audiobuf. */
67 /***************** Constants *****************/
69 #define QUEUE_SIZE 64 /* must be a power of two */
70 #define QUEUE_MASK (QUEUE_SIZE-1)
71 const char* const dir_thumbnail_name
= "_dirname.talk";
72 const char* const file_thumbnail_ext
= ".talk";
74 /***************** Functional Macros *****************/
76 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
78 #define LOADED_MASK 0x80000000 /* MSB */
80 #if CONFIG_CODEC == SWCODEC
81 #define MAX_THUMBNAIL_BUFSIZE 0x10000
84 /***************** Data types *****************/
86 struct clip_entry
/* one entry of the index table */
88 int offset
; /* offset from start of voicefile file */
89 int size
; /* size of the clip */
92 struct voicefile
/* file format of our voice file */
94 int version
; /* version of the voicefile */
95 int target_id
; /* the rockbox target the file was made for */
96 int table
; /* offset to index table, (=header size) */
97 int id1_max
; /* number of "normal" clips contained in above index */
98 int id2_max
; /* number of "voice only" clips contained in above index */
99 struct clip_entry index
[]; /* followed by the index tables */
100 /* and finally the mp3 clips, not visible here, bitswapped
101 for SH based players */
104 struct queue_entry
/* one entry of the internal queue */
111 /***************** Globals *****************/
113 #if CONFIG_STORAGE & STORAGE_MMC
114 /* The MMC storage on the Ondios is slow enough that we want to buffer the
115 * talk clips only when they are needed */
116 # define TALK_PROGRESSIVE_LOAD
117 #elif CONFIG_CODEC == SWCODEC && MEMORYSIZE <= 2
118 /* The entire voice file wouldn't fit in memory together with codecs, so we
119 * load clips each time they are accessed */
120 # define TALK_PARTIAL_LOAD
123 #ifdef TALK_PARTIAL_LOAD
124 static unsigned char *clip_buffer
;
125 static long max_clipsize
; /* size of the biggest clip */
126 static long buffered_id
[QUEUE_SIZE
]; /* IDs of the talk clips */
127 static uint8_t clip_age
[QUEUE_SIZE
];
129 # error clip_age[] type too small
133 static char* voicebuf
; /* root pointer to our buffer */
134 static unsigned char* p_thumbnail
= NULL
; /* buffer for thumbnails */
135 /* Multiple thumbnails can be loaded back-to-back in this buffer. */
136 static volatile int thumbnail_buf_used SHAREDBSS_ATTR
; /* length of data in
138 static long size_for_thumbnail
; /* total thumbnail buffer size */
139 static struct voicefile
* p_voicefile
; /* loaded voicefile */
140 static bool has_voicefile
; /* a voicefile file is present */
141 static bool need_shutup
; /* is there possibly any voice playing to be shutup */
142 static struct queue_entry queue
[QUEUE_SIZE
]; /* queue of scheduled clips */
143 static bool force_enqueue_next
; /* enqueue next utterance even if enqueue is false */
144 static int queue_write
; /* write index of queue, by application */
145 static int queue_read
; /* read index of queue, by ISR context */
146 #if CONFIG_CODEC == SWCODEC
147 /* protects queue_read, queue_write and thumbnail_buf_used */
148 static struct mutex queue_mutex SHAREDBSS_ATTR
;
149 #define talk_queue_lock() ({ mutex_lock(&queue_mutex); })
150 #define talk_queue_unlock() ({ mutex_unlock(&queue_mutex); })
152 #define talk_queue_lock() ({ })
153 #define talk_queue_unlock() ({ })
154 #endif /* CONFIG_CODEC */
155 static int sent
; /* how many bytes handed over to playback, owned by ISR */
156 static unsigned char curr_hd
[3]; /* current frame header, for re-sync */
157 static int filehandle
= -1; /* global, so we can keep the file open if needed */
158 static unsigned char* p_silence
; /* VOICE_PAUSE clip, used for termination */
159 static long silence_len
; /* length of the VOICE_PAUSE clip */
160 static unsigned char* p_lastclip
; /* address of latest clip, for silence add */
161 static unsigned long voicefile_size
= 0; /* size of the loaded voice file */
162 static unsigned char last_lang
[MAX_FILENAME
+1]; /* name of last used lang file (in talk_init) */
163 static bool talk_initialized
; /* true if talk_init has been called */
164 static int talk_temp_disable_count
; /* if positive, temporarily disable voice UI (not saved) */
167 /***************** Private implementation *****************/
169 static int open_voicefile(void)
172 char* p_lang
= "english"; /* default */
174 if ( global_settings
.lang_file
[0] &&
175 global_settings
.lang_file
[0] != 0xff )
176 { /* try to open the voice file of the selected language */
177 p_lang
= (char *)global_settings
.lang_file
;
180 snprintf(buf
, sizeof(buf
), LANG_DIR
"/%s.voice", p_lang
);
182 return open(buf
, O_RDONLY
);
186 /* fetch a clip from the voice file */
187 static unsigned char* get_clip(long id
, long* p_size
)
190 unsigned char* clipbuf
;
192 if (id
> VOICEONLY_DELIMITER
)
193 { /* voice-only entries use the second part of the table */
194 id
-= VOICEONLY_DELIMITER
+ 1;
195 if (id
>= p_voicefile
->id2_max
)
196 return NULL
; /* must be newer than we have */
197 id
+= p_voicefile
->id1_max
; /* table 2 is behind table 1 */
200 { /* normal use of the first table */
201 if (id
>= p_voicefile
->id1_max
)
202 return NULL
; /* must be newer than we have */
205 clipsize
= p_voicefile
->index
[id
].size
;
206 if (clipsize
== 0) /* clip not included in voicefile */
209 #ifndef TALK_PARTIAL_LOAD
210 clipbuf
= (unsigned char *) p_voicefile
+ p_voicefile
->index
[id
].offset
;
213 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
214 if (!(clipsize
& LOADED_MASK
))
215 { /* clip needs loading */
216 #ifdef TALK_PARTIAL_LOAD
218 if (id
== VOICE_PAUSE
) {
219 idx
= QUEUE_SIZE
; /* we keep VOICE_PAUSE loaded */
222 for(i
=0; i
<QUEUE_SIZE
; i
++) {
223 if (buffered_id
[i
] < 0) {
224 /* found a free entry, that means the buffer isn't
230 /* find the oldest clip */
231 if(clip_age
[i
] > oldest
) {
233 oldest
= clip_age
[i
];
236 /* increment age of each loaded clip */
239 clip_age
[idx
] = 0; /* reset clip's age */
241 clipbuf
= clip_buffer
+ idx
* max_clipsize
;
244 lseek(filehandle
, p_voicefile
->index
[id
].offset
, SEEK_SET
);
245 if (read(filehandle
, clipbuf
, clipsize
) != clipsize
)
246 return NULL
; /* read error */
248 p_voicefile
->index
[id
].size
|= LOADED_MASK
; /* mark as loaded */
250 #ifdef TALK_PARTIAL_LOAD
251 if (id
!= VOICE_PAUSE
) {
252 if (buffered_id
[idx
] >= 0) {
253 /* mark previously loaded clip as unloaded */
254 p_voicefile
->index
[buffered_id
[idx
]].size
&= ~LOADED_MASK
;
256 buffered_id
[idx
] = id
;
261 { /* clip is in memory already */
262 #ifdef TALK_PARTIAL_LOAD
263 /* Find where it was loaded */
264 clipbuf
= clip_buffer
;
265 if (id
== VOICE_PAUSE
) {
266 clipbuf
+= QUEUE_SIZE
* max_clipsize
;
269 for (idx
=0; idx
<QUEUE_SIZE
; idx
++)
270 if (buffered_id
[idx
] == id
) {
271 clipbuf
+= idx
* max_clipsize
;
272 clip_age
[idx
] = 0; /* reset clip's age */
277 clipsize
&= ~LOADED_MASK
; /* without the extra bit gives true size */
279 #endif /* defined(TALK_PROGRESSIVE_LOAD) || defined(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 #ifndef TALK_PARTIAL_LOAD
300 #ifdef ROCKBOX_LITTLE_ENDIAN
305 filehandle
= open_voicefile();
306 if (filehandle
< 0) /* failed to open */
313 #ifndef TALK_PARTIAL_LOAD
314 file_size
= filesize(filehandle
);
315 if (file_size
> bufsize
) /* won't fit? */
319 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
320 /* load only the header for now */
321 load_size
= sizeof(struct voicefile
);
322 #else /* load the full file */
323 load_size
= file_size
;
326 #ifdef TALK_PARTIAL_LOAD
327 if (load_size
> bufsize
) /* won't fit? */
331 got_size
= read(filehandle
, voicebuf
.buf
, load_size
);
332 if (got_size
!= (ssize_t
)load_size
/* failure */)
335 alloc_size
= load_size
;
337 #ifdef ROCKBOX_LITTLE_ENDIAN
338 logf("Byte swapping voice file");
339 structec_convert(voicebuf
.buf
, "lllll", 1, true);
343 if (voicebuf
.file
->table
== sizeof(struct voicefile
))
345 p_voicefile
= voicebuf
.file
;
347 if (p_voicefile
->version
!= VOICE_VERSION
||
348 p_voicefile
->target_id
!= TARGET_ID
)
350 logf("Incompatible voice file");
353 p_thumbnail
= voicebuf
.buf
+ file_size
;
354 p_thumbnail
+= (long)p_thumbnail
% 2; /* 16-bit align */
356 MIN(voicebuf
.buf
+ bufsize
- p_thumbnail
, MAX_THUMBNAIL_BUFSIZE
);
357 if (size_for_thumbnail
<= 0)
363 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
364 /* load the index table, now that we know its size from the header */
365 load_size
= (p_voicefile
->id1_max
+ p_voicefile
->id2_max
)
366 * sizeof(struct clip_entry
);
368 #ifdef TALK_PARTIAL_LOAD
369 if (load_size
> bufsize
) /* won't fit? */
373 got_size
= read(filehandle
, &p_voicefile
->index
[0], load_size
);
374 if (got_size
!= (ssize_t
)load_size
) /* read error */
377 alloc_size
+= load_size
;
381 #endif /* defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD) */
383 #ifdef ROCKBOX_LITTLE_ENDIAN
384 for (i
= 0; i
< p_voicefile
->id1_max
+ p_voicefile
->id2_max
; i
++)
385 structec_convert(&p_voicefile
->index
[i
], "ll", 1, true);
388 #ifdef TALK_PARTIAL_LOAD
389 clip_buffer
= (unsigned char *) p_voicefile
+ p_voicefile
->table
;
390 unsigned clips
= p_voicefile
->id1_max
+ p_voicefile
->id2_max
;
391 clip_buffer
+= clips
* sizeof(struct clip_entry
); /* skip index */
394 /* make sure to have the silence clip, if available */
395 p_silence
= get_clip(VOICE_PAUSE
, &silence_len
);
398 #ifdef TALK_PARTIAL_LOAD
399 alloc_size
+= silence_len
+ QUEUE_SIZE
;
401 if (alloc_size
> bufsize
)
407 has_voicefile
= false; /* don't try again */
417 /* called in ISR context if mp3 data got consumed */
418 static void mp3_callback(unsigned char** start
, size_t* size
)
420 queue
[queue_read
].len
-= sent
; /* we completed this */
421 queue
[queue_read
].buf
+= sent
;
423 if (queue
[queue_read
].len
> 0) /* current clip not finished? */
424 { /* feed the next 64K-1 chunk */
425 #if CONFIG_CODEC != SWCODEC
426 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
428 sent
= queue
[queue_read
].len
;
430 *start
= queue
[queue_read
].buf
;
436 && queue
[queue_read
].buf
== p_thumbnail
+thumbnail_buf_used
)
437 thumbnail_buf_used
= 0;
438 if (sent
> 0) /* go to next entry */
440 queue_read
= (queue_read
+ 1) & QUEUE_MASK
;
445 if (QUEUE_LEVEL
!= 0) /* queue is not empty? */
446 { /* start next clip */
447 #if CONFIG_CODEC != SWCODEC
448 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
450 sent
= queue
[queue_read
].len
;
452 *start
= p_lastclip
= queue
[queue_read
].buf
;
454 curr_hd
[0] = p_lastclip
[1];
455 curr_hd
[1] = p_lastclip
[2];
456 curr_hd
[2] = p_lastclip
[3];
458 else if (p_silence
!= NULL
/* silence clip available */
459 && p_lastclip
!= p_silence
/* previous clip wasn't silence */
460 && !(p_lastclip
>= p_thumbnail
/* ..or thumbnail */
461 && p_lastclip
< p_thumbnail
+size_for_thumbnail
))
462 { /* add silence clip when queue runs empty playing a voice clip */
463 queue
[queue_write
].buf
= p_silence
;
464 queue
[queue_write
].len
= silence_len
;
465 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
471 *size
= 0; /* end of data */
476 /***************** Public routines *****************/
478 /* stop the playback and the pending clips */
479 void talk_force_shutup(void)
481 /* Most of this is MAS only */
482 #if CONFIG_CODEC != SWCODEC
487 unsigned char* search
;
489 if (QUEUE_LEVEL
== 0) /* has ended anyway */
492 #if CONFIG_CPU == SH7034
493 CHCR3
&= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
494 #endif /* CONFIG_CPU == SH7034 */
495 /* search next frame boundary and continue up to there */
496 pos
= search
= mp3_get_pos();
497 end
= queue
[queue_read
].buf
+ queue
[queue_read
].len
;
499 if (pos
>= queue
[queue_read
].buf
500 && pos
<= end
) /* really our clip? */
501 { /* (for strange reasons this isn't nesessarily the case) */
502 /* find the next frame boundary */
503 while (search
< end
) /* search the remaining data */
505 if (*search
++ != 0xFF) /* quick search for frame sync byte */
506 continue; /* (this does the majority of the job) */
508 /* look at the (bitswapped) rest of header candidate */
509 if (search
[0] == curr_hd
[0] /* do the quicker checks first */
510 && search
[2] == curr_hd
[2]
511 && (search
[1] & 0x30) == (curr_hd
[1] & 0x30)) /* sample rate */
513 search
--; /* back to the sync byte */
514 break; /* From looking at it, this is our header. */
519 { /* play old data until the frame end, to keep the MAS in sync */
522 queue_write
= (queue_read
+ 1) & QUEUE_MASK
; /* will be empty after next callback */
523 queue
[queue_read
].len
= sent
; /* current one ends after this */
525 #if CONFIG_CPU == SH7034
526 DTCR3
= sent
; /* let the DMA finish this frame */
527 CHCR3
|= 0x0001; /* re-enable DMA */
528 #endif /* CONFIG_CPU == SH7034 */
529 thumbnail_buf_used
= 0;
533 #endif /* CONFIG_CODEC != SWCODEC */
535 /* Either SWCODEC, or MAS had nothing to do (was frame boundary or not our clip) */
538 queue_write
= queue_read
= 0; /* reset the queue */
539 thumbnail_buf_used
= 0;
544 /* Shutup the voice, except if force_enqueue_next is set. */
545 void talk_shutup(void)
547 if (need_shutup
&& !force_enqueue_next
)
551 /* schedule a clip, at the end or discard the existing queue */
552 static void queue_clip(unsigned char* buf
, long size
, bool enqueue
)
557 talk_shutup(); /* cut off all the pending stuff */
558 /* Something is being enqueued, force_enqueue_next override is no
560 force_enqueue_next
= false;
563 return; /* safety check */
564 #if CONFIG_CPU == SH7034
565 /* disable the DMA temporarily, to be safe of race condition */
569 queue_level
= QUEUE_LEVEL
; /* check old level */
571 if (queue_level
< QUEUE_SIZE
- 1) /* space left? */
573 queue
[queue_write
].buf
= buf
; /* populate an entry */
574 queue
[queue_write
].len
= size
;
575 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
579 if (queue_level
== 0)
580 { /* queue was empty, we have to do the initial start */
582 #if CONFIG_CODEC != SWCODEC
583 sent
= MIN(size
, 0xFFFF); /* DMA can do no more */
587 mp3_play_data(buf
, sent
, mp3_callback
);
591 mp3_play_pause(true); /* kickoff audio */
595 #if CONFIG_CPU == SH7034
596 CHCR3
|= 0x0001; /* re-enable DMA */
606 static void alloc_thumbnail_buf(void)
608 /* use the audio buffer now, need to release before loading a voice */
609 p_thumbnail
= voicebuf
;
610 size_for_thumbnail
= MAX_THUMBNAIL_BUFSIZE
;
611 thumbnail_buf_used
= 0;
614 /* common code for talk_init() and talk_buffer_steal() */
615 static void reset_state(void)
617 queue_write
= queue_read
= 0; /* reset the queue */
618 p_voicefile
= NULL
; /* indicate no voicefile (trashed) */
619 p_thumbnail
= NULL
; /* no thumbnails either */
621 #ifdef TALK_PARTIAL_LOAD
623 for(i
=0; i
<QUEUE_SIZE
; i
++)
627 p_silence
= NULL
; /* pause clip not accessible */
632 /***************** Public implementation *****************/
636 talk_temp_disable_count
= 0;
637 if (talk_initialized
&& !strcasecmp(last_lang
, global_settings
.lang_file
))
639 /* not a new file, nothing to do */
643 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
651 #if CONFIG_CODEC == SWCODEC
652 if(!talk_initialized
)
653 mutex_init(&queue_mutex
);
654 #endif /* CONFIG_CODEC == SWCODEC */
656 talk_initialized
= true;
657 strlcpy((char *)last_lang
, (char *)global_settings
.lang_file
,
660 filehandle
= open_voicefile();
661 if (filehandle
< 0) {
662 has_voicefile
= false;
667 voicefile_size
= filesize(filehandle
);
669 audio_get_buffer(false, NULL
); /* Must tell audio to reinitialize */
670 reset_state(); /* use this for most of our inits */
672 #ifdef TALK_PARTIAL_LOAD
674 char* buf
= plugin_get_buffer(&bufsize
);
675 /* we won't load the full file, we only need the index */
676 load_voicefile(true, buf
, bufsize
);
680 unsigned clips
= p_voicefile
->id1_max
+ p_voicefile
->id2_max
;
682 int silence_size
= 0;
684 for(i
=0; i
<clips
; i
++) {
685 int size
= p_voicefile
->index
[i
].size
;
686 if (size
> max_clipsize
)
688 if (i
== VOICE_PAUSE
)
692 voicefile_size
= p_voicefile
->table
+ clips
* sizeof(struct clip_entry
);
693 voicefile_size
+= max_clipsize
* QUEUE_SIZE
+ silence_size
;
694 p_voicefile
= NULL
; /* Don't pretend we can load talk clips just yet */
698 /* test if we can open and if it fits in the audiobuffer */
699 size_t audiobufsz
= buffer_available();
700 if (voicefile_size
<= audiobufsz
) {
701 has_voicefile
= true;
703 has_voicefile
= false;
707 alloc_thumbnail_buf();
708 close(filehandle
); /* close again, this was just to detect presence */
712 #if CONFIG_CODEC == SWCODEC
713 /* return if a voice codec is required or not */
714 bool talk_voice_required(void)
716 return (voicefile_size
!= 0) /* Voice file is available */
717 || (global_settings
.talk_dir_clip
) /* Thumbnail clips are required */
718 || (global_settings
.talk_file_clip
);
722 /* return size of voice file */
723 int talk_get_buffer(void)
725 int ret
= voicefile_size
;
726 #if CONFIG_CODEC == SWCODEC
727 ret
+= MAX_THUMBNAIL_BUFSIZE
;
732 /* Sets the buffer for the voicefile and returns how many bytes of this
733 * buffer we will use for the voicefile */
734 size_t talkbuf_init(char *bufstart
)
736 bool changed
= voicebuf
!= bufstart
;
738 if (changed
) /* must reload voice file */
743 return talk_get_buffer();
746 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
747 void talk_buffer_steal(void)
749 #if CONFIG_CODEC != SWCODEC
752 #if defined(TALK_PROGRESSIVE_LOAD) || defined(TALK_PARTIAL_LOAD)
763 /* play a voice ID from voicefile */
764 int talk_id(int32_t id
, bool enqueue
)
767 unsigned char* clipbuf
;
771 if (talk_temp_disable_count
> 0)
772 return -1; /* talking has been disabled */
773 #if CONFIG_CODEC != SWCODEC
774 if (audio_status()) /* busy, buffer in use */
778 if (p_voicefile
== NULL
&& has_voicefile
)
779 load_voicefile(false, voicebuf
, voicefile_size
); /* reload needed */
781 if (p_voicefile
== NULL
) /* still no voices? */
784 if (id
== -1) /* -1 is an indication for silence */
787 decimals
= (((uint32_t)id
) >> DECIMAL_SHIFT
) & 0x7;
789 /* check if this is a special ID, with a value */
790 unit
= ((uint32_t)id
) >> UNIT_SHIFT
;
791 if (unit
|| decimals
)
792 { /* sign-extend the value */
793 id
= (uint32_t)id
<< (32-DECIMAL_SHIFT
);
794 id
>>= (32-DECIMAL_SHIFT
);
796 talk_value_decimal(id
, unit
, decimals
, enqueue
); /* speak it */
797 return 0; /* and stop, end of special case */
800 clipbuf
= get_clip(id
, &clipsize
);
802 return -1; /* not present */
805 if (id
> VOICEONLY_DELIMITER
)
806 logf("\ntalk_id: Say voice clip 0x%x\n", id
);
808 logf("\ntalk_id: Say '%s'\n", str(id
));
811 queue_clip(clipbuf
, clipsize
, enqueue
);
815 /* Speaks zero or more IDs (from an array). */
816 int talk_idarray(const long *ids
, bool enqueue
)
821 while(*ids
!= TALK_FINAL_ID
)
823 if((r
= talk_id(*ids
++, enqueue
)) <0)
830 /* Make sure the current utterance is not interrupted by the next one. */
831 void talk_force_enqueue_next(void)
833 force_enqueue_next
= true;
836 /* play a thumbnail from file */
837 /* Returns size of spoken thumbnail, so >0 means something is spoken,
838 <=0 means something went wrong. */
839 static int _talk_file(const char* filename
,
840 const long *prefix_ids
, bool enqueue
)
845 #if CONFIG_CODEC != SWCODEC
846 struct mp3entry info
;
849 if (talk_temp_disable_count
> 0)
850 return -1; /* talking has been disabled */
851 #if CONFIG_CODEC != SWCODEC
852 if (audio_status()) /* busy, buffer in use */
856 if (p_thumbnail
== NULL
|| size_for_thumbnail
<= 0)
857 alloc_thumbnail_buf();
859 #if CONFIG_CODEC != SWCODEC
860 if(mp3info(&info
, filename
)) /* use this to find real start */
862 return 0; /* failed to open, or invalid */
867 /* shutup now to free the thumbnail buffer */
870 fd
= open(filename
, O_RDONLY
);
871 if (fd
< 0) /* failed to open */
876 thumb_used
= thumbnail_buf_used
;
877 if(filesize(fd
) > size_for_thumbnail
-thumb_used
)
878 { /* Don't play truncated clips */
883 #if CONFIG_CODEC != SWCODEC
884 lseek(fd
, info
.first_frame_offset
, SEEK_SET
); /* behind ID data */
887 size
= read(fd
, p_thumbnail
+thumb_used
,
888 size_for_thumbnail
-thumb_used
);
891 /* ToDo: find audio, skip ID headers and trailers */
893 if (size
> 0) /* Don't play missing clips */
895 #if CONFIG_CODEC != SWCODEC && !defined(SIMULATOR)
896 bitswap(p_thumbnail
, size
);
899 /* prefix thumbnail by speaking these ids, but only now
900 that we know there's actually a thumbnail to be
902 talk_idarray(prefix_ids
, true);
904 thumbnail_buf_used
= thumb_used
+size
;
906 queue_clip(p_thumbnail
+thumb_used
, size
, true);
912 int talk_file(const char *root
, const char *dir
, const char *file
,
913 const char *ext
, const long *prefix_ids
, bool enqueue
)
914 /* Play a thumbnail file */
917 /* Does root end with a slash */
918 char *slash
= (root
&& root
[0]
919 && root
[strlen(root
)-1] != '/') ? "/" : "";
920 snprintf(buf
, MAX_PATH
, "%s%s%s%s%s%s",
921 root
? root
: "", slash
,
922 dir
? dir
: "", dir
? "/" : "",
925 return _talk_file(buf
, prefix_ids
, enqueue
);
928 static int talk_spell_basename(const char *path
,
929 const long *prefix_ids
, bool enqueue
)
933 talk_idarray(prefix_ids
, enqueue
);
937 /* Spell only the path component after the last slash */
938 strlcpy(buf
, path
, sizeof(buf
));
939 if(strlen(buf
) >1 && buf
[strlen(buf
)-1] == '/')
940 /* strip trailing slash */
941 buf
[strlen(buf
)-1] = '\0';
942 char *ptr
= strrchr(buf
, '/');
943 if(ptr
&& strlen(buf
) >1)
946 return talk_spell(ptr
, enqueue
);
949 /* Play a file's .talk thumbnail, fallback to spelling the filename, or
950 go straight to spelling depending on settings. */
951 int talk_file_or_spell(const char *dirname
, const char *filename
,
952 const long *prefix_ids
, bool enqueue
)
954 if (global_settings
.talk_file_clip
)
955 { /* .talk clips enabled */
956 if(talk_file(dirname
, NULL
, filename
, file_thumbnail_ext
,
957 prefix_ids
, enqueue
) >0)
960 if (global_settings
.talk_file
== 2)
961 /* Either .talk clips are disabled, or as a fallback */
962 return talk_spell_basename(filename
, prefix_ids
, enqueue
);
966 /* Play a directory's .talk thumbnail, fallback to spelling the filename, or
967 go straight to spelling depending on settings. */
968 int talk_dir_or_spell(const char* dirname
,
969 const long *prefix_ids
, bool enqueue
)
971 if (global_settings
.talk_dir_clip
)
972 { /* .talk clips enabled */
973 if(talk_file(dirname
, NULL
, dir_thumbnail_name
, NULL
,
974 prefix_ids
, enqueue
) >0)
977 if (global_settings
.talk_dir
== 2)
978 /* Either .talk clips disabled or as a fallback */
979 return talk_spell_basename(dirname
, prefix_ids
, enqueue
);
983 /* say a numeric value, this word ordering works for english,
984 but not necessarily for other languages (e.g. german) */
985 int talk_number(long n
, bool enqueue
)
987 int level
= 2; /* mille count */
988 long mil
= 1000000000; /* highest possible "-illion" */
990 if (talk_temp_disable_count
> 0)
991 return -1; /* talking has been disabled */
992 #if CONFIG_CODEC != SWCODEC
993 if (audio_status()) /* busy, buffer in use */
998 talk_shutup(); /* cut off all the pending stuff */
1001 { /* special case */
1002 talk_id(VOICE_ZERO
, true);
1008 talk_id(VOICE_MINUS
, true);
1014 int segment
= n
/ mil
; /* extract in groups of 3 digits */
1015 n
-= segment
* mil
; /* remove the used digits from number */
1016 mil
/= 1000; /* digit place for next round */
1020 int hundreds
= segment
/ 100;
1021 int ones
= segment
% 100;
1025 talk_id(VOICE_ZERO
+ hundreds
, true);
1026 talk_id(VOICE_HUNDRED
, true);
1029 /* combination indexing */
1032 int tens
= ones
/10 + 18;
1033 talk_id(VOICE_ZERO
+ tens
, true);
1037 /* direct indexing */
1039 talk_id(VOICE_ZERO
+ ones
, true);
1041 /* add billion, million, thousand */
1043 talk_id(VOICE_THOUSAND
+ level
, true);
1051 /* Say time duration/interval. Input is time in seconds,
1052 say hours,minutes,seconds. */
1053 static int talk_time_unit(long secs
, bool enqueue
)
1058 if((hours
= secs
/3600)) {
1060 talk_value(hours
, UNIT_HOUR
, true);
1062 if((mins
= secs
/60)) {
1064 talk_value(mins
, UNIT_MIN
, true);
1066 if((secs
) || (!hours
&& !mins
))
1067 talk_value(secs
, UNIT_SEC
, true);
1068 else if(!hours
&& secs
)
1069 talk_number(secs
, true);
1073 void talk_fractional(char *tbuf
, int value
, int unit
)
1076 /* strip trailing zeros from the fraction */
1077 for (i
= strlen(tbuf
) - 1; (i
>= 0) && (tbuf
[i
] == '0'); i
--)
1080 talk_number(value
, true);
1083 talk_id(LANG_POINT
, true);
1084 talk_spell(tbuf
, true);
1086 talk_id(unit
, true);
1089 int talk_value(long n
, int unit
, bool enqueue
)
1091 return talk_value_decimal(n
, unit
, 0, enqueue
);
1094 /* singular/plural aware saying of a value */
1095 int talk_value_decimal(long n
, int unit
, int decimals
, bool enqueue
)
1098 static const int unit_voiced
[] =
1099 { /* lookup table for the voice ID of the units */
1100 [0 ... UNIT_LAST
-1] = -1, /* regular ID, int, signed */
1102 = VOICE_MILLISECONDS
, /* here come the "real" units */
1116 = VOICE_MILLIAMPHOURS
,
1126 = VOICE_KBIT_PER_SEC
,
1128 = VOICE_PM_UNITS_PER_TICK
,
1131 static const int pow10
[] = { /* 10^0 - 10^7 */
1132 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
1136 char fmt
[] = "%0nd";
1138 if (talk_temp_disable_count
> 0)
1139 return -1; /* talking has been disabled */
1140 #if CONFIG_CODEC != SWCODEC
1141 if (audio_status()) /* busy, buffer in use */
1145 /* special case for time duration */
1146 if (unit
== UNIT_TIME
)
1147 return talk_time_unit(n
, enqueue
);
1149 if (unit
< 0 || unit
>= UNIT_LAST
)
1152 unit_id
= unit_voiced
[unit
];
1154 if ((n
==1 || n
==-1) /* singular? */
1155 && unit_id
>= VOICE_SECONDS
&& unit_id
<= VOICE_HOURS
)
1157 unit_id
--; /* use the singular for those units which have */
1160 /* special case with a "plus" before */
1161 if (n
> 0 && (unit
== UNIT_SIGNED
|| unit
== UNIT_DB
))
1163 talk_id(VOICE_PLUS
, enqueue
);
1169 /* needed for the "-0.5" corner case */
1172 talk_id(VOICE_MINUS
, enqueue
);
1176 fmt
[2] = '0' + decimals
;
1178 snprintf(tbuf
, sizeof(tbuf
), fmt
, n
% pow10
[decimals
]);
1179 talk_fractional(tbuf
, n
/ pow10
[decimals
], unit_id
);
1184 talk_number(n
, enqueue
); /* say the number */
1185 talk_id(unit_id
, true); /* say the unit, if any */
1190 /* spell a string */
1191 int talk_spell(const char* spell
, bool enqueue
)
1193 char c
; /* currently processed char */
1195 if (talk_temp_disable_count
> 0)
1196 return -1; /* talking has been disabled */
1197 #if CONFIG_CODEC != SWCODEC
1198 if (audio_status()) /* busy, buffer in use */
1203 talk_shutup(); /* cut off all the pending stuff */
1205 while ((c
= *spell
++) != '\0')
1207 /* if this grows into too many cases, I should use a table */
1208 if (c
>= 'A' && c
<= 'Z')
1209 talk_id(VOICE_CHAR_A
+ c
- 'A', true);
1210 else if (c
>= 'a' && c
<= 'z')
1211 talk_id(VOICE_CHAR_A
+ c
- 'a', true);
1212 else if (c
>= '0' && c
<= '9')
1213 talk_id(VOICE_ZERO
+ c
- '0', true);
1215 talk_id(VOICE_MINUS
, true);
1217 talk_id(VOICE_PLUS
, true);
1219 talk_id(VOICE_DOT
, true);
1221 talk_id(VOICE_PAUSE
, true);
1223 talk_id(VOICE_CHAR_SLASH
, true);
1229 void talk_disable(bool disable
)
1232 talk_temp_disable_count
++;
1234 talk_temp_disable_count
--;
1237 void talk_setting(const void *global_settings_variable
)
1239 const struct settings_list
*setting
;
1240 if (!global_settings
.talk_menu
)
1242 setting
= find_setting(global_settings_variable
, NULL
);
1243 if (setting
== NULL
)
1245 if (setting
->lang_id
)
1246 talk_id(setting
->lang_id
,false);
1251 void talk_date(const struct tm
*tm
, bool enqueue
)
1253 talk_id(LANG_MONTH_JANUARY
+ tm
->tm_mon
, enqueue
);
1254 talk_number(tm
->tm_mday
, true);
1255 talk_number(1900 + tm
->tm_year
, true);
1258 void talk_time(const struct tm
*tm
, bool enqueue
)
1260 if (global_settings
.timeformat
== 1)
1262 /* Voice the hour */
1263 long am_pm_id
= VOICE_AM
;
1264 int hour
= tm
->tm_hour
;
1267 am_pm_id
= VOICE_PM
;
1272 talk_number(hour
, enqueue
);
1274 /* Voice the minutes */
1275 if (tm
->tm_min
== 0)
1277 /* Say o'clock if the minute is 0. */
1278 talk_id(VOICE_OCLOCK
, true);
1282 /* Pronounce the leading 0 */
1284 talk_id(VOICE_OH
, true);
1285 talk_number(tm
->tm_min
, true);
1287 talk_id(am_pm_id
, true);
1291 /* Voice the time in 24 hour format */
1292 talk_number(tm
->tm_hour
, enqueue
);
1293 if (tm
->tm_min
== 0)
1295 talk_id(VOICE_HUNDRED
, true);
1296 talk_id(VOICE_HOUR
, true);
1300 /* Pronounce the leading 0 */
1302 talk_id(VOICE_OH
, true);
1303 talk_number(tm
->tm_min
, true);
1308 #endif /* CONFIG_RTC */