Add AAC audio type
[Rockbox.git] / apps / talk.c
blobec92578ee635f2338cd24f14440d1eb06da5407d
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 "audio.h"
32 #include "lang.h"
33 #include "talk.h"
34 #include "id3.h"
35 #include "logf.h"
36 #include "bitswap.h"
37 #if CONFIG_CODEC == SWCODEC
38 #include "playback.h"
39 #endif
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 */
75 unsigned char* buf;
76 long len;
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)
112 char buf[64];
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;
131 #ifdef SIMULATOR
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;
143 #endif
145 /* load the voice file into the mp3 buffer */
146 static void load_voicefile(void)
148 int load_size;
149 int got_size;
150 int file_size;
151 #if CONFIG_CODEC == SWCODEC
152 int length, i;
153 unsigned char *buf, temp;
154 #endif
156 filehandle = open_voicefile();
157 if (filehandle < 0) /* failed to open */
158 goto load_err;
160 file_size = filesize(filehandle);
161 if (file_size > audiobufend - audiobuf) /* won't fit? */
162 goto load_err;
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;
168 #endif
170 got_size = read(filehandle, audiobuf, load_size);
171 if (got_size != load_size /* failure */)
172 goto load_err;
174 #ifdef SIMULATOR
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);
181 p_voicefile = NULL;
182 #endif
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;
194 else
195 goto load_err;
197 #ifdef SIMULATOR
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);
205 #endif
207 /* Do a bitswap as necessary. */
208 #if CONFIG_CODEC == SWCODEC
209 logf("Bitswapping voice file.");
210 cpu_boost(true);
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++)
217 temp = buf[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);
222 cpu_boost(false);
224 #endif
226 #ifdef HAVE_MMC
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 */
233 goto load_err;
234 #else
235 close(filehandle); /* only the MMC variant leaves it open */
236 filehandle = -1;
237 #endif
239 /* make sure to have the silence clip, if available */
240 p_silence = get_clip(VOICE_PAUSE, &silence_len);
242 return;
244 load_err:
245 p_voicefile = NULL;
246 has_voicefile = false; /* don't try again */
247 if (filehandle >= 0)
249 close(filehandle);
250 filehandle = -1;
252 return;
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);
266 #else
267 sent = queue[queue_read].len;
268 #endif
269 *start = queue[queue_read].buf;
270 *size = sent;
271 return;
273 else if (sent > 0) /* go to next entry */
275 queue_read = (queue_read + 1) & QUEUE_MASK;
278 re_check:
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);
284 #else
285 sent = queue[queue_read].len;
286 #endif
287 *start = p_lastclip = queue[queue_read].buf;
288 *size = sent;
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;
301 goto re_check;
303 else
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)
313 unsigned char* pos;
314 unsigned char* search;
315 unsigned char* end;
317 if (QUEUE_LEVEL == 0) /* has ended anyway */
319 return 0;
321 #if CONFIG_CPU == SH7034
322 CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
323 #endif
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. */
347 if (search-pos)
348 { /* play old data until the frame end, to keep the MAS in sync */
349 sent = search-pos;
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 */
357 #endif
358 return 0;
362 /* nothing to do, was frame boundary or not our clip */
363 mp3_play_stop();
364 queue_write = queue_read = 0; /* reset the queue */
366 return 0;
370 /* schedule a clip, at the end or discard the existing queue */
371 static int queue_clip(unsigned char* buf, long size, bool enqueue)
373 int queue_level;
375 if (!enqueue)
376 shutup(); /* cut off all the pending stuff */
378 if (!size)
379 return 0; /* safety check */
380 #if CONFIG_CPU == SH7034
381 /* disable the DMA temporarily, to be safe of race condition */
382 CHCR3 &= ~0x0001;
383 #endif
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 */
395 p_lastclip = buf;
396 #if CONFIG_CODEC != SWCODEC
397 sent = MIN(size, 0xFFFF); /* DMA can do no more */
398 #else
399 sent = size;
400 #endif
401 mp3_play_data(buf, sent, mp3_callback);
402 curr_hd[0] = buf[1];
403 curr_hd[1] = buf[2];
404 curr_hd[2] = buf[3];
405 mp3_play_pause(true); /* kickoff audio */
407 else
409 #if CONFIG_CPU == SH7034
410 CHCR3 |= 0x0001; /* re-enable DMA */
411 #endif
414 return 0;
417 /* fetch a clip from the voice file */
418 static unsigned char* get_clip(long id, long* p_size)
420 long clipsize;
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 */
430 else
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 */
438 return NULL;
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 */
450 else
451 { /* clip is in memory already */
452 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
454 #endif
456 *p_size = clipsize;
457 return clipbuf;
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 *****************/
473 void talk_init(void)
475 #if CONFIG_CODEC == SWCODEC
476 audio_stop();
477 #endif
478 reset_state(); /* use this for most of our inits */
480 #ifdef HAVE_MMC
481 load_voicefile(); /* load the tables right away */
482 has_voicefile = (p_voicefile != NULL);
483 #else
484 filehandle = open_voicefile();
485 has_voicefile = (filehandle >= 0); /* test if we can open it */
486 voicefile_size = 0;
488 if (has_voicefile)
490 voicefile_size = filesize(filehandle);
491 #if CONFIG_CODEC == SWCODEC
492 voice_init();
493 #endif
494 close(filehandle); /* close again, this was just to detect presence */
495 filehandle = -1;
497 #endif
502 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
503 int talk_buffer_steal(void)
505 mp3_play_stop();
506 #ifdef HAVE_MMC
507 if (filehandle >= 0) /* only relevant for MMC */
509 close(filehandle);
510 filehandle = -1;
512 #endif
513 reset_state();
514 return 0;
518 /* play a voice ID from voicefile */
519 int talk_id(long id, bool enqueue)
521 long clipsize;
522 unsigned char* clipbuf;
523 int unit;
525 #if CONFIG_CODEC != SWCODEC
526 if (audio_status()) /* busy, buffer in use */
527 return -1;
528 #endif
530 if (p_voicefile == NULL && has_voicefile)
531 load_voicefile(); /* reload needed */
533 if (p_voicefile == NULL) /* still no voices? */
534 return -1;
536 if (id == -1) /* -1 is an indication for silence */
537 return -1;
539 /* check if this is a special ID, with a value */
540 unit = ((unsigned long)id) >> UNIT_SHIFT;
541 if (unit)
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);
550 if (clipbuf == NULL)
551 return -1; /* not present */
553 queue_clip(clipbuf, clipsize, enqueue);
555 return 0;
559 /* play a thumbnail from file */
560 int talk_file(const char* filename, bool enqueue)
562 int fd;
563 int size;
564 struct mp3entry info;
566 if (audio_status()) /* busy, buffer in use */
567 return -1;
569 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
570 return -1;
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 */
580 return 0;
583 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
585 size = read(fd, p_thumbnail, size_for_thumbnail);
586 close(fd);
588 /* ToDo: find audio, skip ID headers and trailers */
590 if (size)
592 #if CONFIG_CODEC != SWCODEC
593 bitswap(p_thumbnail, size);
594 #endif
595 queue_clip(p_thumbnail, size, enqueue);
598 return size;
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 */
611 return -1;
612 #endif
614 if (!enqueue)
615 shutup(); /* cut off all the pending stuff */
617 if (n==0)
618 { /* special case */
619 talk_id(VOICE_ZERO, true);
620 return 0;
623 if (n<0)
625 talk_id(VOICE_MINUS, true);
626 n = -n;
629 while (n)
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 */
635 if (segment)
637 int hundreds = segment / 100;
638 int ones = segment % 100;
640 if (hundreds)
642 talk_id(VOICE_ZERO + hundreds, true);
643 talk_id(VOICE_HUNDRED, true);
646 /* combination indexing */
647 if (ones > 20)
649 int tens = ones/10 + 18;
650 talk_id(VOICE_ZERO + tens, true);
651 ones %= 10;
654 /* direct indexing */
655 if (ones)
656 talk_id(VOICE_ZERO + ones, true);
658 /* add billion, million, thousand */
659 if (mil)
660 talk_id(VOICE_BILLION + level, true);
662 level++;
665 return 0;
668 /* singular/plural aware saying of a value */
669 int talk_value(long n, int unit, bool enqueue)
671 int unit_id;
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 */
676 VOICE_SECONDS,
677 VOICE_MINUTES,
678 VOICE_HOURS,
679 VOICE_KHZ,
680 VOICE_DB,
681 VOICE_PERCENT,
682 VOICE_MEGABYTE,
683 VOICE_GIGABYTE,
684 VOICE_MILLIAMPHOURS,
685 VOICE_PIXEL,
686 VOICE_PER_SEC,
687 VOICE_HERTZ,
690 #if CONFIG_CODEC != SWCODEC
691 if (audio_status()) /* busy, buffer in use */
692 return -1;
693 #endif
695 if (unit < 0 || unit >= UNIT_LAST)
696 unit_id = -1;
697 else
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);
710 enqueue = true;
713 talk_number(n, enqueue); /* say the number */
714 talk_id(unit_id, true); /* say the unit, if any */
716 return 0;
719 /* spell a string */
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 */
726 return -1;
727 #endif
729 if (!enqueue)
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);
741 else if (c == '-')
742 talk_id(VOICE_MINUS, true);
743 else if (c == '+')
744 talk_id(VOICE_PLUS, true);
745 else if (c == '.')
746 talk_id(VOICE_DOT, true);
747 else if (c == ' ')
748 talk_id(VOICE_PAUSE, true);
751 return 0;