bulgarian update by Hristo Kovachev
[Rockbox.git] / apps / metadata / monkeys.c
blobe200c2c9124eaf85dae62a068dca4144b71d4775
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <inttypes.h>
25 #include "system.h"
26 #include "id3.h"
27 #include "metadata_common.h"
29 bool get_monkeys_metadata(int fd, struct mp3entry* id3)
31 /* Use the trackname part of the id3 structure as a temporary buffer */
32 unsigned char* buf = (unsigned char *)id3->path;
33 unsigned char* header;
34 bool rc = false;
35 uint32_t descriptorlength;
36 uint32_t totalsamples;
37 uint32_t blocksperframe, finalframeblocks, totalframes;
38 int fileversion;
40 lseek(fd, 0, SEEK_SET);
42 if (read(fd, buf, 4) < 4)
44 return rc;
47 if (memcmp(buf, "MAC ", 4) != 0)
49 return rc;
52 read(fd, buf + 4, MAX_PATH - 4);
54 fileversion = get_short_le(buf+4);
55 if (fileversion < 3970)
57 /* Not supported */
58 return false;
61 if (fileversion >= 3980)
63 descriptorlength = get_long_le(buf+8);
65 header = buf + descriptorlength;
67 blocksperframe = get_long_le(header+4);
68 finalframeblocks = get_long_le(header+8);
69 totalframes = get_long_le(header+12);
70 id3->frequency = get_long_le(header+20);
72 else
74 /* v3.95 and later files all have a fixed framesize */
75 blocksperframe = 73728 * 4;
77 finalframeblocks = get_long_le(buf+28);
78 totalframes = get_long_le(buf+24);
79 id3->frequency = get_long_le(buf+12);
82 id3->vbr = true; /* All APE files are VBR */
83 id3->filesize = filesize(fd);
85 totalsamples = finalframeblocks;
86 if (totalframes > 1)
87 totalsamples += blocksperframe * (totalframes-1);
89 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
90 id3->bitrate = (id3->filesize * 8) / id3->length;
91 return true;