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 * All files in this archive are subject to the GNU General Public License.
17 * See the file COPYING in the source tree root for full license agreement.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
32 #include "mp3_playback.h"
40 #if CONFIG_CODEC == SWCODEC
46 /* Memory layout varies between targets because the
47 Archos (MASCODEC) devices cannot mix voice and audio playback
49 MASCODEC | MASCODEC | SWCODEC
50 (playing) | (stopped) |
51 audiobuf-----------+-----------+------------
52 audio | voice | thumbnail
53 |-----------|------------
61 audiobufend----------+-----------+------------
63 SWCODEC allocates dedicated buffers, MASCODEC reuses audiobuf. */
66 /***************** Constants *****************/
68 #define QUEUE_SIZE 64 /* must be a power of two */
69 #define QUEUE_MASK (QUEUE_SIZE-1)
70 const char* const dir_thumbnail_name
= "_dirname.talk";
71 const char* const file_thumbnail_ext
= ".talk";
73 /***************** Functional Macros *****************/
75 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
77 #define LOADED_MASK 0x80000000 /* MSB */
79 #if CONFIG_CODEC == SWCODEC
80 #define MAX_THUMBNAIL_BUFSIZE 32768
84 extern bool audio_is_initialized
;
87 /***************** Data types *****************/
89 struct clip_entry
/* one entry of the index table */
91 int offset
; /* offset from start of voicefile file */
92 int size
; /* size of the clip */
95 struct voicefile
/* file format of our voice file */
97 int version
; /* version of the voicefile */
98 int target_id
; /* the rockbox target the file was made for */
99 int table
; /* offset to index table, (=header size) */
100 int id1_max
; /* number of "normal" clips contained in above index */
101 int id2_max
; /* number of "voice only" clips contained in above index */
102 struct clip_entry index
[]; /* followed by the index tables */
103 /* and finally the mp3 clips, not visible here, bitswapped
104 for SH based players */
107 struct queue_entry
/* one entry of the internal queue */
114 /***************** Globals *****************/
116 static unsigned char* p_thumbnail
= NULL
; /* buffer for thumbnail */
117 static long size_for_thumbnail
; /* leftover buffer size for it */
118 static struct voicefile
* p_voicefile
; /* loaded voicefile */
119 static bool has_voicefile
; /* a voicefile file is present */
120 static struct queue_entry queue
[QUEUE_SIZE
]; /* queue of scheduled clips */
121 /* enqueue next utterance even if enqueue is false. */
122 static bool force_enqueue_next
;
123 static int queue_write
; /* write index of queue, by application */
124 static int queue_read
; /* read index of queue, by ISR context */
125 static int sent
; /* how many bytes handed over to playback, owned by ISR */
126 static unsigned char curr_hd
[3]; /* current frame header, for re-sync */
127 static int filehandle
= -1; /* global, so the MMC variant can keep the file open */
128 static unsigned char* p_silence
; /* VOICE_PAUSE clip, used for termination */
129 static long silence_len
; /* length of the VOICE_PAUSE clip */
130 static unsigned char* p_lastclip
; /* address of latest clip, for silence add */
131 static unsigned long voicefile_size
= 0; /* size of the loaded voice file */
132 static unsigned char last_lang
[MAX_FILENAME
+1]; /* name of last used lang file (in talk_init) */
133 static bool talk_initialized
; /* true if talk_init has been called */
134 static int talk_menu_disable
; /* if non-zero, temporarily disable voice UI (not saved) */
136 /***************** Private prototypes *****************/
138 static void load_voicefile(void);
139 static void mp3_callback(unsigned char** start
, size_t* size
);
140 static int queue_clip(unsigned char* buf
, long size
, bool enqueue
);
141 static int open_voicefile(void);
142 static unsigned char* get_clip(long id
, long* p_size
);
143 static int shutup(void); /* Interrupt voice, as when enqueue is false */
144 static int do_shutup(void); /* kill voice unconditionally */
146 /***************** Private implementation *****************/
148 static int open_voicefile(void)
151 char* p_lang
= "english"; /* default */
153 if ( global_settings
.lang_file
[0] &&
154 global_settings
.lang_file
[0] != 0xff )
155 { /* try to open the voice file of the selected language */
156 p_lang
= (char *)global_settings
.lang_file
;
159 snprintf(buf
, sizeof(buf
), LANG_DIR
"/%s.voice", p_lang
);
161 return open(buf
, O_RDONLY
);
165 /* load the voice file into the mp3 buffer */
166 static void load_voicefile(void)
171 #ifdef ROCKBOX_LITTLE_ENDIAN
175 filehandle
= open_voicefile();
176 if (filehandle
< 0) /* failed to open */
179 file_size
= filesize(filehandle
);
180 if (file_size
> audiobufend
- audiobuf
) /* won't fit? */
183 #ifdef HAVE_MMC /* load only the header for now */
184 load_size
= offsetof(struct voicefile
, index
);
185 #else /* load the full file */
186 load_size
= file_size
;
189 got_size
= read(filehandle
, audiobuf
, load_size
);
190 if (got_size
!= load_size
/* failure */)
193 #ifdef ROCKBOX_LITTLE_ENDIAN
194 logf("Byte swapping voice file");
195 structec_convert(audiobuf
, "lllll", 1, true);
198 if (((struct voicefile
*)audiobuf
)->table
/* format check */
199 == offsetof(struct voicefile
, index
))
201 p_voicefile
= (struct voicefile
*)audiobuf
;
203 if (p_voicefile
->target_id
!= TARGET_ID
)
205 logf("Incompatible voice file (wrong target)");
208 #if CONFIG_CODEC != SWCODEC
209 /* MASCODEC: now use audiobuf for voice then thumbnail */
210 p_thumbnail
= audiobuf
+ file_size
;
211 p_thumbnail
+= (long)p_thumbnail
% 2; /* 16-bit align */
212 size_for_thumbnail
= audiobufend
- p_thumbnail
;
218 #ifdef ROCKBOX_LITTLE_ENDIAN
219 for (i
= 0; i
< p_voicefile
->id1_max
+ p_voicefile
->id2_max
; i
++)
220 structec_convert(&p_voicefile
->index
[i
], "ll", 1, true);
224 /* load the index table, now that we know its size from the header */
225 load_size
= (p_voicefile
->id1_max
+ p_voicefile
->id2_max
)
226 * sizeof(struct clip_entry
);
227 got_size
= read(filehandle
,
228 (unsigned char *) p_voicefile
+ offsetof(struct voicefile
, index
), load_size
);
229 if (got_size
!= load_size
) /* read error */
232 close(filehandle
); /* only the MMC variant leaves it open */
236 /* make sure to have the silence clip, if available */
237 p_silence
= get_clip(VOICE_PAUSE
, &silence_len
);
243 has_voicefile
= false; /* don't try again */
253 /* Are more voice clips queued and waiting? */
254 bool is_voice_queued()
256 return !!QUEUE_LEVEL
;
260 /* called in ISR context if mp3 data got consumed */
261 static void mp3_callback(unsigned char** start
, size_t* size
)
263 queue
[queue_read
].len
-= sent
; /* we completed this */
264 queue
[queue_read
].buf
+= sent
;
266 if (queue
[queue_read
].len
> 0) /* current clip not finished? */
267 { /* feed the next 64K-1 chunk */
268 #if CONFIG_CODEC != SWCODEC
269 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
271 sent
= queue
[queue_read
].len
;
273 *start
= queue
[queue_read
].buf
;
277 else if (sent
> 0) /* go to next entry */
279 queue_read
= (queue_read
+ 1) & QUEUE_MASK
;
284 if (QUEUE_LEVEL
) /* queue is not empty? */
285 { /* start next clip */
286 #if CONFIG_CODEC != SWCODEC
287 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
289 sent
= queue
[queue_read
].len
;
291 *start
= p_lastclip
= queue
[queue_read
].buf
;
293 curr_hd
[0] = p_lastclip
[1];
294 curr_hd
[1] = p_lastclip
[2];
295 curr_hd
[2] = p_lastclip
[3];
297 else if (p_silence
!= NULL
/* silence clip available */
298 && p_lastclip
!= p_silence
/* previous clip wasn't silence */
299 && p_lastclip
!= p_thumbnail
) /* ..or thumbnail */
300 { /* add silence clip when queue runs empty playing a voice clip */
301 queue
[queue_write
].buf
= p_silence
;
302 queue
[queue_write
].len
= silence_len
;
303 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
309 *size
= 0; /* end of data */
313 /* stop the playback and the pending clips */
314 static int do_shutup(void)
316 #if CONFIG_CODEC != SWCODEC
318 unsigned char* search
;
322 if (QUEUE_LEVEL
== 0) /* has ended anyway */
324 #if CONFIG_CODEC == SWCODEC
329 #if CONFIG_CODEC != SWCODEC
330 #if CONFIG_CPU == SH7034
331 CHCR3
&= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
333 /* search next frame boundary and continue up to there */
334 pos
= search
= mp3_get_pos();
335 end
= queue
[queue_read
].buf
+ queue
[queue_read
].len
;
337 if (pos
>= queue
[queue_read
].buf
338 && pos
<= end
) /* really our clip? */
339 { /* (for strange reasons this isn't nesessarily the case) */
340 /* find the next frame boundary */
341 while (search
< end
) /* search the remaining data */
343 if (*search
++ != 0xFF) /* quick search for frame sync byte */
344 continue; /* (this does the majority of the job) */
346 /* look at the (bitswapped) rest of header candidate */
347 if (search
[0] == curr_hd
[0] /* do the quicker checks first */
348 && search
[2] == curr_hd
[2]
349 && (search
[1] & 0x30) == (curr_hd
[1] & 0x30)) /* sample rate */
351 search
--; /* back to the sync byte */
352 break; /* From looking at it, this is our header. */
357 { /* play old data until the frame end, to keep the MAS in sync */
360 queue_write
= (queue_read
+ 1) & QUEUE_MASK
; /* will be empty after next callback */
361 queue
[queue_read
].len
= sent
; /* current one ends after this */
363 #if CONFIG_CPU == SH7034
364 DTCR3
= sent
; /* let the DMA finish this frame */
365 CHCR3
|= 0x0001; /* re-enable DMA */
372 /* nothing to do, was frame boundary or not our clip */
375 queue_write
= queue_read
= 0; /* reset the queue */
380 /* Shutup the voice, except if force_enqueue_next is set. */
381 static int shutup(void)
383 if (!force_enqueue_next
)
388 /* schedule a clip, at the end or discard the existing queue */
389 static int queue_clip(unsigned char* buf
, long size
, bool enqueue
)
394 shutup(); /* cut off all the pending stuff */
395 /* Something is being enqueued, force_enqueue_next override is no
397 force_enqueue_next
= false;
400 return 0; /* safety check */
401 #if CONFIG_CPU == SH7034
402 /* disable the DMA temporarily, to be safe of race condition */
405 queue_level
= QUEUE_LEVEL
; /* check old level */
407 if (queue_level
< QUEUE_SIZE
- 1) /* space left? */
409 queue
[queue_write
].buf
= buf
; /* populate an entry */
410 queue
[queue_write
].len
= size
;
411 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
414 if (queue_level
== 0)
415 { /* queue was empty, we have to do the initial start */
417 #if CONFIG_CODEC != SWCODEC
418 sent
= MIN(size
, 0xFFFF); /* DMA can do no more */
422 mp3_play_data(buf
, sent
, mp3_callback
);
426 mp3_play_pause(true); /* kickoff audio */
430 #if CONFIG_CPU == SH7034
431 CHCR3
|= 0x0001; /* re-enable DMA */
438 /* fetch a clip from the voice file */
439 static unsigned char* get_clip(long id
, long* p_size
)
442 unsigned char* clipbuf
;
444 if (id
> VOICEONLY_DELIMITER
)
445 { /* voice-only entries use the second part of the table */
446 id
-= VOICEONLY_DELIMITER
+ 1;
447 if (id
>= p_voicefile
->id2_max
)
448 return NULL
; /* must be newer than we have */
449 id
+= p_voicefile
->id1_max
; /* table 2 is behind table 1 */
452 { /* normal use of the first table */
453 if (id
>= p_voicefile
->id1_max
)
454 return NULL
; /* must be newer than we have */
457 clipsize
= p_voicefile
->index
[id
].size
;
458 if (clipsize
== 0) /* clip not included in voicefile */
460 clipbuf
= (unsigned char *) p_voicefile
+ p_voicefile
->index
[id
].offset
;
462 #ifdef HAVE_MMC /* dynamic loading, on demand */
463 if (!(clipsize
& LOADED_MASK
))
464 { /* clip used for the first time, needs loading */
465 lseek(filehandle
, p_voicefile
->index
[id
].offset
, SEEK_SET
);
466 if (read(filehandle
, clipbuf
, clipsize
) != clipsize
)
467 return NULL
; /* read error */
469 p_voicefile
->index
[id
].size
|= LOADED_MASK
; /* mark as loaded */
472 { /* clip is in memory already */
473 clipsize
&= ~LOADED_MASK
; /* without the extra bit gives true size */
482 /* common code for talk_init() and talk_buffer_steal() */
483 static void reset_state(void)
485 queue_write
= queue_read
= 0; /* reset the queue */
486 p_voicefile
= NULL
; /* indicate no voicefile (trashed) */
487 #if CONFIG_CODEC == SWCODEC
488 /* Allocate a dedicated thumbnail buffer - once */
489 if (p_thumbnail
== NULL
)
491 size_for_thumbnail
= audiobufend
- audiobuf
;
492 if (size_for_thumbnail
> MAX_THUMBNAIL_BUFSIZE
)
493 size_for_thumbnail
= MAX_THUMBNAIL_BUFSIZE
;
494 p_thumbnail
= buffer_alloc(size_for_thumbnail
);
497 /* Just use the audiobuf, without allocating anything */
498 p_thumbnail
= audiobuf
;
499 size_for_thumbnail
= audiobufend
- audiobuf
;
501 p_silence
= NULL
; /* pause clip not accessible */
504 /***************** Public implementation *****************/
508 talk_menu_disable
= 0;
509 if (talk_initialized
&& !strcasecmp(last_lang
, global_settings
.lang_file
))
511 /* not a new file, nothing to do */
516 if (filehandle
>= 0) /* MMC: An old voice file might still be open */
523 talk_initialized
= true;
524 strncpy((char *) last_lang
, (char *)global_settings
.lang_file
,
527 #if CONFIG_CODEC == SWCODEC
528 audio_get_buffer(false, NULL
); /* Must tell audio to reinitialize */
530 reset_state(); /* use this for most of our inits */
532 filehandle
= open_voicefile();
533 has_voicefile
= (filehandle
>= 0); /* test if we can open it */
538 voicefile_size
= filesize(filehandle
);
539 close(filehandle
); /* close again, this was just to detect presence */
545 #if CONFIG_CODEC == SWCODEC
546 /* return if a voice codec is required or not */
547 bool talk_voice_required(void)
549 return (voicefile_size
!= 0) /* Voice file is available */
550 || (global_settings
.talk_dir_clip
) /* Thumbnail clips are required */
551 || (global_settings
.talk_file_clip
);
555 /* return size of voice file */
556 int talk_get_bufsize(void)
558 return voicefile_size
;
561 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
562 int talk_buffer_steal(void)
564 #if CONFIG_CODEC != SWCODEC
568 if (filehandle
>= 0) /* only relevant for MMC */
580 /* play a voice ID from voicefile */
581 int talk_id(long id
, bool enqueue
)
584 unsigned char* clipbuf
;
587 #if CONFIG_CODEC != SWCODEC
588 if (audio_status()) /* busy, buffer in use */
592 if (p_voicefile
== NULL
&& has_voicefile
)
593 load_voicefile(); /* reload needed */
595 if (p_voicefile
== NULL
) /* still no voices? */
598 if (id
== -1) /* -1 is an indication for silence */
601 /* check if this is a special ID, with a value */
602 unit
= ((unsigned long)id
) >> UNIT_SHIFT
;
604 { /* sign-extend the value */
605 id
= (unsigned long)id
<< (32-UNIT_SHIFT
);
606 id
>>= (32-UNIT_SHIFT
);
607 talk_value(id
, unit
, enqueue
); /* speak it */
608 return 0; /* and stop, end of special case */
611 clipbuf
= get_clip(id
, &clipsize
);
613 return -1; /* not present */
615 queue_clip(clipbuf
, clipsize
, enqueue
);
620 /* Speaks zero or more IDs (from an array). */
621 int talk_idarray(long *ids
, bool enqueue
)
626 while(*ids
!= TALK_FINAL_ID
)
628 if((r
= talk_id(*ids
++, enqueue
)) <0)
635 /* Make sure the current utterance is not interrupted by the next one. */
636 void talk_force_enqueue_next(void)
638 force_enqueue_next
= true;
641 /* play a thumbnail from file */
642 int talk_file(const char* filename
, bool enqueue
)
646 struct mp3entry info
;
648 #if CONFIG_CODEC != SWCODEC
649 if (audio_status()) /* busy, buffer in use */
653 if (p_thumbnail
== NULL
|| size_for_thumbnail
<= 0)
656 if(mp3info(&info
, filename
, false)) /* use this to find real start */
658 return 0; /* failed to open, or invalid */
661 fd
= open(filename
, O_RDONLY
);
662 if (fd
< 0) /* failed to open */
667 lseek(fd
, info
.first_frame_offset
, SEEK_SET
); /* behind ID data */
669 size
= read(fd
, p_thumbnail
, size_for_thumbnail
);
672 /* ToDo: find audio, skip ID headers and trailers */
674 if (size
!= 0 && size
!= size_for_thumbnail
) /* Don't play missing or truncated clips */
676 #if CONFIG_CODEC != SWCODEC
677 bitswap(p_thumbnail
, size
);
679 queue_clip(p_thumbnail
, size
, enqueue
);
686 /* say a numeric value, this word ordering works for english,
687 but not necessarily for other languages (e.g. german) */
688 int talk_number(long n
, bool enqueue
)
690 int level
= 2; /* mille count */
691 long mil
= 1000000000; /* highest possible "-illion" */
693 #if CONFIG_CODEC != SWCODEC
694 if (audio_status()) /* busy, buffer in use */
699 shutup(); /* cut off all the pending stuff */
703 talk_id(VOICE_ZERO
, true);
709 talk_id(VOICE_MINUS
, true);
715 int segment
= n
/ mil
; /* extract in groups of 3 digits */
716 n
-= segment
* mil
; /* remove the used digits from number */
717 mil
/= 1000; /* digit place for next round */
721 int hundreds
= segment
/ 100;
722 int ones
= segment
% 100;
726 talk_id(VOICE_ZERO
+ hundreds
, true);
727 talk_id(VOICE_HUNDRED
, true);
730 /* combination indexing */
733 int tens
= ones
/10 + 18;
734 talk_id(VOICE_ZERO
+ tens
, true);
738 /* direct indexing */
740 talk_id(VOICE_ZERO
+ ones
, true);
742 /* add billion, million, thousand */
744 talk_id(VOICE_THOUSAND
+ level
, true);
752 /* singular/plural aware saying of a value */
753 int talk_value(long n
, int unit
, bool enqueue
)
756 static const int unit_voiced
[] =
757 { /* lookup table for the voice ID of the units */
758 [0 ... UNIT_LAST
-1] = -1, /* regular ID, int, signed */
760 = VOICE_MILLISECONDS
, /* here come the "real" units */
774 = VOICE_MILLIAMPHOURS
,
784 = VOICE_KBIT_PER_SEC
,
787 #if CONFIG_CODEC != SWCODEC
788 if (audio_status()) /* busy, buffer in use */
792 if (unit
< 0 || unit
>= UNIT_LAST
)
795 unit_id
= unit_voiced
[unit
];
797 if ((n
==1 || n
==-1) /* singular? */
798 && unit_id
>= VOICE_SECONDS
&& unit_id
<= VOICE_HOURS
)
800 unit_id
--; /* use the singular for those units which have */
803 /* special case with a "plus" before */
804 if (n
> 0 && (unit
== UNIT_SIGNED
|| unit
== UNIT_DB
))
806 talk_id(VOICE_PLUS
, enqueue
);
810 talk_number(n
, enqueue
); /* say the number */
811 talk_id(unit_id
, true); /* say the unit, if any */
817 int talk_spell(const char* spell
, bool enqueue
)
819 char c
; /* currently processed char */
821 #if CONFIG_CODEC != SWCODEC
822 if (audio_status()) /* busy, buffer in use */
827 shutup(); /* cut off all the pending stuff */
829 while ((c
= *spell
++) != '\0')
831 /* if this grows into too many cases, I should use a table */
832 if (c
>= 'A' && c
<= 'Z')
833 talk_id(VOICE_CHAR_A
+ c
- 'A', true);
834 else if (c
>= 'a' && c
<= 'z')
835 talk_id(VOICE_CHAR_A
+ c
- 'a', true);
836 else if (c
>= '0' && c
<= '9')
837 talk_id(VOICE_ZERO
+ c
- '0', true);
839 talk_id(VOICE_MINUS
, true);
841 talk_id(VOICE_PLUS
, true);
843 talk_id(VOICE_DOT
, true);
845 talk_id(VOICE_PAUSE
, true);
851 bool talk_menus_enabled(void)
853 return (global_settings
.talk_menu
&& talk_menu_disable
== 0);
857 void talk_disable_menus(void)
862 void talk_enable_menus(void)