Add a couple of functions for the rm metadata parser for debugging, and add a comment...
[kugel-rb.git] / apps / metadata / monkeys.c
blob1cacff13af08db841ba250cd21d0b63f2605f1b6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
27 #include "system.h"
28 #include "metadata.h"
29 #include "metadata_common.h"
30 #include "metadata_parsers.h"
32 bool get_monkeys_metadata(int fd, struct mp3entry* id3)
34 /* Use the trackname part of the id3 structure as a temporary buffer */
35 unsigned char* buf = (unsigned char *)id3->path;
36 unsigned char* header;
37 bool rc = false;
38 uint32_t descriptorlength;
39 uint32_t totalsamples;
40 uint32_t blocksperframe, finalframeblocks, totalframes;
41 int fileversion;
43 lseek(fd, 0, SEEK_SET);
45 if (read(fd, buf, 4) < 4)
47 return rc;
50 if (memcmp(buf, "MAC ", 4) != 0)
52 return rc;
55 read(fd, buf + 4, MAX_PATH - 4);
57 fileversion = get_short_le(buf+4);
58 if (fileversion < 3970)
60 /* Not supported */
61 return false;
64 if (fileversion >= 3980)
66 descriptorlength = get_long_le(buf+8);
68 header = buf + descriptorlength;
70 blocksperframe = get_long_le(header+4);
71 finalframeblocks = get_long_le(header+8);
72 totalframes = get_long_le(header+12);
73 id3->frequency = get_long_le(header+20);
75 else
77 /* v3.95 and later files all have a fixed framesize */
78 blocksperframe = 73728 * 4;
80 finalframeblocks = get_long_le(buf+28);
81 totalframes = get_long_le(buf+24);
82 id3->frequency = get_long_le(buf+12);
85 id3->vbr = true; /* All APE files are VBR */
86 id3->filesize = filesize(fd);
88 totalsamples = finalframeblocks;
89 if (totalframes > 1)
90 totalsamples += blocksperframe * (totalframes-1);
92 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
93 id3->bitrate = (id3->filesize * 8) / id3->length;
94 return true;