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 table
; /* offset to index table, (=header size) */
99 int id1_max
; /* number of "normal" clips contained in above index */
100 int id2_max
; /* number of "voice only" clips contained in above index */
101 struct clip_entry index
[]; /* followed by the index tables */
102 /* and finally the bitswapped mp3 clips, not visible here */
105 struct queue_entry
/* one entry of the internal queue */
112 /***************** Globals *****************/
114 static unsigned char* p_thumbnail
= NULL
; /* buffer for thumbnail */
115 static long size_for_thumbnail
; /* leftover buffer size for it */
116 static struct voicefile
* p_voicefile
; /* loaded voicefile */
117 static bool has_voicefile
; /* a voicefile file is present */
118 static struct queue_entry queue
[QUEUE_SIZE
]; /* queue of scheduled clips */
119 static int queue_write
; /* write index of queue, by application */
120 static int queue_read
; /* read index of queue, by ISR context */
121 static int sent
; /* how many bytes handed over to playback, owned by ISR */
122 static unsigned char curr_hd
[3]; /* current frame header, for re-sync */
123 static int filehandle
= -1; /* global, so the MMC variant can keep the file open */
124 static unsigned char* p_silence
; /* VOICE_PAUSE clip, used for termination */
125 static long silence_len
; /* length of the VOICE_PAUSE clip */
126 static unsigned char* p_lastclip
; /* address of latest clip, for silence add */
127 static unsigned long voicefile_size
= 0; /* size of the loaded voice file */
128 static unsigned char last_lang
[MAX_FILENAME
+1]; /* name of last used lang file (in talk_init) */
129 static bool talk_initialized
; /* true if talk_init has been called */
131 /***************** Private prototypes *****************/
133 static void load_voicefile(void);
134 static void mp3_callback(unsigned char** start
, int* size
);
135 static int shutup(void);
136 static int queue_clip(unsigned char* buf
, long size
, bool enqueue
);
137 static int open_voicefile(void);
138 static unsigned char* get_clip(long id
, long* p_size
);
141 /***************** Private implementation *****************/
143 static int open_voicefile(void)
146 char* p_lang
= "english"; /* default */
148 if ( global_settings
.lang_file
[0] &&
149 global_settings
.lang_file
[0] != 0xff )
150 { /* try to open the voice file of the selected language */
151 p_lang
= (char *)global_settings
.lang_file
;
154 snprintf(buf
, sizeof(buf
), LANG_DIR
"/%s.voice", p_lang
);
156 return open(buf
, O_RDONLY
);
160 /* load the voice file into the mp3 buffer */
161 static void load_voicefile(void)
166 #if CONFIG_CODEC == SWCODEC
168 unsigned char *buf
, temp
;
171 filehandle
= open_voicefile();
172 if (filehandle
< 0) /* failed to open */
175 file_size
= filesize(filehandle
);
176 if (file_size
> audiobufend
- audiobuf
) /* won't fit? */
179 #ifdef HAVE_MMC /* load only the header for now */
180 load_size
= offsetof(struct voicefile
, index
);
181 #else /* load the full file */
182 load_size
= file_size
;
185 got_size
= read(filehandle
, audiobuf
, load_size
);
186 if (got_size
!= load_size
/* failure */)
189 #ifdef ROCKBOX_LITTLE_ENDIAN
190 logf("Byte swapping voice file");
191 structec_convert(audiobuf
, "llll", 1, true);
194 if (((struct voicefile
*)audiobuf
)->table
/* format check */
195 == offsetof(struct voicefile
, index
))
197 p_voicefile
= (struct voicefile
*)audiobuf
;
199 #if CONFIG_CODEC != SWCODEC
200 /* MASCODEC: now use audiobuf for voice then thumbnail */
201 p_thumbnail
= audiobuf
+ file_size
;
202 p_thumbnail
+= (long)p_thumbnail
% 2; /* 16-bit align */
203 size_for_thumbnail
= audiobufend
- p_thumbnail
;
209 #ifdef ROCKBOX_LITTLE_ENDIAN
210 for (i
= 0; i
< p_voicefile
->id1_max
+ p_voicefile
->id2_max
; i
++)
211 structec_convert(&p_voicefile
->index
[i
], "ll", 1, true);
214 /* Do a bitswap as necessary. */
215 #if CONFIG_CODEC == SWCODEC
216 logf("Bitswapping voice file.");
218 buf
= (unsigned char *)(&p_voicefile
->index
) +
219 (p_voicefile
->id1_max
+ p_voicefile
->id2_max
) * sizeof(struct clip_entry
);
220 length
= file_size
- (buf
- (unsigned char *) p_voicefile
);
222 for (i
= 0; i
< length
; i
++)
225 temp
= ((temp
>> 4) & 0x0f) | ((temp
& 0x0f) << 4);
226 temp
= ((temp
>> 2) & 0x33) | ((temp
& 0x33) << 2);
227 buf
[i
] = ((temp
>> 1) & 0x55) | ((temp
& 0x55) << 1);
234 /* load the index table, now that we know its size from the header */
235 load_size
= (p_voicefile
->id1_max
+ p_voicefile
->id2_max
)
236 * sizeof(struct clip_entry
);
237 got_size
= read(filehandle
,
238 (unsigned char *) p_voicefile
+ offsetof(struct voicefile
, index
), load_size
);
239 if (got_size
!= load_size
) /* read error */
242 close(filehandle
); /* only the MMC variant leaves it open */
246 /* make sure to have the silence clip, if available */
247 p_silence
= get_clip(VOICE_PAUSE
, &silence_len
);
253 has_voicefile
= false; /* don't try again */
263 /* called in ISR context if mp3 data got consumed */
264 static void mp3_callback(unsigned char** start
, int* size
)
266 queue
[queue_read
].len
-= sent
; /* we completed this */
267 queue
[queue_read
].buf
+= sent
;
269 if (queue
[queue_read
].len
> 0) /* current clip not finished? */
270 { /* feed the next 64K-1 chunk */
271 #if CONFIG_CODEC != SWCODEC
272 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
274 sent
= queue
[queue_read
].len
;
276 *start
= queue
[queue_read
].buf
;
280 else if (sent
> 0) /* go to next entry */
282 queue_read
= (queue_read
+ 1) & QUEUE_MASK
;
287 if (QUEUE_LEVEL
) /* queue is not empty? */
288 { /* start next clip */
289 #if CONFIG_CODEC != SWCODEC
290 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
292 sent
= queue
[queue_read
].len
;
294 *start
= p_lastclip
= queue
[queue_read
].buf
;
296 curr_hd
[0] = p_lastclip
[1];
297 curr_hd
[1] = p_lastclip
[2];
298 curr_hd
[2] = p_lastclip
[3];
300 else if (p_silence
!= NULL
/* silence clip available */
301 && p_lastclip
!= p_silence
/* previous clip wasn't silence */
302 && p_lastclip
!= p_thumbnail
) /* ..or thumbnail */
303 { /* add silence clip when queue runs empty playing a voice clip */
304 queue
[queue_write
].buf
= p_silence
;
305 queue
[queue_write
].len
= silence_len
;
306 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
312 *size
= 0; /* end of data */
316 /* stop the playback and the pending clips */
317 static int shutup(void)
319 #if CONFIG_CODEC != SWCODEC
321 unsigned char* search
;
325 if (QUEUE_LEVEL
== 0) /* has ended anyway */
327 #if CONFIG_CODEC == SWCODEC
332 #if CONFIG_CODEC != SWCODEC
333 #if CONFIG_CPU == SH7034
334 CHCR3
&= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
336 /* search next frame boundary and continue up to there */
337 pos
= search
= mp3_get_pos();
338 end
= queue
[queue_read
].buf
+ queue
[queue_read
].len
;
340 if (pos
>= queue
[queue_read
].buf
341 && pos
<= end
) /* really our clip? */
342 { /* (for strange reasons this isn't nesessarily the case) */
343 /* find the next frame boundary */
344 while (search
< end
) /* search the remaining data */
346 if (*search
++ != 0xFF) /* quick search for frame sync byte */
347 continue; /* (this does the majority of the job) */
349 /* look at the (bitswapped) rest of header candidate */
350 if (search
[0] == curr_hd
[0] /* do the quicker checks first */
351 && search
[2] == curr_hd
[2]
352 && (search
[1] & 0x30) == (curr_hd
[1] & 0x30)) /* sample rate */
354 search
--; /* back to the sync byte */
355 break; /* From looking at it, this is our header. */
360 { /* play old data until the frame end, to keep the MAS in sync */
363 queue_write
= (queue_read
+ 1) & QUEUE_MASK
; /* will be empty after next callback */
364 queue
[queue_read
].len
= sent
; /* current one ends after this */
366 #if CONFIG_CPU == SH7034
367 DTCR3
= sent
; /* let the DMA finish this frame */
368 CHCR3
|= 0x0001; /* re-enable DMA */
375 /* nothing to do, was frame boundary or not our clip */
378 queue_write
= queue_read
= 0; /* reset the queue */
384 /* schedule a clip, at the end or discard the existing queue */
385 static int queue_clip(unsigned char* buf
, long size
, bool enqueue
)
390 shutup(); /* cut off all the pending stuff */
393 return 0; /* safety check */
394 #if CONFIG_CPU == SH7034
395 /* disable the DMA temporarily, to be safe of race condition */
398 queue_level
= QUEUE_LEVEL
; /* check old level */
400 if (queue_level
< QUEUE_SIZE
- 1) /* space left? */
402 queue
[queue_write
].buf
= buf
; /* populate an entry */
403 queue
[queue_write
].len
= size
;
404 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
407 if (queue_level
== 0)
408 { /* queue was empty, we have to do the initial start */
410 #if CONFIG_CODEC != SWCODEC
411 sent
= MIN(size
, 0xFFFF); /* DMA can do no more */
415 mp3_play_data(buf
, sent
, mp3_callback
);
419 mp3_play_pause(true); /* kickoff audio */
423 #if CONFIG_CPU == SH7034
424 CHCR3
|= 0x0001; /* re-enable DMA */
431 /* fetch a clip from the voice file */
432 static unsigned char* get_clip(long id
, long* p_size
)
435 unsigned char* clipbuf
;
437 if (id
> VOICEONLY_DELIMITER
)
438 { /* voice-only entries use the second part of the table */
439 id
-= VOICEONLY_DELIMITER
+ 1;
440 if (id
>= p_voicefile
->id2_max
)
441 return NULL
; /* must be newer than we have */
442 id
+= p_voicefile
->id1_max
; /* table 2 is behind table 1 */
445 { /* normal use of the first table */
446 if (id
>= p_voicefile
->id1_max
)
447 return NULL
; /* must be newer than we have */
450 clipsize
= p_voicefile
->index
[id
].size
;
451 if (clipsize
== 0) /* clip not included in voicefile */
453 clipbuf
= (unsigned char *) p_voicefile
+ p_voicefile
->index
[id
].offset
;
455 #ifdef HAVE_MMC /* dynamic loading, on demand */
456 if (!(clipsize
& LOADED_MASK
))
457 { /* clip used for the first time, needs loading */
458 lseek(filehandle
, p_voicefile
->index
[id
].offset
, SEEK_SET
);
459 if (read(filehandle
, clipbuf
, clipsize
) != clipsize
)
460 return NULL
; /* read error */
462 p_voicefile
->index
[id
].size
|= LOADED_MASK
; /* mark as loaded */
465 { /* clip is in memory already */
466 clipsize
&= ~LOADED_MASK
; /* without the extra bit gives true size */
475 /* common code for talk_init() and talk_buffer_steal() */
476 static void reset_state(void)
478 queue_write
= queue_read
= 0; /* reset the queue */
479 p_voicefile
= NULL
; /* indicate no voicefile (trashed) */
480 #if CONFIG_CODEC == SWCODEC
481 /* Allocate a dedicated thumbnail buffer - once */
482 if (p_thumbnail
== NULL
)
484 size_for_thumbnail
= audiobufend
- audiobuf
;
485 if (size_for_thumbnail
> MAX_THUMBNAIL_BUFSIZE
)
486 size_for_thumbnail
= MAX_THUMBNAIL_BUFSIZE
;
487 p_thumbnail
= buffer_alloc(size_for_thumbnail
);
490 /* Just use the audiobuf, without allocating anything */
491 p_thumbnail
= audiobuf
;
492 size_for_thumbnail
= audiobufend
- audiobuf
;
494 p_silence
= NULL
; /* pause clip not accessible */
497 /***************** Public implementation *****************/
501 if (talk_initialized
&& !strcasecmp(last_lang
, global_settings
.lang_file
))
503 /* not a new file, nothing to do */
508 if (filehandle
>= 0) /* MMC: An old voice file might still be open */
515 talk_initialized
= true;
516 strncpy((char *) last_lang
, (char *)global_settings
.lang_file
,
519 #if CONFIG_CODEC == SWCODEC
520 audio_get_buffer(false, NULL
); /* Must tell audio to reinitialize */
522 reset_state(); /* use this for most of our inits */
524 filehandle
= open_voicefile();
525 has_voicefile
= (filehandle
>= 0); /* test if we can open it */
530 voicefile_size
= filesize(filehandle
);
531 close(filehandle
); /* close again, this was just to detect presence */
537 #if CONFIG_CODEC == SWCODEC
538 /* return if a voice codec is required or not */
539 bool talk_voice_required(void)
541 return (voicefile_size
!= 0) /* Voice file is available */
542 || (global_settings
.talk_dir
== 3) /* Thumbnail clips are required */
543 || (global_settings
.talk_file
== 3);
547 /* return size of voice file */
548 int talk_get_bufsize(void)
550 return voicefile_size
;
553 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
554 int talk_buffer_steal(void)
556 #if CONFIG_CODEC != SWCODEC
560 if (filehandle
>= 0) /* only relevant for MMC */
572 /* play a voice ID from voicefile */
573 int talk_id(long id
, bool enqueue
)
576 unsigned char* clipbuf
;
579 #if CONFIG_CODEC != SWCODEC
580 if (audio_status()) /* busy, buffer in use */
584 if (p_voicefile
== NULL
&& has_voicefile
)
585 load_voicefile(); /* reload needed */
587 if (p_voicefile
== NULL
) /* still no voices? */
590 if (id
== -1) /* -1 is an indication for silence */
593 /* check if this is a special ID, with a value */
594 unit
= ((unsigned long)id
) >> UNIT_SHIFT
;
596 { /* sign-extend the value */
597 id
= (unsigned long)id
<< (32-UNIT_SHIFT
);
598 id
>>= (32-UNIT_SHIFT
);
599 talk_value(id
, unit
, enqueue
); /* speak it */
600 return 0; /* and stop, end of special case */
603 clipbuf
= get_clip(id
, &clipsize
);
605 return -1; /* not present */
607 queue_clip(clipbuf
, clipsize
, enqueue
);
613 /* play a thumbnail from file */
614 int talk_file(const char* filename
, bool enqueue
)
618 struct mp3entry info
;
620 #if CONFIG_CODEC != SWCODEC
621 if (audio_status()) /* busy, buffer in use */
625 if (p_thumbnail
== NULL
|| size_for_thumbnail
<= 0)
628 if(mp3info(&info
, filename
, false)) /* use this to find real start */
630 return 0; /* failed to open, or invalid */
633 fd
= open(filename
, O_RDONLY
);
634 if (fd
< 0) /* failed to open */
639 lseek(fd
, info
.first_frame_offset
, SEEK_SET
); /* behind ID data */
641 size
= read(fd
, p_thumbnail
, size_for_thumbnail
);
644 /* ToDo: find audio, skip ID headers and trailers */
646 if (size
!= 0 && size
!= size_for_thumbnail
) /* Don't play missing or truncated clips */
648 #if CONFIG_CODEC != SWCODEC
649 bitswap(p_thumbnail
, size
);
651 queue_clip(p_thumbnail
, size
, enqueue
);
658 /* say a numeric value, this word ordering works for english,
659 but not necessarily for other languages (e.g. german) */
660 int talk_number(long n
, bool enqueue
)
662 int level
= 2; /* mille count */
663 long mil
= 1000000000; /* highest possible "-illion" */
665 #if CONFIG_CODEC != SWCODEC
666 if (audio_status()) /* busy, buffer in use */
671 shutup(); /* cut off all the pending stuff */
675 talk_id(VOICE_ZERO
, true);
681 talk_id(VOICE_MINUS
, true);
687 int segment
= n
/ mil
; /* extract in groups of 3 digits */
688 n
-= segment
* mil
; /* remove the used digits from number */
689 mil
/= 1000; /* digit place for next round */
693 int hundreds
= segment
/ 100;
694 int ones
= segment
% 100;
698 talk_id(VOICE_ZERO
+ hundreds
, true);
699 talk_id(VOICE_HUNDRED
, true);
702 /* combination indexing */
705 int tens
= ones
/10 + 18;
706 talk_id(VOICE_ZERO
+ tens
, true);
710 /* direct indexing */
712 talk_id(VOICE_ZERO
+ ones
, true);
714 /* add billion, million, thousand */
716 talk_id(VOICE_THOUSAND
+ level
, true);
724 /* singular/plural aware saying of a value */
725 int talk_value(long n
, int unit
, bool enqueue
)
728 static const int unit_voiced
[] =
729 { /* lookup table for the voice ID of the units */
730 [0 ... UNIT_LAST
-1] = -1, /* regular ID, int, signed */
732 = VOICE_MILLISECONDS
, /* here come the "real" units */
746 = VOICE_MILLIAMPHOURS
,
756 = VOICE_KBIT_PER_SEC
,
759 #if CONFIG_CODEC != SWCODEC
760 if (audio_status()) /* busy, buffer in use */
764 if (unit
< 0 || unit
>= UNIT_LAST
)
767 unit_id
= unit_voiced
[unit
];
769 if ((n
==1 || n
==-1) /* singular? */
770 && unit_id
>= VOICE_SECONDS
&& unit_id
<= VOICE_HOURS
)
772 unit_id
--; /* use the singular for those units which have */
775 /* special case with a "plus" before */
776 if (n
> 0 && (unit
== UNIT_SIGNED
|| unit
== UNIT_DB
))
778 talk_id(VOICE_PLUS
, enqueue
);
782 talk_number(n
, enqueue
); /* say the number */
783 talk_id(unit_id
, true); /* say the unit, if any */
789 int talk_spell(const char* spell
, bool enqueue
)
791 char c
; /* currently processed char */
793 #if CONFIG_CODEC != SWCODEC
794 if (audio_status()) /* busy, buffer in use */
799 shutup(); /* cut off all the pending stuff */
801 while ((c
= *spell
++) != '\0')
803 /* if this grows into too many cases, I should use a table */
804 if (c
>= 'A' && c
<= 'Z')
805 talk_id(VOICE_CHAR_A
+ c
- 'A', true);
806 else if (c
>= 'a' && c
<= 'z')
807 talk_id(VOICE_CHAR_A
+ c
- 'a', true);
808 else if (c
>= '0' && c
<= '9')
809 talk_id(VOICE_ZERO
+ c
- '0', true);
811 talk_id(VOICE_MINUS
, true);
813 talk_id(VOICE_PLUS
, true);
815 talk_id(VOICE_DOT
, true);
817 talk_id(VOICE_PAUSE
, true);