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 */
145 /***************** Private implementation *****************/
147 static int open_voicefile(void)
150 char* p_lang
= "english"; /* default */
152 if ( global_settings
.lang_file
[0] &&
153 global_settings
.lang_file
[0] != 0xff )
154 { /* try to open the voice file of the selected language */
155 p_lang
= (char *)global_settings
.lang_file
;
158 snprintf(buf
, sizeof(buf
), LANG_DIR
"/%s.voice", p_lang
);
160 return open(buf
, O_RDONLY
);
164 /* load the voice file into the mp3 buffer */
165 static void load_voicefile(void)
170 #ifdef ROCKBOX_LITTLE_ENDIAN
174 filehandle
= open_voicefile();
175 if (filehandle
< 0) /* failed to open */
178 file_size
= filesize(filehandle
);
179 if (file_size
> audiobufend
- audiobuf
) /* won't fit? */
182 #ifdef HAVE_MMC /* load only the header for now */
183 load_size
= offsetof(struct voicefile
, index
);
184 #else /* load the full file */
185 load_size
= file_size
;
188 got_size
= read(filehandle
, audiobuf
, load_size
);
189 if (got_size
!= load_size
/* failure */)
192 #ifdef ROCKBOX_LITTLE_ENDIAN
193 logf("Byte swapping voice file");
194 structec_convert(audiobuf
, "lllll", 1, true);
197 if (((struct voicefile
*)audiobuf
)->table
/* format check */
198 == offsetof(struct voicefile
, index
))
200 p_voicefile
= (struct voicefile
*)audiobuf
;
202 if (p_voicefile
->target_id
!= TARGET_ID
)
204 logf("Incompatible voice file (wrong target)");
207 #if CONFIG_CODEC != SWCODEC
208 /* MASCODEC: now use audiobuf for voice then thumbnail */
209 p_thumbnail
= audiobuf
+ file_size
;
210 p_thumbnail
+= (long)p_thumbnail
% 2; /* 16-bit align */
211 size_for_thumbnail
= audiobufend
- p_thumbnail
;
217 #ifdef ROCKBOX_LITTLE_ENDIAN
218 for (i
= 0; i
< p_voicefile
->id1_max
+ p_voicefile
->id2_max
; i
++)
219 structec_convert(&p_voicefile
->index
[i
], "ll", 1, true);
223 /* load the index table, now that we know its size from the header */
224 load_size
= (p_voicefile
->id1_max
+ p_voicefile
->id2_max
)
225 * sizeof(struct clip_entry
);
226 got_size
= read(filehandle
,
227 (unsigned char *) p_voicefile
+ offsetof(struct voicefile
, index
), load_size
);
228 if (got_size
!= load_size
) /* read error */
231 close(filehandle
); /* only the MMC variant leaves it open */
235 /* make sure to have the silence clip, if available */
236 p_silence
= get_clip(VOICE_PAUSE
, &silence_len
);
242 has_voicefile
= false; /* don't try again */
252 /* Are more voice clips queued and waiting? */
253 bool is_voice_queued()
255 return !!QUEUE_LEVEL
;
259 /* called in ISR context if mp3 data got consumed */
260 static void mp3_callback(unsigned char** start
, size_t* size
)
262 queue
[queue_read
].len
-= sent
; /* we completed this */
263 queue
[queue_read
].buf
+= sent
;
265 if (queue
[queue_read
].len
> 0) /* current clip not finished? */
266 { /* feed the next 64K-1 chunk */
267 #if CONFIG_CODEC != SWCODEC
268 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
270 sent
= queue
[queue_read
].len
;
272 *start
= queue
[queue_read
].buf
;
276 else if (sent
> 0) /* go to next entry */
278 queue_read
= (queue_read
+ 1) & QUEUE_MASK
;
283 if (QUEUE_LEVEL
) /* queue is not empty? */
284 { /* start next clip */
285 #if CONFIG_CODEC != SWCODEC
286 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
288 sent
= queue
[queue_read
].len
;
290 *start
= p_lastclip
= queue
[queue_read
].buf
;
292 curr_hd
[0] = p_lastclip
[1];
293 curr_hd
[1] = p_lastclip
[2];
294 curr_hd
[2] = p_lastclip
[3];
296 else if (p_silence
!= NULL
/* silence clip available */
297 && p_lastclip
!= p_silence
/* previous clip wasn't silence */
298 && p_lastclip
!= p_thumbnail
) /* ..or thumbnail */
299 { /* add silence clip when queue runs empty playing a voice clip */
300 queue
[queue_write
].buf
= p_silence
;
301 queue
[queue_write
].len
= silence_len
;
302 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
308 *size
= 0; /* end of data */
312 /* stop the playback and the pending clips */
315 #if CONFIG_CODEC != SWCODEC
317 unsigned char* search
;
321 if (QUEUE_LEVEL
== 0) /* has ended anyway */
323 #if CONFIG_CODEC == SWCODEC
328 #if CONFIG_CODEC != SWCODEC
329 #if CONFIG_CPU == SH7034
330 CHCR3
&= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
332 /* search next frame boundary and continue up to there */
333 pos
= search
= mp3_get_pos();
334 end
= queue
[queue_read
].buf
+ queue
[queue_read
].len
;
336 if (pos
>= queue
[queue_read
].buf
337 && pos
<= end
) /* really our clip? */
338 { /* (for strange reasons this isn't nesessarily the case) */
339 /* find the next frame boundary */
340 while (search
< end
) /* search the remaining data */
342 if (*search
++ != 0xFF) /* quick search for frame sync byte */
343 continue; /* (this does the majority of the job) */
345 /* look at the (bitswapped) rest of header candidate */
346 if (search
[0] == curr_hd
[0] /* do the quicker checks first */
347 && search
[2] == curr_hd
[2]
348 && (search
[1] & 0x30) == (curr_hd
[1] & 0x30)) /* sample rate */
350 search
--; /* back to the sync byte */
351 break; /* From looking at it, this is our header. */
356 { /* play old data until the frame end, to keep the MAS in sync */
359 queue_write
= (queue_read
+ 1) & QUEUE_MASK
; /* will be empty after next callback */
360 queue
[queue_read
].len
= sent
; /* current one ends after this */
362 #if CONFIG_CPU == SH7034
363 DTCR3
= sent
; /* let the DMA finish this frame */
364 CHCR3
|= 0x0001; /* re-enable DMA */
371 /* nothing to do, was frame boundary or not our clip */
374 queue_write
= queue_read
= 0; /* reset the queue */
379 /* Shutup the voice, except if force_enqueue_next is set. */
380 static int shutup(void)
382 if (!force_enqueue_next
)
387 /* schedule a clip, at the end or discard the existing queue */
388 static int queue_clip(unsigned char* buf
, long size
, bool enqueue
)
393 shutup(); /* cut off all the pending stuff */
394 /* Something is being enqueued, force_enqueue_next override is no
396 force_enqueue_next
= false;
399 return 0; /* safety check */
400 #if CONFIG_CPU == SH7034
401 /* disable the DMA temporarily, to be safe of race condition */
404 queue_level
= QUEUE_LEVEL
; /* check old level */
406 if (queue_level
< QUEUE_SIZE
- 1) /* space left? */
408 queue
[queue_write
].buf
= buf
; /* populate an entry */
409 queue
[queue_write
].len
= size
;
410 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
413 if (queue_level
== 0)
414 { /* queue was empty, we have to do the initial start */
416 #if CONFIG_CODEC != SWCODEC
417 sent
= MIN(size
, 0xFFFF); /* DMA can do no more */
421 mp3_play_data(buf
, sent
, mp3_callback
);
425 mp3_play_pause(true); /* kickoff audio */
429 #if CONFIG_CPU == SH7034
430 CHCR3
|= 0x0001; /* re-enable DMA */
437 /* fetch a clip from the voice file */
438 static unsigned char* get_clip(long id
, long* p_size
)
441 unsigned char* clipbuf
;
443 if (id
> VOICEONLY_DELIMITER
)
444 { /* voice-only entries use the second part of the table */
445 id
-= VOICEONLY_DELIMITER
+ 1;
446 if (id
>= p_voicefile
->id2_max
)
447 return NULL
; /* must be newer than we have */
448 id
+= p_voicefile
->id1_max
; /* table 2 is behind table 1 */
451 { /* normal use of the first table */
452 if (id
>= p_voicefile
->id1_max
)
453 return NULL
; /* must be newer than we have */
456 clipsize
= p_voicefile
->index
[id
].size
;
457 if (clipsize
== 0) /* clip not included in voicefile */
459 clipbuf
= (unsigned char *) p_voicefile
+ p_voicefile
->index
[id
].offset
;
461 #ifdef HAVE_MMC /* dynamic loading, on demand */
462 if (!(clipsize
& LOADED_MASK
))
463 { /* clip used for the first time, needs loading */
464 lseek(filehandle
, p_voicefile
->index
[id
].offset
, SEEK_SET
);
465 if (read(filehandle
, clipbuf
, clipsize
) != clipsize
)
466 return NULL
; /* read error */
468 p_voicefile
->index
[id
].size
|= LOADED_MASK
; /* mark as loaded */
471 { /* clip is in memory already */
472 clipsize
&= ~LOADED_MASK
; /* without the extra bit gives true size */
481 /* common code for talk_init() and talk_buffer_steal() */
482 static void reset_state(void)
484 queue_write
= queue_read
= 0; /* reset the queue */
485 p_voicefile
= NULL
; /* indicate no voicefile (trashed) */
486 #if CONFIG_CODEC == SWCODEC
487 /* Allocate a dedicated thumbnail buffer - once */
488 if (p_thumbnail
== NULL
)
490 size_for_thumbnail
= audiobufend
- audiobuf
;
491 if (size_for_thumbnail
> MAX_THUMBNAIL_BUFSIZE
)
492 size_for_thumbnail
= MAX_THUMBNAIL_BUFSIZE
;
493 p_thumbnail
= buffer_alloc(size_for_thumbnail
);
496 /* Just use the audiobuf, without allocating anything */
497 p_thumbnail
= audiobuf
;
498 size_for_thumbnail
= audiobufend
- audiobuf
;
500 p_silence
= NULL
; /* pause clip not accessible */
503 /***************** Public implementation *****************/
507 talk_menu_disable
= 0;
508 if (talk_initialized
&& !strcasecmp(last_lang
, global_settings
.lang_file
))
510 /* not a new file, nothing to do */
515 if (filehandle
>= 0) /* MMC: An old voice file might still be open */
522 talk_initialized
= true;
523 strncpy((char *) last_lang
, (char *)global_settings
.lang_file
,
526 #if CONFIG_CODEC == SWCODEC
527 audio_get_buffer(false, NULL
); /* Must tell audio to reinitialize */
529 reset_state(); /* use this for most of our inits */
531 filehandle
= open_voicefile();
532 has_voicefile
= (filehandle
>= 0); /* test if we can open it */
537 voicefile_size
= filesize(filehandle
);
538 close(filehandle
); /* close again, this was just to detect presence */
544 #if CONFIG_CODEC == SWCODEC
545 /* return if a voice codec is required or not */
546 bool talk_voice_required(void)
548 return (voicefile_size
!= 0) /* Voice file is available */
549 || (global_settings
.talk_dir_clip
) /* Thumbnail clips are required */
550 || (global_settings
.talk_file_clip
);
554 /* return size of voice file */
555 int talk_get_bufsize(void)
557 return voicefile_size
;
560 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
561 int talk_buffer_steal(void)
563 #if CONFIG_CODEC != SWCODEC
567 if (filehandle
>= 0) /* only relevant for MMC */
579 /* play a voice ID from voicefile */
580 int talk_id(long id
, bool enqueue
)
583 unsigned char* clipbuf
;
586 #if CONFIG_CODEC != SWCODEC
587 if (audio_status()) /* busy, buffer in use */
591 if (p_voicefile
== NULL
&& has_voicefile
)
592 load_voicefile(); /* reload needed */
594 if (p_voicefile
== NULL
) /* still no voices? */
597 if (id
== -1) /* -1 is an indication for silence */
600 /* check if this is a special ID, with a value */
601 unit
= ((unsigned long)id
) >> UNIT_SHIFT
;
603 { /* sign-extend the value */
604 id
= (unsigned long)id
<< (32-UNIT_SHIFT
);
605 id
>>= (32-UNIT_SHIFT
);
606 talk_value(id
, unit
, enqueue
); /* speak it */
607 return 0; /* and stop, end of special case */
610 clipbuf
= get_clip(id
, &clipsize
);
612 return -1; /* not present */
614 queue_clip(clipbuf
, clipsize
, enqueue
);
619 /* Speaks zero or more IDs (from an array). */
620 int talk_idarray(long *ids
, bool enqueue
)
625 while(*ids
!= TALK_FINAL_ID
)
627 if((r
= talk_id(*ids
++, enqueue
)) <0)
634 /* Make sure the current utterance is not interrupted by the next one. */
635 void talk_force_enqueue_next(void)
637 force_enqueue_next
= true;
640 /* play a thumbnail from file */
641 int talk_file(const char* filename
, bool enqueue
)
645 struct mp3entry info
;
647 #if CONFIG_CODEC != SWCODEC
648 if (audio_status()) /* busy, buffer in use */
652 if (p_thumbnail
== NULL
|| size_for_thumbnail
<= 0)
655 if(mp3info(&info
, filename
, false)) /* use this to find real start */
657 return 0; /* failed to open, or invalid */
660 fd
= open(filename
, O_RDONLY
);
661 if (fd
< 0) /* failed to open */
666 lseek(fd
, info
.first_frame_offset
, SEEK_SET
); /* behind ID data */
668 size
= read(fd
, p_thumbnail
, size_for_thumbnail
);
671 /* ToDo: find audio, skip ID headers and trailers */
673 if (size
!= 0 && size
!= size_for_thumbnail
) /* Don't play missing or truncated clips */
675 #if CONFIG_CODEC != SWCODEC
676 bitswap(p_thumbnail
, size
);
678 queue_clip(p_thumbnail
, size
, enqueue
);
685 /* say a numeric value, this word ordering works for english,
686 but not necessarily for other languages (e.g. german) */
687 int talk_number(long n
, bool enqueue
)
689 int level
= 2; /* mille count */
690 long mil
= 1000000000; /* highest possible "-illion" */
692 #if CONFIG_CODEC != SWCODEC
693 if (audio_status()) /* busy, buffer in use */
698 shutup(); /* cut off all the pending stuff */
702 talk_id(VOICE_ZERO
, true);
708 talk_id(VOICE_MINUS
, true);
714 int segment
= n
/ mil
; /* extract in groups of 3 digits */
715 n
-= segment
* mil
; /* remove the used digits from number */
716 mil
/= 1000; /* digit place for next round */
720 int hundreds
= segment
/ 100;
721 int ones
= segment
% 100;
725 talk_id(VOICE_ZERO
+ hundreds
, true);
726 talk_id(VOICE_HUNDRED
, true);
729 /* combination indexing */
732 int tens
= ones
/10 + 18;
733 talk_id(VOICE_ZERO
+ tens
, true);
737 /* direct indexing */
739 talk_id(VOICE_ZERO
+ ones
, true);
741 /* add billion, million, thousand */
743 talk_id(VOICE_THOUSAND
+ level
, true);
751 /* singular/plural aware saying of a value */
752 int talk_value(long n
, int unit
, bool enqueue
)
755 static const int unit_voiced
[] =
756 { /* lookup table for the voice ID of the units */
757 [0 ... UNIT_LAST
-1] = -1, /* regular ID, int, signed */
759 = VOICE_MILLISECONDS
, /* here come the "real" units */
773 = VOICE_MILLIAMPHOURS
,
783 = VOICE_KBIT_PER_SEC
,
785 = VOICE_PM_UNITS_PER_TICK
,
788 #if CONFIG_CODEC != SWCODEC
789 if (audio_status()) /* busy, buffer in use */
793 if (unit
< 0 || unit
>= UNIT_LAST
)
796 unit_id
= unit_voiced
[unit
];
798 if ((n
==1 || n
==-1) /* singular? */
799 && unit_id
>= VOICE_SECONDS
&& unit_id
<= VOICE_HOURS
)
801 unit_id
--; /* use the singular for those units which have */
804 /* special case with a "plus" before */
805 if (n
> 0 && (unit
== UNIT_SIGNED
|| unit
== UNIT_DB
))
807 talk_id(VOICE_PLUS
, enqueue
);
811 talk_number(n
, enqueue
); /* say the number */
812 talk_id(unit_id
, true); /* say the unit, if any */
818 int talk_spell(const char* spell
, bool enqueue
)
820 char c
; /* currently processed char */
822 #if CONFIG_CODEC != SWCODEC
823 if (audio_status()) /* busy, buffer in use */
828 shutup(); /* cut off all the pending stuff */
830 while ((c
= *spell
++) != '\0')
832 /* if this grows into too many cases, I should use a table */
833 if (c
>= 'A' && c
<= 'Z')
834 talk_id(VOICE_CHAR_A
+ c
- 'A', true);
835 else if (c
>= 'a' && c
<= 'z')
836 talk_id(VOICE_CHAR_A
+ c
- 'a', true);
837 else if (c
>= '0' && c
<= '9')
838 talk_id(VOICE_ZERO
+ c
- '0', true);
840 talk_id(VOICE_MINUS
, true);
842 talk_id(VOICE_PLUS
, true);
844 talk_id(VOICE_DOT
, true);
846 talk_id(VOICE_PAUSE
, true);
852 bool talk_menus_enabled(void)
854 return (global_settings
.talk_menu
&& talk_menu_disable
== 0);
858 void talk_disable_menus(void)
863 void talk_enable_menus(void)