summaryrefslogtreecommitdiff
path: root/apps/metadata/monkeys.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/metadata/monkeys.c')
-rw-r--r--apps/metadata/monkeys.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/apps/metadata/monkeys.c b/apps/metadata/monkeys.c
new file mode 100644
index 0000000000..bda3a01643
--- /dev/null
+++ b/apps/metadata/monkeys.c
@@ -0,0 +1,92 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Dave Chapman
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <ctype.h>
23#include <inttypes.h>
24
25#include "system.h"
26#include "id3.h"
27#include "metadata_common.h"
28
29bool get_monkeys_metadata(int fd, struct mp3entry* id3)
30{
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;
39
40 lseek(fd, 0, SEEK_SET);
41
42 if (read(fd, buf, 4) < 4)
43 {
44 return rc;
45 }
46
47 if (memcmp(buf, "MAC ", 4) != 0)
48 {
49 return rc;
50 }
51
52 read(fd, buf + 4, MAX_PATH - 4);
53
54 fileversion = get_short_le(buf+4);
55 if (fileversion < 3970)
56 {
57 /* Not supported */
58 return false;
59 }
60
61 if (fileversion >= 3980)
62 {
63 descriptorlength = get_long_le(buf+8);
64
65 header = buf + descriptorlength;
66
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);
71 }
72 else
73 {
74 /* v3.95 and later files all have a fixed framesize */
75 blocksperframe = 73728 * 4;
76
77 finalframeblocks = get_long_le(buf+28);
78 totalframes = get_long_le(buf+24);
79 id3->frequency = get_long_le(buf+12);
80 }
81
82 id3->vbr = true; /* All FLAC files are VBR */
83 id3->filesize = filesize(fd);
84
85 totalsamples = finalframeblocks;
86 if (totalframes > 1)
87 totalsamples += blocksperframe * (totalframes-1);
88
89 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
90 id3->bitrate = (id3->filesize * 8) / id3->length;
91 return true;
92}