typo fixes (; instead of , at the end of two assignments) (same as r17398 for usb_ser...
[kugel-rb.git] / apps / talk.c
blob5bbe0d4f3efea3937c9d04703b791a6ce9f005d4
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 <string.h>
27 #include "file.h"
28 #include "buffer.h"
29 #include "system.h"
30 #include "kernel.h"
31 #include "settings.h"
32 #include "settings_list.h"
33 #include "mp3_playback.h"
34 #include "audio.h"
35 #include "lang.h"
36 #include "talk.h"
37 #include "id3.h"
38 #include "logf.h"
39 #include "bitswap.h"
40 #include "structec.h"
41 #if CONFIG_CODEC == SWCODEC
42 #include "playback.h"
43 #endif
44 #include "debug.h"
47 /* Memory layout varies between targets because the
48 Archos (MASCODEC) devices cannot mix voice and audio playback
50 MASCODEC | MASCODEC | SWCODEC
51 (playing) | (stopped) |
52 audiobuf-----------+-----------+------------
53 audio | voice | thumbnail
54 |-----------|------------
55 | thumbnail | voice
56 | |------------
57 | | filebuf
58 | |------------
59 | | audio
60 audiobufend----------+-----------+------------
62 SWCODEC allocates dedicated buffers, MASCODEC reuses audiobuf. */
65 /***************** Constants *****************/
67 #define QUEUE_SIZE 64 /* must be a power of two */
68 #define QUEUE_MASK (QUEUE_SIZE-1)
69 const char* const dir_thumbnail_name = "_dirname.talk";
70 const char* const file_thumbnail_ext = ".talk";
72 /***************** Functional Macros *****************/
74 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
76 #define LOADED_MASK 0x80000000 /* MSB */
78 #if CONFIG_CODEC == SWCODEC
79 #define MAX_THUMBNAIL_BUFSIZE 0x10000
80 #endif
82 /***************** Data types *****************/
84 struct clip_entry /* one entry of the index table */
86 int offset; /* offset from start of voicefile file */
87 int size; /* size of the clip */
90 struct voicefile /* file format of our voice file */
92 int version; /* version of the voicefile */
93 int target_id; /* the rockbox target the file was made for */
94 int table; /* offset to index table, (=header size) */
95 int id1_max; /* number of "normal" clips contained in above index */
96 int id2_max; /* number of "voice only" clips contained in above index */
97 struct clip_entry index[]; /* followed by the index tables */
98 /* and finally the mp3 clips, not visible here, bitswapped
99 for SH based players */
102 struct queue_entry /* one entry of the internal queue */
104 unsigned char* buf;
105 long len;
109 /***************** Globals *****************/
111 static unsigned char* p_thumbnail = NULL; /* buffer for thumbnail */
112 static long size_for_thumbnail; /* leftover buffer size for it */
113 static struct voicefile* p_voicefile; /* loaded voicefile */
114 static bool has_voicefile; /* a voicefile file is present */
115 static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
116 static bool force_enqueue_next; /* enqueue next utterance even if enqueue is false */
117 static int queue_write; /* write index of queue, by application */
118 static int queue_read; /* read index of queue, by ISR context */
119 static int sent; /* how many bytes handed over to playback, owned by ISR */
120 static unsigned char curr_hd[3]; /* current frame header, for re-sync */
121 static int filehandle = -1; /* global, so the MMC variant can keep the file open */
122 static unsigned char* p_silence; /* VOICE_PAUSE clip, used for termination */
123 static long silence_len; /* length of the VOICE_PAUSE clip */
124 static unsigned char* p_lastclip; /* address of latest clip, for silence add */
125 static unsigned long voicefile_size = 0; /* size of the loaded voice file */
126 static unsigned char last_lang[MAX_FILENAME+1]; /* name of last used lang file (in talk_init) */
127 static bool talk_initialized; /* true if talk_init has been called */
128 static int talk_temp_disable_count; /* if positive, temporarily disable voice UI (not saved) */
131 /***************** Private implementation *****************/
133 static int open_voicefile(void)
135 char buf[64];
136 char* p_lang = "english"; /* default */
138 if ( global_settings.lang_file[0] &&
139 global_settings.lang_file[0] != 0xff )
140 { /* try to open the voice file of the selected language */
141 p_lang = (char *)global_settings.lang_file;
144 snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang);
146 return open(buf, O_RDONLY);
150 /* fetch a clip from the voice file */
151 static unsigned char* get_clip(long id, long* p_size)
153 long clipsize;
154 unsigned char* clipbuf;
156 if (id > VOICEONLY_DELIMITER)
157 { /* voice-only entries use the second part of the table */
158 id -= VOICEONLY_DELIMITER + 1;
159 if (id >= p_voicefile->id2_max)
160 return NULL; /* must be newer than we have */
161 id += p_voicefile->id1_max; /* table 2 is behind table 1 */
163 else
164 { /* normal use of the first table */
165 if (id >= p_voicefile->id1_max)
166 return NULL; /* must be newer than we have */
169 clipsize = p_voicefile->index[id].size;
170 if (clipsize == 0) /* clip not included in voicefile */
171 return NULL;
172 clipbuf = (unsigned char *) p_voicefile + p_voicefile->index[id].offset;
174 #ifdef HAVE_MMC /* dynamic loading, on demand */
175 if (!(clipsize & LOADED_MASK))
176 { /* clip used for the first time, needs loading */
177 lseek(filehandle, p_voicefile->index[id].offset, SEEK_SET);
178 if (read(filehandle, clipbuf, clipsize) != clipsize)
179 return NULL; /* read error */
181 p_voicefile->index[id].size |= LOADED_MASK; /* mark as loaded */
183 else
184 { /* clip is in memory already */
185 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
187 #endif
189 *p_size = clipsize;
190 return clipbuf;
194 /* load the voice file into the mp3 buffer */
195 static void load_voicefile(void)
197 int load_size;
198 int got_size;
199 int file_size;
200 #ifdef ROCKBOX_LITTLE_ENDIAN
201 int i;
202 #endif
204 filehandle = open_voicefile();
205 if (filehandle < 0) /* failed to open */
206 goto load_err;
208 file_size = filesize(filehandle);
209 if (file_size > audiobufend - audiobuf) /* won't fit? */
210 goto load_err;
212 #ifdef HAVE_MMC /* load only the header for now */
213 load_size = offsetof(struct voicefile, index);
214 #else /* load the full file */
215 load_size = file_size;
216 #endif
218 got_size = read(filehandle, audiobuf, load_size);
219 if (got_size != load_size /* failure */)
220 goto load_err;
222 #ifdef ROCKBOX_LITTLE_ENDIAN
223 logf("Byte swapping voice file");
224 structec_convert(audiobuf, "lllll", 1, true);
225 #endif
227 if (((struct voicefile*)audiobuf)->table /* format check */
228 == offsetof(struct voicefile, index))
230 p_voicefile = (struct voicefile*)audiobuf;
232 if (p_voicefile->version != VOICE_VERSION ||
233 p_voicefile->target_id != TARGET_ID)
235 logf("Incompatible voice file");
236 goto load_err;
238 #if CONFIG_CODEC != SWCODEC
239 /* MASCODEC: now use audiobuf for voice then thumbnail */
240 p_thumbnail = audiobuf + file_size;
241 p_thumbnail += (long)p_thumbnail % 2; /* 16-bit align */
242 size_for_thumbnail = audiobufend - p_thumbnail;
243 #endif
245 else
246 goto load_err;
248 #ifdef ROCKBOX_LITTLE_ENDIAN
249 for (i = 0; i < p_voicefile->id1_max + p_voicefile->id2_max; i++)
250 structec_convert(&p_voicefile->index[i], "ll", 1, true);
251 #endif
253 #ifdef HAVE_MMC
254 /* load the index table, now that we know its size from the header */
255 load_size = (p_voicefile->id1_max + p_voicefile->id2_max)
256 * sizeof(struct clip_entry);
257 got_size = read(filehandle,
258 (unsigned char *) p_voicefile + offsetof(struct voicefile, index), load_size);
259 if (got_size != load_size) /* read error */
260 goto load_err;
261 #else
262 close(filehandle); /* only the MMC variant leaves it open */
263 filehandle = -1;
264 #endif
266 /* make sure to have the silence clip, if available */
267 p_silence = get_clip(VOICE_PAUSE, &silence_len);
269 return;
271 load_err:
272 p_voicefile = NULL;
273 has_voicefile = false; /* don't try again */
274 if (filehandle >= 0)
276 close(filehandle);
277 filehandle = -1;
279 return;
283 /* called in ISR context if mp3 data got consumed */
284 static void mp3_callback(unsigned char** start, size_t* size)
286 queue[queue_read].len -= sent; /* we completed this */
287 queue[queue_read].buf += sent;
289 if (queue[queue_read].len > 0) /* current clip not finished? */
290 { /* feed the next 64K-1 chunk */
291 #if CONFIG_CODEC != SWCODEC
292 sent = MIN(queue[queue_read].len, 0xFFFF);
293 #else
294 sent = queue[queue_read].len;
295 #endif
296 *start = queue[queue_read].buf;
297 *size = sent;
298 return;
300 else if (sent > 0) /* go to next entry */
302 queue_read = (queue_read + 1) & QUEUE_MASK;
305 re_check:
307 if (QUEUE_LEVEL != 0) /* queue is not empty? */
308 { /* start next clip */
309 #if CONFIG_CODEC != SWCODEC
310 sent = MIN(queue[queue_read].len, 0xFFFF);
311 #else
312 sent = queue[queue_read].len;
313 #endif
314 *start = p_lastclip = queue[queue_read].buf;
315 *size = sent;
316 curr_hd[0] = p_lastclip[1];
317 curr_hd[1] = p_lastclip[2];
318 curr_hd[2] = p_lastclip[3];
320 else if (p_silence != NULL /* silence clip available */
321 && p_lastclip != p_silence /* previous clip wasn't silence */
322 && p_lastclip != p_thumbnail) /* ..or thumbnail */
323 { /* add silence clip when queue runs empty playing a voice clip */
324 queue[queue_write].buf = p_silence;
325 queue[queue_write].len = silence_len;
326 queue_write = (queue_write + 1) & QUEUE_MASK;
328 goto re_check;
330 else
332 *size = 0; /* end of data */
336 /***************** Public routines *****************/
338 /* stop the playback and the pending clips */
339 void talk_force_shutup(void)
341 /* Most of this is MAS only */
342 #if CONFIG_CODEC != SWCODEC
343 #ifdef SIMULATOR
344 return;
345 #endif
346 unsigned char* pos;
347 unsigned char* search;
348 unsigned char* end;
349 if (QUEUE_LEVEL == 0) /* has ended anyway */
350 return;
352 #if CONFIG_CPU == SH7034
353 CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
354 #endif /* CONFIG_CPU == SH7034 */
355 /* search next frame boundary and continue up to there */
356 pos = search = mp3_get_pos();
357 end = queue[queue_read].buf + queue[queue_read].len;
359 if (pos >= queue[queue_read].buf
360 && pos <= end) /* really our clip? */
361 { /* (for strange reasons this isn't nesessarily the case) */
362 /* find the next frame boundary */
363 while (search < end) /* search the remaining data */
365 if (*search++ != 0xFF) /* quick search for frame sync byte */
366 continue; /* (this does the majority of the job) */
368 /* look at the (bitswapped) rest of header candidate */
369 if (search[0] == curr_hd[0] /* do the quicker checks first */
370 && search[2] == curr_hd[2]
371 && (search[1] & 0x30) == (curr_hd[1] & 0x30)) /* sample rate */
373 search--; /* back to the sync byte */
374 break; /* From looking at it, this is our header. */
378 if (search-pos)
379 { /* play old data until the frame end, to keep the MAS in sync */
380 sent = search-pos;
382 queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */
383 queue[queue_read].len = sent; /* current one ends after this */
385 #if CONFIG_CPU == SH7034
386 DTCR3 = sent; /* let the DMA finish this frame */
387 CHCR3 |= 0x0001; /* re-enable DMA */
388 #endif /* CONFIG_CPU == SH7034 */
389 return;
392 #endif /* CONFIG_CODEC != SWCODEC */
394 /* Either SWCODEC, or MAS had nothing to do (was frame boundary or not our clip) */
395 mp3_play_stop();
396 queue_write = queue_read = 0; /* reset the queue */
397 return;
400 /* Shutup the voice, except if force_enqueue_next is set. */
401 void talk_shutup(void)
403 if (!force_enqueue_next)
404 talk_force_shutup();
407 /* schedule a clip, at the end or discard the existing queue */
408 static void queue_clip(unsigned char* buf, long size, bool enqueue)
410 int queue_level;
412 if (!enqueue)
413 talk_shutup(); /* cut off all the pending stuff */
414 /* Something is being enqueued, force_enqueue_next override is no
415 longer in effect. */
416 force_enqueue_next = false;
418 if (!size)
419 return; /* safety check */
420 #if CONFIG_CPU == SH7034
421 /* disable the DMA temporarily, to be safe of race condition */
422 CHCR3 &= ~0x0001;
423 #endif
424 queue_level = QUEUE_LEVEL; /* check old level */
426 if (queue_level < QUEUE_SIZE - 1) /* space left? */
428 queue[queue_write].buf = buf; /* populate an entry */
429 queue[queue_write].len = size;
430 queue_write = (queue_write + 1) & QUEUE_MASK;
433 if (queue_level == 0)
434 { /* queue was empty, we have to do the initial start */
435 p_lastclip = buf;
436 #if CONFIG_CODEC != SWCODEC
437 sent = MIN(size, 0xFFFF); /* DMA can do no more */
438 #else
439 sent = size;
440 #endif
441 mp3_play_data(buf, sent, mp3_callback);
442 curr_hd[0] = buf[1];
443 curr_hd[1] = buf[2];
444 curr_hd[2] = buf[3];
445 mp3_play_pause(true); /* kickoff audio */
447 else
449 #if CONFIG_CPU == SH7034
450 CHCR3 |= 0x0001; /* re-enable DMA */
451 #endif
454 return;
458 /* common code for talk_init() and talk_buffer_steal() */
459 static void reset_state(void)
461 queue_write = queue_read = 0; /* reset the queue */
462 p_voicefile = NULL; /* indicate no voicefile (trashed) */
463 #if CONFIG_CODEC == SWCODEC
464 /* Allocate a dedicated thumbnail buffer - once */
465 if (p_thumbnail == NULL)
467 size_for_thumbnail = audiobufend - audiobuf;
468 if (size_for_thumbnail > MAX_THUMBNAIL_BUFSIZE)
469 size_for_thumbnail = MAX_THUMBNAIL_BUFSIZE;
470 p_thumbnail = buffer_alloc(size_for_thumbnail);
472 #else
473 /* Just use the audiobuf, without allocating anything */
474 p_thumbnail = audiobuf;
475 size_for_thumbnail = audiobufend - audiobuf;
476 #endif
477 p_silence = NULL; /* pause clip not accessible */
481 /***************** Public implementation *****************/
483 void talk_init(void)
485 talk_temp_disable_count = 0;
486 if (talk_initialized && !strcasecmp(last_lang, global_settings.lang_file))
488 /* not a new file, nothing to do */
489 return;
492 #ifdef HAVE_MMC
493 if (filehandle >= 0) /* MMC: An old voice file might still be open */
495 close(filehandle);
496 filehandle = -1;
498 #endif
500 talk_initialized = true;
501 strncpy((char *) last_lang, (char *)global_settings.lang_file,
502 MAX_FILENAME);
504 #if CONFIG_CODEC == SWCODEC
505 audio_get_buffer(false, NULL); /* Must tell audio to reinitialize */
506 #endif
507 reset_state(); /* use this for most of our inits */
509 filehandle = open_voicefile();
510 has_voicefile = (filehandle >= 0); /* test if we can open it */
511 voicefile_size = 0;
513 if (has_voicefile)
515 voicefile_size = filesize(filehandle);
516 close(filehandle); /* close again, this was just to detect presence */
517 filehandle = -1;
522 #if CONFIG_CODEC == SWCODEC
523 /* return if a voice codec is required or not */
524 bool talk_voice_required(void)
526 return (voicefile_size != 0) /* Voice file is available */
527 || (global_settings.talk_dir_clip) /* Thumbnail clips are required */
528 || (global_settings.talk_file_clip);
530 #endif
532 /* return size of voice file */
533 int talk_get_bufsize(void)
535 return voicefile_size;
538 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
539 void talk_buffer_steal(void)
541 #if CONFIG_CODEC != SWCODEC
542 mp3_play_stop();
543 #endif
544 #ifdef HAVE_MMC
545 if (filehandle >= 0) /* only relevant for MMC */
547 close(filehandle);
548 filehandle = -1;
550 #endif
551 reset_state();
555 /* play a voice ID from voicefile */
556 int talk_id(int32_t id, bool enqueue)
558 long clipsize;
559 unsigned char* clipbuf;
560 int32_t unit;
561 int decimals;
563 if (talk_temp_disable_count > 0)
564 return -1; /* talking has been disabled */
565 #if CONFIG_CODEC != SWCODEC
566 if (audio_status()) /* busy, buffer in use */
567 return -1;
568 #endif
570 if (p_voicefile == NULL && has_voicefile)
571 load_voicefile(); /* reload needed */
573 if (p_voicefile == NULL) /* still no voices? */
574 return -1;
576 if (id == -1) /* -1 is an indication for silence */
577 return -1;
579 decimals = (((uint32_t)id) >> DECIMAL_SHIFT) & 0x7;
581 /* check if this is a special ID, with a value */
582 unit = ((uint32_t)id) >> UNIT_SHIFT;
583 if (unit || decimals)
584 { /* sign-extend the value */
585 id = (uint32_t)id << (32-DECIMAL_SHIFT);
586 id >>= (32-DECIMAL_SHIFT);
588 talk_value_decimal(id, unit, decimals, enqueue); /* speak it */
589 return 0; /* and stop, end of special case */
592 clipbuf = get_clip(id, &clipsize);
593 if (clipbuf == NULL)
594 return -1; /* not present */
596 queue_clip(clipbuf, clipsize, enqueue);
598 return 0;
600 /* Speaks zero or more IDs (from an array). */
601 int talk_idarray(long *ids, bool enqueue)
603 int r;
604 if(!ids)
605 return 0;
606 while(*ids != TALK_FINAL_ID)
608 if((r = talk_id(*ids++, enqueue)) <0)
609 return r;
610 enqueue = true;
612 return 0;
615 /* Make sure the current utterance is not interrupted by the next one. */
616 void talk_force_enqueue_next(void)
618 force_enqueue_next = true;
621 /* play a thumbnail from file */
622 int talk_file(const char* filename, bool enqueue)
624 int fd;
625 int size;
626 #if CONFIG_CODEC != SWCODEC
627 struct mp3entry info;
628 #endif
630 if (talk_temp_disable_count > 0)
631 return -1; /* talking has been disabled */
632 #if CONFIG_CODEC != SWCODEC
633 if (audio_status()) /* busy, buffer in use */
634 return -1;
635 #endif
637 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
638 return -1;
640 #if CONFIG_CODEC != SWCODEC
641 if(mp3info(&info, filename)) /* use this to find real start */
643 return 0; /* failed to open, or invalid */
645 #endif
647 fd = open(filename, O_RDONLY);
648 if (fd < 0) /* failed to open */
650 return 0;
653 #if CONFIG_CODEC != SWCODEC
654 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
655 #endif
657 size = read(fd, p_thumbnail, size_for_thumbnail);
658 close(fd);
660 /* ToDo: find audio, skip ID headers and trailers */
662 if (size > 0 && size != size_for_thumbnail) /* Don't play missing or truncated clips */
664 #if CONFIG_CODEC != SWCODEC && !defined(SIMULATOR)
665 bitswap(p_thumbnail, size);
666 #endif
667 queue_clip(p_thumbnail, size, enqueue);
670 return size;
674 /* say a numeric value, this word ordering works for english,
675 but not necessarily for other languages (e.g. german) */
676 int talk_number(long n, bool enqueue)
678 int level = 2; /* mille count */
679 long mil = 1000000000; /* highest possible "-illion" */
681 if (talk_temp_disable_count > 0)
682 return -1; /* talking has been disabled */
683 #if CONFIG_CODEC != SWCODEC
684 if (audio_status()) /* busy, buffer in use */
685 return -1;
686 #endif
688 if (!enqueue)
689 talk_shutup(); /* cut off all the pending stuff */
691 if (n==0)
692 { /* special case */
693 talk_id(VOICE_ZERO, true);
694 return 0;
697 if (n<0)
699 talk_id(VOICE_MINUS, true);
700 n = -n;
703 while (n)
705 int segment = n / mil; /* extract in groups of 3 digits */
706 n -= segment * mil; /* remove the used digits from number */
707 mil /= 1000; /* digit place for next round */
709 if (segment)
711 int hundreds = segment / 100;
712 int ones = segment % 100;
714 if (hundreds)
716 talk_id(VOICE_ZERO + hundreds, true);
717 talk_id(VOICE_HUNDRED, true);
720 /* combination indexing */
721 if (ones > 20)
723 int tens = ones/10 + 18;
724 talk_id(VOICE_ZERO + tens, true);
725 ones %= 10;
728 /* direct indexing */
729 if (ones)
730 talk_id(VOICE_ZERO + ones, true);
732 /* add billion, million, thousand */
733 if (mil)
734 talk_id(VOICE_THOUSAND + level, true);
736 level--;
739 return 0;
742 /* Say time duration/interval. Input is time in seconds,
743 say hours,minutes,seconds. */
744 static int talk_time_unit(long secs, bool exact, bool enqueue)
746 int hours, mins;
747 if (!enqueue)
748 talk_shutup();
749 if((hours = secs/3600)) {
750 secs %= 3600;
751 talk_value(hours, UNIT_HOUR, true);
753 if((mins = secs/60)) {
754 secs %= 60;
755 if(exact || !hours)
756 talk_value(mins, UNIT_MIN, true);
757 else talk_number(mins, true); /* don't say "minutes" */
759 if((exact && secs) || (!hours && !mins))
760 talk_value(secs, UNIT_SEC, true);
761 else if(!hours && secs)
762 talk_number(secs, true);
763 return 0;
766 void talk_fractional(char *tbuf, int value, int unit)
768 int i;
769 /* strip trailing zeros from the fraction */
770 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
771 tbuf[i] = '\0';
773 talk_number(value, true);
774 if (tbuf[0] != 0)
776 talk_id(LANG_POINT, true);
777 talk_spell(tbuf, true);
779 talk_id(unit, true);
782 int talk_value(long n, int unit, bool enqueue)
784 return talk_value_decimal(n, unit, 0, enqueue);
787 /* singular/plural aware saying of a value */
788 int talk_value_decimal(long n, int unit, int decimals, bool enqueue)
790 int unit_id;
791 static const int unit_voiced[] =
792 { /* lookup table for the voice ID of the units */
793 [0 ... UNIT_LAST-1] = -1, /* regular ID, int, signed */
794 [UNIT_MS]
795 = VOICE_MILLISECONDS, /* here come the "real" units */
796 [UNIT_SEC]
797 = VOICE_SECONDS,
798 [UNIT_MIN]
799 = VOICE_MINUTES,
800 [UNIT_HOUR]
801 = VOICE_HOURS,
802 [UNIT_KHZ]
803 = VOICE_KHZ,
804 [UNIT_DB]
805 = VOICE_DB,
806 [UNIT_PERCENT]
807 = VOICE_PERCENT,
808 [UNIT_MAH]
809 = VOICE_MILLIAMPHOURS,
810 [UNIT_PIXEL]
811 = VOICE_PIXEL,
812 [UNIT_PER_SEC]
813 = VOICE_PER_SEC,
814 [UNIT_HERTZ]
815 = VOICE_HERTZ,
816 [UNIT_MB]
817 = LANG_MEGABYTE,
818 [UNIT_KBIT]
819 = VOICE_KBIT_PER_SEC,
820 [UNIT_PM_TICK]
821 = VOICE_PM_UNITS_PER_TICK,
824 static const int pow10[] = { /* 10^0 - 10^7 */
825 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
828 char tbuf[8];
829 char fmt[] = "%0nd";
831 if (talk_temp_disable_count > 0)
832 return -1; /* talking has been disabled */
833 #if CONFIG_CODEC != SWCODEC
834 if (audio_status()) /* busy, buffer in use */
835 return -1;
836 #endif
838 /* special case for time duration */
839 if (unit == UNIT_TIME || unit == UNIT_TIME_EXACT)
840 return talk_time_unit(n, unit == UNIT_TIME_EXACT, enqueue);
842 if (unit < 0 || unit >= UNIT_LAST)
843 unit_id = -1;
844 else
845 unit_id = unit_voiced[unit];
847 if ((n==1 || n==-1) /* singular? */
848 && unit_id >= VOICE_SECONDS && unit_id <= VOICE_HOURS)
850 unit_id--; /* use the singular for those units which have */
853 /* special case with a "plus" before */
854 if (n > 0 && (unit == UNIT_SIGNED || unit == UNIT_DB))
856 talk_id(VOICE_PLUS, enqueue);
857 enqueue = true;
860 if (decimals)
862 /* needed for the "-0.5" corner case */
863 if (n < 0)
865 talk_id(VOICE_MINUS, enqueue);
866 n = -n;
869 fmt[2] = '0' + decimals;
871 snprintf(tbuf, sizeof(tbuf), fmt, n % pow10[decimals]);
872 talk_fractional(tbuf, n / pow10[decimals], unit_id);
874 return 0;
877 talk_number(n, enqueue); /* say the number */
878 talk_id(unit_id, true); /* say the unit, if any */
880 return 0;
883 /* spell a string */
884 int talk_spell(const char* spell, bool enqueue)
886 char c; /* currently processed char */
888 if (talk_temp_disable_count > 0)
889 return -1; /* talking has been disabled */
890 #if CONFIG_CODEC != SWCODEC
891 if (audio_status()) /* busy, buffer in use */
892 return -1;
893 #endif
895 if (!enqueue)
896 talk_shutup(); /* cut off all the pending stuff */
898 while ((c = *spell++) != '\0')
900 /* if this grows into too many cases, I should use a table */
901 if (c >= 'A' && c <= 'Z')
902 talk_id(VOICE_CHAR_A + c - 'A', true);
903 else if (c >= 'a' && c <= 'z')
904 talk_id(VOICE_CHAR_A + c - 'a', true);
905 else if (c >= '0' && c <= '9')
906 talk_id(VOICE_ZERO + c - '0', true);
907 else if (c == '-')
908 talk_id(VOICE_MINUS, true);
909 else if (c == '+')
910 talk_id(VOICE_PLUS, true);
911 else if (c == '.')
912 talk_id(VOICE_DOT, true);
913 else if (c == ' ')
914 talk_id(VOICE_PAUSE, true);
917 return 0;
920 void talk_disable(bool disable)
922 if (disable)
923 talk_temp_disable_count++;
924 else
925 talk_temp_disable_count--;
928 void talk_setting(const void *global_settings_variable)
930 const struct settings_list *setting;
931 if (!global_settings.talk_menu)
932 return;
933 setting = find_setting(global_settings_variable, NULL);
934 if (setting == NULL)
935 return;
936 if (setting->lang_id)
937 talk_id(setting->lang_id,false);
941 #if CONFIG_RTC
942 void talk_date(struct tm *tm, bool enqueue)
944 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, enqueue);
945 talk_number(tm->tm_mday, true);
946 talk_number(1900 + tm->tm_year, true);
949 void talk_time(struct tm *tm, bool enqueue)
951 if (global_settings.timeformat == 1)
953 /* Voice the hour */
954 long am_pm_id = VOICE_AM;
955 int hour = tm->tm_hour;
956 if (hour >= 12)
958 am_pm_id = VOICE_PM;
959 hour -= 12;
961 if (hour == 0)
962 hour = 12;
963 talk_number(hour, enqueue);
965 /* Voice the minutes */
966 if (tm->tm_min == 0)
968 /* Say o'clock if the minute is 0. */
969 talk_id(VOICE_OCLOCK, true);
971 else
973 /* Pronounce the leading 0 */
974 if(tm->tm_min < 10)
975 talk_id(VOICE_OH, true);
976 talk_number(tm->tm_min, true);
978 talk_id(am_pm_id, true);
980 else
982 /* Voice the time in 24 hour format */
983 talk_number(tm->tm_hour, enqueue);
984 if (tm->tm_min == 0)
986 talk_id(VOICE_HUNDRED, true);
987 talk_id(VOICE_HOUR, true);
989 else
991 /* Pronounce the leading 0 */
992 if(tm->tm_min < 10)
993 talk_id(VOICE_OH, true);
994 talk_number(tm->tm_min, true);
999 #endif