blob: d59e7ee2b4d7d79920f3334b346c838ff1d0bda6 [file] [log] [blame]
Marcoen Hirschberg2175d1e2007-06-16 18:19:51 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
Dave Chapmane5ee30e2007-06-16 21:07:53 +000010 * Copyright (C) 2007 Dave Chapman
Marcoen Hirschberg2175d1e2007-06-16 18:19:51 +000011 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Marcoen Hirschberg2175d1e2007-06-16 18:19:51 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include <inttypes.h>
26
27#include "system.h"
28#include "id3.h"
29#include "metadata_common.h"
Bertrik Sikken1273f952008-05-03 07:03:59 +000030#include "metadata_parsers.h"
Marcoen Hirschberg2175d1e2007-06-16 18:19:51 +000031
32bool get_monkeys_metadata(int fd, struct mp3entry* id3)
33{
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;
42
43 lseek(fd, 0, SEEK_SET);
44
45 if (read(fd, buf, 4) < 4)
46 {
47 return rc;
48 }
49
50 if (memcmp(buf, "MAC ", 4) != 0)
51 {
52 return rc;
53 }
54
55 read(fd, buf + 4, MAX_PATH - 4);
56
57 fileversion = get_short_le(buf+4);
58 if (fileversion < 3970)
59 {
60 /* Not supported */
61 return false;
62 }
63
64 if (fileversion >= 3980)
65 {
66 descriptorlength = get_long_le(buf+8);
67
68 header = buf + descriptorlength;
69
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);
74 }
75 else
76 {
77 /* v3.95 and later files all have a fixed framesize */
78 blocksperframe = 73728 * 4;
79
80 finalframeblocks = get_long_le(buf+28);
81 totalframes = get_long_le(buf+24);
82 id3->frequency = get_long_le(buf+12);
83 }
84
Dave Chapman62dab052007-06-19 19:03:37 +000085 id3->vbr = true; /* All APE files are VBR */
Marcoen Hirschberg2175d1e2007-06-16 18:19:51 +000086 id3->filesize = filesize(fd);
87
88 totalsamples = finalframeblocks;
89 if (totalframes > 1)
90 totalsamples += blocksperframe * (totalframes-1);
91
92 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
93 id3->bitrate = (id3->filesize * 8) / id3->length;
94 return true;
95}