rbutil: improve voice creation: better parser for genlang output. Make progressbar...
[Rockbox.git] / apps / talk.c
bloba4f253dadc2f3a8b99ca6937d9fc64d2989f1f2b
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 "mp3_playback.h"
33 #include "audio.h"
34 #include "lang.h"
35 #include "talk.h"
36 #include "id3.h"
37 #include "logf.h"
38 #include "bitswap.h"
39 #include "structec.h"
40 #if CONFIG_CODEC == SWCODEC
41 #include "playback.h"
42 #endif
43 #include "debug.h"
46 /* Memory layout varies between targets because the
47 Archos (MASCODEC) devices cannot mix voice and audio playback
49 MASCODEC | MASCODEC | SWCODEC
50 (playing) | (stopped) |
51 audiobuf-----------+-----------+------------
52 audio | voice | thumbnail
53 |-----------|------------
54 | thumbnail | voice
55 | |------------
56 | | filebuf
57 | |------------
58 | | audio
59 audiobufend----------+-----------+------------
61 SWCODEC allocates dedicated buffers, MASCODEC reuses audiobuf. */
64 /***************** Constants *****************/
66 #define QUEUE_SIZE 64 /* must be a power of two */
67 #define QUEUE_MASK (QUEUE_SIZE-1)
68 const char* const dir_thumbnail_name = "_dirname.talk";
69 const char* const file_thumbnail_ext = ".talk";
71 /***************** Functional Macros *****************/
73 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
75 #define LOADED_MASK 0x80000000 /* MSB */
77 #if CONFIG_CODEC == SWCODEC
78 #define MAX_THUMBNAIL_BUFSIZE 0x10000
79 #endif
81 /***************** Data types *****************/
83 struct clip_entry /* one entry of the index table */
85 int offset; /* offset from start of voicefile file */
86 int size; /* size of the clip */
89 struct voicefile /* file format of our voice file */
91 int version; /* version of the voicefile */
92 int target_id; /* the rockbox target the file was made for */
93 int table; /* offset to index table, (=header size) */
94 int id1_max; /* number of "normal" clips contained in above index */
95 int id2_max; /* number of "voice only" clips contained in above index */
96 struct clip_entry index[]; /* followed by the index tables */
97 /* and finally the mp3 clips, not visible here, bitswapped
98 for SH based players */
101 struct queue_entry /* one entry of the internal queue */
103 unsigned char* buf;
104 long len;
108 /***************** Globals *****************/
110 static unsigned char* p_thumbnail = NULL; /* buffer for thumbnail */
111 static long size_for_thumbnail; /* leftover buffer size for it */
112 static struct voicefile* p_voicefile; /* loaded voicefile */
113 static bool has_voicefile; /* a voicefile file is present */
114 static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
115 static bool force_enqueue_next; /* enqueue next utterance even if enqueue is false */
116 static int queue_write; /* write index of queue, by application */
117 static int queue_read; /* read index of queue, by ISR context */
118 static int sent; /* how many bytes handed over to playback, owned by ISR */
119 static unsigned char curr_hd[3]; /* current frame header, for re-sync */
120 static int filehandle = -1; /* global, so the MMC variant can keep the file open */
121 static unsigned char* p_silence; /* VOICE_PAUSE clip, used for termination */
122 static long silence_len; /* length of the VOICE_PAUSE clip */
123 static unsigned char* p_lastclip; /* address of latest clip, for silence add */
124 static unsigned long voicefile_size = 0; /* size of the loaded voice file */
125 static unsigned char last_lang[MAX_FILENAME+1]; /* name of last used lang file (in talk_init) */
126 static bool talk_initialized; /* true if talk_init has been called */
127 static int talk_temp_disable_count; /* if positive, temporarily disable voice UI (not saved) */
130 /***************** Private implementation *****************/
132 static int open_voicefile(void)
134 char buf[64];
135 char* p_lang = "english"; /* default */
137 if ( global_settings.lang_file[0] &&
138 global_settings.lang_file[0] != 0xff )
139 { /* try to open the voice file of the selected language */
140 p_lang = (char *)global_settings.lang_file;
143 snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang);
145 return open(buf, O_RDONLY);
149 /* fetch a clip from the voice file */
150 static unsigned char* get_clip(long id, long* p_size)
152 long clipsize;
153 unsigned char* clipbuf;
155 if (id > VOICEONLY_DELIMITER)
156 { /* voice-only entries use the second part of the table */
157 id -= VOICEONLY_DELIMITER + 1;
158 if (id >= p_voicefile->id2_max)
159 return NULL; /* must be newer than we have */
160 id += p_voicefile->id1_max; /* table 2 is behind table 1 */
162 else
163 { /* normal use of the first table */
164 if (id >= p_voicefile->id1_max)
165 return NULL; /* must be newer than we have */
168 clipsize = p_voicefile->index[id].size;
169 if (clipsize == 0) /* clip not included in voicefile */
170 return NULL;
171 clipbuf = (unsigned char *) p_voicefile + p_voicefile->index[id].offset;
173 #ifdef HAVE_MMC /* dynamic loading, on demand */
174 if (!(clipsize & LOADED_MASK))
175 { /* clip used for the first time, needs loading */
176 lseek(filehandle, p_voicefile->index[id].offset, SEEK_SET);
177 if (read(filehandle, clipbuf, clipsize) != clipsize)
178 return NULL; /* read error */
180 p_voicefile->index[id].size |= LOADED_MASK; /* mark as loaded */
182 else
183 { /* clip is in memory already */
184 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
186 #endif
188 *p_size = clipsize;
189 return clipbuf;
193 /* load the voice file into the mp3 buffer */
194 static void load_voicefile(void)
196 int load_size;
197 int got_size;
198 int file_size;
199 #ifdef ROCKBOX_LITTLE_ENDIAN
200 int i;
201 #endif
203 filehandle = open_voicefile();
204 if (filehandle < 0) /* failed to open */
205 goto load_err;
207 file_size = filesize(filehandle);
208 if (file_size > audiobufend - audiobuf) /* won't fit? */
209 goto load_err;
211 #ifdef HAVE_MMC /* load only the header for now */
212 load_size = offsetof(struct voicefile, index);
213 #else /* load the full file */
214 load_size = file_size;
215 #endif
217 got_size = read(filehandle, audiobuf, load_size);
218 if (got_size != load_size /* failure */)
219 goto load_err;
221 #ifdef ROCKBOX_LITTLE_ENDIAN
222 logf("Byte swapping voice file");
223 structec_convert(audiobuf, "lllll", 1, true);
224 #endif
226 if (((struct voicefile*)audiobuf)->table /* format check */
227 == offsetof(struct voicefile, index))
229 p_voicefile = (struct voicefile*)audiobuf;
231 if (p_voicefile->version != VOICE_VERSION ||
232 p_voicefile->target_id != TARGET_ID)
234 logf("Incompatible voice file");
235 goto load_err;
237 #if CONFIG_CODEC != SWCODEC
238 /* MASCODEC: now use audiobuf for voice then thumbnail */
239 p_thumbnail = audiobuf + file_size;
240 p_thumbnail += (long)p_thumbnail % 2; /* 16-bit align */
241 size_for_thumbnail = audiobufend - p_thumbnail;
242 #endif
244 else
245 goto load_err;
247 #ifdef ROCKBOX_LITTLE_ENDIAN
248 for (i = 0; i < p_voicefile->id1_max + p_voicefile->id2_max; i++)
249 structec_convert(&p_voicefile->index[i], "ll", 1, true);
250 #endif
252 #ifdef HAVE_MMC
253 /* load the index table, now that we know its size from the header */
254 load_size = (p_voicefile->id1_max + p_voicefile->id2_max)
255 * sizeof(struct clip_entry);
256 got_size = read(filehandle,
257 (unsigned char *) p_voicefile + offsetof(struct voicefile, index), load_size);
258 if (got_size != load_size) /* read error */
259 goto load_err;
260 #else
261 close(filehandle); /* only the MMC variant leaves it open */
262 filehandle = -1;
263 #endif
265 /* make sure to have the silence clip, if available */
266 p_silence = get_clip(VOICE_PAUSE, &silence_len);
268 return;
270 load_err:
271 p_voicefile = NULL;
272 has_voicefile = false; /* don't try again */
273 if (filehandle >= 0)
275 close(filehandle);
276 filehandle = -1;
278 return;
282 /* called in ISR context if mp3 data got consumed */
283 static void mp3_callback(unsigned char** start, size_t* size)
285 queue[queue_read].len -= sent; /* we completed this */
286 queue[queue_read].buf += sent;
288 if (queue[queue_read].len > 0) /* current clip not finished? */
289 { /* feed the next 64K-1 chunk */
290 #if CONFIG_CODEC != SWCODEC
291 sent = MIN(queue[queue_read].len, 0xFFFF);
292 #else
293 sent = queue[queue_read].len;
294 #endif
295 *start = queue[queue_read].buf;
296 *size = sent;
297 return;
299 else if (sent > 0) /* go to next entry */
301 queue_read = (queue_read + 1) & QUEUE_MASK;
304 re_check:
306 if (QUEUE_LEVEL != 0) /* queue is not empty? */
307 { /* start next clip */
308 #if CONFIG_CODEC != SWCODEC
309 sent = MIN(queue[queue_read].len, 0xFFFF);
310 #else
311 sent = queue[queue_read].len;
312 #endif
313 *start = p_lastclip = queue[queue_read].buf;
314 *size = sent;
315 curr_hd[0] = p_lastclip[1];
316 curr_hd[1] = p_lastclip[2];
317 curr_hd[2] = p_lastclip[3];
319 else if (p_silence != NULL /* silence clip available */
320 && p_lastclip != p_silence /* previous clip wasn't silence */
321 && p_lastclip != p_thumbnail) /* ..or thumbnail */
322 { /* add silence clip when queue runs empty playing a voice clip */
323 queue[queue_write].buf = p_silence;
324 queue[queue_write].len = silence_len;
325 queue_write = (queue_write + 1) & QUEUE_MASK;
327 goto re_check;
329 else
331 *size = 0; /* end of data */
335 /***************** Public routines *****************/
337 /* stop the playback and the pending clips */
338 void talk_force_shutup(void)
340 /* Most of this is MAS only */
341 #if CONFIG_CODEC != SWCODEC
342 #ifdef SIMULATOR
343 return;
344 #endif
345 unsigned char* pos;
346 unsigned char* search;
347 unsigned char* end;
348 if (QUEUE_LEVEL == 0) /* has ended anyway */
349 return;
351 #if CONFIG_CPU == SH7034
352 CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
353 #endif /* CONFIG_CPU == SH7034 */
354 /* search next frame boundary and continue up to there */
355 pos = search = mp3_get_pos();
356 end = queue[queue_read].buf + queue[queue_read].len;
358 if (pos >= queue[queue_read].buf
359 && pos <= end) /* really our clip? */
360 { /* (for strange reasons this isn't nesessarily the case) */
361 /* find the next frame boundary */
362 while (search < end) /* search the remaining data */
364 if (*search++ != 0xFF) /* quick search for frame sync byte */
365 continue; /* (this does the majority of the job) */
367 /* look at the (bitswapped) rest of header candidate */
368 if (search[0] == curr_hd[0] /* do the quicker checks first */
369 && search[2] == curr_hd[2]
370 && (search[1] & 0x30) == (curr_hd[1] & 0x30)) /* sample rate */
372 search--; /* back to the sync byte */
373 break; /* From looking at it, this is our header. */
377 if (search-pos)
378 { /* play old data until the frame end, to keep the MAS in sync */
379 sent = search-pos;
381 queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */
382 queue[queue_read].len = sent; /* current one ends after this */
384 #if CONFIG_CPU == SH7034
385 DTCR3 = sent; /* let the DMA finish this frame */
386 CHCR3 |= 0x0001; /* re-enable DMA */
387 #endif /* CONFIG_CPU == SH7034 */
388 return;
391 #endif /* CONFIG_CODEC != SWCODEC */
393 /* Either SWCODEC, or MAS had nothing to do (was frame boundary or not our clip) */
394 mp3_play_stop();
395 queue_write = queue_read = 0; /* reset the queue */
396 return;
399 /* Shutup the voice, except if force_enqueue_next is set. */
400 void talk_shutup(void)
402 if (!force_enqueue_next)
403 talk_force_shutup();
406 /* schedule a clip, at the end or discard the existing queue */
407 static void queue_clip(unsigned char* buf, long size, bool enqueue)
409 int queue_level;
411 if (!enqueue)
412 talk_shutup(); /* cut off all the pending stuff */
413 /* Something is being enqueued, force_enqueue_next override is no
414 longer in effect. */
415 force_enqueue_next = false;
417 if (!size)
418 return; /* safety check */
419 #if CONFIG_CPU == SH7034
420 /* disable the DMA temporarily, to be safe of race condition */
421 CHCR3 &= ~0x0001;
422 #endif
423 queue_level = QUEUE_LEVEL; /* check old level */
425 if (queue_level < QUEUE_SIZE - 1) /* space left? */
427 queue[queue_write].buf = buf; /* populate an entry */
428 queue[queue_write].len = size;
429 queue_write = (queue_write + 1) & QUEUE_MASK;
432 if (queue_level == 0)
433 { /* queue was empty, we have to do the initial start */
434 p_lastclip = buf;
435 #if CONFIG_CODEC != SWCODEC
436 sent = MIN(size, 0xFFFF); /* DMA can do no more */
437 #else
438 sent = size;
439 #endif
440 mp3_play_data(buf, sent, mp3_callback);
441 curr_hd[0] = buf[1];
442 curr_hd[1] = buf[2];
443 curr_hd[2] = buf[3];
444 mp3_play_pause(true); /* kickoff audio */
446 else
448 #if CONFIG_CPU == SH7034
449 CHCR3 |= 0x0001; /* re-enable DMA */
450 #endif
453 return;
457 /* common code for talk_init() and talk_buffer_steal() */
458 static void reset_state(void)
460 queue_write = queue_read = 0; /* reset the queue */
461 p_voicefile = NULL; /* indicate no voicefile (trashed) */
462 #if CONFIG_CODEC == SWCODEC
463 /* Allocate a dedicated thumbnail buffer - once */
464 if (p_thumbnail == NULL)
466 size_for_thumbnail = audiobufend - audiobuf;
467 if (size_for_thumbnail > MAX_THUMBNAIL_BUFSIZE)
468 size_for_thumbnail = MAX_THUMBNAIL_BUFSIZE;
469 p_thumbnail = buffer_alloc(size_for_thumbnail);
471 #else
472 /* Just use the audiobuf, without allocating anything */
473 p_thumbnail = audiobuf;
474 size_for_thumbnail = audiobufend - audiobuf;
475 #endif
476 p_silence = NULL; /* pause clip not accessible */
480 /***************** Public implementation *****************/
482 void talk_init(void)
484 talk_temp_disable_count = 0;
485 if (talk_initialized && !strcasecmp(last_lang, global_settings.lang_file))
487 /* not a new file, nothing to do */
488 return;
491 #ifdef HAVE_MMC
492 if (filehandle >= 0) /* MMC: An old voice file might still be open */
494 close(filehandle);
495 filehandle = -1;
497 #endif
499 talk_initialized = true;
500 strncpy((char *) last_lang, (char *)global_settings.lang_file,
501 MAX_FILENAME);
503 #if CONFIG_CODEC == SWCODEC
504 audio_get_buffer(false, NULL); /* Must tell audio to reinitialize */
505 #endif
506 reset_state(); /* use this for most of our inits */
508 filehandle = open_voicefile();
509 has_voicefile = (filehandle >= 0); /* test if we can open it */
510 voicefile_size = 0;
512 if (has_voicefile)
514 voicefile_size = filesize(filehandle);
515 close(filehandle); /* close again, this was just to detect presence */
516 filehandle = -1;
521 #if CONFIG_CODEC == SWCODEC
522 /* return if a voice codec is required or not */
523 bool talk_voice_required(void)
525 return (voicefile_size != 0) /* Voice file is available */
526 || (global_settings.talk_dir_clip) /* Thumbnail clips are required */
527 || (global_settings.talk_file_clip);
529 #endif
531 /* return size of voice file */
532 int talk_get_bufsize(void)
534 return voicefile_size;
537 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
538 void talk_buffer_steal(void)
540 #if CONFIG_CODEC != SWCODEC
541 mp3_play_stop();
542 #endif
543 #ifdef HAVE_MMC
544 if (filehandle >= 0) /* only relevant for MMC */
546 close(filehandle);
547 filehandle = -1;
549 #endif
550 reset_state();
554 /* play a voice ID from voicefile */
555 int talk_id(int32_t id, bool enqueue)
557 long clipsize;
558 unsigned char* clipbuf;
559 int32_t unit;
561 if (talk_temp_disable_count > 0)
562 return -1; /* talking has been disabled */
563 #if CONFIG_CODEC != SWCODEC
564 if (audio_status()) /* busy, buffer in use */
565 return -1;
566 #endif
568 if (p_voicefile == NULL && has_voicefile)
569 load_voicefile(); /* reload needed */
571 if (p_voicefile == NULL) /* still no voices? */
572 return -1;
574 if (id == -1) /* -1 is an indication for silence */
575 return -1;
577 /* check if this is a special ID, with a value */
578 unit = ((uint32_t)id) >> UNIT_SHIFT;
579 if (unit)
580 { /* sign-extend the value */
581 id = (uint32_t)id << (32-UNIT_SHIFT);
582 id >>= (32-UNIT_SHIFT);
583 talk_value(id, unit, enqueue); /* speak it */
584 return 0; /* and stop, end of special case */
587 clipbuf = get_clip(id, &clipsize);
588 if (clipbuf == NULL)
589 return -1; /* not present */
591 queue_clip(clipbuf, clipsize, enqueue);
593 return 0;
596 /* Speaks zero or more IDs (from an array). */
597 int talk_idarray(long *ids, bool enqueue)
599 int r;
600 if(!ids)
601 return 0;
602 while(*ids != TALK_FINAL_ID)
604 if((r = talk_id(*ids++, enqueue)) <0)
605 return r;
606 enqueue = true;
608 return 0;
611 /* Make sure the current utterance is not interrupted by the next one. */
612 void talk_force_enqueue_next(void)
614 force_enqueue_next = true;
617 /* play a thumbnail from file */
618 int talk_file(const char* filename, bool enqueue)
620 int fd;
621 int size;
622 #if CONFIG_CODEC != SWCODEC
623 struct mp3entry info;
624 #endif
626 if (talk_temp_disable_count > 0)
627 return -1; /* talking has been disabled */
628 #if CONFIG_CODEC != SWCODEC
629 if (audio_status()) /* busy, buffer in use */
630 return -1;
631 #endif
633 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
634 return -1;
636 #if CONFIG_CODEC != SWCODEC
637 if(mp3info(&info, filename)) /* use this to find real start */
639 return 0; /* failed to open, or invalid */
641 #endif
643 fd = open(filename, O_RDONLY);
644 if (fd < 0) /* failed to open */
646 return 0;
649 #if CONFIG_CODEC != SWCODEC
650 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
651 #endif
653 size = read(fd, p_thumbnail, size_for_thumbnail);
654 close(fd);
656 /* ToDo: find audio, skip ID headers and trailers */
658 if (size > 0 && size != size_for_thumbnail) /* Don't play missing or truncated clips */
660 #if CONFIG_CODEC != SWCODEC && !defined(SIMULATOR)
661 bitswap(p_thumbnail, size);
662 #endif
663 queue_clip(p_thumbnail, size, enqueue);
666 return size;
670 /* say a numeric value, this word ordering works for english,
671 but not necessarily for other languages (e.g. german) */
672 int talk_number(long n, bool enqueue)
674 int level = 2; /* mille count */
675 long mil = 1000000000; /* highest possible "-illion" */
677 if (talk_temp_disable_count > 0)
678 return -1; /* talking has been disabled */
679 #if CONFIG_CODEC != SWCODEC
680 if (audio_status()) /* busy, buffer in use */
681 return -1;
682 #endif
684 if (!enqueue)
685 talk_shutup(); /* cut off all the pending stuff */
687 if (n==0)
688 { /* special case */
689 talk_id(VOICE_ZERO, true);
690 return 0;
693 if (n<0)
695 talk_id(VOICE_MINUS, true);
696 n = -n;
699 while (n)
701 int segment = n / mil; /* extract in groups of 3 digits */
702 n -= segment * mil; /* remove the used digits from number */
703 mil /= 1000; /* digit place for next round */
705 if (segment)
707 int hundreds = segment / 100;
708 int ones = segment % 100;
710 if (hundreds)
712 talk_id(VOICE_ZERO + hundreds, true);
713 talk_id(VOICE_HUNDRED, true);
716 /* combination indexing */
717 if (ones > 20)
719 int tens = ones/10 + 18;
720 talk_id(VOICE_ZERO + tens, true);
721 ones %= 10;
724 /* direct indexing */
725 if (ones)
726 talk_id(VOICE_ZERO + ones, true);
728 /* add billion, million, thousand */
729 if (mil)
730 talk_id(VOICE_THOUSAND + level, true);
732 level--;
735 return 0;
738 /* Say time duration/interval. Input is time in seconds,
739 say hours,minutes,seconds. */
740 static int talk_time_unit(long secs, bool exact, bool enqueue)
742 int hours, mins;
743 if (!enqueue)
744 talk_shutup();
745 if((hours = secs/3600)) {
746 secs %= 3600;
747 talk_value(hours, UNIT_HOUR, true);
749 if((mins = secs/60)) {
750 secs %= 60;
751 if(exact || !hours)
752 talk_value(mins, UNIT_MIN, true);
753 else talk_number(mins, true); /* don't say "minutes" */
755 if((exact && secs) || (!hours && !mins))
756 talk_value(secs, UNIT_SEC, true);
757 else if(!hours && secs)
758 talk_number(secs, true);
759 return 0;
762 /* singular/plural aware saying of a value */
763 int talk_value(long n, int unit, bool enqueue)
765 int unit_id;
766 static const int unit_voiced[] =
767 { /* lookup table for the voice ID of the units */
768 [0 ... UNIT_LAST-1] = -1, /* regular ID, int, signed */
769 [UNIT_MS]
770 = VOICE_MILLISECONDS, /* here come the "real" units */
771 [UNIT_SEC]
772 = VOICE_SECONDS,
773 [UNIT_MIN]
774 = VOICE_MINUTES,
775 [UNIT_HOUR]
776 = VOICE_HOURS,
777 [UNIT_KHZ]
778 = VOICE_KHZ,
779 [UNIT_DB]
780 = VOICE_DB,
781 [UNIT_PERCENT]
782 = VOICE_PERCENT,
783 [UNIT_MAH]
784 = VOICE_MILLIAMPHOURS,
785 [UNIT_PIXEL]
786 = VOICE_PIXEL,
787 [UNIT_PER_SEC]
788 = VOICE_PER_SEC,
789 [UNIT_HERTZ]
790 = VOICE_HERTZ,
791 [UNIT_MB]
792 = LANG_MEGABYTE,
793 [UNIT_KBIT]
794 = VOICE_KBIT_PER_SEC,
795 [UNIT_PM_TICK]
796 = VOICE_PM_UNITS_PER_TICK,
799 if (talk_temp_disable_count > 0)
800 return -1; /* talking has been disabled */
801 #if CONFIG_CODEC != SWCODEC
802 if (audio_status()) /* busy, buffer in use */
803 return -1;
804 #endif
806 /* special case for time duration */
807 if (unit == UNIT_TIME || unit == UNIT_TIME_EXACT)
808 return talk_time_unit(n, unit == UNIT_TIME_EXACT, enqueue);
810 if (unit < 0 || unit >= UNIT_LAST)
811 unit_id = -1;
812 else
813 unit_id = unit_voiced[unit];
815 if ((n==1 || n==-1) /* singular? */
816 && unit_id >= VOICE_SECONDS && unit_id <= VOICE_HOURS)
818 unit_id--; /* use the singular for those units which have */
821 /* special case with a "plus" before */
822 if (n > 0 && (unit == UNIT_SIGNED || unit == UNIT_DB))
824 talk_id(VOICE_PLUS, enqueue);
825 enqueue = true;
828 talk_number(n, enqueue); /* say the number */
829 talk_id(unit_id, true); /* say the unit, if any */
831 return 0;
834 /* spell a string */
835 int talk_spell(const char* spell, bool enqueue)
837 char c; /* currently processed char */
839 if (talk_temp_disable_count > 0)
840 return -1; /* talking has been disabled */
841 #if CONFIG_CODEC != SWCODEC
842 if (audio_status()) /* busy, buffer in use */
843 return -1;
844 #endif
846 if (!enqueue)
847 talk_shutup(); /* cut off all the pending stuff */
849 while ((c = *spell++) != '\0')
851 /* if this grows into too many cases, I should use a table */
852 if (c >= 'A' && c <= 'Z')
853 talk_id(VOICE_CHAR_A + c - 'A', true);
854 else if (c >= 'a' && c <= 'z')
855 talk_id(VOICE_CHAR_A + c - 'a', true);
856 else if (c >= '0' && c <= '9')
857 talk_id(VOICE_ZERO + c - '0', true);
858 else if (c == '-')
859 talk_id(VOICE_MINUS, true);
860 else if (c == '+')
861 talk_id(VOICE_PLUS, true);
862 else if (c == '.')
863 talk_id(VOICE_DOT, true);
864 else if (c == ' ')
865 talk_id(VOICE_PAUSE, true);
868 return 0;
871 void talk_disable(bool disable)
873 if (disable)
874 talk_temp_disable_count++;
875 else
876 talk_temp_disable_count--;
879 #if CONFIG_RTC
880 void talk_date(struct tm *tm, bool enqueue)
882 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, enqueue);
883 talk_number(tm->tm_mday, true);
884 talk_number(1900 + tm->tm_year, true);
887 void talk_time(struct tm *tm, bool enqueue)
889 if (global_settings.timeformat == 1)
891 /* Voice the hour */
892 long am_pm_id = VOICE_AM;
893 int hour = tm->tm_hour;
894 if (hour >= 12)
896 am_pm_id = VOICE_PM;
897 hour -= 12;
899 if (hour == 0)
900 hour = 12;
901 talk_number(hour, enqueue);
903 /* Voice the minutes */
904 if (tm->tm_min == 0)
906 /* Say o'clock if the minute is 0. */
907 talk_id(VOICE_OCLOCK, true);
909 else
911 /* Pronounce the leading 0 */
912 if(tm->tm_min < 10)
913 talk_id(VOICE_OH, true);
914 talk_number(tm->tm_min, true);
916 talk_id(am_pm_id, true);
918 else
920 /* Voice the time in 24 hour format */
921 talk_number(tm->tm_hour, enqueue);
922 if (tm->tm_min == 0)
924 talk_id(VOICE_HUNDRED, true);
925 talk_id(VOICE_HOUR, true);
927 else
929 /* Pronounce the leading 0 */
930 if(tm->tm_min < 10)
931 talk_id(VOICE_OH, true);
932 talk_number(tm->tm_min, true);
937 #endif