2 * libid3v1 (http://libid3v1.dottedmag.net/)
4 * Copyright (c) 2006,2007 Mikhail Gusarov <dottedmag@dottedmag.net>
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #include <sys/types.h>
34 #define ID3V1_TAG_LENGTH 128
35 #define ID3V1_HEADER_LENGTH 3
36 #define ID3V1_HEADER "TAG"
38 static int read_all(int fh, char* buf, size_t size)
41 ssize_t read_ = read(fh, buf, size);
42 if (read_ == -1 && (errno == EAGAIN || errno == EINTR)) continue;
43 if (read_ == -1) return 0;
44 if (read_ == 0) break;
51 int id3v1_parse(int fh, id3v1_t* tag)
53 char rawtag[ID3V1_TAG_LENGTH];
55 if ((off_t)-1 == lseek(fh, (off_t)-128, SEEK_END)) return -1;
56 if (!read_all(fh, rawtag, ID3V1_TAG_LENGTH)) return -1;
57 if (memcmp(ID3V1_HEADER, rawtag, ID3V1_HEADER_LENGTH)) return 0;
59 memset(tag, 0, sizeof(id3v1_t));
60 memcpy(tag->songname, rawtag + 3, 30);
61 memcpy(tag->artist, rawtag + 33, 30);
62 memcpy(tag->album, rawtag + 63, 30);
63 memcpy(tag->year, rawtag + 93, 4);
64 memcpy(tag->comment, rawtag + 97, 30);
65 tag->genre = rawtag[127];
67 if (tag->comment[28] == 0) { 68 tag->track = tag->comment[29];
75 static char* genres[] = { 76 "Unknown", "Blues", "Classic Rock", "Country", "Dance", "Disco",
77 "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
78 "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative",
79 "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient",
80 "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical",
81 "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise",
82 "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative",
83 "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave",
84 "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream",
85 "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap",
86 "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychedelic",
87 "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
88 "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock",
90 /* The following genres are Winamp extenisons */
92 "Folk", "Folk/Rock", "National Folk", "Swing", "Fast Fusion", "Bebob",
93 "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
94 "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock",
95 "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
96 "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
97 "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
98 "Folklore", "Ballad", "Power Ballad", "Rhytmic Soul", "Freestyle", "Duet",
99 "Punk Rock", "Drum Solo", "A Capella", "Euro-House", "Dance Hall"
102 const char* id3v1_genre_name(short genre)
104 if (genre < 0 || genre >= sizeof(genres) / sizeof(genres[0])) return NULL;
105 return genres[genre];