codec_seek_buffer_callback(): Check whether it's possible to seek or not
[Rockbox.git] / apps / talk.c
blob0c8fafbe59c964e54152f7c80021743297a0d5fe
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 | |------------
60 | | codec swap
61 audiobufend----------+-----------+------------
63 SWCODEC allocates dedicated buffers, MASCODEC reuses audiobuf. */
66 /***************** Constants *****************/
68 #define QUEUE_SIZE 64 /* must be a power of two */
69 #define QUEUE_MASK (QUEUE_SIZE-1)
70 const char* const dir_thumbnail_name = "_dirname.talk";
71 const char* const file_thumbnail_ext = ".talk";
73 /***************** Functional Macros *****************/
75 #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
77 #define LOADED_MASK 0x80000000 /* MSB */
79 #if CONFIG_CODEC == SWCODEC
80 #define MAX_THUMBNAIL_BUFSIZE 0x10000
81 #endif
83 /***************** Data types *****************/
85 struct clip_entry /* one entry of the index table */
87 int offset; /* offset from start of voicefile file */
88 int size; /* size of the clip */
91 struct voicefile /* file format of our voice file */
93 int version; /* version of the voicefile */
94 int target_id; /* the rockbox target the file was made for */
95 int table; /* offset to index table, (=header size) */
96 int id1_max; /* number of "normal" clips contained in above index */
97 int id2_max; /* number of "voice only" clips contained in above index */
98 struct clip_entry index[]; /* followed by the index tables */
99 /* and finally the mp3 clips, not visible here, bitswapped
100 for SH based players */
103 struct queue_entry /* one entry of the internal queue */
105 unsigned char* buf;
106 long len;
110 /***************** Globals *****************/
112 static unsigned char* p_thumbnail = NULL; /* buffer for thumbnail */
113 static long size_for_thumbnail; /* leftover buffer size for it */
114 static struct voicefile* p_voicefile; /* loaded voicefile */
115 static bool has_voicefile; /* a voicefile file is present */
116 static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
117 static bool force_enqueue_next; /* enqueue next utterance even if enqueue is false */
118 static int queue_write; /* write index of queue, by application */
119 static int queue_read; /* read index of queue, by ISR context */
120 static int sent; /* how many bytes handed over to playback, owned by ISR */
121 static unsigned char curr_hd[3]; /* current frame header, for re-sync */
122 static int filehandle = -1; /* global, so the MMC variant can keep the file open */
123 static unsigned char* p_silence; /* VOICE_PAUSE clip, used for termination */
124 static long silence_len; /* length of the VOICE_PAUSE clip */
125 static unsigned char* p_lastclip; /* address of latest clip, for silence add */
126 static unsigned long voicefile_size = 0; /* size of the loaded voice file */
127 static unsigned char last_lang[MAX_FILENAME+1]; /* name of last used lang file (in talk_init) */
128 static bool talk_initialized; /* true if talk_init has been called */
129 static int talk_temp_disable_count; /* if positive, temporarily disable voice UI (not saved) */
132 /***************** Private implementation *****************/
134 static int open_voicefile(void)
136 char buf[64];
137 char* p_lang = "english"; /* default */
139 if ( global_settings.lang_file[0] &&
140 global_settings.lang_file[0] != 0xff )
141 { /* try to open the voice file of the selected language */
142 p_lang = (char *)global_settings.lang_file;
145 snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang);
147 return open(buf, O_RDONLY);
151 /* fetch a clip from the voice file */
152 static unsigned char* get_clip(long id, long* p_size)
154 long clipsize;
155 unsigned char* clipbuf;
157 if (id > VOICEONLY_DELIMITER)
158 { /* voice-only entries use the second part of the table */
159 id -= VOICEONLY_DELIMITER + 1;
160 if (id >= p_voicefile->id2_max)
161 return NULL; /* must be newer than we have */
162 id += p_voicefile->id1_max; /* table 2 is behind table 1 */
164 else
165 { /* normal use of the first table */
166 if (id >= p_voicefile->id1_max)
167 return NULL; /* must be newer than we have */
170 clipsize = p_voicefile->index[id].size;
171 if (clipsize == 0) /* clip not included in voicefile */
172 return NULL;
173 clipbuf = (unsigned char *) p_voicefile + p_voicefile->index[id].offset;
175 #ifdef HAVE_MMC /* dynamic loading, on demand */
176 if (!(clipsize & LOADED_MASK))
177 { /* clip used for the first time, needs loading */
178 lseek(filehandle, p_voicefile->index[id].offset, SEEK_SET);
179 if (read(filehandle, clipbuf, clipsize) != clipsize)
180 return NULL; /* read error */
182 p_voicefile->index[id].size |= LOADED_MASK; /* mark as loaded */
184 else
185 { /* clip is in memory already */
186 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
188 #endif
190 *p_size = clipsize;
191 return clipbuf;
195 /* load the voice file into the mp3 buffer */
196 static void load_voicefile(void)
198 int load_size;
199 int got_size;
200 int file_size;
201 #ifdef ROCKBOX_LITTLE_ENDIAN
202 int i;
203 #endif
205 filehandle = open_voicefile();
206 if (filehandle < 0) /* failed to open */
207 goto load_err;
209 file_size = filesize(filehandle);
210 if (file_size > audiobufend - audiobuf) /* won't fit? */
211 goto load_err;
213 #ifdef HAVE_MMC /* load only the header for now */
214 load_size = offsetof(struct voicefile, index);
215 #else /* load the full file */
216 load_size = file_size;
217 #endif
219 got_size = read(filehandle, audiobuf, load_size);
220 if (got_size != load_size /* failure */)
221 goto load_err;
223 #ifdef ROCKBOX_LITTLE_ENDIAN
224 logf("Byte swapping voice file");
225 structec_convert(audiobuf, "lllll", 1, true);
226 #endif
228 if (((struct voicefile*)audiobuf)->table /* format check */
229 == offsetof(struct voicefile, index))
231 p_voicefile = (struct voicefile*)audiobuf;
233 if (p_voicefile->target_id != TARGET_ID)
235 logf("Incompatible voice file (wrong target)");
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 /* Are more voice clips queued and waiting? */
284 bool is_voice_queued()
286 return (QUEUE_LEVEL != 0);
290 /* called in ISR context if mp3 data got consumed */
291 static void mp3_callback(unsigned char** start, size_t* size)
293 queue[queue_read].len -= sent; /* we completed this */
294 queue[queue_read].buf += sent;
296 if (queue[queue_read].len > 0) /* current clip not finished? */
297 { /* feed the next 64K-1 chunk */
298 #if CONFIG_CODEC != SWCODEC
299 sent = MIN(queue[queue_read].len, 0xFFFF);
300 #else
301 sent = queue[queue_read].len;
302 #endif
303 *start = queue[queue_read].buf;
304 *size = sent;
305 return;
307 else if (sent > 0) /* go to next entry */
309 queue_read = (queue_read + 1) & QUEUE_MASK;
312 re_check:
314 if (QUEUE_LEVEL != 0) /* queue is not empty? */
315 { /* start next clip */
316 #if CONFIG_CODEC != SWCODEC
317 sent = MIN(queue[queue_read].len, 0xFFFF);
318 #else
319 sent = queue[queue_read].len;
320 #endif
321 *start = p_lastclip = queue[queue_read].buf;
322 *size = sent;
323 curr_hd[0] = p_lastclip[1];
324 curr_hd[1] = p_lastclip[2];
325 curr_hd[2] = p_lastclip[3];
327 else if (p_silence != NULL /* silence clip available */
328 && p_lastclip != p_silence /* previous clip wasn't silence */
329 && p_lastclip != p_thumbnail) /* ..or thumbnail */
330 { /* add silence clip when queue runs empty playing a voice clip */
331 queue[queue_write].buf = p_silence;
332 queue[queue_write].len = silence_len;
333 queue_write = (queue_write + 1) & QUEUE_MASK;
335 goto re_check;
337 else
339 *size = 0; /* end of data */
343 /***************** Public routines *****************/
345 /* stop the playback and the pending clips */
346 void talk_force_shutup(void)
348 /* Most of this is MAS only */
349 #if CONFIG_CODEC != SWCODEC
350 unsigned char* pos;
351 unsigned char* search;
352 unsigned char* end;
353 if (QUEUE_LEVEL == 0) /* has ended anyway */
354 return;
356 #if CONFIG_CPU == SH7034
357 CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
358 #endif /* CONFIG_CPU == SH7034 */
359 /* search next frame boundary and continue up to there */
360 pos = search = mp3_get_pos();
361 end = queue[queue_read].buf + queue[queue_read].len;
363 if (pos >= queue[queue_read].buf
364 && pos <= end) /* really our clip? */
365 { /* (for strange reasons this isn't nesessarily the case) */
366 /* find the next frame boundary */
367 while (search < end) /* search the remaining data */
369 if (*search++ != 0xFF) /* quick search for frame sync byte */
370 continue; /* (this does the majority of the job) */
372 /* look at the (bitswapped) rest of header candidate */
373 if (search[0] == curr_hd[0] /* do the quicker checks first */
374 && search[2] == curr_hd[2]
375 && (search[1] & 0x30) == (curr_hd[1] & 0x30)) /* sample rate */
377 search--; /* back to the sync byte */
378 break; /* From looking at it, this is our header. */
382 if (search-pos)
383 { /* play old data until the frame end, to keep the MAS in sync */
384 sent = search-pos;
386 queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */
387 queue[queue_read].len = sent; /* current one ends after this */
389 #if CONFIG_CPU == SH7034
390 DTCR3 = sent; /* let the DMA finish this frame */
391 CHCR3 |= 0x0001; /* re-enable DMA */
392 #endif /* CONFIG_CPU == SH7034 */
393 return;
396 #endif /* CONFIG_CODEC != SWCODEC */
398 /* Either SWCODEC, or MAS had nothing to do (was frame boundary or not our clip) */
399 mp3_play_stop();
400 queue_write = queue_read = 0; /* reset the queue */
401 return;
404 /* Shutup the voice, except if force_enqueue_next is set. */
405 void talk_shutup(void)
407 if (!force_enqueue_next)
408 talk_force_shutup();
411 /* schedule a clip, at the end or discard the existing queue */
412 static void queue_clip(unsigned char* buf, long size, bool enqueue)
414 int queue_level;
416 if (!enqueue)
417 talk_shutup(); /* cut off all the pending stuff */
418 /* Something is being enqueued, force_enqueue_next override is no
419 longer in effect. */
420 force_enqueue_next = false;
422 if (!size)
423 return; /* safety check */
424 #if CONFIG_CPU == SH7034
425 /* disable the DMA temporarily, to be safe of race condition */
426 CHCR3 &= ~0x0001;
427 #endif
428 queue_level = QUEUE_LEVEL; /* check old level */
430 if (queue_level < QUEUE_SIZE - 1) /* space left? */
432 queue[queue_write].buf = buf; /* populate an entry */
433 queue[queue_write].len = size;
434 queue_write = (queue_write + 1) & QUEUE_MASK;
437 if (queue_level == 0)
438 { /* queue was empty, we have to do the initial start */
439 p_lastclip = buf;
440 #if CONFIG_CODEC != SWCODEC
441 sent = MIN(size, 0xFFFF); /* DMA can do no more */
442 #else
443 sent = size;
444 #endif
445 mp3_play_data(buf, sent, mp3_callback);
446 curr_hd[0] = buf[1];
447 curr_hd[1] = buf[2];
448 curr_hd[2] = buf[3];
449 mp3_play_pause(true); /* kickoff audio */
451 else
453 #if CONFIG_CPU == SH7034
454 CHCR3 |= 0x0001; /* re-enable DMA */
455 #endif
458 return;
462 /* common code for talk_init() and talk_buffer_steal() */
463 static void reset_state(void)
465 queue_write = queue_read = 0; /* reset the queue */
466 p_voicefile = NULL; /* indicate no voicefile (trashed) */
467 #if CONFIG_CODEC == SWCODEC
468 /* Allocate a dedicated thumbnail buffer - once */
469 if (p_thumbnail == NULL)
471 size_for_thumbnail = audiobufend - audiobuf;
472 if (size_for_thumbnail > MAX_THUMBNAIL_BUFSIZE)
473 size_for_thumbnail = MAX_THUMBNAIL_BUFSIZE;
474 p_thumbnail = buffer_alloc(size_for_thumbnail);
476 #else
477 /* Just use the audiobuf, without allocating anything */
478 p_thumbnail = audiobuf;
479 size_for_thumbnail = audiobufend - audiobuf;
480 #endif
481 p_silence = NULL; /* pause clip not accessible */
485 /***************** Public implementation *****************/
487 void talk_init(void)
489 talk_temp_disable_count = 0;
490 if (talk_initialized && !strcasecmp(last_lang, global_settings.lang_file))
492 /* not a new file, nothing to do */
493 return;
496 #ifdef HAVE_MMC
497 if (filehandle >= 0) /* MMC: An old voice file might still be open */
499 close(filehandle);
500 filehandle = -1;
502 #endif
504 talk_initialized = true;
505 strncpy((char *) last_lang, (char *)global_settings.lang_file,
506 MAX_FILENAME);
508 #if CONFIG_CODEC == SWCODEC
509 audio_get_buffer(false, NULL); /* Must tell audio to reinitialize */
510 #endif
511 reset_state(); /* use this for most of our inits */
513 filehandle = open_voicefile();
514 has_voicefile = (filehandle >= 0); /* test if we can open it */
515 voicefile_size = 0;
517 if (has_voicefile)
519 voicefile_size = filesize(filehandle);
520 close(filehandle); /* close again, this was just to detect presence */
521 filehandle = -1;
526 #if CONFIG_CODEC == SWCODEC
527 /* return if a voice codec is required or not */
528 bool talk_voice_required(void)
530 return (voicefile_size != 0) /* Voice file is available */
531 || (global_settings.talk_dir_clip) /* Thumbnail clips are required */
532 || (global_settings.talk_file_clip);
534 #endif
536 /* return size of voice file */
537 int talk_get_bufsize(void)
539 return voicefile_size;
542 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
543 void talk_buffer_steal(void)
545 #if CONFIG_CODEC != SWCODEC
546 mp3_play_stop();
547 #endif
548 #ifdef HAVE_MMC
549 if (filehandle >= 0) /* only relevant for MMC */
551 close(filehandle);
552 filehandle = -1;
554 #endif
555 reset_state();;
559 /* play a voice ID from voicefile */
560 int talk_id(long id, bool enqueue)
562 long clipsize;
563 unsigned char* clipbuf;
564 int unit;
566 if (talk_temp_disable_count > 0)
567 return -1; /* talking has been disabled */
568 #if CONFIG_CODEC != SWCODEC
569 if (audio_status()) /* busy, buffer in use */
570 return -1;
571 #endif
573 if (p_voicefile == NULL && has_voicefile)
574 load_voicefile(); /* reload needed */
576 if (p_voicefile == NULL) /* still no voices? */
577 return -1;
579 if (id == -1) /* -1 is an indication for silence */
580 return -1;
582 /* check if this is a special ID, with a value */
583 unit = ((unsigned long)id) >> UNIT_SHIFT;
584 if (unit)
585 { /* sign-extend the value */
586 id = (unsigned long)id << (32-UNIT_SHIFT);
587 id >>= (32-UNIT_SHIFT);
588 talk_value(id, unit, 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;
601 /* Speaks zero or more IDs (from an array). */
602 int talk_idarray(long *ids, bool enqueue)
604 int r;
605 if(!ids)
606 return 0;
607 while(*ids != TALK_FINAL_ID)
609 if((r = talk_id(*ids++, enqueue)) <0)
610 return r;
611 enqueue = true;
613 return 0;
616 /* Make sure the current utterance is not interrupted by the next one. */
617 void talk_force_enqueue_next(void)
619 force_enqueue_next = true;
622 /* play a thumbnail from file */
623 int talk_file(const char* filename, bool enqueue)
625 int fd;
626 int size;
627 struct mp3entry info;
629 if (talk_temp_disable_count > 0)
630 return -1; /* talking has been disabled */
631 #if CONFIG_CODEC != SWCODEC
632 if (audio_status()) /* busy, buffer in use */
633 return -1;
634 #endif
636 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
637 return -1;
639 if(mp3info(&info, filename)) /* use this to find real start */
641 return 0; /* failed to open, or invalid */
644 fd = open(filename, O_RDONLY);
645 if (fd < 0) /* failed to open */
647 return 0;
650 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
652 size = read(fd, p_thumbnail, size_for_thumbnail);
653 close(fd);
655 /* ToDo: find audio, skip ID headers and trailers */
657 if (size != 0 && size != size_for_thumbnail) /* Don't play missing or truncated clips */
659 #if CONFIG_CODEC != SWCODEC
660 bitswap(p_thumbnail, size);
661 #endif
662 queue_clip(p_thumbnail, size, enqueue);
665 return size;
669 /* say a numeric value, this word ordering works for english,
670 but not necessarily for other languages (e.g. german) */
671 int talk_number(long n, bool enqueue)
673 int level = 2; /* mille count */
674 long mil = 1000000000; /* highest possible "-illion" */
676 if (talk_temp_disable_count > 0)
677 return -1; /* talking has been disabled */
678 #if CONFIG_CODEC != SWCODEC
679 if (audio_status()) /* busy, buffer in use */
680 return -1;
681 #endif
683 if (!enqueue)
684 talk_shutup(); /* cut off all the pending stuff */
686 if (n==0)
687 { /* special case */
688 talk_id(VOICE_ZERO, true);
689 return 0;
692 if (n<0)
694 talk_id(VOICE_MINUS, true);
695 n = -n;
698 while (n)
700 int segment = n / mil; /* extract in groups of 3 digits */
701 n -= segment * mil; /* remove the used digits from number */
702 mil /= 1000; /* digit place for next round */
704 if (segment)
706 int hundreds = segment / 100;
707 int ones = segment % 100;
709 if (hundreds)
711 talk_id(VOICE_ZERO + hundreds, true);
712 talk_id(VOICE_HUNDRED, true);
715 /* combination indexing */
716 if (ones > 20)
718 int tens = ones/10 + 18;
719 talk_id(VOICE_ZERO + tens, true);
720 ones %= 10;
723 /* direct indexing */
724 if (ones)
725 talk_id(VOICE_ZERO + ones, true);
727 /* add billion, million, thousand */
728 if (mil)
729 talk_id(VOICE_THOUSAND + level, true);
731 level--;
734 return 0;
737 /* singular/plural aware saying of a value */
738 int talk_value(long n, int unit, bool enqueue)
740 int unit_id;
741 static const int unit_voiced[] =
742 { /* lookup table for the voice ID of the units */
743 [0 ... UNIT_LAST-1] = -1, /* regular ID, int, signed */
744 [UNIT_MS]
745 = VOICE_MILLISECONDS, /* here come the "real" units */
746 [UNIT_SEC]
747 = VOICE_SECONDS,
748 [UNIT_MIN]
749 = VOICE_MINUTES,
750 [UNIT_HOUR]
751 = VOICE_HOURS,
752 [UNIT_KHZ]
753 = VOICE_KHZ,
754 [UNIT_DB]
755 = VOICE_DB,
756 [UNIT_PERCENT]
757 = VOICE_PERCENT,
758 [UNIT_MAH]
759 = VOICE_MILLIAMPHOURS,
760 [UNIT_PIXEL]
761 = VOICE_PIXEL,
762 [UNIT_PER_SEC]
763 = VOICE_PER_SEC,
764 [UNIT_HERTZ]
765 = VOICE_HERTZ,
766 [UNIT_MB]
767 = LANG_MEGABYTE,
768 [UNIT_KBIT]
769 = VOICE_KBIT_PER_SEC,
770 [UNIT_PM_TICK]
771 = VOICE_PM_UNITS_PER_TICK,
774 if (talk_temp_disable_count > 0)
775 return -1; /* talking has been disabled */
776 #if CONFIG_CODEC != SWCODEC
777 if (audio_status()) /* busy, buffer in use */
778 return -1;
779 #endif
781 if (unit < 0 || unit >= UNIT_LAST)
782 unit_id = -1;
783 else
784 unit_id = unit_voiced[unit];
786 if ((n==1 || n==-1) /* singular? */
787 && unit_id >= VOICE_SECONDS && unit_id <= VOICE_HOURS)
789 unit_id--; /* use the singular for those units which have */
792 /* special case with a "plus" before */
793 if (n > 0 && (unit == UNIT_SIGNED || unit == UNIT_DB))
795 talk_id(VOICE_PLUS, enqueue);
796 enqueue = true;
799 talk_number(n, enqueue); /* say the number */
800 talk_id(unit_id, true); /* say the unit, if any */
802 return 0;
805 /* spell a string */
806 int talk_spell(const char* spell, bool enqueue)
808 char c; /* currently processed char */
810 if (talk_temp_disable_count > 0)
811 return -1; /* talking has been disabled */
812 #if CONFIG_CODEC != SWCODEC
813 if (audio_status()) /* busy, buffer in use */
814 return -1;
815 #endif
817 if (!enqueue)
818 talk_shutup(); /* cut off all the pending stuff */
820 while ((c = *spell++) != '\0')
822 /* if this grows into too many cases, I should use a table */
823 if (c >= 'A' && c <= 'Z')
824 talk_id(VOICE_CHAR_A + c - 'A', true);
825 else if (c >= 'a' && c <= 'z')
826 talk_id(VOICE_CHAR_A + c - 'a', true);
827 else if (c >= '0' && c <= '9')
828 talk_id(VOICE_ZERO + c - '0', true);
829 else if (c == '-')
830 talk_id(VOICE_MINUS, true);
831 else if (c == '+')
832 talk_id(VOICE_PLUS, true);
833 else if (c == '.')
834 talk_id(VOICE_DOT, true);
835 else if (c == ' ')
836 talk_id(VOICE_PAUSE, true);
839 return 0;
842 void talk_disable(bool disable)
844 if (disable)
845 talk_temp_disable_count++;
846 else
847 talk_temp_disable_count--;
850 #if CONFIG_RTC
851 void talk_date(struct tm *tm, bool enqueue)
853 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, enqueue);
854 talk_number(tm->tm_mday, true);
855 talk_number(1900 + tm->tm_year, true);
858 void talk_time(struct tm *tm, bool enqueue)
860 if (global_settings.timeformat == 1)
862 /* Voice the hour */
863 long am_pm_id = VOICE_AM;
864 int hour = tm->tm_hour;
865 if (hour >= 12)
867 am_pm_id = VOICE_PM;
868 hour -= 12;
870 if (hour == 0)
871 hour = 12;
872 talk_number(hour, enqueue);
874 /* Voice the minutes */
875 if (tm->tm_min == 0)
877 /* Say o'clock if the minute is 0. */
878 talk_id(VOICE_OCLOCK, true);
880 else
882 /* Pronounce the leading 0 */
883 if(tm->tm_min < 10)
884 talk_id(VOICE_OH, true);
885 talk_number(tm->tm_min, true);
887 talk_id(am_pm_id, true);
889 else
891 /* Voice the time in 24 hour format */
892 talk_number(tm->tm_hour, enqueue);
893 if (tm->tm_min == 0)
895 talk_id(VOICE_HUNDRED, true);
896 talk_id(VOICE_HOUR, true);
898 else
900 /* Pronounce the leading 0 */
901 if(tm->tm_min < 10)
902 talk_id(VOICE_OH, true);
903 talk_number(tm->tm_min, true);
908 #endif