Extend HttpGet class to allow downloading a file to a buffer instead of a file.
[Rockbox.git] / apps / talk.c
blobef8c22225c35c98cc4175f66a2cad6e08000127e
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 32768
81 #endif
83 #ifndef SIMULATOR
84 extern bool audio_is_initialized;
85 #endif
87 /***************** Data types *****************/
89 struct clip_entry /* one entry of the index table */
91 int offset; /* offset from start of voicefile file */
92 int size; /* size of the clip */
95 struct voicefile /* file format of our voice file */
97 int version; /* version of the voicefile */
98 int target_id; /* the rockbox target the file was made for */
99 int table; /* offset to index table, (=header size) */
100 int id1_max; /* number of "normal" clips contained in above index */
101 int id2_max; /* number of "voice only" clips contained in above index */
102 struct clip_entry index[]; /* followed by the index tables */
103 /* and finally the mp3 clips, not visible here, bitswapped
104 for SH based players */
107 struct queue_entry /* one entry of the internal queue */
109 unsigned char* buf;
110 long len;
114 /***************** Globals *****************/
116 static unsigned char* p_thumbnail = NULL; /* buffer for thumbnail */
117 static long size_for_thumbnail; /* leftover buffer size for it */
118 static struct voicefile* p_voicefile; /* loaded voicefile */
119 static bool has_voicefile; /* a voicefile file is present */
120 static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
121 /* enqueue next utterance even if enqueue is false. */
122 static bool force_enqueue_next;
123 static int queue_write; /* write index of queue, by application */
124 static int queue_read; /* read index of queue, by ISR context */
125 static int sent; /* how many bytes handed over to playback, owned by ISR */
126 static unsigned char curr_hd[3]; /* current frame header, for re-sync */
127 static int filehandle = -1; /* global, so the MMC variant can keep the file open */
128 static unsigned char* p_silence; /* VOICE_PAUSE clip, used for termination */
129 static long silence_len; /* length of the VOICE_PAUSE clip */
130 static unsigned char* p_lastclip; /* address of latest clip, for silence add */
131 static unsigned long voicefile_size = 0; /* size of the loaded voice file */
132 static unsigned char last_lang[MAX_FILENAME+1]; /* name of last used lang file (in talk_init) */
133 static bool talk_initialized; /* true if talk_init has been called */
134 static int talk_menu_disable; /* if non-zero, temporarily disable voice UI (not saved) */
136 /***************** Private prototypes *****************/
138 static void load_voicefile(void);
139 static void mp3_callback(unsigned char** start, size_t* size);
140 static int queue_clip(unsigned char* buf, long size, bool enqueue);
141 static int open_voicefile(void);
142 static unsigned char* get_clip(long id, long* p_size);
143 static int shutup(void); /* Interrupt voice, as when enqueue is false */
144 static int do_shutup(void); /* kill voice unconditionally */
146 /***************** Private implementation *****************/
148 static int open_voicefile(void)
150 char buf[64];
151 char* p_lang = "english"; /* default */
153 if ( global_settings.lang_file[0] &&
154 global_settings.lang_file[0] != 0xff )
155 { /* try to open the voice file of the selected language */
156 p_lang = (char *)global_settings.lang_file;
159 snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang);
161 return open(buf, O_RDONLY);
165 /* load the voice file into the mp3 buffer */
166 static void load_voicefile(void)
168 int load_size;
169 int got_size;
170 int file_size;
171 #ifdef ROCKBOX_LITTLE_ENDIAN
172 int i;
173 #endif
175 filehandle = open_voicefile();
176 if (filehandle < 0) /* failed to open */
177 goto load_err;
179 file_size = filesize(filehandle);
180 if (file_size > audiobufend - audiobuf) /* won't fit? */
181 goto load_err;
183 #ifdef HAVE_MMC /* load only the header for now */
184 load_size = offsetof(struct voicefile, index);
185 #else /* load the full file */
186 load_size = file_size;
187 #endif
189 got_size = read(filehandle, audiobuf, load_size);
190 if (got_size != load_size /* failure */)
191 goto load_err;
193 #ifdef ROCKBOX_LITTLE_ENDIAN
194 logf("Byte swapping voice file");
195 structec_convert(audiobuf, "lllll", 1, true);
196 #endif
198 if (((struct voicefile*)audiobuf)->table /* format check */
199 == offsetof(struct voicefile, index))
201 p_voicefile = (struct voicefile*)audiobuf;
203 if (p_voicefile->target_id != TARGET_ID)
205 logf("Incompatible voice file (wrong target)");
206 goto load_err;
208 #if CONFIG_CODEC != SWCODEC
209 /* MASCODEC: now use audiobuf for voice then thumbnail */
210 p_thumbnail = audiobuf + file_size;
211 p_thumbnail += (long)p_thumbnail % 2; /* 16-bit align */
212 size_for_thumbnail = audiobufend - p_thumbnail;
213 #endif
215 else
216 goto load_err;
218 #ifdef ROCKBOX_LITTLE_ENDIAN
219 for (i = 0; i < p_voicefile->id1_max + p_voicefile->id2_max; i++)
220 structec_convert(&p_voicefile->index[i], "ll", 1, true);
221 #endif
223 #ifdef HAVE_MMC
224 /* load the index table, now that we know its size from the header */
225 load_size = (p_voicefile->id1_max + p_voicefile->id2_max)
226 * sizeof(struct clip_entry);
227 got_size = read(filehandle,
228 (unsigned char *) p_voicefile + offsetof(struct voicefile, index), load_size);
229 if (got_size != load_size) /* read error */
230 goto load_err;
231 #else
232 close(filehandle); /* only the MMC variant leaves it open */
233 filehandle = -1;
234 #endif
236 /* make sure to have the silence clip, if available */
237 p_silence = get_clip(VOICE_PAUSE, &silence_len);
239 return;
241 load_err:
242 p_voicefile = NULL;
243 has_voicefile = false; /* don't try again */
244 if (filehandle >= 0)
246 close(filehandle);
247 filehandle = -1;
249 return;
253 /* Are more voice clips queued and waiting? */
254 bool is_voice_queued()
256 return !!QUEUE_LEVEL;
260 /* called in ISR context if mp3 data got consumed */
261 static void mp3_callback(unsigned char** start, size_t* size)
263 queue[queue_read].len -= sent; /* we completed this */
264 queue[queue_read].buf += sent;
266 if (queue[queue_read].len > 0) /* current clip not finished? */
267 { /* feed the next 64K-1 chunk */
268 #if CONFIG_CODEC != SWCODEC
269 sent = MIN(queue[queue_read].len, 0xFFFF);
270 #else
271 sent = queue[queue_read].len;
272 #endif
273 *start = queue[queue_read].buf;
274 *size = sent;
275 return;
277 else if (sent > 0) /* go to next entry */
279 queue_read = (queue_read + 1) & QUEUE_MASK;
282 re_check:
284 if (QUEUE_LEVEL) /* queue is not empty? */
285 { /* start next clip */
286 #if CONFIG_CODEC != SWCODEC
287 sent = MIN(queue[queue_read].len, 0xFFFF);
288 #else
289 sent = queue[queue_read].len;
290 #endif
291 *start = p_lastclip = queue[queue_read].buf;
292 *size = sent;
293 curr_hd[0] = p_lastclip[1];
294 curr_hd[1] = p_lastclip[2];
295 curr_hd[2] = p_lastclip[3];
297 else if (p_silence != NULL /* silence clip available */
298 && p_lastclip != p_silence /* previous clip wasn't silence */
299 && p_lastclip != p_thumbnail) /* ..or thumbnail */
300 { /* add silence clip when queue runs empty playing a voice clip */
301 queue[queue_write].buf = p_silence;
302 queue[queue_write].len = silence_len;
303 queue_write = (queue_write + 1) & QUEUE_MASK;
305 goto re_check;
307 else
309 *size = 0; /* end of data */
313 /* stop the playback and the pending clips */
314 static int do_shutup(void)
316 #if CONFIG_CODEC != SWCODEC
317 unsigned char* pos;
318 unsigned char* search;
319 unsigned char* end;
320 #endif
322 if (QUEUE_LEVEL == 0) /* has ended anyway */
324 #if CONFIG_CODEC == SWCODEC
325 mp3_play_stop();
326 #endif
327 return 0;
329 #if CONFIG_CODEC != SWCODEC
330 #if CONFIG_CPU == SH7034
331 CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
332 #endif
333 /* search next frame boundary and continue up to there */
334 pos = search = mp3_get_pos();
335 end = queue[queue_read].buf + queue[queue_read].len;
337 if (pos >= queue[queue_read].buf
338 && pos <= end) /* really our clip? */
339 { /* (for strange reasons this isn't nesessarily the case) */
340 /* find the next frame boundary */
341 while (search < end) /* search the remaining data */
343 if (*search++ != 0xFF) /* quick search for frame sync byte */
344 continue; /* (this does the majority of the job) */
346 /* look at the (bitswapped) rest of header candidate */
347 if (search[0] == curr_hd[0] /* do the quicker checks first */
348 && search[2] == curr_hd[2]
349 && (search[1] & 0x30) == (curr_hd[1] & 0x30)) /* sample rate */
351 search--; /* back to the sync byte */
352 break; /* From looking at it, this is our header. */
356 if (search-pos)
357 { /* play old data until the frame end, to keep the MAS in sync */
358 sent = search-pos;
360 queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */
361 queue[queue_read].len = sent; /* current one ends after this */
363 #if CONFIG_CPU == SH7034
364 DTCR3 = sent; /* let the DMA finish this frame */
365 CHCR3 |= 0x0001; /* re-enable DMA */
366 #endif
367 return 0;
370 #endif
372 /* nothing to do, was frame boundary or not our clip */
373 mp3_play_stop();
375 queue_write = queue_read = 0; /* reset the queue */
377 return 0;
380 /* Shutup the voice, except if force_enqueue_next is set. */
381 static int shutup(void)
383 if (!force_enqueue_next)
384 return do_shutup();
385 return 0;
388 /* schedule a clip, at the end or discard the existing queue */
389 static int queue_clip(unsigned char* buf, long size, bool enqueue)
391 int queue_level;
393 if (!enqueue)
394 shutup(); /* cut off all the pending stuff */
395 /* Something is being enqueued, force_enqueue_next override is no
396 longer in effect. */
397 force_enqueue_next = false;
399 if (!size)
400 return 0; /* safety check */
401 #if CONFIG_CPU == SH7034
402 /* disable the DMA temporarily, to be safe of race condition */
403 CHCR3 &= ~0x0001;
404 #endif
405 queue_level = QUEUE_LEVEL; /* check old level */
407 if (queue_level < QUEUE_SIZE - 1) /* space left? */
409 queue[queue_write].buf = buf; /* populate an entry */
410 queue[queue_write].len = size;
411 queue_write = (queue_write + 1) & QUEUE_MASK;
414 if (queue_level == 0)
415 { /* queue was empty, we have to do the initial start */
416 p_lastclip = buf;
417 #if CONFIG_CODEC != SWCODEC
418 sent = MIN(size, 0xFFFF); /* DMA can do no more */
419 #else
420 sent = size;
421 #endif
422 mp3_play_data(buf, sent, mp3_callback);
423 curr_hd[0] = buf[1];
424 curr_hd[1] = buf[2];
425 curr_hd[2] = buf[3];
426 mp3_play_pause(true); /* kickoff audio */
428 else
430 #if CONFIG_CPU == SH7034
431 CHCR3 |= 0x0001; /* re-enable DMA */
432 #endif
435 return 0;
438 /* fetch a clip from the voice file */
439 static unsigned char* get_clip(long id, long* p_size)
441 long clipsize;
442 unsigned char* clipbuf;
444 if (id > VOICEONLY_DELIMITER)
445 { /* voice-only entries use the second part of the table */
446 id -= VOICEONLY_DELIMITER + 1;
447 if (id >= p_voicefile->id2_max)
448 return NULL; /* must be newer than we have */
449 id += p_voicefile->id1_max; /* table 2 is behind table 1 */
451 else
452 { /* normal use of the first table */
453 if (id >= p_voicefile->id1_max)
454 return NULL; /* must be newer than we have */
457 clipsize = p_voicefile->index[id].size;
458 if (clipsize == 0) /* clip not included in voicefile */
459 return NULL;
460 clipbuf = (unsigned char *) p_voicefile + p_voicefile->index[id].offset;
462 #ifdef HAVE_MMC /* dynamic loading, on demand */
463 if (!(clipsize & LOADED_MASK))
464 { /* clip used for the first time, needs loading */
465 lseek(filehandle, p_voicefile->index[id].offset, SEEK_SET);
466 if (read(filehandle, clipbuf, clipsize) != clipsize)
467 return NULL; /* read error */
469 p_voicefile->index[id].size |= LOADED_MASK; /* mark as loaded */
471 else
472 { /* clip is in memory already */
473 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
475 #endif
477 *p_size = clipsize;
478 return clipbuf;
482 /* common code for talk_init() and talk_buffer_steal() */
483 static void reset_state(void)
485 queue_write = queue_read = 0; /* reset the queue */
486 p_voicefile = NULL; /* indicate no voicefile (trashed) */
487 #if CONFIG_CODEC == SWCODEC
488 /* Allocate a dedicated thumbnail buffer - once */
489 if (p_thumbnail == NULL)
491 size_for_thumbnail = audiobufend - audiobuf;
492 if (size_for_thumbnail > MAX_THUMBNAIL_BUFSIZE)
493 size_for_thumbnail = MAX_THUMBNAIL_BUFSIZE;
494 p_thumbnail = buffer_alloc(size_for_thumbnail);
496 #else
497 /* Just use the audiobuf, without allocating anything */
498 p_thumbnail = audiobuf;
499 size_for_thumbnail = audiobufend - audiobuf;
500 #endif
501 p_silence = NULL; /* pause clip not accessible */
504 /***************** Public implementation *****************/
506 void talk_init(void)
508 talk_menu_disable = 0;
509 if (talk_initialized && !strcasecmp(last_lang, global_settings.lang_file))
511 /* not a new file, nothing to do */
512 return;
515 #ifdef HAVE_MMC
516 if (filehandle >= 0) /* MMC: An old voice file might still be open */
518 close(filehandle);
519 filehandle = -1;
521 #endif
523 talk_initialized = true;
524 strncpy((char *) last_lang, (char *)global_settings.lang_file,
525 MAX_FILENAME);
527 #if CONFIG_CODEC == SWCODEC
528 audio_get_buffer(false, NULL); /* Must tell audio to reinitialize */
529 #endif
530 reset_state(); /* use this for most of our inits */
532 filehandle = open_voicefile();
533 has_voicefile = (filehandle >= 0); /* test if we can open it */
534 voicefile_size = 0;
536 if (has_voicefile)
538 voicefile_size = filesize(filehandle);
539 close(filehandle); /* close again, this was just to detect presence */
540 filehandle = -1;
545 #if CONFIG_CODEC == SWCODEC
546 /* return if a voice codec is required or not */
547 bool talk_voice_required(void)
549 return (voicefile_size != 0) /* Voice file is available */
550 || (global_settings.talk_dir_clip) /* Thumbnail clips are required */
551 || (global_settings.talk_file_clip);
553 #endif
555 /* return size of voice file */
556 int talk_get_bufsize(void)
558 return voicefile_size;
561 /* somebody else claims the mp3 buffer, e.g. for regular play/record */
562 int talk_buffer_steal(void)
564 #if CONFIG_CODEC != SWCODEC
565 mp3_play_stop();
566 #endif
567 #ifdef HAVE_MMC
568 if (filehandle >= 0) /* only relevant for MMC */
570 close(filehandle);
571 filehandle = -1;
573 #endif
574 reset_state();
576 return 0;
580 /* play a voice ID from voicefile */
581 int talk_id(long id, bool enqueue)
583 long clipsize;
584 unsigned char* clipbuf;
585 int unit;
587 #if CONFIG_CODEC != SWCODEC
588 if (audio_status()) /* busy, buffer in use */
589 return -1;
590 #endif
592 if (p_voicefile == NULL && has_voicefile)
593 load_voicefile(); /* reload needed */
595 if (p_voicefile == NULL) /* still no voices? */
596 return -1;
598 if (id == -1) /* -1 is an indication for silence */
599 return -1;
601 /* check if this is a special ID, with a value */
602 unit = ((unsigned long)id) >> UNIT_SHIFT;
603 if (unit)
604 { /* sign-extend the value */
605 id = (unsigned long)id << (32-UNIT_SHIFT);
606 id >>= (32-UNIT_SHIFT);
607 talk_value(id, unit, enqueue); /* speak it */
608 return 0; /* and stop, end of special case */
611 clipbuf = get_clip(id, &clipsize);
612 if (clipbuf == NULL)
613 return -1; /* not present */
615 queue_clip(clipbuf, clipsize, enqueue);
617 return 0;
620 /* Speaks zero or more IDs (from an array). */
621 int talk_idarray(long *ids, bool enqueue)
623 int r;
624 if(!ids)
625 return 0;
626 while(*ids != TALK_FINAL_ID)
628 if((r = talk_id(*ids++, enqueue)) <0)
629 return r;
630 enqueue = true;
632 return 0;
635 /* Make sure the current utterance is not interrupted by the next one. */
636 void talk_force_enqueue_next(void)
638 force_enqueue_next = true;
641 /* play a thumbnail from file */
642 int talk_file(const char* filename, bool enqueue)
644 int fd;
645 int size;
646 struct mp3entry info;
648 #if CONFIG_CODEC != SWCODEC
649 if (audio_status()) /* busy, buffer in use */
650 return -1;
651 #endif
653 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
654 return -1;
656 if(mp3info(&info, filename, false)) /* use this to find real start */
658 return 0; /* failed to open, or invalid */
661 fd = open(filename, O_RDONLY);
662 if (fd < 0) /* failed to open */
664 return 0;
667 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
669 size = read(fd, p_thumbnail, size_for_thumbnail);
670 close(fd);
672 /* ToDo: find audio, skip ID headers and trailers */
674 if (size != 0 && size != size_for_thumbnail) /* Don't play missing or truncated clips */
676 #if CONFIG_CODEC != SWCODEC
677 bitswap(p_thumbnail, size);
678 #endif
679 queue_clip(p_thumbnail, size, enqueue);
682 return size;
686 /* say a numeric value, this word ordering works for english,
687 but not necessarily for other languages (e.g. german) */
688 int talk_number(long n, bool enqueue)
690 int level = 2; /* mille count */
691 long mil = 1000000000; /* highest possible "-illion" */
693 #if CONFIG_CODEC != SWCODEC
694 if (audio_status()) /* busy, buffer in use */
695 return -1;
696 #endif
698 if (!enqueue)
699 shutup(); /* cut off all the pending stuff */
701 if (n==0)
702 { /* special case */
703 talk_id(VOICE_ZERO, true);
704 return 0;
707 if (n<0)
709 talk_id(VOICE_MINUS, true);
710 n = -n;
713 while (n)
715 int segment = n / mil; /* extract in groups of 3 digits */
716 n -= segment * mil; /* remove the used digits from number */
717 mil /= 1000; /* digit place for next round */
719 if (segment)
721 int hundreds = segment / 100;
722 int ones = segment % 100;
724 if (hundreds)
726 talk_id(VOICE_ZERO + hundreds, true);
727 talk_id(VOICE_HUNDRED, true);
730 /* combination indexing */
731 if (ones > 20)
733 int tens = ones/10 + 18;
734 talk_id(VOICE_ZERO + tens, true);
735 ones %= 10;
738 /* direct indexing */
739 if (ones)
740 talk_id(VOICE_ZERO + ones, true);
742 /* add billion, million, thousand */
743 if (mil)
744 talk_id(VOICE_THOUSAND + level, true);
746 level--;
749 return 0;
752 /* singular/plural aware saying of a value */
753 int talk_value(long n, int unit, bool enqueue)
755 int unit_id;
756 static const int unit_voiced[] =
757 { /* lookup table for the voice ID of the units */
758 [0 ... UNIT_LAST-1] = -1, /* regular ID, int, signed */
759 [UNIT_MS]
760 = VOICE_MILLISECONDS, /* here come the "real" units */
761 [UNIT_SEC]
762 = VOICE_SECONDS,
763 [UNIT_MIN]
764 = VOICE_MINUTES,
765 [UNIT_HOUR]
766 = VOICE_HOURS,
767 [UNIT_KHZ]
768 = VOICE_KHZ,
769 [UNIT_DB]
770 = VOICE_DB,
771 [UNIT_PERCENT]
772 = VOICE_PERCENT,
773 [UNIT_MAH]
774 = VOICE_MILLIAMPHOURS,
775 [UNIT_PIXEL]
776 = VOICE_PIXEL,
777 [UNIT_PER_SEC]
778 = VOICE_PER_SEC,
779 [UNIT_HERTZ]
780 = VOICE_HERTZ,
781 [UNIT_MB]
782 = LANG_MEGABYTE,
783 [UNIT_KBIT]
784 = VOICE_KBIT_PER_SEC,
787 #if CONFIG_CODEC != SWCODEC
788 if (audio_status()) /* busy, buffer in use */
789 return -1;
790 #endif
792 if (unit < 0 || unit >= UNIT_LAST)
793 unit_id = -1;
794 else
795 unit_id = unit_voiced[unit];
797 if ((n==1 || n==-1) /* singular? */
798 && unit_id >= VOICE_SECONDS && unit_id <= VOICE_HOURS)
800 unit_id--; /* use the singular for those units which have */
803 /* special case with a "plus" before */
804 if (n > 0 && (unit == UNIT_SIGNED || unit == UNIT_DB))
806 talk_id(VOICE_PLUS, enqueue);
807 enqueue = true;
810 talk_number(n, enqueue); /* say the number */
811 talk_id(unit_id, true); /* say the unit, if any */
813 return 0;
816 /* spell a string */
817 int talk_spell(const char* spell, bool enqueue)
819 char c; /* currently processed char */
821 #if CONFIG_CODEC != SWCODEC
822 if (audio_status()) /* busy, buffer in use */
823 return -1;
824 #endif
826 if (!enqueue)
827 shutup(); /* cut off all the pending stuff */
829 while ((c = *spell++) != '\0')
831 /* if this grows into too many cases, I should use a table */
832 if (c >= 'A' && c <= 'Z')
833 talk_id(VOICE_CHAR_A + c - 'A', true);
834 else if (c >= 'a' && c <= 'z')
835 talk_id(VOICE_CHAR_A + c - 'a', true);
836 else if (c >= '0' && c <= '9')
837 talk_id(VOICE_ZERO + c - '0', true);
838 else if (c == '-')
839 talk_id(VOICE_MINUS, true);
840 else if (c == '+')
841 talk_id(VOICE_PLUS, true);
842 else if (c == '.')
843 talk_id(VOICE_DOT, true);
844 else if (c == ' ')
845 talk_id(VOICE_PAUSE, true);
848 return 0;
851 bool talk_menus_enabled(void)
853 return (global_settings.talk_menu && talk_menu_disable == 0);
857 void talk_disable_menus(void)
859 talk_menu_disable++;
862 void talk_enable_menus(void)
864 talk_menu_disable--;