initial commit
[kugel-rb.git] / apps / metadata / adx.c
bloba903a6d0532b0a460d747fa2b7f85b81b6aa0f45
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 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"
31 #include "debug.h"
33 bool get_adx_metadata(int fd, struct mp3entry* id3)
35 /* Use the trackname part of the id3 structure as a temporary buffer */
36 unsigned char * buf = (unsigned char *)id3->path;
37 int chanstart, channels, read_bytes;
38 int looping = 0, start_adr = 0, end_adr = 0;
40 /* try to get the basic header */
41 if ((lseek(fd, 0, SEEK_SET) < 0)
42 || ((read_bytes = read(fd, buf, 0x38)) < 0x38))
44 DEBUGF("lseek or read failed\n");
45 return false;
48 /* ADX starts with 0x80 */
49 if (buf[0] != 0x80) {
50 DEBUGF("get_adx_metadata: wrong first byte %c\n",buf[0]);
51 return false;
54 /* check for a reasonable offset */
55 chanstart = ((buf[2] << 8) | buf[3]) + 4;
56 if (chanstart > 4096) {
57 DEBUGF("get_adx_metadata: bad chanstart %i\n", chanstart);
58 return false;
61 /* check for a workable number of channels */
62 channels = buf[7];
63 if (channels != 1 && channels != 2) {
64 DEBUGF("get_adx_metadata: bad channel count %i\n",channels);
65 return false;
68 id3->frequency = get_long_be(&buf[8]);
69 /* 32 samples per 18 bytes */
70 id3->bitrate = id3->frequency * channels * 18 * 8 / 32 / 1000;
71 id3->length = get_long_be(&buf[12]) / id3->frequency * 1000;
72 id3->vbr = false;
73 id3->filesize = filesize(fd);
75 /* get loop info */
76 if (!memcmp(buf+0x10,"\x01\xF4\x03\x00",4)) {
77 /* Soul Calibur 2 style (type 03) */
78 DEBUGF("get_adx_metadata: type 03 found\n");
79 /* check if header is too small for loop data */
80 if (chanstart-6 < 0x2c) looping=0;
81 else {
82 looping = get_long_be(&buf[0x18]);
83 end_adr = get_long_be(&buf[0x28]);
84 start_adr = get_long_be(&buf[0x1c])/32*channels*18+chanstart;
86 } else if (!memcmp(buf+0x10,"\x01\xF4\x04\x00",4)) {
87 /* Standard (type 04) */
88 DEBUGF("get_adx_metadata: type 04 found\n");
89 /* check if header is too small for loop data */
90 if (chanstart-6 < 0x38) looping=0;
91 else {
92 looping = get_long_be(&buf[0x24]);
93 end_adr = get_long_be(&buf[0x34]);
94 start_adr = get_long_be(&buf[0x28])/32*channels*18+chanstart;
96 } else {
97 DEBUGF("get_adx_metadata: error, couldn't determine ADX type\n");
98 return false;
101 if (looping) {
102 /* 2 loops, 10 second fade */
103 id3->length = (start_adr-chanstart + 2*(end_adr-start_adr))
104 *8 / id3->bitrate + 10000;
107 /* try to get the channel header */
108 if ((lseek(fd, chanstart-6, SEEK_SET) < 0)
109 || ((read_bytes = read(fd, buf, 6)) < 6))
111 return false;
114 /* check channel header */
115 if (memcmp(buf, "(c)CRI", 6) != 0) return false;
117 return true;