Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 14 | * the voice UI lets menus and screens "talk" from a voicefile in memory. |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 15 | * |
| 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 Hohensohn | 6e77d1f | 2004-04-06 07:06:59 +0000 | [diff] [blame] | 29 | #include "settings.h" |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 30 | #include "mp3_playback.h" |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 31 | #include "audio.h" |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 32 | #include "lang.h" |
| 33 | #include "talk.h" |
Jörg Hohensohn | 3aa99e1 | 2004-03-27 00:11:01 +0000 | [diff] [blame] | 34 | #include "id3.h" |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 35 | #include "logf.h" |
Jens Arnold | 4341211 | 2004-09-26 09:25:59 +0000 | [diff] [blame] | 36 | #include "bitswap.h" |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 37 | #if CONFIG_HWCODEC == MASNONE |
| 38 | #include "playback.h" |
| 39 | #endif |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 40 | |
| 41 | /***************** Constants *****************/ |
| 42 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 43 | #define QUEUE_SIZE 64 /* must be a power of two */ |
| 44 | #define QUEUE_MASK (QUEUE_SIZE-1) |
Jens Arnold | 839067b | 2004-08-01 23:34:44 +0000 | [diff] [blame] | 45 | const char* const dir_thumbnail_name = "_dirname.talk"; |
Jörg Hohensohn | 40ae63b | 2004-10-21 18:34:48 +0000 | [diff] [blame] | 46 | const char* const file_thumbnail_ext = ".talk"; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 47 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 48 | /***************** Functional Macros *****************/ |
| 49 | |
| 50 | #define QUEUE_LEVEL ((queue_write - queue_read) & QUEUE_MASK) |
| 51 | |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 52 | #define LOADED_MASK 0x80000000 /* MSB */ |
| 53 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 54 | |
| 55 | /***************** Data types *****************/ |
| 56 | |
| 57 | struct clip_entry /* one entry of the index table */ |
| 58 | { |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 59 | int offset; /* offset from start of voicefile file */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 60 | int size; /* size of the clip */ |
| 61 | }; |
| 62 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 63 | struct voicefile /* file format of our voice file */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 64 | { |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 65 | int version; /* version of the voicefile */ |
Jörg Hohensohn | f9495cb | 2004-04-03 20:52:24 +0000 | [diff] [blame] | 66 | 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 Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 70 | /* and finally the bitswapped mp3 clips, not visible here */ |
| 71 | }; |
| 72 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 73 | struct queue_entry /* one entry of the internal queue */ |
| 74 | { |
| 75 | unsigned char* buf; |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 76 | long len; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 80 | /***************** Globals *****************/ |
| 81 | |
| 82 | static unsigned char* p_thumbnail; /* buffer for thumbnail */ |
| 83 | static long size_for_thumbnail; /* leftover buffer size for it */ |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 84 | static struct voicefile* p_voicefile; /* loaded voicefile */ |
| 85 | static bool has_voicefile; /* a voicefile file is present */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 86 | static struct queue_entry queue[QUEUE_SIZE]; /* queue of scheduled clips */ |
| 87 | static int queue_write; /* write index of queue, by application */ |
| 88 | static int queue_read; /* read index of queue, by ISR context */ |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 89 | static int sent; /* how many bytes handed over to playback, owned by ISR */ |
Jörg Hohensohn | c898a02 | 2004-04-20 21:47:07 +0000 | [diff] [blame] | 90 | static unsigned char curr_hd[3]; /* current frame header, for re-sync */ |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 91 | static int filehandle; /* global, so the MMC variant can keep the file open */ |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 92 | static unsigned char* p_silence; /* VOICE_PAUSE clip, used for termination */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 93 | static long silence_len; /* length of the VOICE_PAUSE clip */ |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 94 | static unsigned char* p_lastclip; /* address of latest clip, for silence add */ |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 95 | static unsigned long voicefile_size = 0; /* size of the loaded voice file */ |
Jörg Hohensohn | c898a02 | 2004-04-20 21:47:07 +0000 | [diff] [blame] | 96 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 97 | |
Jörg Hohensohn | 590e6af | 2004-03-21 17:45:45 +0000 | [diff] [blame] | 98 | /***************** Private prototypes *****************/ |
| 99 | |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 100 | static void load_voicefile(void); |
Jörg Hohensohn | 590e6af | 2004-03-21 17:45:45 +0000 | [diff] [blame] | 101 | static void mp3_callback(unsigned char** start, int* size); |
| 102 | static int shutup(void); |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 103 | static int queue_clip(unsigned char* buf, long size, bool enqueue); |
Jörg Hohensohn | 6e77d1f | 2004-04-06 07:06:59 +0000 | [diff] [blame] | 104 | static int open_voicefile(void); |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 105 | static unsigned char* get_clip(long id, long* p_size); |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | /***************** Private implementation *****************/ |
| 109 | |
Jörg Hohensohn | 6e77d1f | 2004-04-06 07:06:59 +0000 | [diff] [blame] | 110 | static 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 Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 122 | |
Jörg Hohensohn | 6e77d1f | 2004-04-06 07:06:59 +0000 | [diff] [blame] | 123 | return open(buf, O_RDONLY); |
| 124 | } |
| 125 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 126 | int talk_get_bufsize(void) |
| 127 | { |
| 128 | return voicefile_size; |
| 129 | } |
| 130 | |
| 131 | #ifdef SIMULATOR |
| 132 | static unsigned short BSWAP16(unsigned short value) |
| 133 | { |
| 134 | return (value >> 8) | (value << 8); |
| 135 | } |
| 136 | |
| 137 | static 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 Hohensohn | 6e77d1f | 2004-04-06 07:06:59 +0000 | [diff] [blame] | 144 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 145 | /* load the voice file into the mp3 buffer */ |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 146 | static void load_voicefile(void) |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 147 | { |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 148 | int load_size; |
| 149 | int got_size; |
| 150 | int file_size; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 151 | #if CONFIG_HWCODEC == MASNONE |
| 152 | int length, i; |
| 153 | unsigned char *buf, temp; |
| 154 | #endif |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 155 | |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 156 | filehandle = open_voicefile(); |
| 157 | if (filehandle < 0) /* failed to open */ |
| 158 | goto load_err; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 159 | |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 160 | file_size = filesize(filehandle); |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 161 | if (file_size > audiobufend - audiobuf) /* won't fit? */ |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 162 | goto load_err; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 163 | |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 164 | #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 Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 170 | got_size = read(filehandle, audiobuf, load_size); |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 171 | 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 Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 185 | == offsetof(struct voicefile, index)) |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 186 | { |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 187 | p_voicefile = (struct voicefile*)audiobuf; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 188 | |
| 189 | /* thumbnail buffer is the remaining space behind */ |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 190 | p_thumbnail = audiobuf + file_size; |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 191 | p_thumbnail += (long)p_thumbnail % 2; /* 16-bit align */ |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 192 | size_for_thumbnail = audiobufend - p_thumbnail; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 193 | } |
| 194 | else |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 195 | goto load_err; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 196 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 197 | #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 Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 233 | /* 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 Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 237 | audiobuf + offsetof(struct voicefile, index), load_size); |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 238 | 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 Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 245 | /* make sure to have the silence clip, if available */ |
| 246 | p_silence = get_clip(VOICE_PAUSE, &silence_len); |
| 247 | |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 248 | return; |
| 249 | |
| 250 | load_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 Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | /* called in ISR context if mp3 data got consumed */ |
| 263 | static void mp3_callback(unsigned char** start, int* size) |
| 264 | { |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 265 | queue[queue_read].len -= sent; /* we completed this */ |
| 266 | queue[queue_read].buf += sent; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 267 | |
| 268 | if (queue[queue_read].len > 0) /* current clip not finished? */ |
| 269 | { /* feed the next 64K-1 chunk */ |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 270 | #if CONFIG_HWCODEC != MASNONE |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 271 | sent = MIN(queue[queue_read].len, 0xFFFF); |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 272 | #else |
| 273 | sent = queue[queue_read].len; |
| 274 | #endif |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 275 | *start = queue[queue_read].buf; |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 276 | *size = sent; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 277 | return; |
| 278 | } |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 279 | else if (sent > 0) /* go to next entry */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 280 | { |
Jens Arnold | 1a479d6 | 2004-11-18 22:59:20 +0000 | [diff] [blame] | 281 | queue_read = (queue_read + 1) & QUEUE_MASK; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Jens Arnold | 1a479d6 | 2004-11-18 22:59:20 +0000 | [diff] [blame] | 284 | re_check: |
| 285 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 286 | if (QUEUE_LEVEL) /* queue is not empty? */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 287 | { /* start next clip */ |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 288 | #if CONFIG_HWCODEC != MASNONE |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 289 | sent = MIN(queue[queue_read].len, 0xFFFF); |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 290 | #else |
| 291 | sent = queue[queue_read].len; |
| 292 | #endif |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 293 | *start = p_lastclip = queue[queue_read].buf; |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 294 | *size = sent; |
Jens Arnold | 1a479d6 | 2004-11-18 22:59:20 +0000 | [diff] [blame] | 295 | curr_hd[0] = p_lastclip[1]; |
| 296 | curr_hd[1] = p_lastclip[2]; |
| 297 | curr_hd[2] = p_lastclip[3]; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 298 | } |
Jens Arnold | 1a479d6 | 2004-11-18 22:59:20 +0000 | [diff] [blame] | 299 | 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 Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 302 | { /* add silence clip when queue runs empty playing a voice clip */ |
Jens Arnold | 1a479d6 | 2004-11-18 22:59:20 +0000 | [diff] [blame] | 303 | 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 Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 308 | } |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 309 | else |
| 310 | { |
| 311 | *size = 0; /* end of data */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 312 | mp3_play_stop(); /* fixme: should be done by caller */ |
| 313 | } |
| 314 | } |
| 315 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 316 | /* stop the playback and the pending clips, but at frame boundary */ |
| 317 | static int shutup(void) |
| 318 | { |
Jörg Hohensohn | 590e6af | 2004-03-21 17:45:45 +0000 | [diff] [blame] | 319 | unsigned char* pos; |
| 320 | unsigned char* search; |
| 321 | unsigned char* end; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 322 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 323 | if (QUEUE_LEVEL == 0) /* has ended anyway */ |
| 324 | { |
Jörg Hohensohn | 590e6af | 2004-03-21 17:45:45 +0000 | [diff] [blame] | 325 | return 0; |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 326 | } |
Daniel Stenberg | 003c0d2 | 2005-02-02 21:50:24 +0000 | [diff] [blame] | 327 | #if CONFIG_CPU == SH7034 |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 328 | CHCR3 &= ~0x0001; /* disable the DMA (and therefore the interrupt also) */ |
Daniel Stenberg | 003c0d2 | 2005-02-02 21:50:24 +0000 | [diff] [blame] | 329 | #endif |
Jörg Hohensohn | 590e6af | 2004-03-21 17:45:45 +0000 | [diff] [blame] | 330 | /* 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 Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 333 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 334 | 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 Hohensohn | b109c1e | 2004-03-29 08:19:47 +0000 | [diff] [blame] | 339 | { |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 340 | 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 Arnold | 1a479d6 | 2004-11-18 22:59:20 +0000 | [diff] [blame] | 357 | queue_write = (queue_read + 1) & QUEUE_MASK; /* will be empty after next callback */ |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 358 | queue[queue_read].len = sent; /* current one ends after this */ |
| 359 | |
Daniel Stenberg | 003c0d2 | 2005-02-02 21:50:24 +0000 | [diff] [blame] | 360 | #if CONFIG_CPU == SH7034 |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 361 | DTCR3 = sent; /* let the DMA finish this frame */ |
| 362 | CHCR3 |= 0x0001; /* re-enable DMA */ |
Daniel Stenberg | 003c0d2 | 2005-02-02 21:50:24 +0000 | [diff] [blame] | 363 | #endif |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 364 | return 0; |
Jörg Hohensohn | b109c1e | 2004-03-29 08:19:47 +0000 | [diff] [blame] | 365 | } |
Jörg Hohensohn | 590e6af | 2004-03-21 17:45:45 +0000 | [diff] [blame] | 366 | } |
Jörg Hohensohn | c49129c | 2004-04-20 22:11:20 +0000 | [diff] [blame] | 367 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 368 | /* nothing to do, was frame boundary or not our clip */ |
| 369 | mp3_play_stop(); |
| 370 | queue_write = queue_read = 0; /* reset the queue */ |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 371 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* schedule a clip, at the end or discard the existing queue */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 377 | static int queue_clip(unsigned char* buf, long size, bool enqueue) |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 378 | { |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 379 | int queue_level; |
| 380 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 381 | if (!enqueue) |
| 382 | shutup(); /* cut off all the pending stuff */ |
| 383 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 384 | if (!size) |
| 385 | return 0; /* safety check */ |
Daniel Stenberg | 003c0d2 | 2005-02-02 21:50:24 +0000 | [diff] [blame] | 386 | #if CONFIG_CPU == SH7034 |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 387 | /* disable the DMA temporarily, to be safe of race condition */ |
| 388 | CHCR3 &= ~0x0001; |
Daniel Stenberg | 003c0d2 | 2005-02-02 21:50:24 +0000 | [diff] [blame] | 389 | #endif |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 390 | 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 Arnold | 1a479d6 | 2004-11-18 22:59:20 +0000 | [diff] [blame] | 396 | queue_write = (queue_write + 1) & QUEUE_MASK; |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 397 | } |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 398 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 399 | if (queue_level == 0) |
| 400 | { /* queue was empty, we have to do the initial start */ |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 401 | p_lastclip = buf; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 402 | #if CONFIG_HWCODEC != MASNONE |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 403 | sent = MIN(size, 0xFFFF); /* DMA can do no more */ |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 404 | #else |
| 405 | sent = size; |
| 406 | #endif |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 407 | mp3_play_data(buf, sent, mp3_callback); |
Jörg Hohensohn | c898a02 | 2004-04-20 21:47:07 +0000 | [diff] [blame] | 408 | curr_hd[0] = buf[1]; |
| 409 | curr_hd[1] = buf[2]; |
| 410 | curr_hd[2] = buf[3]; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 411 | mp3_play_pause(true); /* kickoff audio */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 412 | } |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 413 | else |
| 414 | { |
Daniel Stenberg | 003c0d2 | 2005-02-02 21:50:24 +0000 | [diff] [blame] | 415 | #if CONFIG_CPU == SH7034 |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 416 | CHCR3 |= 0x0001; /* re-enable DMA */ |
Daniel Stenberg | 003c0d2 | 2005-02-02 21:50:24 +0000 | [diff] [blame] | 417 | #endif |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 418 | } |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 423 | /* fetch a clip from the voice file */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 424 | static unsigned char* get_clip(long id, long* p_size) |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 425 | { |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 426 | long clipsize; |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 427 | 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 Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 445 | clipbuf = audiobuf + p_voicefile->index[id].offset; |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 446 | |
| 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 Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 467 | /* common code for talk_init() and talk_buffer_steal() */ |
Jörg Hohensohn | 3e41780 | 2004-08-31 22:13:20 +0000 | [diff] [blame] | 468 | static void reset_state(void) |
| 469 | { |
| 470 | queue_write = queue_read = 0; /* reset the queue */ |
| 471 | p_voicefile = NULL; /* indicate no voicefile (trashed) */ |
Linus Nielsen Feltzing | d34865a | 2005-04-05 11:33:58 +0000 | [diff] [blame] | 472 | p_thumbnail = audiobuf; /* whole space for thumbnail */ |
| 473 | size_for_thumbnail = audiobufend - audiobuf; |
Jörg Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 474 | p_silence = NULL; /* pause clip not accessible */ |
Jörg Hohensohn | 3e41780 | 2004-08-31 22:13:20 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 477 | /***************** Public implementation *****************/ |
| 478 | |
| 479 | void talk_init(void) |
| 480 | { |
Jörg Hohensohn | 3e41780 | 2004-08-31 22:13:20 +0000 | [diff] [blame] | 481 | reset_state(); /* use this for most of our inits */ |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 482 | |
| 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 Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 489 | voicefile_size = 0; |
| 490 | |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 491 | if (has_voicefile) |
| 492 | { |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 493 | voicefile_size = filesize(filehandle); |
| 494 | #if CONFIG_HWCODEC == MASNONE |
| 495 | voice_init(); |
| 496 | #endif |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 497 | close(filehandle); /* close again, this was just to detect presence */ |
| 498 | filehandle = -1; |
| 499 | } |
| 500 | #endif |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 501 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | |
| 505 | /* somebody else claims the mp3 buffer, e.g. for regular play/record */ |
| 506 | int talk_buffer_steal(void) |
| 507 | { |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 508 | mp3_play_stop(); |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 509 | #ifdef HAVE_MMC |
| 510 | if (filehandle >= 0) /* only relevant for MMC */ |
| 511 | { |
| 512 | close(filehandle); |
| 513 | filehandle = -1; |
| 514 | } |
| 515 | #endif |
Jörg Hohensohn | 3e41780 | 2004-08-31 22:13:20 +0000 | [diff] [blame] | 516 | reset_state(); |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 521 | /* play a voice ID from voicefile */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 522 | int talk_id(long id, bool enqueue) |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 523 | { |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 524 | long clipsize; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 525 | unsigned char* clipbuf; |
| 526 | int unit; |
| 527 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 528 | #if CONFIG_HWCODEC != MASNONE |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 529 | if (audio_status()) /* busy, buffer in use */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 530 | return -1; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 531 | #endif |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 532 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 533 | if (p_voicefile == NULL && has_voicefile) |
| 534 | load_voicefile(); /* reload needed */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 535 | |
Jörg Hohensohn | 2fef5b7 | 2004-05-09 09:41:23 +0000 | [diff] [blame] | 536 | if (p_voicefile == NULL) /* still no voices? */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 537 | return -1; |
| 538 | |
Jörg Hohensohn | f9495cb | 2004-04-03 20:52:24 +0000 | [diff] [blame] | 539 | if (id == -1) /* -1 is an indication for silence */ |
| 540 | return -1; |
| 541 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 542 | /* check if this is a special ID, with a value */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 543 | unit = ((unsigned long)id) >> UNIT_SHIFT; |
Jörg Hohensohn | f9495cb | 2004-04-03 20:52:24 +0000 | [diff] [blame] | 544 | if (unit) |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 545 | { /* sign-extend the value */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 546 | id = (unsigned long)id << (32-UNIT_SHIFT); |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 547 | 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 Hohensohn | 24e6dff | 2004-11-17 22:30:38 +0000 | [diff] [blame] | 552 | clipbuf = get_clip(id, &clipsize); |
| 553 | if (clipbuf == NULL) |
| 554 | return -1; /* not present */ |
Jörg Hohensohn | a7aa17a | 2004-10-12 16:43:22 +0000 | [diff] [blame] | 555 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 556 | queue_clip(clipbuf, clipsize, enqueue); |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | |
| 562 | /* play a thumbnail from file */ |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 563 | int talk_file(const char* filename, bool enqueue) |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 564 | { |
| 565 | int fd; |
| 566 | int size; |
Jörg Hohensohn | 3aa99e1 | 2004-03-27 00:11:01 +0000 | [diff] [blame] | 567 | struct mp3entry info; |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 568 | |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 569 | if (audio_status()) /* busy, buffer in use */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 570 | return -1; |
| 571 | |
| 572 | if (p_thumbnail == NULL || size_for_thumbnail <= 0) |
| 573 | return -1; |
| 574 | |
Linus Nielsen Feltzing | f4f4111 | 2004-07-27 14:10:48 +0000 | [diff] [blame] | 575 | if(mp3info(&info, filename, false)) /* use this to find real start */ |
Jörg Hohensohn | 3aa99e1 | 2004-03-27 00:11:01 +0000 | [diff] [blame] | 576 | { |
| 577 | return 0; /* failed to open, or invalid */ |
| 578 | } |
| 579 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 580 | fd = open(filename, O_RDONLY); |
| 581 | if (fd < 0) /* failed to open */ |
| 582 | { |
| 583 | return 0; |
| 584 | } |
| 585 | |
Jörg Hohensohn | 3aa99e1 | 2004-03-27 00:11:01 +0000 | [diff] [blame] | 586 | lseek(fd, info.first_frame_offset, SEEK_SET); /* behind ID data */ |
| 587 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 588 | 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 Feltzing | a12ccab | 2005-06-06 00:34:07 +0000 | [diff] [blame] | 595 | #if CONFIG_HWCODEC != MASNONE |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 596 | bitswap(p_thumbnail, size); |
Linus Nielsen Feltzing | a12ccab | 2005-06-06 00:34:07 +0000 | [diff] [blame] | 597 | #endif |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 598 | queue_clip(p_thumbnail, size, enqueue); |
| 599 | } |
| 600 | |
| 601 | return size; |
| 602 | } |
| 603 | |
| 604 | |
Jörg Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 605 | /* say a numeric value, this word ordering works for english, |
| 606 | but not necessarily for other languages (e.g. german) */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 607 | int talk_number(long n, bool enqueue) |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 608 | { |
Jörg Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 609 | int level = 0; /* mille count */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 610 | long mil = 1000000000; /* highest possible "-illion" */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 611 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 612 | #if CONFIG_HWCODEC != MASNONE |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 613 | if (audio_status()) /* busy, buffer in use */ |
Linus Nielsen Feltzing | 9e11ff8 | 2004-03-22 12:34:06 +0000 | [diff] [blame] | 614 | return -1; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 615 | #endif |
Linus Nielsen Feltzing | 9e11ff8 | 2004-03-22 12:34:06 +0000 | [diff] [blame] | 616 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 617 | if (!enqueue) |
| 618 | shutup(); /* cut off all the pending stuff */ |
| 619 | |
| 620 | if (n==0) |
Jörg Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 621 | { /* special case */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 622 | 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 Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 634 | 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 Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 637 | |
| 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 Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 649 | /* combination indexing */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 650 | if (ones > 20) |
| 651 | { |
| 652 | int tens = ones/10 + 18; |
| 653 | talk_id(VOICE_ZERO + tens, true); |
| 654 | ones %= 10; |
| 655 | } |
| 656 | |
Jörg Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 657 | /* direct indexing */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 658 | if (ones) |
| 659 | talk_id(VOICE_ZERO + ones, true); |
| 660 | |
Jörg Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 661 | /* add billion, million, thousand */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 662 | if (mil) |
| 663 | talk_id(VOICE_BILLION + level, true); |
| 664 | } |
| 665 | level++; |
| 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
Jörg Hohensohn | c898a02 | 2004-04-20 21:47:07 +0000 | [diff] [blame] | 671 | /* singular/plural aware saying of a value */ |
Jean-Philippe Bernardy | 45e0de3 | 2005-02-12 11:26:36 +0000 | [diff] [blame] | 672 | int talk_value(long n, int unit, bool enqueue) |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 673 | { |
| 674 | int unit_id; |
Jens Arnold | 033ba16 | 2004-08-26 20:30:22 +0000 | [diff] [blame] | 675 | static const int unit_voiced[] = |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 676 | { /* 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 Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 686 | VOICE_GIGABYTE, |
| 687 | VOICE_MILLIAMPHOURS, |
| 688 | VOICE_PIXEL, |
| 689 | VOICE_PER_SEC, |
| 690 | VOICE_HERTZ, |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 691 | }; |
| 692 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 693 | #if CONFIG_HWCODEC != MASNONE |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 694 | if (audio_status()) /* busy, buffer in use */ |
Linus Nielsen Feltzing | 9e11ff8 | 2004-03-22 12:34:06 +0000 | [diff] [blame] | 695 | return -1; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 696 | #endif |
Linus Nielsen Feltzing | 9e11ff8 | 2004-03-22 12:34:06 +0000 | [diff] [blame] | 697 | |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 698 | if (unit < 0 || unit >= UNIT_LAST) |
| 699 | unit_id = -1; |
| 700 | else |
| 701 | unit_id = unit_voiced[unit]; |
| 702 | |
Jörg Hohensohn | beec2e9 | 2004-03-20 16:49:58 +0000 | [diff] [blame] | 703 | if ((n==1 || n==-1) /* singular? */ |
Jörg Hohensohn | fa97f16 | 2004-03-19 22:15:53 +0000 | [diff] [blame] | 704 | && 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 Hohensohn | c8592ba | 2004-04-04 19:08:44 +0000 | [diff] [blame] | 722 | /* spell a string */ |
Jens Arnold | 033ba16 | 2004-08-26 20:30:22 +0000 | [diff] [blame] | 723 | int talk_spell(const char* spell, bool enqueue) |
Jörg Hohensohn | c8592ba | 2004-04-04 19:08:44 +0000 | [diff] [blame] | 724 | { |
| 725 | char c; /* currently processed char */ |
| 726 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 727 | #if CONFIG_HWCODEC != MASNONE |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 728 | if (audio_status()) /* busy, buffer in use */ |
Jörg Hohensohn | c8592ba | 2004-04-04 19:08:44 +0000 | [diff] [blame] | 729 | return -1; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame^] | 730 | #endif |
Jörg Hohensohn | c8592ba | 2004-04-04 19:08:44 +0000 | [diff] [blame] | 731 | |
| 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 Hohensohn | ae34f64 | 2004-10-25 20:44:37 +0000 | [diff] [blame] | 749 | talk_id(VOICE_DOT, true); |
Jörg Hohensohn | de79e68 | 2004-10-23 17:58:38 +0000 | [diff] [blame] | 750 | else if (c == ' ') |
| 751 | talk_id(VOICE_PAUSE, true); |
Jörg Hohensohn | c8592ba | 2004-04-04 19:08:44 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | return 0; |
| 755 | } |