blob: b417046a61701008b6c4ba9367634652af5e778c [file] [log] [blame]
Jörg Hohensohnfa97f162004-03-19 22:15:53 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2004 Jörg Hohensohn
11 *
12 * This module collects the Talkbox and voice UI functions.
13 * (Talkbox reads directory names from mp3 clips called thumbnails,
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +000014 * the voice UI lets menus and screens "talk" from a voicefile in memory.
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000015 *
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.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23
24#include <stdio.h>
25#include <stddef.h>
26#include "file.h"
27#include "buffer.h"
28#include "system.h"
Jörg Hohensohn6e77d1f2004-04-06 07:06:59 +000029#include "settings.h"
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000030#include "mp3_playback.h"
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +000031#include "audio.h"
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000032#include "lang.h"
33#include "talk.h"
Jörg Hohensohn3aa99e12004-03-27 00:11:01 +000034#include "id3.h"
Miika Pekkarinen159c52d2005-08-20 11:13:19 +000035#include "logf.h"
Jens Arnold43412112004-09-26 09:25:59 +000036#include "bitswap.h"
Miika Pekkarinen159c52d2005-08-20 11:13:19 +000037#if CONFIG_HWCODEC == MASNONE
38#include "playback.h"
39#endif
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000040
41/***************** Constants *****************/
42
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +000043#define QUEUE_SIZE 64 /* must be a power of two */
44#define QUEUE_MASK (QUEUE_SIZE-1)
Jens Arnold839067b2004-08-01 23:34:44 +000045const char* const dir_thumbnail_name = "_dirname.talk";
Jörg Hohensohn40ae63b2004-10-21 18:34:48 +000046const char* const file_thumbnail_ext = ".talk";
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000047
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +000048/***************** Functional Macros *****************/
49
50#define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK)
51
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +000052#define LOADED_MASK 0x80000000 /* MSB */
53
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000054
55/***************** Data types *****************/
56
57struct clip_entry /* one entry of the index table */
58{
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +000059 int offset; /* offset from start of voicefile file */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000060 int size; /* size of the clip */
61};
62
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +000063struct voicefile /* file format of our voice file */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000064{
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +000065 int version; /* version of the voicefile */
Jörg Hohensohnf9495cb2004-04-03 20:52:24 +000066 int table; /* offset to index table, (=header size) */
67 int id1_max; /* number of "normal" clips contained in above index */
68 int id2_max; /* number of "voice only" clips contained in above index */
69 struct clip_entry index[]; /* followed by the index tables */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000070 /* and finally the bitswapped mp3 clips, not visible here */
71};
72
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000073struct queue_entry /* one entry of the internal queue */
74{
75 unsigned char* buf;
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +000076 long len;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000077};
78
79
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000080/***************** Globals *****************/
81
82static unsigned char* p_thumbnail; /* buffer for thumbnail */
83static long size_for_thumbnail; /* leftover buffer size for it */
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +000084static struct voicefile* p_voicefile; /* loaded voicefile */
85static bool has_voicefile; /* a voicefile file is present */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000086static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */
87static int queue_write; /* write index of queue, by application */
88static int queue_read; /* read index of queue, by ISR context */
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +000089static int sent; /* how many bytes handed over to playback, owned by ISR */
Jörg Hohensohnc898a022004-04-20 21:47:07 +000090static unsigned char curr_hd[3]; /* current frame header, for re-sync */
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +000091static int filehandle; /* global, so the MMC variant can keep the file open */
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +000092static unsigned char* p_silence; /* VOICE_PAUSE clip, used for termination */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +000093static long silence_len; /* length of the VOICE_PAUSE clip */
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +000094static unsigned char* p_lastclip; /* address of latest clip, for silence add */
Miika Pekkarinen159c52d2005-08-20 11:13:19 +000095static unsigned long voicefile_size = 0; /* size of the loaded voice file */
Jörg Hohensohnc898a022004-04-20 21:47:07 +000096
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000097
Jörg Hohensohn590e6af2004-03-21 17:45:45 +000098/***************** Private prototypes *****************/
99
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000100static void load_voicefile(void);
Jörg Hohensohn590e6af2004-03-21 17:45:45 +0000101static void mp3_callback(unsigned char** start, int* size);
102static int shutup(void);
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000103static int queue_clip(unsigned char* buf, long size, bool enqueue);
Jörg Hohensohn6e77d1f2004-04-06 07:06:59 +0000104static int open_voicefile(void);
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000105static unsigned char* get_clip(long id, long* p_size);
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000106
107
108/***************** Private implementation *****************/
109
Jörg Hohensohn6e77d1f2004-04-06 07:06:59 +0000110static int open_voicefile(void)
111{
112 char buf[64];
113 char* p_lang = "english"; /* default */
114
115 if ( global_settings.lang_file[0] &&
116 global_settings.lang_file[0] != 0xff )
117 { /* try to open the voice file of the selected language */
118 p_lang = global_settings.lang_file;
119 }
120
121 snprintf(buf, sizeof(buf), ROCKBOX_DIR LANG_DIR "/%s.voice", p_lang);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000122
Jörg Hohensohn6e77d1f2004-04-06 07:06:59 +0000123 return open(buf, O_RDONLY);
124}
125
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000126int talk_get_bufsize(void)
127{
128 return voicefile_size;
129}
130
131#ifdef SIMULATOR
132static unsigned short BSWAP16(unsigned short value)
133{
134 return (value >> 8) | (value << 8);
135}
136
137static unsigned long BSWAP32(unsigned long value)
138{
139 unsigned long hi = BSWAP16(value >> 16);
140 unsigned long lo = BSWAP16(value & 0xffff);
141 return (lo << 16) | hi;
142}
143#endif
Jörg Hohensohn6e77d1f2004-04-06 07:06:59 +0000144
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000145/* load the voice file into the mp3 buffer */
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000146static void load_voicefile(void)
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000147{
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000148 int load_size;
149 int got_size;
150 int file_size;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000151#if CONFIG_HWCODEC == MASNONE
152 int length, i;
153 unsigned char *buf, temp;
154#endif
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000155
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000156 filehandle = open_voicefile();
157 if (filehandle < 0) /* failed to open */
158 goto load_err;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000159
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000160 file_size = filesize(filehandle);
Linus Nielsen Feltzingd34865a2005-04-05 11:33:58 +0000161 if (file_size > audiobufend - audiobuf) /* won't fit? */
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000162 goto load_err;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000163
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000164#ifdef HAVE_MMC /* load only the header for now */
165 load_size = offsetof(struct voicefile, index);
166#else /* load the full file */
167 load_size = file_size;
168#endif
169
Linus Nielsen Feltzingd34865a2005-04-05 11:33:58 +0000170 got_size = read(filehandle, audiobuf, load_size);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000171 if (got_size != load_size /* failure */)
172 goto load_err;
173
174#ifdef SIMULATOR
175 logf("Byte swapping voice file");
176 p_voicefile = (struct voicefile*)audiobuf;
177 p_voicefile->version = BSWAP32(p_voicefile->version);
178 p_voicefile->table = BSWAP32(p_voicefile->table);
179 p_voicefile->id1_max = BSWAP32(p_voicefile->id1_max);
180 p_voicefile->id2_max = BSWAP32(p_voicefile->id2_max);
181 p_voicefile = NULL;
182#endif
183
184 if (((struct voicefile*)audiobuf)->table /* format check */
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000185 == offsetof(struct voicefile, index))
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000186 {
Linus Nielsen Feltzingd34865a2005-04-05 11:33:58 +0000187 p_voicefile = (struct voicefile*)audiobuf;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000188
189 /* thumbnail buffer is the remaining space behind */
Linus Nielsen Feltzingd34865a2005-04-05 11:33:58 +0000190 p_thumbnail = audiobuf + file_size;
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000191 p_thumbnail += (long)p_thumbnail % 2; /* 16-bit align */
Linus Nielsen Feltzingd34865a2005-04-05 11:33:58 +0000192 size_for_thumbnail = audiobufend - p_thumbnail;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000193 }
194 else
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000195 goto load_err;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000196
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000197#ifdef SIMULATOR
198 for (i = 0; i < p_voicefile->id1_max + p_voicefile->id2_max; i++)
199 {
200 struct clip_entry *ce;
201 ce = &p_voicefile->index[i];
202 ce->offset = BSWAP32(ce->offset);
203 ce->size = BSWAP32(ce->size);
204 }
205#endif
206
207 /* Do a bitswap as necessary. */
208#if CONFIG_HWCODEC == MASNONE
209 logf("Bitswapping voice file.");
210 cpu_boost(true);
211 buf = (unsigned char *)(&p_voicefile->index) +
212 (p_voicefile->id1_max + p_voicefile->id2_max) * sizeof(struct clip_entry);
213 length = file_size - offsetof(struct voicefile, index) -
214 (p_voicefile->id1_max - p_voicefile->id2_max) * sizeof(struct clip_entry);
215
216 for (i = 0; i < length; i++)
217 {
218 temp = buf[i];
219 buf[i] = ((temp >> 7) & 0x01)
220 | ((temp >> 5) & 0x02)
221 | ((temp >> 3) & 0x04)
222 | ((temp >> 1) & 0x08)
223 | ((temp << 1) & 0x10)
224 | ((temp << 3) & 0x20)
225 | ((temp << 5) & 0x40)
226 | ((temp << 7) & 0x80);
227 }
228 cpu_boost(false);
229
230#endif
231
232#ifdef HAVE_MMC
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000233 /* load the index table, now that we know its size from the header */
234 load_size = (p_voicefile->id1_max + p_voicefile->id2_max)
235 * sizeof(struct clip_entry);
236 got_size = read(filehandle,
Linus Nielsen Feltzingd34865a2005-04-05 11:33:58 +0000237 audiobuf + offsetof(struct voicefile, index), load_size);
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000238 if (got_size != load_size) /* read error */
239 goto load_err;
240#else
241 close(filehandle); /* only the MMC variant leaves it open */
242 filehandle = -1;
243#endif
244
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000245 /* make sure to have the silence clip, if available */
246 p_silence = get_clip(VOICE_PAUSE, &silence_len);
247
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000248 return;
249
250load_err:
251 p_voicefile = NULL;
252 has_voicefile = false; /* don't try again */
253 if (filehandle >= 0)
254 {
255 close(filehandle);
256 filehandle = -1;
257 }
258 return;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000259}
260
261
262/* called in ISR context if mp3 data got consumed */
263static void mp3_callback(unsigned char** start, int* size)
264{
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000265 queue[queue_read].len -= sent; /* we completed this */
266 queue[queue_read].buf += sent;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000267
268 if (queue[queue_read].len > 0) /* current clip not finished? */
269 { /* feed the next 64K-1 chunk */
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000270#if CONFIG_HWCODEC != MASNONE
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000271 sent = MIN(queue[queue_read].len, 0xFFFF);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000272#else
273 sent = queue[queue_read].len;
274#endif
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000275 *start = queue[queue_read].buf;
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000276 *size = sent;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000277 return;
278 }
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000279 else if (sent > 0) /* go to next entry */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000280 {
Jens Arnold1a479d62004-11-18 22:59:20 +0000281 queue_read = (queue_read + 1) & QUEUE_MASK;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000282 }
283
Jens Arnold1a479d62004-11-18 22:59:20 +0000284re_check:
285
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000286 if (QUEUE_LEVEL) /* queue is not empty? */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000287 { /* start next clip */
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000288#if CONFIG_HWCODEC != MASNONE
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000289 sent = MIN(queue[queue_read].len, 0xFFFF);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000290#else
291 sent = queue[queue_read].len;
292#endif
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000293 *start = p_lastclip = queue[queue_read].buf;
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000294 *size = sent;
Jens Arnold1a479d62004-11-18 22:59:20 +0000295 curr_hd[0] = p_lastclip[1];
296 curr_hd[1] = p_lastclip[2];
297 curr_hd[2] = p_lastclip[3];
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000298 }
Jens Arnold1a479d62004-11-18 22:59:20 +0000299 else if (p_silence != NULL /* silence clip available */
300 && p_lastclip != p_silence /* previous clip wasn't silence */
301 && p_lastclip < p_thumbnail) /* ..and not a thumbnail */
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000302 { /* add silence clip when queue runs empty playing a voice clip */
Jens Arnold1a479d62004-11-18 22:59:20 +0000303 queue[queue_write].buf = p_silence;
304 queue[queue_write].len = silence_len;
305 queue_write = (queue_write + 1) & QUEUE_MASK;
306
307 goto re_check;
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000308 }
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000309 else
310 {
311 *size = 0; /* end of data */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000312 mp3_play_stop(); /* fixme: should be done by caller */
313 }
314}
315
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000316/* stop the playback and the pending clips, but at frame boundary */
317static int shutup(void)
318{
Jörg Hohensohn590e6af2004-03-21 17:45:45 +0000319 unsigned char* pos;
320 unsigned char* search;
321 unsigned char* end;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000322
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000323 if (QUEUE_LEVEL == 0) /* has ended anyway */
324 {
Jörg Hohensohn590e6af2004-03-21 17:45:45 +0000325 return 0;
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000326 }
Daniel Stenberg003c0d22005-02-02 21:50:24 +0000327#if CONFIG_CPU == SH7034
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000328 CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */
Daniel Stenberg003c0d22005-02-02 21:50:24 +0000329#endif
Jörg Hohensohn590e6af2004-03-21 17:45:45 +0000330 /* search next frame boundary and continue up to there */
331 pos = search = mp3_get_pos();
332 end = queue[queue_read].buf + queue[queue_read].len;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000333
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000334 if (pos >= queue[queue_read].buf
335 && pos <= end) /* really our clip? */
336 { /* (for strange reasons this isn't nesessarily the case) */
337 /* find the next frame boundary */
338 while (search < end) /* search the remaining data */
Jörg Hohensohnb109c1e2004-03-29 08:19:47 +0000339 {
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000340 if (*search++ != 0xFF) /* quick search for frame sync byte */
341 continue; /* (this does the majority of the job) */
342
343 /* look at the (bitswapped) rest of header candidate */
344 if (search[0] == curr_hd[0] /* do the quicker checks first */
345 && search[2] == curr_hd[2]
346 && (search[1] & 0x30) == (curr_hd[1] & 0x30)) /* sample rate */
347 {
348 search--; /* back to the sync byte */
349 break; /* From looking at it, this is our header. */
350 }
351 }
352
353 if (search-pos)
354 { /* play old data until the frame end, to keep the MAS in sync */
355 sent = search-pos;
356
Jens Arnold1a479d62004-11-18 22:59:20 +0000357 queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000358 queue[queue_read].len = sent; /* current one ends after this */
359
Daniel Stenberg003c0d22005-02-02 21:50:24 +0000360#if CONFIG_CPU == SH7034
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000361 DTCR3 = sent; /* let the DMA finish this frame */
362 CHCR3 |= 0x0001; /* re-enable DMA */
Daniel Stenberg003c0d22005-02-02 21:50:24 +0000363#endif
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000364 return 0;
Jörg Hohensohnb109c1e2004-03-29 08:19:47 +0000365 }
Jörg Hohensohn590e6af2004-03-21 17:45:45 +0000366 }
Jörg Hohensohnc49129c2004-04-20 22:11:20 +0000367
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000368 /* nothing to do, was frame boundary or not our clip */
369 mp3_play_stop();
370 queue_write = queue_read = 0; /* reset the queue */
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000371
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000372 return 0;
373}
374
375
376/* schedule a clip, at the end or discard the existing queue */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000377static int queue_clip(unsigned char* buf, long size, bool enqueue)
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000378{
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000379 int queue_level;
380
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000381 if (!enqueue)
382 shutup(); /* cut off all the pending stuff */
383
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000384 if (!size)
385 return 0; /* safety check */
Daniel Stenberg003c0d22005-02-02 21:50:24 +0000386#if CONFIG_CPU == SH7034
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000387 /* disable the DMA temporarily, to be safe of race condition */
388 CHCR3 &= ~0x0001;
Daniel Stenberg003c0d22005-02-02 21:50:24 +0000389#endif
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000390 queue_level = QUEUE_LEVEL; /* check old level */
391
392 if (queue_level < QUEUE_SIZE - 1) /* space left? */
393 {
394 queue[queue_write].buf = buf; /* populate an entry */
395 queue[queue_write].len = size;
Jens Arnold1a479d62004-11-18 22:59:20 +0000396 queue_write = (queue_write + 1) & QUEUE_MASK;
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000397 }
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000398
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000399 if (queue_level == 0)
400 { /* queue was empty, we have to do the initial start */
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000401 p_lastclip = buf;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000402#if CONFIG_HWCODEC != MASNONE
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000403 sent = MIN(size, 0xFFFF); /* DMA can do no more */
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000404#else
405 sent = size;
406#endif
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000407 mp3_play_data(buf, sent, mp3_callback);
Jörg Hohensohnc898a022004-04-20 21:47:07 +0000408 curr_hd[0] = buf[1];
409 curr_hd[1] = buf[2];
410 curr_hd[2] = buf[3];
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000411 mp3_play_pause(true); /* kickoff audio */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000412 }
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000413 else
414 {
Daniel Stenberg003c0d22005-02-02 21:50:24 +0000415#if CONFIG_CPU == SH7034
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000416 CHCR3 |= 0x0001; /* re-enable DMA */
Daniel Stenberg003c0d22005-02-02 21:50:24 +0000417#endif
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000418 }
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000419
420 return 0;
421}
422
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000423/* fetch a clip from the voice file */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000424static unsigned char* get_clip(long id, long* p_size)
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000425{
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000426 long clipsize;
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000427 unsigned char* clipbuf;
428
429 if (id > VOICEONLY_DELIMITER)
430 { /* voice-only entries use the second part of the table */
431 id -= VOICEONLY_DELIMITER + 1;
432 if (id >= p_voicefile->id2_max)
433 return NULL; /* must be newer than we have */
434 id += p_voicefile->id1_max; /* table 2 is behind table 1 */
435 }
436 else
437 { /* normal use of the first table */
438 if (id >= p_voicefile->id1_max)
439 return NULL; /* must be newer than we have */
440 }
441
442 clipsize = p_voicefile->index[id].size;
443 if (clipsize == 0) /* clip not included in voicefile */
444 return NULL;
Linus Nielsen Feltzingd34865a2005-04-05 11:33:58 +0000445 clipbuf = audiobuf + p_voicefile->index[id].offset;
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000446
447#ifdef HAVE_MMC /* dynamic loading, on demand */
448 if (!(clipsize & LOADED_MASK))
449 { /* clip used for the first time, needs loading */
450 lseek(filehandle, p_voicefile->index[id].offset, SEEK_SET);
451 if (read(filehandle, clipbuf, clipsize) != clipsize)
452 return NULL; /* read error */
453
454 p_voicefile->index[id].size |= LOADED_MASK; /* mark as loaded */
455 }
456 else
457 { /* clip is in memory already */
458 clipsize &= ~LOADED_MASK; /* without the extra bit gives true size */
459 }
460#endif
461
462 *p_size = clipsize;
463 return clipbuf;
464}
465
466
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000467/* common code for talk_init() and talk_buffer_steal() */
Jörg Hohensohn3e417802004-08-31 22:13:20 +0000468static void reset_state(void)
469{
470 queue_write = queue_read = 0; /* reset the queue */
471 p_voicefile = NULL; /* indicate no voicefile (trashed) */
Linus Nielsen Feltzingd34865a2005-04-05 11:33:58 +0000472 p_thumbnail = audiobuf; /* whole space for thumbnail */
473 size_for_thumbnail = audiobufend - audiobuf;
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000474 p_silence = NULL; /* pause clip not accessible */
Jörg Hohensohn3e417802004-08-31 22:13:20 +0000475}
476
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000477/***************** Public implementation *****************/
478
479void talk_init(void)
480{
Jörg Hohensohn3e417802004-08-31 22:13:20 +0000481 reset_state(); /* use this for most of our inits */
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000482
483#ifdef HAVE_MMC
484 load_voicefile(); /* load the tables right away */
485 has_voicefile = (p_voicefile != NULL);
486#else
487 filehandle = open_voicefile();
488 has_voicefile = (filehandle >= 0); /* test if we can open it */
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000489 voicefile_size = 0;
490
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000491 if (has_voicefile)
492 {
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000493 voicefile_size = filesize(filehandle);
494#if CONFIG_HWCODEC == MASNONE
495 voice_init();
496#endif
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000497 close(filehandle); /* close again, this was just to detect presence */
498 filehandle = -1;
499 }
500#endif
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000501
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000502}
503
504
505/* somebody else claims the mp3 buffer, e.g. for regular play/record */
506int talk_buffer_steal(void)
507{
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000508 mp3_play_stop();
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000509#ifdef HAVE_MMC
510 if (filehandle >= 0) /* only relevant for MMC */
511 {
512 close(filehandle);
513 filehandle = -1;
514 }
515#endif
Jörg Hohensohn3e417802004-08-31 22:13:20 +0000516 reset_state();
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000517 return 0;
518}
519
520
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000521/* play a voice ID from voicefile */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000522int talk_id(long id, bool enqueue)
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000523{
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000524 long clipsize;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000525 unsigned char* clipbuf;
526 int unit;
527
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000528#if CONFIG_HWCODEC != MASNONE
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +0000529 if (audio_status()) /* busy, buffer in use */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000530 return -1;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000531#endif
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000532
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000533 if (p_voicefile == NULL && has_voicefile)
534 load_voicefile(); /* reload needed */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000535
Jörg Hohensohn2fef5b72004-05-09 09:41:23 +0000536 if (p_voicefile == NULL) /* still no voices? */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000537 return -1;
538
Jörg Hohensohnf9495cb2004-04-03 20:52:24 +0000539 if (id == -1) /* -1 is an indication for silence */
540 return -1;
541
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000542 /* check if this is a special ID, with a value */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000543 unit = ((unsigned long)id) >> UNIT_SHIFT;
Jörg Hohensohnf9495cb2004-04-03 20:52:24 +0000544 if (unit)
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000545 { /* sign-extend the value */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000546 id = (unsigned long)id << (32-UNIT_SHIFT);
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000547 id >>= (32-UNIT_SHIFT);
548 talk_value(id, unit, enqueue); /* speak it */
549 return 0; /* and stop, end of special case */
550 }
551
Jörg Hohensohn24e6dff2004-11-17 22:30:38 +0000552 clipbuf = get_clip(id, &clipsize);
553 if (clipbuf == NULL)
554 return -1; /* not present */
Jörg Hohensohna7aa17a2004-10-12 16:43:22 +0000555
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000556 queue_clip(clipbuf, clipsize, enqueue);
557
558 return 0;
559}
560
561
562/* play a thumbnail from file */
Jens Arnold8fb33612004-08-18 01:09:31 +0000563int talk_file(const char* filename, bool enqueue)
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000564{
565 int fd;
566 int size;
Jörg Hohensohn3aa99e12004-03-27 00:11:01 +0000567 struct mp3entry info;
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000568
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +0000569 if (audio_status()) /* busy, buffer in use */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000570 return -1;
571
572 if (p_thumbnail == NULL || size_for_thumbnail <= 0)
573 return -1;
574
Linus Nielsen Feltzingf4f41112004-07-27 14:10:48 +0000575 if(mp3info(&info, filename, false)) /* use this to find real start */
Jörg Hohensohn3aa99e12004-03-27 00:11:01 +0000576 {
577 return 0; /* failed to open, or invalid */
578 }
579
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000580 fd = open(filename, O_RDONLY);
581 if (fd < 0) /* failed to open */
582 {
583 return 0;
584 }
585
Jörg Hohensohn3aa99e12004-03-27 00:11:01 +0000586 lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */
587
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000588 size = read(fd, p_thumbnail, size_for_thumbnail);
589 close(fd);
590
591 /* ToDo: find audio, skip ID headers and trailers */
592
593 if (size)
594 {
Linus Nielsen Feltzinga12ccab2005-06-06 00:34:07 +0000595#if CONFIG_HWCODEC != MASNONE
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000596 bitswap(p_thumbnail, size);
Linus Nielsen Feltzinga12ccab2005-06-06 00:34:07 +0000597#endif
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000598 queue_clip(p_thumbnail, size, enqueue);
599 }
600
601 return size;
602}
603
604
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000605/* say a numeric value, this word ordering works for english,
606 but not necessarily for other languages (e.g. german) */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000607int talk_number(long n, bool enqueue)
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000608{
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000609 int level = 0; /* mille count */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000610 long mil = 1000000000; /* highest possible "-illion" */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000611
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000612#if CONFIG_HWCODEC != MASNONE
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +0000613 if (audio_status()) /* busy, buffer in use */
Linus Nielsen Feltzing9e11ff82004-03-22 12:34:06 +0000614 return -1;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000615#endif
Linus Nielsen Feltzing9e11ff82004-03-22 12:34:06 +0000616
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000617 if (!enqueue)
618 shutup(); /* cut off all the pending stuff */
619
620 if (n==0)
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000621 { /* special case */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000622 talk_id(VOICE_ZERO, true);
623 return 0;
624 }
625
626 if (n<0)
627 {
628 talk_id(VOICE_MINUS, true);
629 n = -n;
630 }
631
632 while (n)
633 {
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000634 int segment = n / mil; /* extract in groups of 3 digits */
635 n -= segment * mil; /* remove the used digits from number */
636 mil /= 1000; /* digit place for next round */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000637
638 if (segment)
639 {
640 int hundreds = segment / 100;
641 int ones = segment % 100;
642
643 if (hundreds)
644 {
645 talk_id(VOICE_ZERO + hundreds, true);
646 talk_id(VOICE_HUNDRED, true);
647 }
648
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000649 /* combination indexing */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000650 if (ones > 20)
651 {
652 int tens = ones/10 + 18;
653 talk_id(VOICE_ZERO + tens, true);
654 ones %= 10;
655 }
656
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000657 /* direct indexing */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000658 if (ones)
659 talk_id(VOICE_ZERO + ones, true);
660
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000661 /* add billion, million, thousand */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000662 if (mil)
663 talk_id(VOICE_BILLION + level, true);
664 }
665 level++;
666 }
667
668 return 0;
669}
670
Jörg Hohensohnc898a022004-04-20 21:47:07 +0000671/* singular/plural aware saying of a value */
Jean-Philippe Bernardy45e0de32005-02-12 11:26:36 +0000672int talk_value(long n, int unit, bool enqueue)
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000673{
674 int unit_id;
Jens Arnold033ba162004-08-26 20:30:22 +0000675 static const int unit_voiced[] =
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000676 { /* lookup table for the voice ID of the units */
677 -1, -1, -1, /* regular ID, int, signed */
678 VOICE_MILLISECONDS, /* here come the "real" units */
679 VOICE_SECONDS,
680 VOICE_MINUTES,
681 VOICE_HOURS,
682 VOICE_KHZ,
683 VOICE_DB,
684 VOICE_PERCENT,
685 VOICE_MEGABYTE,
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000686 VOICE_GIGABYTE,
687 VOICE_MILLIAMPHOURS,
688 VOICE_PIXEL,
689 VOICE_PER_SEC,
690 VOICE_HERTZ,
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000691 };
692
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000693#if CONFIG_HWCODEC != MASNONE
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +0000694 if (audio_status()) /* busy, buffer in use */
Linus Nielsen Feltzing9e11ff82004-03-22 12:34:06 +0000695 return -1;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000696#endif
Linus Nielsen Feltzing9e11ff82004-03-22 12:34:06 +0000697
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000698 if (unit < 0 || unit >= UNIT_LAST)
699 unit_id = -1;
700 else
701 unit_id = unit_voiced[unit];
702
Jörg Hohensohnbeec2e92004-03-20 16:49:58 +0000703 if ((n==1 || n==-1) /* singular? */
Jörg Hohensohnfa97f162004-03-19 22:15:53 +0000704 && unit_id >= VOICE_SECONDS && unit_id <= VOICE_HOURS)
705 {
706 unit_id--; /* use the singular for those units which have */
707 }
708
709 /* special case with a "plus" before */
710 if (n > 0 && (unit == UNIT_SIGNED || unit == UNIT_DB))
711 {
712 talk_id(VOICE_PLUS, enqueue);
713 enqueue = true;
714 }
715
716 talk_number(n, enqueue); /* say the number */
717 talk_id(unit_id, true); /* say the unit, if any */
718
719 return 0;
720}
721
Jörg Hohensohnc8592ba2004-04-04 19:08:44 +0000722/* spell a string */
Jens Arnold033ba162004-08-26 20:30:22 +0000723int talk_spell(const char* spell, bool enqueue)
Jörg Hohensohnc8592ba2004-04-04 19:08:44 +0000724{
725 char c; /* currently processed char */
726
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000727#if CONFIG_HWCODEC != MASNONE
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +0000728 if (audio_status()) /* busy, buffer in use */
Jörg Hohensohnc8592ba2004-04-04 19:08:44 +0000729 return -1;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000730#endif
Jörg Hohensohnc8592ba2004-04-04 19:08:44 +0000731
732 if (!enqueue)
733 shutup(); /* cut off all the pending stuff */
734
735 while ((c = *spell++) != '\0')
736 {
737 /* if this grows into too many cases, I should use a table */
738 if (c >= 'A' && c <= 'Z')
739 talk_id(VOICE_CHAR_A + c - 'A', true);
740 else if (c >= 'a' && c <= 'z')
741 talk_id(VOICE_CHAR_A + c - 'a', true);
742 else if (c >= '0' && c <= '9')
743 talk_id(VOICE_ZERO + c - '0', true);
744 else if (c == '-')
745 talk_id(VOICE_MINUS, true);
746 else if (c == '+')
747 talk_id(VOICE_PLUS, true);
748 else if (c == '.')
Jörg Hohensohnae34f642004-10-25 20:44:37 +0000749 talk_id(VOICE_DOT, true);
Jörg Hohensohnde79e682004-10-23 17:58:38 +0000750 else if (c == ' ')
751 talk_id(VOICE_PAUSE, true);
Jörg Hohensohnc8592ba2004-04-04 19:08:44 +0000752 }
753
754 return 0;
755}