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 ****************************************************************************/
30 #include "mp3_playback.h"
37 #if CONFIG_CODEC == SWCODEC
41 /***************** Constants *****************/
43 #define QUEUE_SIZE 64 /* must be a power of two */
44 #define QUEUE_MASK (QUEUE_SIZE-1)
45 const char* const dir_thumbnail_name
= "_dirname.talk";
46 const char* const file_thumbnail_ext
= ".talk";
48 /***************** Functional Macros *****************/
50 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
52 #define LOADED_MASK 0x80000000 /* MSB */
55 /***************** Data types *****************/
57 struct clip_entry
/* one entry of the index table */
59 int offset
; /* offset from start of voicefile file */
60 int size
; /* size of the clip */
63 struct voicefile
/* file format of our voice file */
65 int version
; /* version of the voicefile */
66 int table
; /* offset to index table, (=header size) */
67 int id1_max
; /* number of "normal" clips contained in above index */
68 int id2_max
; /* number of "voice only" clips contained in above index */
69 struct clip_entry index
[]; /* followed by the index tables */
70 /* and finally the bitswapped mp3 clips, not visible here */
73 struct queue_entry
/* one entry of the internal queue */
80 /***************** Globals *****************/
82 static unsigned char* p_thumbnail
; /* buffer for thumbnail */
83 static long size_for_thumbnail
; /* leftover buffer size for it */
84 static struct voicefile
* p_voicefile
; /* loaded voicefile */
85 static bool has_voicefile
; /* a voicefile file is present */
86 static struct queue_entry queue
[QUEUE_SIZE
]; /* queue of scheduled clips */
87 static int queue_write
; /* write index of queue, by application */
88 static int queue_read
; /* read index of queue, by ISR context */
89 static int sent
; /* how many bytes handed over to playback, owned by ISR */
90 static unsigned char curr_hd
[3]; /* current frame header, for re-sync */
91 static int filehandle
; /* global, so the MMC variant can keep the file open */
92 static unsigned char* p_silence
; /* VOICE_PAUSE clip, used for termination */
93 static long silence_len
; /* length of the VOICE_PAUSE clip */
94 static unsigned char* p_lastclip
; /* address of latest clip, for silence add */
95 static unsigned long voicefile_size
= 0; /* size of the loaded voice file */
98 /***************** Private prototypes *****************/
100 static void load_voicefile(void);
101 static void mp3_callback(unsigned char** start
, int* size
);
102 static int shutup(void);
103 static int queue_clip(unsigned char* buf
, long size
, bool enqueue
);
104 static int open_voicefile(void);
105 static unsigned char* get_clip(long id
, long* p_size
);
108 /***************** Private implementation *****************/
110 static int open_voicefile(void)
113 char* p_lang
= "english"; /* default */
115 if ( global_settings
.lang_file
[0] &&
116 global_settings
.lang_file
[0] != 0xff )
117 { /* try to open the voice file of the selected language */
118 p_lang
= global_settings
.lang_file
;
121 snprintf(buf
, sizeof(buf
), ROCKBOX_DIR LANG_DIR
"/%s.voice", p_lang
);
123 return open(buf
, O_RDONLY
);
126 int talk_get_bufsize(void)
128 return voicefile_size
;
132 static unsigned short BSWAP16(unsigned short value
)
134 return (value
>> 8) | (value
<< 8);
137 static unsigned long BSWAP32(unsigned long value
)
139 unsigned long hi
= BSWAP16(value
>> 16);
140 unsigned long lo
= BSWAP16(value
& 0xffff);
141 return (lo
<< 16) | hi
;
145 /* load the voice file into the mp3 buffer */
146 static void load_voicefile(void)
151 #if CONFIG_CODEC == SWCODEC
153 unsigned char *buf
, temp
;
156 filehandle
= open_voicefile();
157 if (filehandle
< 0) /* failed to open */
160 file_size
= filesize(filehandle
);
161 if (file_size
> audiobufend
- audiobuf
) /* won't fit? */
164 #ifdef HAVE_MMC /* load only the header for now */
165 load_size
= offsetof(struct voicefile
, index
);
166 #else /* load the full file */
167 load_size
= file_size
;
170 got_size
= read(filehandle
, audiobuf
, load_size
);
171 if (got_size
!= load_size
/* failure */)
175 logf("Byte swapping voice file");
176 p_voicefile
= (struct voicefile
*)audiobuf
;
177 p_voicefile
->version
= BSWAP32(p_voicefile
->version
);
178 p_voicefile
->table
= BSWAP32(p_voicefile
->table
);
179 p_voicefile
->id1_max
= BSWAP32(p_voicefile
->id1_max
);
180 p_voicefile
->id2_max
= BSWAP32(p_voicefile
->id2_max
);
184 if (((struct voicefile
*)audiobuf
)->table
/* format check */
185 == offsetof(struct voicefile
, index
))
187 p_voicefile
= (struct voicefile
*)audiobuf
;
189 /* thumbnail buffer is the remaining space behind */
190 p_thumbnail
= audiobuf
+ file_size
;
191 p_thumbnail
+= (long)p_thumbnail
% 2; /* 16-bit align */
192 size_for_thumbnail
= audiobufend
- p_thumbnail
;
198 for (i
= 0; i
< p_voicefile
->id1_max
+ p_voicefile
->id2_max
; i
++)
200 struct clip_entry
*ce
;
201 ce
= &p_voicefile
->index
[i
];
202 ce
->offset
= BSWAP32(ce
->offset
);
203 ce
->size
= BSWAP32(ce
->size
);
207 /* Do a bitswap as necessary. */
208 #if CONFIG_CODEC == SWCODEC
209 logf("Bitswapping voice file.");
211 buf
= (unsigned char *)(&p_voicefile
->index
) +
212 (p_voicefile
->id1_max
+ p_voicefile
->id2_max
) * sizeof(struct clip_entry
);
213 length
= file_size
- (buf
- audiobuf
);
215 for (i
= 0; i
< length
; i
++)
218 temp
= ((temp
>> 4) & 0x0f) | ((temp
& 0x0f) << 4);
219 temp
= ((temp
>> 2) & 0x33) | ((temp
& 0x33) << 2);
220 buf
[i
] = ((temp
>> 1) & 0x55) | ((temp
& 0x55) << 1);
227 /* load the index table, now that we know its size from the header */
228 load_size
= (p_voicefile
->id1_max
+ p_voicefile
->id2_max
)
229 * sizeof(struct clip_entry
);
230 got_size
= read(filehandle
,
231 audiobuf
+ offsetof(struct voicefile
, index
), load_size
);
232 if (got_size
!= load_size
) /* read error */
235 close(filehandle
); /* only the MMC variant leaves it open */
239 /* make sure to have the silence clip, if available */
240 p_silence
= get_clip(VOICE_PAUSE
, &silence_len
);
246 has_voicefile
= false; /* don't try again */
256 /* called in ISR context if mp3 data got consumed */
257 static void mp3_callback(unsigned char** start
, int* size
)
259 queue
[queue_read
].len
-= sent
; /* we completed this */
260 queue
[queue_read
].buf
+= sent
;
262 if (queue
[queue_read
].len
> 0) /* current clip not finished? */
263 { /* feed the next 64K-1 chunk */
264 #if CONFIG_CODEC != SWCODEC
265 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
267 sent
= queue
[queue_read
].len
;
269 *start
= queue
[queue_read
].buf
;
273 else if (sent
> 0) /* go to next entry */
275 queue_read
= (queue_read
+ 1) & QUEUE_MASK
;
280 if (QUEUE_LEVEL
) /* queue is not empty? */
281 { /* start next clip */
282 #if CONFIG_CODEC != SWCODEC
283 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
285 sent
= queue
[queue_read
].len
;
287 *start
= p_lastclip
= queue
[queue_read
].buf
;
289 curr_hd
[0] = p_lastclip
[1];
290 curr_hd
[1] = p_lastclip
[2];
291 curr_hd
[2] = p_lastclip
[3];
293 else if (p_silence
!= NULL
/* silence clip available */
294 && p_lastclip
!= p_silence
/* previous clip wasn't silence */
295 && p_lastclip
< p_thumbnail
) /* ..and not a thumbnail */
296 { /* add silence clip when queue runs empty playing a voice clip */
297 queue
[queue_write
].buf
= p_silence
;
298 queue
[queue_write
].len
= silence_len
;
299 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
305 *size
= 0; /* end of data */
306 mp3_play_stop(); /* fixme: should be done by caller */
310 /* stop the playback and the pending clips, but at frame boundary */
311 static int shutup(void)
314 unsigned char* search
;
317 if (QUEUE_LEVEL
== 0) /* has ended anyway */
321 #if CONFIG_CPU == SH7034
322 CHCR3
&= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
324 /* search next frame boundary and continue up to there */
325 pos
= search
= mp3_get_pos();
326 end
= queue
[queue_read
].buf
+ queue
[queue_read
].len
;
328 if (pos
>= queue
[queue_read
].buf
329 && pos
<= end
) /* really our clip? */
330 { /* (for strange reasons this isn't nesessarily the case) */
331 /* find the next frame boundary */
332 while (search
< end
) /* search the remaining data */
334 if (*search
++ != 0xFF) /* quick search for frame sync byte */
335 continue; /* (this does the majority of the job) */
337 /* look at the (bitswapped) rest of header candidate */
338 if (search
[0] == curr_hd
[0] /* do the quicker checks first */
339 && search
[2] == curr_hd
[2]
340 && (search
[1] & 0x30) == (curr_hd
[1] & 0x30)) /* sample rate */
342 search
--; /* back to the sync byte */
343 break; /* From looking at it, this is our header. */
348 { /* play old data until the frame end, to keep the MAS in sync */
351 queue_write
= (queue_read
+ 1) & QUEUE_MASK
; /* will be empty after next callback */
352 queue
[queue_read
].len
= sent
; /* current one ends after this */
354 #if CONFIG_CPU == SH7034
355 DTCR3
= sent
; /* let the DMA finish this frame */
356 CHCR3
|= 0x0001; /* re-enable DMA */
362 /* nothing to do, was frame boundary or not our clip */
364 queue_write
= queue_read
= 0; /* reset the queue */
370 /* schedule a clip, at the end or discard the existing queue */
371 static int queue_clip(unsigned char* buf
, long size
, bool enqueue
)
376 shutup(); /* cut off all the pending stuff */
379 return 0; /* safety check */
380 #if CONFIG_CPU == SH7034
381 /* disable the DMA temporarily, to be safe of race condition */
384 queue_level
= QUEUE_LEVEL
; /* check old level */
386 if (queue_level
< QUEUE_SIZE
- 1) /* space left? */
388 queue
[queue_write
].buf
= buf
; /* populate an entry */
389 queue
[queue_write
].len
= size
;
390 queue_write
= (queue_write
+ 1) & QUEUE_MASK
;
393 if (queue_level
== 0)
394 { /* queue was empty, we have to do the initial start */
396 #if CONFIG_CODEC != SWCODEC
397 sent
= MIN(size
, 0xFFFF); /* DMA can do no more */
401 mp3_play_data(buf
, sent
, mp3_callback
);
405 mp3_play_pause(true); /* kickoff audio */
409 #if CONFIG_CPU == SH7034
410 CHCR3
|= 0x0001; /* re-enable DMA */
417 /* fetch a clip from the voice file */
418 static unsigned char* get_clip(long id
, long* p_size
)
421 unsigned char* clipbuf
;
423 if (id
> VOICEONLY_DELIMITER
)
424 { /* voice-only entries use the second part of the table */
425 id
-= VOICEONLY_DELIMITER
+ 1;
426 if (id
>= p_voicefile
->id2_max
)
427 return NULL
; /* must be newer than we have */
428 id
+= p_voicefile
->id1_max
; /* table 2 is behind table 1 */
431 { /* normal use of the first table */
432 if (id
>= p_voicefile
->id1_max
)
433 return NULL
; /* must be newer than we have */
436 clipsize
= p_voicefile
->index
[id
].size
;
437 if (clipsize
== 0) /* clip not included in voicefile */
439 clipbuf
= audiobuf
+ p_voicefile
->index
[id
].offset
;
441 #ifdef HAVE_MMC /* dynamic loading, on demand */
442 if (!(clipsize
& LOADED_MASK
))
443 { /* clip used for the first time, needs loading */
444 lseek(filehandle
, p_voicefile
->index
[id
].offset
, SEEK_SET
);
445 if (read(filehandle
, clipbuf
, clipsize
) != clipsize
)
446 return NULL
; /* read error */
448 p_voicefile
->index
[id
].size
|= LOADED_MASK
; /* mark as loaded */
451 { /* clip is in memory already */
452 clipsize
&= ~LOADED_MASK
; /* without the extra bit gives true size */
461 /* common code for talk_init() and talk_buffer_steal() */
462 static void reset_state(void)
464 queue_write
= queue_read
= 0; /* reset the queue */
465 p_voicefile
= NULL
; /* indicate no voicefile (trashed) */
466 p_thumbnail
= audiobuf
; /* whole space for thumbnail */
467 size_for_thumbnail
= audiobufend
- audiobuf
;
468 p_silence
= NULL
; /* pause clip not accessible */
471 /***************** Public implementation *****************/
475 #if CONFIG_CODEC == SWCODEC
478 reset_state(); /* use this for most of our inits */
481 load_voicefile(); /* load the tables right away */
482 has_voicefile
= (p_voicefile
!= NULL
);
484 filehandle
= open_voicefile();
485 has_voicefile
= (filehandle
>= 0); /* test if we can open it */
490 voicefile_size
= filesize(filehandle
);
491 #if CONFIG_CODEC == SWCODEC
494 close(filehandle
); /* close again, this was just to detect presence */
502 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
503 int talk_buffer_steal(void)
507 if (filehandle
>= 0) /* only relevant for MMC */
518 /* play a voice ID from voicefile */
519 int talk_id(long id
, bool enqueue
)
522 unsigned char* clipbuf
;
525 #if CONFIG_CODEC != SWCODEC
526 if (audio_status()) /* busy, buffer in use */
530 if (p_voicefile
== NULL
&& has_voicefile
)
531 load_voicefile(); /* reload needed */
533 if (p_voicefile
== NULL
) /* still no voices? */
536 if (id
== -1) /* -1 is an indication for silence */
539 /* check if this is a special ID, with a value */
540 unit
= ((unsigned long)id
) >> UNIT_SHIFT
;
542 { /* sign-extend the value */
543 id
= (unsigned long)id
<< (32-UNIT_SHIFT
);
544 id
>>= (32-UNIT_SHIFT
);
545 talk_value(id
, unit
, enqueue
); /* speak it */
546 return 0; /* and stop, end of special case */
549 clipbuf
= get_clip(id
, &clipsize
);
551 return -1; /* not present */
553 queue_clip(clipbuf
, clipsize
, enqueue
);
559 /* play a thumbnail from file */
560 int talk_file(const char* filename
, bool enqueue
)
564 struct mp3entry info
;
566 if (audio_status()) /* busy, buffer in use */
569 if (p_thumbnail
== NULL
|| size_for_thumbnail
<= 0)
572 if(mp3info(&info
, filename
, false)) /* use this to find real start */
574 return 0; /* failed to open, or invalid */
577 fd
= open(filename
, O_RDONLY
);
578 if (fd
< 0) /* failed to open */
583 lseek(fd
, info
.first_frame_offset
, SEEK_SET
); /* behind ID data */
585 size
= read(fd
, p_thumbnail
, size_for_thumbnail
);
588 /* ToDo: find audio, skip ID headers and trailers */
592 #if CONFIG_CODEC != SWCODEC
593 bitswap(p_thumbnail
, size
);
595 queue_clip(p_thumbnail
, size
, enqueue
);
602 /* say a numeric value, this word ordering works for english,
603 but not necessarily for other languages (e.g. german) */
604 int talk_number(long n
, bool enqueue
)
606 int level
= 0; /* mille count */
607 long mil
= 1000000000; /* highest possible "-illion" */
609 #if CONFIG_CODEC != SWCODEC
610 if (audio_status()) /* busy, buffer in use */
615 shutup(); /* cut off all the pending stuff */
619 talk_id(VOICE_ZERO
, true);
625 talk_id(VOICE_MINUS
, true);
631 int segment
= n
/ mil
; /* extract in groups of 3 digits */
632 n
-= segment
* mil
; /* remove the used digits from number */
633 mil
/= 1000; /* digit place for next round */
637 int hundreds
= segment
/ 100;
638 int ones
= segment
% 100;
642 talk_id(VOICE_ZERO
+ hundreds
, true);
643 talk_id(VOICE_HUNDRED
, true);
646 /* combination indexing */
649 int tens
= ones
/10 + 18;
650 talk_id(VOICE_ZERO
+ tens
, true);
654 /* direct indexing */
656 talk_id(VOICE_ZERO
+ ones
, true);
658 /* add billion, million, thousand */
660 talk_id(VOICE_BILLION
+ level
, true);
668 /* singular/plural aware saying of a value */
669 int talk_value(long n
, int unit
, bool enqueue
)
672 static const int unit_voiced
[] =
673 { /* lookup table for the voice ID of the units */
674 -1, -1, -1, /* regular ID, int, signed */
675 VOICE_MILLISECONDS
, /* here come the "real" units */
690 #if CONFIG_CODEC != SWCODEC
691 if (audio_status()) /* busy, buffer in use */
695 if (unit
< 0 || unit
>= UNIT_LAST
)
698 unit_id
= unit_voiced
[unit
];
700 if ((n
==1 || n
==-1) /* singular? */
701 && unit_id
>= VOICE_SECONDS
&& unit_id
<= VOICE_HOURS
)
703 unit_id
--; /* use the singular for those units which have */
706 /* special case with a "plus" before */
707 if (n
> 0 && (unit
== UNIT_SIGNED
|| unit
== UNIT_DB
))
709 talk_id(VOICE_PLUS
, enqueue
);
713 talk_number(n
, enqueue
); /* say the number */
714 talk_id(unit_id
, true); /* say the unit, if any */
720 int talk_spell(const char* spell
, bool enqueue
)
722 char c
; /* currently processed char */
724 #if CONFIG_CODEC != SWCODEC
725 if (audio_status()) /* busy, buffer in use */
730 shutup(); /* cut off all the pending stuff */
732 while ((c
= *spell
++) != '\0')
734 /* if this grows into too many cases, I should use a table */
735 if (c
>= 'A' && c
<= 'Z')
736 talk_id(VOICE_CHAR_A
+ c
- 'A', true);
737 else if (c
>= 'a' && c
<= 'z')
738 talk_id(VOICE_CHAR_A
+ c
- 'a', true);
739 else if (c
>= '0' && c
<= '9')
740 talk_id(VOICE_ZERO
+ c
- '0', true);
742 talk_id(VOICE_MINUS
, true);
744 talk_id(VOICE_PLUS
, true);
746 talk_id(VOICE_DOT
, true);
748 talk_id(VOICE_PAUSE
, true);