Simplify touchscreen scrollbar handling code
[Rockbox.git] / apps / talk.c
blob61dafb0c6cc61e9f87ff3b9713698ab40816b1a0
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 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
26 #include <stdio.h>
27 #include <stddef.h>
28 #include <string.h>
29 #include "file.h"
30 #include "buffer.h"
31 #include "system.h"
32 #include "kernel.h"
33 #include "settings.h"
34 #include "settings_list.h"
35 #include "mp3_playback.h"
36 #include "audio.h"
37 #include "lang.h"
38 #include "talk.h"
39 #include "id3.h"
40 #include "logf.h"
41 #include "bitswap.h"
42 #include "structec.h"
43 #if CONFIG_CODEC == SWCODEC
44 #include "playback.h"
45 #endif
46 #include "debug.h"
49 /* Memory layout varies between targets because the
50 Archos (MASCODEC) devices cannot mix voice and audio playback
52 MASCODEC | MASCODEC | SWCODEC
53 (playing) | (stopped) |
54 audiobuf-----------+-----------+------------
55 audio | voice | thumbnail
56 |-----------|------------
57 | thumbnail | voice
58 | |------------
59 | | filebuf
60 | |------------
61 | | audio
62 audiobufend----------+-----------+------------
64 SWCODEC allocates dedicated buffers, MASCODEC reuses audiobuf. */
67 /***************** Constants *****************/
69 #define QUEUE_SIZE 64 /* must be a power of two */
70 #define QUEUE_MASK (QUEUE_SIZE-1)
71 const char* const dir_thumbnail_name = "_dirname.talk";
72 const char* const file_thumbnail_ext = ".talk";
74 /***************** Functional Macros *****************/
76 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
78 #define LOADED_MASK 0x80000000 /* MSB */
80 #if CONFIG_CODEC == SWCODEC
81 #define MAX_THUMBNAIL_BUFSIZE 0x10000
82 #endif
84 /***************** Data types *****************/
86 struct clip_entry /* one entry of the index table */
88 int offset; /* offset from start of voicefile file */
89 int size; /* size of the clip */
92 struct voicefile /* file format of our voice file */
94 int version; /* version of the voicefile */
95 int target_id; /* the rockbox target the file was made for */
96 int table; /* offset to index table, (=header size) */
97 int id1_max; /* number of "normal" clips contained in above index */
98 int id2_max; /* number of "voice only" clips contained in above index */
99 struct clip_entry index[]; /* followed by the index tables */
100 /* and finally the mp3 clips, not visible here, bitswapped
101 for SH based players */
104 struct queue_entry /* one entry of the internal queue */
106 unsigned char* buf;
107 long len;
111 /***************** Globals *****************/
113 static unsigned char* p_thumbnail = NULL; /* buffer for thumbnail */
114 static long size_for_thumbnail; /* leftover buffer size for it */
115 static struct voicefile* p_voicefile; /* loaded voicefile */
116 static bool has_voicefile; /* a voicefile file is present */
117 static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
118 static bool force_enqueue_next; /* enqueue next utterance even if enqueue is false */
119 static int queue_write; /* write index of queue, by application */
120 static int queue_read; /* read index of queue, by ISR context */
121 static int sent; /* how many bytes handed over to playback, owned by ISR */
122 static unsigned char curr_hd[3]; /* current frame header, for re-sync */
123 static int filehandle = -1; /* global, so the MMC variant can keep the file open */
124 static unsigned char* p_silence; /* VOICE_PAUSE clip, used for termination */
125 static long silence_len; /* length of the VOICE_PAUSE clip */
126 static unsigned char* p_lastclip; /* address of latest clip, for silence add */
127 static unsigned long voicefile_size = 0; /* size of the loaded voice file */
128 static unsigned char last_lang[MAX_FILENAME+1]; /* name of last used lang file (in talk_init) */
129 static bool talk_initialized; /* true if talk_init has been called */
130 static int talk_temp_disable_count; /* if positive, temporarily disable voice UI (not saved) */
133 /***************** Private implementation *****************/
135 static int open_voicefile(void)
137 char buf[64];
138 char* p_lang = "english"; /* default */
140 if ( global_settings.lang_file[0] &&
141 global_settings.lang_file[0] != 0xff )
142 { /* try to open the voice file of the selected language */
143 p_lang = (char *)global_settings.lang_file;
146 snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang);
148 return open(buf, O_RDONLY);
152 /* fetch a clip from the voice file */
153 static unsigned char* get_clip(long id, long* p_size)
155 long clipsize;
156 unsigned char* clipbuf;
158 if (id > VOICEONLY_DELIMITER)
159 { /* voice-only entries use the second part of the table */
160 id -= VOICEONLY_DELIMITER + 1;
161 if (id >= p_voicefile->id2_max)
162 return NULL; /* must be newer than we have */
163 id += p_voicefile->id1_max; /* table 2 is behind table 1 */
165 else
166 { /* normal use of the first table */
167 if (id >= p_voicefile->id1_max)
168 return NULL; /* must be newer than we have */
171 clipsize = p_voicefile->index[id].size;
172 if (clipsize == 0) /* clip not included in voicefile */
173 return NULL;
174 clipbuf = (unsigned char *) p_voicefile + p_voicefile->index[id].offset;
176 #ifdef HAVE_MMC /* dynamic loading, on demand */
177 if (!(clipsize & LOADED_MASK))
178 { /* clip used for the first time, needs loading */
179 lseek(filehandle, p_voicefile->index[id].offset, SEEK_SET);
180 if (read(filehandle, clipbuf, clipsize) != clipsize)
181 return NULL; /* read error */
183 p_voicefile->index[id].size |= LOADED_MASK; /* mark as loaded */
185 else
186 { /* clip is in memory already */
187 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
189 #endif
191 *p_size = clipsize;
192 return clipbuf;
196 /* load the voice file into the mp3 buffer */
197 static void load_voicefile(void)
199 int load_size;
200 int got_size;
201 int file_size;
202 #ifdef ROCKBOX_LITTLE_ENDIAN
203 int i;
204 #endif
206 filehandle = open_voicefile();
207 if (filehandle < 0) /* failed to open */
208 goto load_err;
210 file_size = filesize(filehandle);
211 if (file_size > audiobufend - audiobuf) /* won't fit? */
212 goto load_err;
214 #ifdef HAVE_MMC /* load only the header for now */
215 load_size = offsetof(struct voicefile, index);
216 #else /* load the full file */
217 load_size = file_size;
218 #endif
220 got_size = read(filehandle, audiobuf, load_size);
221 if (got_size != load_size /* failure */)
222 goto load_err;
224 #ifdef ROCKBOX_LITTLE_ENDIAN
225 logf("Byte swapping voice file");
226 structec_convert(audiobuf, "lllll", 1, true);
227 #endif
229 if (((struct voicefile*)audiobuf)->table /* format check */
230 == offsetof(struct voicefile, index))
232 p_voicefile = (struct voicefile*)audiobuf;
234 if (p_voicefile->version != VOICE_VERSION ||
235 p_voicefile->target_id != TARGET_ID)
237 logf("Incompatible voice file");
238 goto load_err;
240 #if CONFIG_CODEC != SWCODEC
241 /* MASCODEC: now use audiobuf for voice then thumbnail */
242 p_thumbnail = audiobuf + file_size;
243 p_thumbnail += (long)p_thumbnail % 2; /* 16-bit align */
244 size_for_thumbnail = audiobufend - p_thumbnail;
245 #endif
247 else
248 goto load_err;
250 #ifdef ROCKBOX_LITTLE_ENDIAN
251 for (i = 0; i < p_voicefile->id1_max + p_voicefile->id2_max; i++)
252 structec_convert(&p_voicefile->index[i], "ll", 1, true);
253 #endif
255 #ifdef HAVE_MMC
256 /* load the index table, now that we know its size from the header */
257 load_size = (p_voicefile->id1_max + p_voicefile->id2_max)
258 * sizeof(struct clip_entry);
259 got_size = read(filehandle,
260 (unsigned char *) p_voicefile + offsetof(struct voicefile, index), load_size);
261 if (got_size != load_size) /* read error */
262 goto load_err;
263 #else
264 close(filehandle); /* only the MMC variant leaves it open */
265 filehandle = -1;
266 #endif
268 /* make sure to have the silence clip, if available */
269 p_silence = get_clip(VOICE_PAUSE, &silence_len);
271 return;
273 load_err:
274 p_voicefile = NULL;
275 has_voicefile = false; /* don't try again */
276 if (filehandle >= 0)
278 close(filehandle);
279 filehandle = -1;
281 return;
285 /* called in ISR context if mp3 data got consumed */
286 static void mp3_callback(unsigned char** start, size_t* size)
288 queue[queue_read].len -= sent; /* we completed this */
289 queue[queue_read].buf += sent;
291 if (queue[queue_read].len > 0) /* current clip not finished? */
292 { /* feed the next 64K-1 chunk */
293 #if CONFIG_CODEC != SWCODEC
294 sent = MIN(queue[queue_read].len, 0xFFFF);
295 #else
296 sent = queue[queue_read].len;
297 #endif
298 *start = queue[queue_read].buf;
299 *size = sent;
300 return;
302 else if (sent > 0) /* go to next entry */
304 queue_read = (queue_read + 1) & QUEUE_MASK;
307 re_check:
309 if (QUEUE_LEVEL != 0) /* queue is not empty? */
310 { /* start next clip */
311 #if CONFIG_CODEC != SWCODEC
312 sent = MIN(queue[queue_read].len, 0xFFFF);
313 #else
314 sent = queue[queue_read].len;
315 #endif
316 *start = p_lastclip = queue[queue_read].buf;
317 *size = sent;
318 curr_hd[0] = p_lastclip[1];
319 curr_hd[1] = p_lastclip[2];
320 curr_hd[2] = p_lastclip[3];
322 else if (p_silence != NULL /* silence clip available */
323 && p_lastclip != p_silence /* previous clip wasn't silence */
324 && p_lastclip != p_thumbnail) /* ..or thumbnail */
325 { /* add silence clip when queue runs empty playing a voice clip */
326 queue[queue_write].buf = p_silence;
327 queue[queue_write].len = silence_len;
328 queue_write = (queue_write + 1) & QUEUE_MASK;
330 goto re_check;
332 else
334 *size = 0; /* end of data */
338 /***************** Public routines *****************/
340 /* stop the playback and the pending clips */
341 void talk_force_shutup(void)
343 /* Most of this is MAS only */
344 #if CONFIG_CODEC != SWCODEC
345 #ifdef SIMULATOR
346 return;
347 #endif
348 unsigned char* pos;
349 unsigned char* search;
350 unsigned char* end;
351 if (QUEUE_LEVEL == 0) /* has ended anyway */
352 return;
354 #if CONFIG_CPU == SH7034
355 CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
356 #endif /* CONFIG_CPU == SH7034 */
357 /* search next frame boundary and continue up to there */
358 pos = search = mp3_get_pos();
359 end = queue[queue_read].buf + queue[queue_read].len;
361 if (pos >= queue[queue_read].buf
362 && pos <= end) /* really our clip? */
363 { /* (for strange reasons this isn't nesessarily the case) */
364 /* find the next frame boundary */
365 while (search < end) /* search the remaining data */
367 if (*search++ != 0xFF) /* quick search for frame sync byte */
368 continue; /* (this does the majority of the job) */
370 /* look at the (bitswapped) rest of header candidate */
371 if (search[0] == curr_hd[0] /* do the quicker checks first */
372 && search[2] == curr_hd[2]
373 && (search[1] & 0x30) == (curr_hd[1] & 0x30)) /* sample rate */
375 search--; /* back to the sync byte */
376 break; /* From looking at it, this is our header. */
380 if (search-pos)
381 { /* play old data until the frame end, to keep the MAS in sync */
382 sent = search-pos;
384 queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */
385 queue[queue_read].len = sent; /* current one ends after this */
387 #if CONFIG_CPU == SH7034
388 DTCR3 = sent; /* let the DMA finish this frame */
389 CHCR3 |= 0x0001; /* re-enable DMA */
390 #endif /* CONFIG_CPU == SH7034 */
391 return;
394 #endif /* CONFIG_CODEC != SWCODEC */
396 /* Either SWCODEC, or MAS had nothing to do (was frame boundary or not our clip) */
397 mp3_play_stop();
398 queue_write = queue_read = 0; /* reset the queue */
399 return;
402 /* Shutup the voice, except if force_enqueue_next is set. */
403 void talk_shutup(void)
405 if (!force_enqueue_next)
406 talk_force_shutup();
409 /* schedule a clip, at the end or discard the existing queue */
410 static void queue_clip(unsigned char* buf, long size, bool enqueue)
412 int queue_level;
414 if (!enqueue)
415 talk_shutup(); /* cut off all the pending stuff */
416 /* Something is being enqueued, force_enqueue_next override is no
417 longer in effect. */
418 force_enqueue_next = false;
420 if (!size)
421 return; /* safety check */
422 #if CONFIG_CPU == SH7034
423 /* disable the DMA temporarily, to be safe of race condition */
424 CHCR3 &= ~0x0001;
425 #endif
426 queue_level = QUEUE_LEVEL; /* check old level */
428 if (queue_level < QUEUE_SIZE - 1) /* space left? */
430 queue[queue_write].buf = buf; /* populate an entry */
431 queue[queue_write].len = size;
432 queue_write = (queue_write + 1) & QUEUE_MASK;
435 if (queue_level == 0)
436 { /* queue was empty, we have to do the initial start */
437 p_lastclip = buf;
438 #if CONFIG_CODEC != SWCODEC
439 sent = MIN(size, 0xFFFF); /* DMA can do no more */
440 #else
441 sent = size;
442 #endif
443 mp3_play_data(buf, sent, mp3_callback);
444 curr_hd[0] = buf[1];
445 curr_hd[1] = buf[2];
446 curr_hd[2] = buf[3];
447 mp3_play_pause(true); /* kickoff audio */
449 else
451 #if CONFIG_CPU == SH7034
452 CHCR3 |= 0x0001; /* re-enable DMA */
453 #endif
456 return;
460 /* common code for talk_init() and talk_buffer_steal() */
461 static void reset_state(void)
463 queue_write = queue_read = 0; /* reset the queue */
464 p_voicefile = NULL; /* indicate no voicefile (trashed) */
465 #if CONFIG_CODEC == SWCODEC
466 /* Allocate a dedicated thumbnail buffer - once */
467 if (p_thumbnail == NULL)
469 size_for_thumbnail = audiobufend - audiobuf;
470 if (size_for_thumbnail > MAX_THUMBNAIL_BUFSIZE)
471 size_for_thumbnail = MAX_THUMBNAIL_BUFSIZE;
472 p_thumbnail = buffer_alloc(size_for_thumbnail);
474 #else
475 /* Just use the audiobuf, without allocating anything */
476 p_thumbnail = audiobuf;
477 size_for_thumbnail = audiobufend - audiobuf;
478 #endif
479 p_silence = NULL; /* pause clip not accessible */
483 /***************** Public implementation *****************/
485 void talk_init(void)
487 talk_temp_disable_count = 0;
488 if (talk_initialized && !strcasecmp(last_lang, global_settings.lang_file))
490 /* not a new file, nothing to do */
491 return;
494 #ifdef HAVE_MMC
495 if (filehandle >= 0) /* MMC: An old voice file might still be open */
497 close(filehandle);
498 filehandle = -1;
500 #endif
502 talk_initialized = true;
503 strncpy((char *) last_lang, (char *)global_settings.lang_file,
504 MAX_FILENAME);
506 #if CONFIG_CODEC == SWCODEC
507 audio_get_buffer(false, NULL); /* Must tell audio to reinitialize */
508 #endif
509 reset_state(); /* use this for most of our inits */
511 filehandle = open_voicefile();
512 has_voicefile = (filehandle >= 0); /* test if we can open it */
513 voicefile_size = 0;
515 if (has_voicefile)
517 voicefile_size = filesize(filehandle);
518 close(filehandle); /* close again, this was just to detect presence */
519 filehandle = -1;
524 #if CONFIG_CODEC == SWCODEC
525 /* return if a voice codec is required or not */
526 bool talk_voice_required(void)
528 return (voicefile_size != 0) /* Voice file is available */
529 || (global_settings.talk_dir_clip) /* Thumbnail clips are required */
530 || (global_settings.talk_file_clip);
532 #endif
534 /* return size of voice file */
535 int talk_get_bufsize(void)
537 return voicefile_size;
540 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
541 void talk_buffer_steal(void)
543 #if CONFIG_CODEC != SWCODEC
544 mp3_play_stop();
545 #endif
546 #ifdef HAVE_MMC
547 if (filehandle >= 0) /* only relevant for MMC */
549 close(filehandle);
550 filehandle = -1;
552 #endif
553 reset_state();
557 /* play a voice ID from voicefile */
558 int talk_id(int32_t id, bool enqueue)
560 long clipsize;
561 unsigned char* clipbuf;
562 int32_t unit;
563 int decimals;
565 if (talk_temp_disable_count > 0)
566 return -1; /* talking has been disabled */
567 #if CONFIG_CODEC != SWCODEC
568 if (audio_status()) /* busy, buffer in use */
569 return -1;
570 #endif
572 if (p_voicefile == NULL && has_voicefile)
573 load_voicefile(); /* reload needed */
575 if (p_voicefile == NULL) /* still no voices? */
576 return -1;
578 if (id == -1) /* -1 is an indication for silence */
579 return -1;
581 decimals = (((uint32_t)id) >> DECIMAL_SHIFT) & 0x7;
583 /* check if this is a special ID, with a value */
584 unit = ((uint32_t)id) >> UNIT_SHIFT;
585 if (unit || decimals)
586 { /* sign-extend the value */
587 id = (uint32_t)id << (32-DECIMAL_SHIFT);
588 id >>= (32-DECIMAL_SHIFT);
590 talk_value_decimal(id, unit, decimals, enqueue); /* speak it */
591 return 0; /* and stop, end of special case */
594 clipbuf = get_clip(id, &clipsize);
595 if (clipbuf == NULL)
596 return -1; /* not present */
598 queue_clip(clipbuf, clipsize, enqueue);
600 return 0;
602 /* Speaks zero or more IDs (from an array). */
603 int talk_idarray(long *ids, bool enqueue)
605 int r;
606 if(!ids)
607 return 0;
608 while(*ids != TALK_FINAL_ID)
610 if((r = talk_id(*ids++, enqueue)) <0)
611 return r;
612 enqueue = true;
614 return 0;
617 /* Make sure the current utterance is not interrupted by the next one. */
618 void talk_force_enqueue_next(void)
620 force_enqueue_next = true;
623 /* play a thumbnail from file */
624 int talk_file(const char* filename, bool enqueue)
626 int fd;
627 int size;
628 #if CONFIG_CODEC != SWCODEC
629 struct mp3entry info;
630 #endif
632 if (talk_temp_disable_count > 0)
633 return -1; /* talking has been disabled */
634 #if CONFIG_CODEC != SWCODEC
635 if (audio_status()) /* busy, buffer in use */
636 return -1;
637 #endif
639 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
640 return -1;
642 #if CONFIG_CODEC != SWCODEC
643 if(mp3info(&info, filename)) /* use this to find real start */
645 return 0; /* failed to open, or invalid */
647 #endif
649 fd = open(filename, O_RDONLY);
650 if (fd < 0) /* failed to open */
652 return 0;
655 #if CONFIG_CODEC != SWCODEC
656 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
657 #endif
659 size = read(fd, p_thumbnail, size_for_thumbnail);
660 close(fd);
662 /* ToDo: find audio, skip ID headers and trailers */
664 if (size > 0 && size != size_for_thumbnail) /* Don't play missing or truncated clips */
666 #if CONFIG_CODEC != SWCODEC && !defined(SIMULATOR)
667 bitswap(p_thumbnail, size);
668 #endif
669 queue_clip(p_thumbnail, size, enqueue);
672 return size;
676 /* say a numeric value, this word ordering works for english,
677 but not necessarily for other languages (e.g. german) */
678 int talk_number(long n, bool enqueue)
680 int level = 2; /* mille count */
681 long mil = 1000000000; /* highest possible "-illion" */
683 if (talk_temp_disable_count > 0)
684 return -1; /* talking has been disabled */
685 #if CONFIG_CODEC != SWCODEC
686 if (audio_status()) /* busy, buffer in use */
687 return -1;
688 #endif
690 if (!enqueue)
691 talk_shutup(); /* cut off all the pending stuff */
693 if (n==0)
694 { /* special case */
695 talk_id(VOICE_ZERO, true);
696 return 0;
699 if (n<0)
701 talk_id(VOICE_MINUS, true);
702 n = -n;
705 while (n)
707 int segment = n / mil; /* extract in groups of 3 digits */
708 n -= segment * mil; /* remove the used digits from number */
709 mil /= 1000; /* digit place for next round */
711 if (segment)
713 int hundreds = segment / 100;
714 int ones = segment % 100;
716 if (hundreds)
718 talk_id(VOICE_ZERO + hundreds, true);
719 talk_id(VOICE_HUNDRED, true);
722 /* combination indexing */
723 if (ones > 20)
725 int tens = ones/10 + 18;
726 talk_id(VOICE_ZERO + tens, true);
727 ones %= 10;
730 /* direct indexing */
731 if (ones)
732 talk_id(VOICE_ZERO + ones, true);
734 /* add billion, million, thousand */
735 if (mil)
736 talk_id(VOICE_THOUSAND + level, true);
738 level--;
741 return 0;
744 /* Say time duration/interval. Input is time in seconds,
745 say hours,minutes,seconds. */
746 static int talk_time_unit(long secs, bool exact, bool enqueue)
748 int hours, mins;
749 if (!enqueue)
750 talk_shutup();
751 if((hours = secs/3600)) {
752 secs %= 3600;
753 talk_value(hours, UNIT_HOUR, true);
755 if((mins = secs/60)) {
756 secs %= 60;
757 if(exact || !hours)
758 talk_value(mins, UNIT_MIN, true);
759 else talk_number(mins, true); /* don't say "minutes" */
761 if((exact && secs) || (!hours && !mins))
762 talk_value(secs, UNIT_SEC, true);
763 else if(!hours && secs)
764 talk_number(secs, true);
765 return 0;
768 void talk_fractional(char *tbuf, int value, int unit)
770 int i;
771 /* strip trailing zeros from the fraction */
772 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
773 tbuf[i] = '\0';
775 talk_number(value, true);
776 if (tbuf[0] != 0)
778 talk_id(LANG_POINT, true);
779 talk_spell(tbuf, true);
781 talk_id(unit, true);
784 int talk_value(long n, int unit, bool enqueue)
786 return talk_value_decimal(n, unit, 0, enqueue);
789 /* singular/plural aware saying of a value */
790 int talk_value_decimal(long n, int unit, int decimals, bool enqueue)
792 int unit_id;
793 static const int unit_voiced[] =
794 { /* lookup table for the voice ID of the units */
795 [0 ... UNIT_LAST-1] = -1, /* regular ID, int, signed */
796 [UNIT_MS]
797 = VOICE_MILLISECONDS, /* here come the "real" units */
798 [UNIT_SEC]
799 = VOICE_SECONDS,
800 [UNIT_MIN]
801 = VOICE_MINUTES,
802 [UNIT_HOUR]
803 = VOICE_HOURS,
804 [UNIT_KHZ]
805 = VOICE_KHZ,
806 [UNIT_DB]
807 = VOICE_DB,
808 [UNIT_PERCENT]
809 = VOICE_PERCENT,
810 [UNIT_MAH]
811 = VOICE_MILLIAMPHOURS,
812 [UNIT_PIXEL]
813 = VOICE_PIXEL,
814 [UNIT_PER_SEC]
815 = VOICE_PER_SEC,
816 [UNIT_HERTZ]
817 = VOICE_HERTZ,
818 [UNIT_MB]
819 = LANG_MEGABYTE,
820 [UNIT_KBIT]
821 = VOICE_KBIT_PER_SEC,
822 [UNIT_PM_TICK]
823 = VOICE_PM_UNITS_PER_TICK,
826 static const int pow10[] = { /* 10^0 - 10^7 */
827 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
830 char tbuf[8];
831 char fmt[] = "%0nd";
833 if (talk_temp_disable_count > 0)
834 return -1; /* talking has been disabled */
835 #if CONFIG_CODEC != SWCODEC
836 if (audio_status()) /* busy, buffer in use */
837 return -1;
838 #endif
840 /* special case for time duration */
841 if (unit == UNIT_TIME || unit == UNIT_TIME_EXACT)
842 return talk_time_unit(n, unit == UNIT_TIME_EXACT, enqueue);
844 if (unit < 0 || unit >= UNIT_LAST)
845 unit_id = -1;
846 else
847 unit_id = unit_voiced[unit];
849 if ((n==1 || n==-1) /* singular? */
850 && unit_id >= VOICE_SECONDS && unit_id <= VOICE_HOURS)
852 unit_id--; /* use the singular for those units which have */
855 /* special case with a "plus" before */
856 if (n > 0 && (unit == UNIT_SIGNED || unit == UNIT_DB))
858 talk_id(VOICE_PLUS, enqueue);
859 enqueue = true;
862 if (decimals)
864 /* needed for the "-0.5" corner case */
865 if (n < 0)
867 talk_id(VOICE_MINUS, enqueue);
868 n = -n;
871 fmt[2] = '0' + decimals;
873 snprintf(tbuf, sizeof(tbuf), fmt, n % pow10[decimals]);
874 talk_fractional(tbuf, n / pow10[decimals], unit_id);
876 return 0;
879 talk_number(n, enqueue); /* say the number */
880 talk_id(unit_id, true); /* say the unit, if any */
882 return 0;
885 /* spell a string */
886 int talk_spell(const char* spell, bool enqueue)
888 char c; /* currently processed char */
890 if (talk_temp_disable_count > 0)
891 return -1; /* talking has been disabled */
892 #if CONFIG_CODEC != SWCODEC
893 if (audio_status()) /* busy, buffer in use */
894 return -1;
895 #endif
897 if (!enqueue)
898 talk_shutup(); /* cut off all the pending stuff */
900 while ((c = *spell++) != '\0')
902 /* if this grows into too many cases, I should use a table */
903 if (c >= 'A' && c <= 'Z')
904 talk_id(VOICE_CHAR_A + c - 'A', true);
905 else if (c >= 'a' && c <= 'z')
906 talk_id(VOICE_CHAR_A + c - 'a', true);
907 else if (c >= '0' && c <= '9')
908 talk_id(VOICE_ZERO + c - '0', true);
909 else if (c == '-')
910 talk_id(VOICE_MINUS, true);
911 else if (c == '+')
912 talk_id(VOICE_PLUS, true);
913 else if (c == '.')
914 talk_id(VOICE_DOT, true);
915 else if (c == ' ')
916 talk_id(VOICE_PAUSE, true);
919 return 0;
922 void talk_disable(bool disable)
924 if (disable)
925 talk_temp_disable_count++;
926 else
927 talk_temp_disable_count--;
930 void talk_setting(const void *global_settings_variable)
932 const struct settings_list *setting;
933 if (!global_settings.talk_menu)
934 return;
935 setting = find_setting(global_settings_variable, NULL);
936 if (setting == NULL)
937 return;
938 if (setting->lang_id)
939 talk_id(setting->lang_id,false);
943 #if CONFIG_RTC
944 void talk_date(struct tm *tm, bool enqueue)
946 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, enqueue);
947 talk_number(tm->tm_mday, true);
948 talk_number(1900 + tm->tm_year, true);
951 void talk_time(struct tm *tm, bool enqueue)
953 if (global_settings.timeformat == 1)
955 /* Voice the hour */
956 long am_pm_id = VOICE_AM;
957 int hour = tm->tm_hour;
958 if (hour >= 12)
960 am_pm_id = VOICE_PM;
961 hour -= 12;
963 if (hour == 0)
964 hour = 12;
965 talk_number(hour, enqueue);
967 /* Voice the minutes */
968 if (tm->tm_min == 0)
970 /* Say o'clock if the minute is 0. */
971 talk_id(VOICE_OCLOCK, true);
973 else
975 /* Pronounce the leading 0 */
976 if(tm->tm_min < 10)
977 talk_id(VOICE_OH, true);
978 talk_number(tm->tm_min, true);
980 talk_id(am_pm_id, true);
982 else
984 /* Voice the time in 24 hour format */
985 talk_number(tm->tm_hour, enqueue);
986 if (tm->tm_min == 0)
988 talk_id(VOICE_HUNDRED, true);
989 talk_id(VOICE_HOUR, true);
991 else
993 /* Pronounce the leading 0 */
994 if(tm->tm_min < 10)
995 talk_id(VOICE_OH, true);
996 talk_number(tm->tm_min, true);
1001 #endif