reduce firmware and sun audio codec.
[kugel-rb.git] / apps / metadata / au.c
blob94e7453644f625d359dd7f0ec38bc303d6f2e87c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Yoshihisa Uchida
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 <inttypes.h>
25 #include "system.h"
26 #include "metadata.h"
27 #include "metadata_common.h"
28 #include "metadata_parsers.h"
29 #include "logf.h"
31 static const unsigned char bitspersamples[9] = {
32 0, /* encoding */
33 8, /* 1: G.711 MULAW */
34 8, /* 2: Linear PCM 8bit */
35 16, /* 3: Linear PCM 16bit */
36 24, /* 4: Linear PCM 24bit */
37 32, /* 5: Linear PCM 32bit */
38 32, /* 6: IEEE float 32bit */
39 64, /* 7: IEEE float 64bit */
40 /* encoding 8 - 26 unsupported. */
41 8, /* 27: G.711 ALAW */
44 static inline unsigned char get_au_bitspersample(unsigned int encoding)
46 if (encoding < 8)
47 return bitspersamples[encoding];
48 else if (encoding == 27)
49 return bitspersamples[8];
51 return 0;
54 bool get_au_metadata(int fd, struct mp3entry* id3)
56 /* temporary buffer */
57 unsigned char* buf = (unsigned char *)id3->path;
58 unsigned long numbytes = 0;
59 int offset;
61 id3->vbr = false; /* All Sun audio files are CBR */
62 id3->filesize = filesize(fd);
63 id3->length = 0;
65 lseek(fd, 0, SEEK_SET);
66 if ((read(fd, buf, 24) < 24) || (memcmp(buf, ".snd", 4) != 0))
69 * no header
71 * frequency: 8000 Hz
72 * bits per sample: 8 bit
73 * channel: mono
75 numbytes = id3->filesize;
76 id3->frequency = 8000;
77 id3->bitrate = 8;
79 else
81 /* parse header */
83 /* data offset */
84 offset = get_long_be(buf + 4);
85 if (offset < 24)
87 DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset);
88 return false;
90 /* data size */
91 numbytes = get_long_be(buf + 8);
92 if (numbytes == (uint32_t)0xffffffff)
93 numbytes = id3->filesize - offset;
95 id3->frequency = get_long_be(buf + 16);
96 id3->bitrate = get_au_bitspersample(get_long_be(buf + 12)) * get_long_be(buf + 20)
97 * id3->frequency / 1000;
100 /* Calculate track length [ms] */
101 if (id3->bitrate)
102 id3->length = (numbytes << 3) / id3->bitrate;
104 return true;