Mark array as const
[kugel-rb.git] / apps / metadata / au.c
blobedd72375b79c82ba16878d61faf89468c87f48a5
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 <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 "logf.h"
33 static const unsigned char bitspersamples[28] = {
35 8, /* G.711 MULAW */
36 8, /* 8bit */
37 16, /* 16bit */
38 24, /* 24bit */
39 32, /* 32bit */
40 32, /* 32bit */
41 64, /* 64bit */
42 0, /* Fragmented sample data */
43 0, /* DSP program */
44 0, /* 8bit fixed point */
45 0, /* 16bit fixed point */
46 0, /* 24bit fixed point */
47 0, /* 32bit fixed point */
52 0, /* 16bit linear with emphasis */
53 0, /* 16bit linear compressed */
54 0, /* 16bit linear with emphasis and compression */
55 0, /* Music kit DSP commands */
57 0, /* G.721 MULAW */
58 0, /* G.722 */
59 0, /* G.723 3bit */
60 0, /* G.723 5bit */
61 8, /* G.711 ALAW */
64 static int get_au_bitspersample(unsigned int encoding)
66 if (encoding > 27)
67 return 0;
68 return bitspersamples[encoding];
71 bool get_au_metadata(int fd, struct mp3entry* id3)
73 /* Use the trackname part of the id3 structure as a temporary buffer */
74 unsigned char* buf = (unsigned char *)id3->path;
75 unsigned long totalsamples = 0;
76 unsigned long channels = 0;
77 unsigned long bitspersample = 0;
78 unsigned long numbytes = 0;
79 int read_bytes;
80 int offset;
82 id3->vbr = false; /* All Sun audio files are CBR */
83 id3->filesize = filesize(fd);
85 if ((lseek(fd, 0, SEEK_SET) < 0) || ((read_bytes = read(fd, buf, 24)) < 0))
86 return false;
88 if (read_bytes < 24 || (memcmp(buf, ".snd", 4) != 0))
90 /* no header */
91 numbytes = id3->filesize;
92 bitspersample = 8;
93 id3->frequency = 8000;
94 channels = 1;
96 else
98 /* data offset */
99 offset = get_long_be(buf + 4);
100 if (offset < 24)
102 DEBUGF("CODEC_ERROR: sun audio offset size is small: %d\n", offset);
103 return false;
105 /* data size */
106 numbytes = get_long_be(buf + 8);
107 if (numbytes == (uint32_t)0xffffffff)
108 numbytes = id3->filesize - offset;
109 /* bitspersample */
110 bitspersample = get_au_bitspersample(get_long_be(buf + 12));
111 /* sample rate */
112 id3->frequency = get_long_be(buf + 16);
113 channels = get_long_be(buf + 20);
116 totalsamples = numbytes / ((((bitspersample - 1) / 8) + 1) * channels);
118 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
119 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
121 return true;