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 /***************** Constants *****************/
39 #define QUEUE_SIZE 64 /* must be a power of two */
40 #define QUEUE_MASK (QUEUE_SIZE-1)
41 const char* const dir_thumbnail_name
= "_dirname.talk";
43 /***************** Functional Macros *****************/
45 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
47 #define LOADED_MASK 0x80000000 /* MSB */
50 /***************** Data types *****************/
52 struct clip_entry
/* one entry of the index table */
54 int offset
; /* offset from start of voicefile file */
55 int size
; /* size of the clip */
58 struct voicefile
/* file format of our voice file */
60 int version
; /* version of the voicefile */
61 int table
; /* offset to index table, (=header size) */
62 int id1_max
; /* number of "normal" clips contained in above index */
63 int id2_max
; /* number of "voice only" clips contained in above index */
64 struct clip_entry index
[]; /* followed by the index tables */
65 /* and finally the bitswapped mp3 clips, not visible here */
68 struct queue_entry
/* one entry of the internal queue */
75 /***************** Globals *****************/
77 static unsigned char* p_thumbnail
; /* buffer for thumbnail */
78 static long size_for_thumbnail
; /* leftover buffer size for it */
79 static struct voicefile
* p_voicefile
; /* loaded voicefile */
80 static bool has_voicefile
; /* a voicefile file is present */
81 static struct queue_entry queue
[QUEUE_SIZE
]; /* queue of scheduled clips */
82 static int queue_write
; /* write index of queue, by application */
83 static int queue_read
; /* read index of queue, by ISR context */
84 static int sent
; /* how many bytes handed over to playback, owned by ISR */
85 static unsigned char curr_hd
[3]; /* current frame header, for re-sync */
86 static int filehandle
; /* global, so the MMC variant can keep the file open */
89 /***************** Private prototypes *****************/
91 static void load_voicefile(void);
92 static void mp3_callback(unsigned char** start
, int* size
);
93 static int shutup(void);
94 static int queue_clip(unsigned char* buf
, int size
, bool enqueue
);
95 static int open_voicefile(void);
98 /***************** Private implementation *****************/
100 static int open_voicefile(void)
103 char* p_lang
= "english"; /* default */
105 if ( global_settings
.lang_file
[0] &&
106 global_settings
.lang_file
[0] != 0xff )
107 { /* try to open the voice file of the selected language */
108 p_lang
= global_settings
.lang_file
;
111 snprintf(buf
, sizeof(buf
), ROCKBOX_DIR LANG_DIR
"/%s.voice", p_lang
);
113 return open(buf
, O_RDONLY
);
117 /* load the voice file into the mp3 buffer */
118 static void load_voicefile(void)
124 filehandle
= open_voicefile();
125 if (filehandle
< 0) /* failed to open */
128 file_size
= filesize(filehandle
);
129 if (file_size
> mp3end
- mp3buf
) /* won't fit? */
132 #ifdef HAVE_MMC /* load only the header for now */
133 load_size
= offsetof(struct voicefile
, index
);
134 #else /* load the full file */
135 load_size
= file_size
;
138 got_size
= read(filehandle
, mp3buf
, load_size
);
139 if (got_size
== load_size
/* success */
140 && ((struct voicefile
*)mp3buf
)->table
/* format check */
141 == offsetof(struct voicefile
, index
))
143 p_voicefile
= (struct voicefile
*)mp3buf
;
145 /* thumbnail buffer is the remaining space behind */
146 p_thumbnail
= mp3buf
+ file_size
;
147 p_thumbnail
+= (int)p_thumbnail
% 2; /* 16-bit align */
148 size_for_thumbnail
= mp3end
- p_thumbnail
;
154 /* load the index table, now that we know its size from the header */
155 load_size
= (p_voicefile
->id1_max
+ p_voicefile
->id2_max
)
156 * sizeof(struct clip_entry
);
157 got_size
= read(filehandle
,
158 mp3buf
+ offsetof(struct voicefile
, index
), load_size
);
159 if (got_size
!= load_size
) /* read error */
162 close(filehandle
); /* only the MMC variant leaves it open */
170 has_voicefile
= false; /* don't try again */
180 /* called in ISR context if mp3 data got consumed */
181 static void mp3_callback(unsigned char** start
, int* size
)
183 queue
[queue_read
].len
-= sent
; /* we completed this */
184 queue
[queue_read
].buf
+= sent
;
186 if (queue
[queue_read
].len
> 0) /* current clip not finished? */
187 { /* feed the next 64K-1 chunk */
188 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
189 *start
= queue
[queue_read
].buf
;
193 else /* go to next entry */
196 if (queue_read
>= QUEUE_SIZE
)
200 if (QUEUE_LEVEL
) /* queue is not empty? */
201 { /* start next clip */
202 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
203 *start
= queue
[queue_read
].buf
;
205 curr_hd
[0] = *start
[1];
206 curr_hd
[1] = *start
[2];
207 curr_hd
[2] = *start
[3];
211 *size
= 0; /* end of data */
212 mp3_play_stop(); /* fixme: should be done by caller */
216 /* stop the playback and the pending clips, but at frame boundary */
217 static int shutup(void)
220 unsigned char* search
;
223 if (QUEUE_LEVEL
== 0) /* has ended anyway */
228 CHCR3
&= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
230 /* search next frame boundary and continue up to there */
231 pos
= search
= mp3_get_pos();
232 end
= queue
[queue_read
].buf
+ queue
[queue_read
].len
;
234 if (pos
>= queue
[queue_read
].buf
235 && pos
<= end
) /* really our clip? */
236 { /* (for strange reasons this isn't nesessarily the case) */
237 /* find the next frame boundary */
238 while (search
< end
) /* search the remaining data */
240 if (*search
++ != 0xFF) /* quick search for frame sync byte */
241 continue; /* (this does the majority of the job) */
243 /* look at the (bitswapped) rest of header candidate */
244 if (search
[0] == curr_hd
[0] /* do the quicker checks first */
245 && search
[2] == curr_hd
[2]
246 && (search
[1] & 0x30) == (curr_hd
[1] & 0x30)) /* sample rate */
248 search
--; /* back to the sync byte */
249 break; /* From looking at it, this is our header. */
254 { /* play old data until the frame end, to keep the MAS in sync */
257 queue_write
= queue_read
+ 1; /* will be empty after next callback */
258 if (queue_write
>= QUEUE_SIZE
)
260 queue
[queue_read
].len
= sent
; /* current one ends after this */
262 DTCR3
= sent
; /* let the DMA finish this frame */
263 CHCR3
|= 0x0001; /* re-enable DMA */
268 /* nothing to do, was frame boundary or not our clip */
270 queue_write
= queue_read
= 0; /* reset the queue */
276 /* schedule a clip, at the end or discard the existing queue */
277 static int queue_clip(unsigned char* buf
, int size
, bool enqueue
)
282 shutup(); /* cut off all the pending stuff */
285 return 0; /* safety check */
287 /* disable the DMA temporarily, to be safe of race condition */
290 queue_level
= QUEUE_LEVEL
; /* check old level */
292 if (queue_level
< QUEUE_SIZE
- 1) /* space left? */
294 queue
[queue_write
].buf
= buf
; /* populate an entry */
295 queue
[queue_write
].len
= size
;
297 queue_write
++; /* increase queue */
298 if (queue_write
>= QUEUE_SIZE
)
302 if (queue_level
== 0)
303 { /* queue was empty, we have to do the initial start */
304 sent
= MIN(size
, 0xFFFF); /* DMA can do no more */
305 mp3_play_data(buf
, sent
, mp3_callback
);
309 mp3_play_pause(true); /* kickoff audio */
313 CHCR3
|= 0x0001; /* re-enable DMA */
319 /* common code for talk_init() and talk_buffer_steal() */
320 static void reset_state(void)
322 queue_write
= queue_read
= 0; /* reset the queue */
323 p_voicefile
= NULL
; /* indicate no voicefile (trashed) */
324 p_thumbnail
= mp3buf
; /* whole space for thumbnail */
325 size_for_thumbnail
= mp3end
- mp3buf
;
328 /***************** Public implementation *****************/
332 reset_state(); /* use this for most of our inits */
335 load_voicefile(); /* load the tables right away */
336 has_voicefile
= (p_voicefile
!= NULL
);
338 filehandle
= open_voicefile();
339 has_voicefile
= (filehandle
>= 0); /* test if we can open it */
342 close(filehandle
); /* close again, this was just to detect presence */
349 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
350 int talk_buffer_steal(void)
354 if (filehandle
>= 0) /* only relevant for MMC */
365 /* play a voice ID from voicefile */
366 int talk_id(int id
, bool enqueue
)
369 unsigned char* clipbuf
;
372 if (mpeg_status()) /* busy, buffer in use */
375 if (p_voicefile
== NULL
&& has_voicefile
)
376 load_voicefile(); /* reload needed */
378 if (p_voicefile
== NULL
) /* still no voices? */
381 if (id
== -1) /* -1 is an indication for silence */
384 /* check if this is a special ID, with a value */
385 unit
= ((unsigned)id
) >> UNIT_SHIFT
;
387 { /* sign-extend the value */
388 id
= (unsigned)id
<< (32-UNIT_SHIFT
);
389 id
>>= (32-UNIT_SHIFT
);
390 talk_value(id
, unit
, enqueue
); /* speak it */
391 return 0; /* and stop, end of special case */
394 if (id
> VOICEONLY_DELIMITER
)
395 { /* voice-only entries use the second part of the table */
396 id
-= VOICEONLY_DELIMITER
+ 1;
397 if (id
>= p_voicefile
->id2_max
)
398 return -1; /* must be newer than we have */
399 id
+= p_voicefile
->id1_max
; /* table 2 is behind table 1 */
402 { /* normal use of the first table */
403 if (id
>= p_voicefile
->id1_max
)
404 return -1; /* must be newer than we have */
407 clipsize
= p_voicefile
->index
[id
].size
;
408 if (clipsize
== 0) /* clip not included in voicefile */
410 clipbuf
= mp3buf
+ p_voicefile
->index
[id
].offset
;
412 #ifdef HAVE_MMC /* dynamic loading, on demand */
413 if (!(clipsize
& LOADED_MASK
))
414 { /* clip used for the first time, needs loading */
415 lseek(filehandle
, p_voicefile
->index
[id
].offset
, SEEK_SET
);
416 if (read(filehandle
, clipbuf
, clipsize
) != clipsize
)
417 return -1; /* read error */
419 p_voicefile
->index
[id
].size
|= LOADED_MASK
; /* mark as loaded */
422 { /* clip is in memory already */
423 clipsize
&= ~LOADED_MASK
; /* without the extra bit gives true size */
427 queue_clip(clipbuf
, clipsize
, enqueue
);
433 /* play a thumbnail from file */
434 int talk_file(const char* filename
, bool enqueue
)
438 struct mp3entry info
;
440 if (mpeg_status()) /* busy, buffer in use */
443 if (p_thumbnail
== NULL
|| size_for_thumbnail
<= 0)
446 if(mp3info(&info
, filename
, false)) /* use this to find real start */
448 return 0; /* failed to open, or invalid */
451 fd
= open(filename
, O_RDONLY
);
452 if (fd
< 0) /* failed to open */
457 lseek(fd
, info
.first_frame_offset
, SEEK_SET
); /* behind ID data */
459 size
= read(fd
, p_thumbnail
, size_for_thumbnail
);
462 /* ToDo: find audio, skip ID headers and trailers */
466 bitswap(p_thumbnail
, size
);
467 queue_clip(p_thumbnail
, size
, enqueue
);
474 /* say a numeric value, this word ordering works for english,
475 but not necessarily for other languages (e.g. german) */
476 int talk_number(int n
, bool enqueue
)
478 int level
= 0; /* mille count */
479 int mil
= 1000000000; /* highest possible "-illion" */
481 if (mpeg_status()) /* busy, buffer in use */
485 shutup(); /* cut off all the pending stuff */
489 talk_id(VOICE_ZERO
, true);
495 talk_id(VOICE_MINUS
, true);
501 int segment
= n
/ mil
; /* extract in groups of 3 digits */
502 n
-= segment
* mil
; /* remove the used digits from number */
503 mil
/= 1000; /* digit place for next round */
507 int hundreds
= segment
/ 100;
508 int ones
= segment
% 100;
512 talk_id(VOICE_ZERO
+ hundreds
, true);
513 talk_id(VOICE_HUNDRED
, true);
516 /* combination indexing */
519 int tens
= ones
/10 + 18;
520 talk_id(VOICE_ZERO
+ tens
, true);
524 /* direct indexing */
526 talk_id(VOICE_ZERO
+ ones
, true);
528 /* add billion, million, thousand */
530 talk_id(VOICE_BILLION
+ level
, true);
538 /* singular/plural aware saying of a value */
539 int talk_value(int n
, int unit
, bool enqueue
)
542 static const int unit_voiced
[] =
543 { /* lookup table for the voice ID of the units */
544 -1, -1, -1, /* regular ID, int, signed */
545 VOICE_MILLISECONDS
, /* here come the "real" units */
560 if (mpeg_status()) /* busy, buffer in use */
563 if (unit
< 0 || unit
>= UNIT_LAST
)
566 unit_id
= unit_voiced
[unit
];
568 if ((n
==1 || n
==-1) /* singular? */
569 && unit_id
>= VOICE_SECONDS
&& unit_id
<= VOICE_HOURS
)
571 unit_id
--; /* use the singular for those units which have */
574 /* special case with a "plus" before */
575 if (n
> 0 && (unit
== UNIT_SIGNED
|| unit
== UNIT_DB
))
577 talk_id(VOICE_PLUS
, enqueue
);
581 talk_number(n
, enqueue
); /* say the number */
582 talk_id(unit_id
, true); /* say the unit, if any */
588 int talk_spell(const char* spell
, bool enqueue
)
590 char c
; /* currently processed char */
592 if (mpeg_status()) /* busy, buffer in use */
596 shutup(); /* cut off all the pending stuff */
598 while ((c
= *spell
++) != '\0')
600 /* if this grows into too many cases, I should use a table */
601 if (c
>= 'A' && c
<= 'Z')
602 talk_id(VOICE_CHAR_A
+ c
- 'A', true);
603 else if (c
>= 'a' && c
<= 'z')
604 talk_id(VOICE_CHAR_A
+ c
- 'a', true);
605 else if (c
>= '0' && c
<= '9')
606 talk_id(VOICE_ZERO
+ c
- '0', true);
608 talk_id(VOICE_MINUS
, true);
610 talk_id(VOICE_PLUS
, true);
612 talk_id(VOICE_POINT
, true);