Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 Dave Chapman |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <ctype.h> |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #include "system.h" |
| 28 | #include "id3.h" |
| 29 | #include "metadata_common.h" |
Bertrik Sikken | 1273f95 | 2008-05-03 07:03:59 +0000 | [diff] [blame] | 30 | #include "metadata_parsers.h" |
Marcoen Hirschberg | 2175d1e | 2007-06-16 18:19:51 +0000 | [diff] [blame] | 31 | |
| 32 | bool get_aiff_metadata(int fd, struct mp3entry* id3) |
| 33 | { |
| 34 | /* Use the trackname part of the id3 structure as a temporary buffer */ |
| 35 | unsigned char* buf = (unsigned char *)id3->path; |
| 36 | unsigned long numChannels = 0; |
| 37 | unsigned long numSampleFrames = 0; |
| 38 | unsigned long sampleSize = 0; |
| 39 | unsigned long sampleRate = 0; |
| 40 | unsigned long numbytes = 0; |
| 41 | int read_bytes; |
| 42 | int i; |
| 43 | |
| 44 | if ((lseek(fd, 0, SEEK_SET) < 0) |
| 45 | || ((read_bytes = read(fd, buf, sizeof(id3->path))) < 54)) |
| 46 | { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | if ((memcmp(buf, "FORM",4) != 0) |
| 51 | || (memcmp(&buf[8], "AIFF", 4) !=0 )) |
| 52 | { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | buf += 12; |
| 57 | read_bytes -= 12; |
| 58 | |
| 59 | while ((numbytes == 0) && (read_bytes >= 8)) |
| 60 | { |
| 61 | /* chunkSize */ |
| 62 | i = get_long_be(&buf[4]); |
| 63 | |
| 64 | if (memcmp(buf, "COMM", 4) == 0) |
| 65 | { |
| 66 | /* numChannels */ |
| 67 | numChannels = ((buf[8]<<8)|buf[9]); |
| 68 | /* numSampleFrames */ |
| 69 | numSampleFrames = get_long_be(&buf[10]); |
| 70 | /* sampleSize */ |
| 71 | sampleSize = ((buf[14]<<8)|buf[15]); |
| 72 | /* sampleRate */ |
| 73 | sampleRate = get_long_be(&buf[18]); |
| 74 | sampleRate = sampleRate >> (16+14-buf[17]); |
| 75 | /* save format infos */ |
| 76 | id3->bitrate = (sampleSize * numChannels * sampleRate) / 1000; |
| 77 | id3->frequency = sampleRate; |
| 78 | id3->length = ((int64_t) numSampleFrames * 1000) / id3->frequency; |
| 79 | |
| 80 | id3->vbr = false; /* AIFF files are CBR */ |
| 81 | id3->filesize = filesize(fd); |
| 82 | } |
| 83 | else if (memcmp(buf, "SSND", 4) == 0) |
| 84 | { |
| 85 | numbytes = i - 8; |
| 86 | } |
| 87 | |
| 88 | if (i & 0x01) |
| 89 | { |
| 90 | i++; /* odd chunk sizes must be padded */ |
| 91 | } |
| 92 | buf += i + 8; |
| 93 | read_bytes -= i + 8; |
| 94 | } |
| 95 | |
| 96 | if ((numbytes == 0) || (numChannels == 0)) |
| 97 | { |
| 98 | return false; |
| 99 | } |
| 100 | return true; |
| 101 | } |