Improved ff/rw max step calculation (patch #882931 by Craigh Sather)
[kugel-rb.git] / apps / talk.c
blob462ab85dd17a376730b7721e320fc7abcbcf7771
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 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.talk";
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 */
68 unsigned char* buf;
69 int len;
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)
99 char buf[64];
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)
117 int fd;
118 int size;
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 */
127 return 0;
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;
143 else
145 has_voicefile = false; /* don't try again */
147 close(fd);
149 return size;
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;
163 *size = sent;
164 return;
166 else /* go to next entry */
168 queue_read++;
169 if (queue_read >= QUEUE_SIZE)
170 queue_read = 0;
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;
177 *size = sent;
178 curr_hd[0] = *start[1];
179 curr_hd[1] = *start[2];
180 curr_hd[2] = *start[3];
182 else
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)
192 unsigned char* pos;
193 unsigned char* search;
194 unsigned char* end;
196 if (QUEUE_LEVEL == 0) /* has ended anyway */
198 return 0;
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. */
226 if (search-pos)
227 { /* play old data until the frame end, to keep the MAS in sync */
228 sent = search-pos;
230 queue_write = queue_read + 1; /* will be empty after next callback */
231 if (queue_write >= QUEUE_SIZE)
232 queue_write = 0;
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 */
237 return 0;
241 /* nothing to do, was frame boundary or not our clip */
242 mp3_play_stop();
243 queue_write = queue_read = 0; /* reset the queue */
245 return 0;
249 /* schedule a clip, at the end or discard the existing queue */
250 static int queue_clip(unsigned char* buf, int size, bool enqueue)
252 int queue_level;
254 if (!enqueue)
255 shutup(); /* cut off all the pending stuff */
257 if (!size)
258 return 0; /* safety check */
260 /* disable the DMA temporarily, to be safe of race condition */
261 CHCR3 &= ~0x0001;
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)
272 queue_write = 0;
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);
279 curr_hd[0] = buf[1];
280 curr_hd[1] = buf[2];
281 curr_hd[2] = buf[3];
282 mp3_play_pause(true); /* kickoff audio */
284 else
286 CHCR3 |= 0x0001; /* re-enable DMA */
289 return 0;
293 /***************** Public implementation *****************/
295 void talk_init(void)
297 int fd;
299 fd = open_voicefile();
300 if (fd >= 0) /* success */
302 close(fd);
303 has_voicefile = true;
305 else
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)
317 mp3_play_stop();
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;
322 return 0;
326 /* play a voice ID from voicefile */
327 int talk_id(int id, bool enqueue)
329 int clipsize;
330 unsigned char* clipbuf;
331 int unit;
333 if (mpeg_status()) /* busy, buffer in use */
334 return -1;
336 if (p_voicefile == NULL && has_voicefile)
337 load_voicefile(); /* reload needed */
339 if (p_voicefile == NULL) /* still no voices? */
340 return -1;
342 if (id == -1) /* -1 is an indication for silence */
343 return -1;
345 /* check if this is a special ID, with a value */
346 unit = ((unsigned)id) >> UNIT_SHIFT;
347 if (unit)
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 */
362 else
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 */
370 return -1;
372 clipbuf = mp3buf + p_voicefile->index[id].offset;
374 queue_clip(clipbuf, clipsize, enqueue);
376 return 0;
380 /* play a thumbnail from file */
381 int talk_file(char* filename, bool enqueue)
383 int fd;
384 int size;
385 struct mp3entry info;
387 if (mpeg_status()) /* busy, buffer in use */
388 return -1;
390 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
391 return -1;
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 */
401 return 0;
404 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
406 size = read(fd, p_thumbnail, size_for_thumbnail);
407 close(fd);
409 /* ToDo: find audio, skip ID headers and trailers */
411 if (size)
413 bitswap(p_thumbnail, size);
414 queue_clip(p_thumbnail, size, enqueue);
417 return size;
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 */
429 return -1;
431 if (!enqueue)
432 shutup(); /* cut off all the pending stuff */
434 if (n==0)
435 { /* special case */
436 talk_id(VOICE_ZERO, true);
437 return 0;
440 if (n<0)
442 talk_id(VOICE_MINUS, true);
443 n = -n;
446 while (n)
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 */
452 if (segment)
454 int hundreds = segment / 100;
455 int ones = segment % 100;
457 if (hundreds)
459 talk_id(VOICE_ZERO + hundreds, true);
460 talk_id(VOICE_HUNDRED, true);
463 /* combination indexing */
464 if (ones > 20)
466 int tens = ones/10 + 18;
467 talk_id(VOICE_ZERO + tens, true);
468 ones %= 10;
471 /* direct indexing */
472 if (ones)
473 talk_id(VOICE_ZERO + ones, true);
475 /* add billion, million, thousand */
476 if (mil)
477 talk_id(VOICE_BILLION + level, true);
479 level++;
482 return 0;
485 /* singular/plural aware saying of a value */
486 int talk_value(int n, int unit, bool enqueue)
488 int unit_id;
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 */
493 VOICE_SECONDS,
494 VOICE_MINUTES,
495 VOICE_HOURS,
496 VOICE_KHZ,
497 VOICE_DB,
498 VOICE_PERCENT,
499 VOICE_MEGABYTE,
500 VOICE_GIGABYTE,
501 VOICE_MILLIAMPHOURS,
502 VOICE_PIXEL,
503 VOICE_PER_SEC,
504 VOICE_HERTZ,
507 if (mpeg_status()) /* busy, buffer in use */
508 return -1;
510 if (unit < 0 || unit >= UNIT_LAST)
511 unit_id = -1;
512 else
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);
525 enqueue = true;
528 talk_number(n, enqueue); /* say the number */
529 talk_id(unit_id, true); /* say the unit, if any */
531 return 0;
534 /* spell a string */
535 int talk_spell(char* spell, bool enqueue)
537 char c; /* currently processed char */
539 if (mpeg_status()) /* busy, buffer in use */
540 return -1;
542 if (!enqueue)
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);
554 else if (c == '-')
555 talk_id(VOICE_MINUS, true);
556 else if (c == '+')
557 talk_id(VOICE_PLUS, true);
558 else if (c == '.')
559 talk_id(VOICE_POINT, true);
562 return 0;