Fake power off in clean_shutdown() on Ondio players, to make the user releases the...
[kugel-rb.git] / apps / talk.c
blob7cfcb46254b18612b203e1f8ced02025c4a8868e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
24 #include <stdio.h>
25 #include <stddef.h>
26 #include "file.h"
27 #include "buffer.h"
28 #include "system.h"
29 #include "settings.h"
30 #include "mp3_playback.h"
31 #include "mpeg.h"
32 #include "lang.h"
33 #include "talk.h"
34 #include "id3.h"
35 #include "bitswap.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 */
70 unsigned char* buf;
71 int len;
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)
102 char buf[64];
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)
120 int load_size;
121 int got_size;
122 int file_size;
124 filehandle = open_voicefile();
125 if (filehandle < 0) /* failed to open */
126 goto load_err;
128 file_size = filesize(filehandle);
129 if (file_size > mp3end - mp3buf) /* won't fit? */
130 goto load_err;
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;
136 #endif
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;
150 else
151 goto load_err;
153 #ifdef HAVE_MMC
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 */
160 goto load_err;
161 #else
162 close(filehandle); /* only the MMC variant leaves it open */
163 filehandle = -1;
164 #endif
166 return;
168 load_err:
169 p_voicefile = NULL;
170 has_voicefile = false; /* don't try again */
171 if (filehandle >= 0)
173 close(filehandle);
174 filehandle = -1;
176 return;
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;
190 *size = sent;
191 return;
193 else /* go to next entry */
195 queue_read++;
196 if (queue_read >= QUEUE_SIZE)
197 queue_read = 0;
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;
204 *size = sent;
205 curr_hd[0] = *start[1];
206 curr_hd[1] = *start[2];
207 curr_hd[2] = *start[3];
209 else
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)
219 unsigned char* pos;
220 unsigned char* search;
221 unsigned char* end;
223 if (QUEUE_LEVEL == 0) /* has ended anyway */
225 return 0;
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. */
253 if (search-pos)
254 { /* play old data until the frame end, to keep the MAS in sync */
255 sent = search-pos;
257 queue_write = queue_read + 1; /* will be empty after next callback */
258 if (queue_write >= QUEUE_SIZE)
259 queue_write = 0;
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 */
264 return 0;
268 /* nothing to do, was frame boundary or not our clip */
269 mp3_play_stop();
270 queue_write = queue_read = 0; /* reset the queue */
272 return 0;
276 /* schedule a clip, at the end or discard the existing queue */
277 static int queue_clip(unsigned char* buf, int size, bool enqueue)
279 int queue_level;
281 if (!enqueue)
282 shutup(); /* cut off all the pending stuff */
284 if (!size)
285 return 0; /* safety check */
287 /* disable the DMA temporarily, to be safe of race condition */
288 CHCR3 &= ~0x0001;
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)
299 queue_write = 0;
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);
306 curr_hd[0] = buf[1];
307 curr_hd[1] = buf[2];
308 curr_hd[2] = buf[3];
309 mp3_play_pause(true); /* kickoff audio */
311 else
313 CHCR3 |= 0x0001; /* re-enable DMA */
316 return 0;
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 *****************/
330 void talk_init(void)
332 reset_state(); /* use this for most of our inits */
334 #ifdef HAVE_MMC
335 load_voicefile(); /* load the tables right away */
336 has_voicefile = (p_voicefile != NULL);
337 #else
338 filehandle = open_voicefile();
339 has_voicefile = (filehandle >= 0); /* test if we can open it */
340 if (has_voicefile)
342 close(filehandle); /* close again, this was just to detect presence */
343 filehandle = -1;
345 #endif
349 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
350 int talk_buffer_steal(void)
352 mp3_play_stop();
353 #ifdef HAVE_MMC
354 if (filehandle >= 0) /* only relevant for MMC */
356 close(filehandle);
357 filehandle = -1;
359 #endif
360 reset_state();
361 return 0;
365 /* play a voice ID from voicefile */
366 int talk_id(int id, bool enqueue)
368 int clipsize;
369 unsigned char* clipbuf;
370 int unit;
372 if (mpeg_status()) /* busy, buffer in use */
373 return -1;
375 if (p_voicefile == NULL && has_voicefile)
376 load_voicefile(); /* reload needed */
378 if (p_voicefile == NULL) /* still no voices? */
379 return -1;
381 if (id == -1) /* -1 is an indication for silence */
382 return -1;
384 /* check if this is a special ID, with a value */
385 unit = ((unsigned)id) >> UNIT_SHIFT;
386 if (unit)
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 */
401 else
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 */
409 return -1;
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 */
421 else
422 { /* clip is in memory already */
423 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
425 #endif
427 queue_clip(clipbuf, clipsize, enqueue);
429 return 0;
433 /* play a thumbnail from file */
434 int talk_file(const char* filename, bool enqueue)
436 int fd;
437 int size;
438 struct mp3entry info;
440 if (mpeg_status()) /* busy, buffer in use */
441 return -1;
443 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
444 return -1;
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 */
454 return 0;
457 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
459 size = read(fd, p_thumbnail, size_for_thumbnail);
460 close(fd);
462 /* ToDo: find audio, skip ID headers and trailers */
464 if (size)
466 bitswap(p_thumbnail, size);
467 queue_clip(p_thumbnail, size, enqueue);
470 return size;
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 */
482 return -1;
484 if (!enqueue)
485 shutup(); /* cut off all the pending stuff */
487 if (n==0)
488 { /* special case */
489 talk_id(VOICE_ZERO, true);
490 return 0;
493 if (n<0)
495 talk_id(VOICE_MINUS, true);
496 n = -n;
499 while (n)
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 */
505 if (segment)
507 int hundreds = segment / 100;
508 int ones = segment % 100;
510 if (hundreds)
512 talk_id(VOICE_ZERO + hundreds, true);
513 talk_id(VOICE_HUNDRED, true);
516 /* combination indexing */
517 if (ones > 20)
519 int tens = ones/10 + 18;
520 talk_id(VOICE_ZERO + tens, true);
521 ones %= 10;
524 /* direct indexing */
525 if (ones)
526 talk_id(VOICE_ZERO + ones, true);
528 /* add billion, million, thousand */
529 if (mil)
530 talk_id(VOICE_BILLION + level, true);
532 level++;
535 return 0;
538 /* singular/plural aware saying of a value */
539 int talk_value(int n, int unit, bool enqueue)
541 int unit_id;
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 */
546 VOICE_SECONDS,
547 VOICE_MINUTES,
548 VOICE_HOURS,
549 VOICE_KHZ,
550 VOICE_DB,
551 VOICE_PERCENT,
552 VOICE_MEGABYTE,
553 VOICE_GIGABYTE,
554 VOICE_MILLIAMPHOURS,
555 VOICE_PIXEL,
556 VOICE_PER_SEC,
557 VOICE_HERTZ,
560 if (mpeg_status()) /* busy, buffer in use */
561 return -1;
563 if (unit < 0 || unit >= UNIT_LAST)
564 unit_id = -1;
565 else
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);
578 enqueue = true;
581 talk_number(n, enqueue); /* say the number */
582 talk_id(unit_id, true); /* say the unit, if any */
584 return 0;
587 /* spell a string */
588 int talk_spell(const char* spell, bool enqueue)
590 char c; /* currently processed char */
592 if (mpeg_status()) /* busy, buffer in use */
593 return -1;
595 if (!enqueue)
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);
607 else if (c == '-')
608 talk_id(VOICE_MINUS, true);
609 else if (c == '+')
610 talk_id(VOICE_PLUS, true);
611 else if (c == '.')
612 talk_id(VOICE_POINT, true);
615 return 0;