mktccboot: Remove excessive use of defines in Makefile
[kugel-rb.git] / apps / metadata / wave.c
blobcf676f89b041c5e345d24ca8714bf29e166dabc9
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"
32 bool get_wave_metadata(int fd, struct mp3entry* id3)
34 /* Use the trackname part of the id3 structure as a temporary buffer */
35 unsigned char* buf = (unsigned char *)id3->path;
36 unsigned long totalsamples = 0;
37 unsigned long channels = 0;
38 unsigned long bitspersample = 0;
39 unsigned long numbytes = 0;
40 int read_bytes;
41 int i;
43 /* get RIFF chunk header */
44 if ((lseek(fd, 0, SEEK_SET) < 0)
45 || ((read_bytes = read(fd, buf, 12)) < 12))
47 return false;
50 if ((memcmp(buf, "RIFF",4) != 0)
51 || (memcmp(&buf[8], "WAVE", 4) !=0 ))
53 return false;
56 /* iterate over WAVE chunks until 'data' chunk */
57 while (true)
59 /* get chunk header */
60 if ((read_bytes = read(fd, buf, 8)) < 8)
61 return false;
63 /* chunkSize */
64 i = get_long_le(&buf[4]);
66 if (memcmp(buf, "fmt ", 4) == 0)
68 /* get rest of chunk */
69 if ((read_bytes = read(fd, buf, 16)) < 16)
70 return false;
72 i -= 16;
74 /* skipping wFormatTag */
75 /* wChannels */
76 channels = buf[2] | (buf[3] << 8);
77 /* dwSamplesPerSec */
78 id3->frequency = get_long_le(&buf[4]);
79 /* dwAvgBytesPerSec */
80 id3->bitrate = (get_long_le(&buf[8]) * 8) / 1000;
81 /* skipping wBlockAlign */
82 /* wBitsPerSample */
83 bitspersample = buf[14] | (buf[15] << 8);
85 else if (memcmp(buf, "data", 4) == 0)
87 numbytes = i;
88 break;
90 else if (memcmp(buf, "fact", 4) == 0)
92 /* dwSampleLength */
93 if (i >= 4)
95 /* get rest of chunk */
96 if ((read_bytes = read(fd, buf, 4)) < 4)
97 return false;
99 i -= 4;
100 totalsamples = get_long_le(buf);
104 /* seek to next chunk (even chunk sizes must be padded) */
105 if (i & 0x01)
106 i++;
108 if(lseek(fd, i, SEEK_CUR) < 0)
109 return false;
112 if ((numbytes == 0) || (channels == 0))
114 return false;
117 if (totalsamples == 0)
119 /* for PCM only */
120 totalsamples = numbytes
121 / ((((bitspersample - 1) / 8) + 1) * channels);
124 id3->vbr = false; /* All WAV files are CBR */
125 id3->filesize = filesize(fd);
127 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
128 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
130 return true;