Fix Pacbox controls for D2 touchscreen
[Rockbox.git] / apps / metadata / wave.c
blob7a5fd7a10d4e077786774e3baf85f80760fffe90
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <inttypes.h>
25 #include "system.h"
26 #include "id3.h"
27 #include "metadata_common.h"
28 #include "metadata_parsers.h"
30 bool get_wave_metadata(int fd, struct mp3entry* id3)
32 /* Use the trackname part of the id3 structure as a temporary buffer */
33 unsigned char* buf = (unsigned char *)id3->path;
34 unsigned long totalsamples = 0;
35 unsigned long channels = 0;
36 unsigned long bitspersample = 0;
37 unsigned long numbytes = 0;
38 int read_bytes;
39 int i;
41 /* get RIFF chunk header */
42 if ((lseek(fd, 0, SEEK_SET) < 0)
43 || ((read_bytes = read(fd, buf, 12)) < 12))
45 return false;
48 if ((memcmp(buf, "RIFF",4) != 0)
49 || (memcmp(&buf[8], "WAVE", 4) !=0 ))
51 return false;
54 /* iterate over WAVE chunks until 'data' chunk */
55 while (true)
57 /* get chunk header */
58 if ((read_bytes = read(fd, buf, 8)) < 8)
59 return false;
61 /* chunkSize */
62 i = get_long_le(&buf[4]);
64 if (memcmp(buf, "fmt ", 4) == 0)
66 /* get rest of chunk */
67 if ((read_bytes = read(fd, buf, 16)) < 16)
68 return false;
70 i -= 16;
72 /* skipping wFormatTag */
73 /* wChannels */
74 channels = buf[2] | (buf[3] << 8);
75 /* dwSamplesPerSec */
76 id3->frequency = get_long_le(&buf[4]);
77 /* dwAvgBytesPerSec */
78 id3->bitrate = (get_long_le(&buf[8]) * 8) / 1000;
79 /* skipping wBlockAlign */
80 /* wBitsPerSample */
81 bitspersample = buf[14] | (buf[15] << 8);
83 else if (memcmp(buf, "data", 4) == 0)
85 numbytes = i;
86 break;
88 else if (memcmp(buf, "fact", 4) == 0)
90 /* dwSampleLength */
91 if (i >= 4)
93 /* get rest of chunk */
94 if ((read_bytes = read(fd, buf, 4)) < 4)
95 return false;
97 i -= 4;
98 totalsamples = get_long_le(buf);
102 /* seek to next chunk (even chunk sizes must be padded) */
103 if (i & 0x01)
104 i++;
106 if(lseek(fd, i, SEEK_CUR) < 0)
107 return false;
110 if ((numbytes == 0) || (channels == 0))
112 return false;
115 if (totalsamples == 0)
117 /* for PCM only */
118 totalsamples = numbytes
119 / ((((bitspersample - 1) / 8) + 1) * channels);
122 id3->vbr = false; /* All WAV files are CBR */
123 id3->filesize = filesize(fd);
125 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
126 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
128 return true;