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"
35 extern void bitswap(unsigned char *data
, int length
); /* no header for this */
37 /***************** Constants *****************/
39 #define QUEUE_SIZE 64 /* must be a power of two */
40 #define QUEUE_MASK (QUEUE_SIZE-1)
41 const char* dir_thumbnail_name
= ".dirname.tbx";
43 /***************** Functional Macros *****************/
45 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
48 /***************** Data types *****************/
50 struct clip_entry
/* one entry of the index table */
52 int offset
; /* offset from start of voicefile file */
53 int size
; /* size of the clip */
56 struct voicefile
/* file format of our voice file */
58 int version
; /* version of the voicefile */
59 int table
; /* offset to index table, (=header size) */
60 int id1_max
; /* number of "normal" clips contained in above index */
61 int id2_max
; /* number of "voice only" clips contained in above index */
62 struct clip_entry index
[]; /* followed by the index tables */
63 /* and finally the bitswapped mp3 clips, not visible here */
66 struct queue_entry
/* one entry of the internal queue */
73 /***************** Globals *****************/
75 static unsigned char* p_thumbnail
; /* buffer for thumbnail */
76 static long size_for_thumbnail
; /* leftover buffer size for it */
77 static struct voicefile
* p_voicefile
; /* loaded voicefile */
78 static bool has_voicefile
; /* a voicefile file is present */
79 static struct queue_entry queue
[QUEUE_SIZE
]; /* queue of scheduled clips */
80 static int queue_write
; /* write index of queue, by application */
81 static int queue_read
; /* read index of queue, by ISR context */
82 static int sent
; /* how many bytes handed over to playback, owned by ISR */
83 static unsigned char curr_hd
[3]; /* current frame header, for re-sync */
86 /***************** Private prototypes *****************/
88 static int load_voicefile(void);
89 static void mp3_callback(unsigned char** start
, int* size
);
90 static int shutup(void);
91 static int queue_clip(unsigned char* buf
, int size
, bool enqueue
);
92 static int open_voicefile(void);
95 /***************** Private implementation *****************/
97 static int open_voicefile(void)
100 char* p_lang
= "english"; /* default */
102 if ( global_settings
.lang_file
[0] &&
103 global_settings
.lang_file
[0] != 0xff )
104 { /* try to open the voice file of the selected language */
105 p_lang
= global_settings
.lang_file
;
108 snprintf(buf
, sizeof(buf
), ROCKBOX_DIR LANG_DIR
"/%s.voice", p_lang
);
110 return open(buf
, O_RDONLY
);
114 /* load the voice file into the mp3 buffer */
115 static int load_voicefile(void)
120 p_voicefile
= NULL
; /* indicate no voicefile if we fail below */
122 fd
= open_voicefile();
123 if (fd
< 0) /* failed to open */
125 p_voicefile
= NULL
; /* indicate no voicefile */
126 has_voicefile
= false; /* don't try again */
130 size
= read(fd
, mp3buf
, mp3end
- mp3buf
);
131 if (size
> 10000 /* too small is probably invalid */
132 && size
== filesize(fd
) /* has to fit completely */
133 && ((struct voicefile
*)mp3buf
)->table
/* format check */
134 == offsetof(struct voicefile
, index
))
136 p_voicefile
= (struct voicefile
*)mp3buf
;
138 /* thumbnail buffer is the remaining space behind */
139 p_thumbnail
= mp3buf
+ size
;
140 p_thumbnail
+= (int)p_thumbnail
% 2; /* 16-bit align */
141 size_for_thumbnail
= mp3end
- p_thumbnail
;
145 has_voicefile
= false; /* don't try again */
153 /* called in ISR context if mp3 data got consumed */
154 static void mp3_callback(unsigned char** start
, int* size
)
156 queue
[queue_read
].len
-= sent
; /* we completed this */
157 queue
[queue_read
].buf
+= sent
;
159 if (queue
[queue_read
].len
> 0) /* current clip not finished? */
160 { /* feed the next 64K-1 chunk */
161 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
162 *start
= queue
[queue_read
].buf
;
166 else /* go to next entry */
169 if (queue_read
>= QUEUE_SIZE
)
173 if (QUEUE_LEVEL
) /* queue is not empty? */
174 { /* start next clip */
175 sent
= MIN(queue
[queue_read
].len
, 0xFFFF);
176 *start
= queue
[queue_read
].buf
;
178 curr_hd
[0] = *start
[1];
179 curr_hd
[1] = *start
[2];
180 curr_hd
[2] = *start
[3];
184 *size
= 0; /* end of data */
185 mp3_play_stop(); /* fixme: should be done by caller */
189 /* stop the playback and the pending clips, but at frame boundary */
190 static int shutup(void)
193 unsigned char* search
;
196 if (QUEUE_LEVEL
== 0) /* has ended anyway */
201 CHCR3
&= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
203 /* search next frame boundary and continue up to there */
204 pos
= search
= mp3_get_pos();
205 end
= queue
[queue_read
].buf
+ queue
[queue_read
].len
;
207 if (pos
>= queue
[queue_read
].buf
208 && pos
<= end
) /* really our clip? */
209 { /* (for strange reasons this isn't nesessarily the case) */
210 /* find the next frame boundary */
211 while (search
< end
) /* search the remaining data */
213 if (*search
++ != 0xFF) /* quick search for frame sync byte */
214 continue; /* (this does the majority of the job) */
216 /* look at the (bitswapped) rest of header candidate */
217 if (search
[0] == curr_hd
[0] /* do the quicker checks first */
218 && search
[2] == curr_hd
[2]
219 && (search
[1] & 0x30) == (curr_hd
[1] & 0x30)) /* sample rate */
221 search
--; /* back to the sync byte */
222 break; /* From looking at it, this is our header. */
227 { /* play old data until the frame end, to keep the MAS in sync */
230 queue_write
= queue_read
+ 1; /* will be empty after next callback */
231 if (queue_write
>= QUEUE_SIZE
)
233 queue
[queue_read
].len
= sent
; /* current one ends after this */
235 DTCR3
= sent
; /* let the DMA finish this frame */
236 CHCR3
|= 0x0001; /* re-enable DMA */
241 /* nothing to do, was frame boundary or not our clip */
243 queue_write
= queue_read
= 0; /* reset the queue */
249 /* schedule a clip, at the end or discard the existing queue */
250 static int queue_clip(unsigned char* buf
, int size
, bool enqueue
)
255 shutup(); /* cut off all the pending stuff */
258 return 0; /* safety check */
260 /* disable the DMA temporarily, to be safe of race condition */
263 queue_level
= QUEUE_LEVEL
; /* check old level */
265 if (queue_level
< QUEUE_SIZE
- 1) /* space left? */
267 queue
[queue_write
].buf
= buf
; /* populate an entry */
268 queue
[queue_write
].len
= size
;
270 queue_write
++; /* increase queue */
271 if (queue_write
>= QUEUE_SIZE
)
275 if (queue_level
== 0)
276 { /* queue was empty, we have to do the initial start */
277 sent
= MIN(size
, 0xFFFF); /* DMA can do no more */
278 mp3_play_data(buf
, sent
, mp3_callback
);
282 mp3_play_pause(true); /* kickoff audio */
286 CHCR3
|= 0x0001; /* re-enable DMA */
293 /***************** Public implementation *****************/
299 fd
= open_voicefile();
300 if (fd
>= 0) /* success */
303 has_voicefile
= true;
307 has_voicefile
= false; /* no voice file available */
310 talk_buffer_steal(); /* abuse this for most of our inits */
314 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
315 int talk_buffer_steal(void)
318 queue_write
= queue_read
= 0; /* reset the queue */
319 p_voicefile
= NULL
; /* indicate no voicefile (trashed) */
320 p_thumbnail
= mp3buf
; /* whole space for thumbnail */
321 size_for_thumbnail
= mp3end
- mp3buf
;
326 /* play a voice ID from voicefile */
327 int talk_id(int id
, bool enqueue
)
330 unsigned char* clipbuf
;
333 if (mpeg_status()) /* busy, buffer in use */
336 if (p_voicefile
== NULL
&& has_voicefile
)
337 load_voicefile(); /* reload needed */
339 if (p_voicefile
== NULL
) /* still no voices? */
342 if (id
== -1) /* -1 is an indication for silence */
345 /* check if this is a special ID, with a value */
346 unit
= ((unsigned)id
) >> UNIT_SHIFT
;
348 { /* sign-extend the value */
349 id
= (unsigned)id
<< (32-UNIT_SHIFT
);
350 id
>>= (32-UNIT_SHIFT
);
351 talk_value(id
, unit
, enqueue
); /* speak it */
352 return 0; /* and stop, end of special case */
355 if (id
> VOICEONLY_DELIMITER
)
356 { /* voice-only entries use the second part of the table */
357 id
-= VOICEONLY_DELIMITER
+ 1;
358 if (id
>= p_voicefile
->id2_max
)
359 return -1; /* must be newer than we have */
360 id
+= p_voicefile
->id1_max
; /* table 2 is behind table 1 */
363 { /* normal use of the first table */
364 if (id
>= p_voicefile
->id1_max
)
365 return -1; /* must be newer than we have */
368 clipsize
= p_voicefile
->index
[id
].size
;
369 if (clipsize
== 0) /* clip not included in voicefile */
372 clipbuf
= mp3buf
+ p_voicefile
->index
[id
].offset
;
374 queue_clip(clipbuf
, clipsize
, enqueue
);
380 /* play a thumbnail from file */
381 int talk_file(char* filename
, bool enqueue
)
385 struct mp3entry info
;
387 if (mpeg_status()) /* busy, buffer in use */
390 if (p_thumbnail
== NULL
|| size_for_thumbnail
<= 0)
393 if(mp3info(&info
, filename
)) /* use this to find real start */
395 return 0; /* failed to open, or invalid */
398 fd
= open(filename
, O_RDONLY
);
399 if (fd
< 0) /* failed to open */
404 lseek(fd
, info
.first_frame_offset
, SEEK_SET
); /* behind ID data */
406 size
= read(fd
, p_thumbnail
, size_for_thumbnail
);
409 /* ToDo: find audio, skip ID headers and trailers */
413 bitswap(p_thumbnail
, size
);
414 queue_clip(p_thumbnail
, size
, enqueue
);
421 /* say a numeric value, this word ordering works for english,
422 but not necessarily for other languages (e.g. german) */
423 int talk_number(int n
, bool enqueue
)
425 int level
= 0; /* mille count */
426 int mil
= 1000000000; /* highest possible "-illion" */
428 if (mpeg_status()) /* busy, buffer in use */
432 shutup(); /* cut off all the pending stuff */
436 talk_id(VOICE_ZERO
, true);
442 talk_id(VOICE_MINUS
, true);
448 int segment
= n
/ mil
; /* extract in groups of 3 digits */
449 n
-= segment
* mil
; /* remove the used digits from number */
450 mil
/= 1000; /* digit place for next round */
454 int hundreds
= segment
/ 100;
455 int ones
= segment
% 100;
459 talk_id(VOICE_ZERO
+ hundreds
, true);
460 talk_id(VOICE_HUNDRED
, true);
463 /* combination indexing */
466 int tens
= ones
/10 + 18;
467 talk_id(VOICE_ZERO
+ tens
, true);
471 /* direct indexing */
473 talk_id(VOICE_ZERO
+ ones
, true);
475 /* add billion, million, thousand */
477 talk_id(VOICE_BILLION
+ level
, true);
485 /* singular/plural aware saying of a value */
486 int talk_value(int n
, int unit
, bool enqueue
)
489 const int unit_voiced
[] =
490 { /* lookup table for the voice ID of the units */
491 -1, -1, -1, /* regular ID, int, signed */
492 VOICE_MILLISECONDS
, /* here come the "real" units */
507 if (mpeg_status()) /* busy, buffer in use */
510 if (unit
< 0 || unit
>= UNIT_LAST
)
513 unit_id
= unit_voiced
[unit
];
515 if ((n
==1 || n
==-1) /* singular? */
516 && unit_id
>= VOICE_SECONDS
&& unit_id
<= VOICE_HOURS
)
518 unit_id
--; /* use the singular for those units which have */
521 /* special case with a "plus" before */
522 if (n
> 0 && (unit
== UNIT_SIGNED
|| unit
== UNIT_DB
))
524 talk_id(VOICE_PLUS
, enqueue
);
528 talk_number(n
, enqueue
); /* say the number */
529 talk_id(unit_id
, true); /* say the unit, if any */
535 int talk_spell(char* spell
, bool enqueue
)
537 char c
; /* currently processed char */
539 if (mpeg_status()) /* busy, buffer in use */
543 shutup(); /* cut off all the pending stuff */
545 while ((c
= *spell
++) != '\0')
547 /* if this grows into too many cases, I should use a table */
548 if (c
>= 'A' && c
<= 'Z')
549 talk_id(VOICE_CHAR_A
+ c
- 'A', true);
550 else if (c
>= 'a' && c
<= 'z')
551 talk_id(VOICE_CHAR_A
+ c
- 'a', true);
552 else if (c
>= '0' && c
<= '9')
553 talk_id(VOICE_ZERO
+ c
- '0', true);
555 talk_id(VOICE_MINUS
, true);
557 talk_id(VOICE_PLUS
, true);
559 talk_id(VOICE_POINT
, true);