1 /* libxmms-flac - XMMS FLAC input plugin
2 * Copyright (C) 2000,2001,2002 Josh Coalson
3 * Copyright (C) 2002 Daisuke Shimamura
5 * Based on FLAC plugin.c and mpg123 plugin
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <xmms/plugin.h>
27 #include <xmms/util.h>
28 #include <xmms/configfile.h>
29 #include <xmms/titlestring.h>
31 #include "FLAC/metadata.h"
34 #include "configure.h"
36 #ifdef FLAC__HAS_ID3LIB
44 typedef struct id3v1tag_t {
45 char tag[3]; /* always "TAG": defines ID3v1 tag 128 bytes before EOF */
63 typedef struct id3tag_t {
73 static gboolean local__get_id3v1_tag_as_v2_(const char *filename, id3v2_struct *tag);
74 static void local__id3v1_to_id3v2(id3v1_struct *v1, id3v2_struct *v2);
75 static const char *local__get_id3_genre(unsigned char genre_code);
76 #endif /* FLAC__HAS_ID3LIB */
78 static gchar *local__extname(const char *filename);
79 static char* local__getstr(char* str);
80 static int local__getnum(char* str);
82 static int local__vcentry_matches(const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry *entry);
83 static void local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, gchar **dest);
86 * Function flac_format_song_title (tag, filename)
88 * Create song title according to `tag' and/or `filename' and
89 * return it. The title must be subsequently freed using g_free().
92 gchar *flac_format_song_title(gchar * filename)
95 TitleInput *input = NULL;
97 #ifdef FLAC__HAS_ID3LIB
102 int got_vorbis_comments = 0;
104 #ifdef FLAC__HAS_ID3LIB
105 Initialize_File_Tag_Item (&tag);
107 memset(&tag, 0, sizeof(tag));
110 /* first, parse vorbis comments if they exist */
112 FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
115 if(FLAC__metadata_simple_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
116 FLAC__StreamMetadata *block = FLAC__metadata_simple_iterator_get_block(iterator);
119 const FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
120 for(i = 0; i < vc->num_comments; i++) {
123 if(local__vcentry_matches("artist", &vc->comments[i]))
124 local__vcentry_parse_value(&vc->comments[i], &tag.artist);
125 else if(local__vcentry_matches("performer", &vc->comments[i]))
126 local__vcentry_parse_value(&vc->comments[i], &tag.artist);
127 else if(local__vcentry_matches("album", &vc->comments[i]))
128 local__vcentry_parse_value(&vc->comments[i], &tag.album);
129 else if(local__vcentry_matches("title", &vc->comments[i]))
130 local__vcentry_parse_value(&vc->comments[i], &tag.title);
131 else if(local__vcentry_matches("tracknumber", &vc->comments[i]))
132 local__vcentry_parse_value(&vc->comments[i], &tag.track);
133 else if(local__vcentry_matches("genre", &vc->comments[i]))
134 local__vcentry_parse_value(&vc->comments[i], &tag.genre);
135 else if(local__vcentry_matches("description", &vc->comments[i]))
136 local__vcentry_parse_value(&vc->comments[i], &tag.comment);
139 FLAC__metadata_object_delete(block);
140 got_vorbis_comments = 1;
143 } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
144 FLAC__metadata_simple_iterator_delete(iterator);
148 if(!got_vorbis_comments) {
149 #ifdef FLAC__HAS_ID3LIB
150 rc = Id3tag_Read_File_Tag(filename, &tag);
152 rc = local__get_id3v1_tag_as_v2_(filename, &tag);
156 XMMS_NEW_TITLEINPUT(input);
158 if (got_vorbis_comments || rc)
160 input->performer = local__getstr(tag.artist);
161 input->album_name = local__getstr(tag.album);
162 input->track_name = local__getstr(tag.title);
163 input->track_number = local__getnum(tag.track);
164 input->year = local__getnum(tag.year);
165 input->genre = local__getstr(tag.genre);
166 input->comment = local__getstr(tag.comment);
168 input->file_name = g_basename(filename);
169 input->file_path = filename;
170 input->file_ext = local__extname(filename);
171 ret = xmms_get_titlestring(flac_cfg.tag_override ? flac_cfg.tag_format : xmms_get_gentitle_format(), input);
177 * Format according to filename.
179 ret = g_strdup(g_basename(filename));
180 if (local__extname(ret) != NULL)
181 *(local__extname(ret) - 1) = '\0'; /* removes period */
184 #ifdef FLAC__HAS_ID3LIB
185 Free_File_Tag_Item (&tag);
190 #ifndef FLAC__HAS_ID3LIB
192 * Function get_idv2_tag_(filename, ID3v2tag)
194 * Get ID3v2 tag from file.
197 gboolean local__get_id3v1_tag_as_v2_(const char *filename, id3v2_struct *id3v2tag)
200 id3v1_struct id3v1tag;
202 memset(id3v2tag, 0, sizeof(id3v2_struct));
204 if ((file = fopen(filename, "rb")) != 0)
206 if ((fseek(file, -1 * sizeof (id3v1tag), SEEK_END) == 0) &&
207 (fread(&id3v1tag, 1, sizeof (id3v1tag), file) == sizeof (id3v1tag)) &&
208 (strncmp(id3v1tag.tag, "TAG", 3) == 0))
210 local__id3v1_to_id3v2(&id3v1tag, id3v2tag);
212 if (flac_cfg.convert_char_set)
216 string = convert_from_file_to_user(id3v2tag->title);
217 strcpy(id3v2tag->title, string);
220 string = convert_from_file_to_user(id3v2tag->artist);
221 strcpy(id3v2tag->artist, string);
224 string = convert_from_file_to_user(id3v2tag->album);
225 strcpy(id3v2tag->album, string);
228 string = convert_from_file_to_user(id3v2tag->comment);
229 strcpy(id3v2tag->comment, string);
232 string = convert_from_file_to_user(id3v2tag->genre);
233 strcpy(id3v2tag->genre, string);
236 string = convert_from_file_to_user(id3v2tag->year);
237 strcpy(id3v2tag->year, string);
240 string = convert_from_file_to_user(id3v2tag->track);
241 strcpy(id3v2tag->track, string);
256 * Function local__id3v1_to_id3v2 (v1, v2)
258 * Convert ID3v1 tag `v1' to ID3v2 tag `v2'.
261 void local__id3v1_to_id3v2(id3v1_struct *v1, id3v2_struct *v2)
263 memset(v2,0,sizeof(id3v2_struct));
264 strncpy(v2->title, v1->title, 30);
265 strncpy(v2->artist, v1->artist, 30);
266 strncpy(v2->album, v1->album, 30);
267 strncpy(v2->comment, v1->u.v1_0.comment, 30);
268 strncpy(v2->genre, local__get_id3_genre(v1->genre), sizeof (v2->genre));
269 strncpy(v2->year, v1->year, 4);
271 /* Check for v1.1 tags. */
272 if (v1->u.v1_1.__zero == 0)
273 sprintf(v2->track, "%d", v1->u.v1_1.track);
275 strcpy(v2->track, "0");
277 g_strstrip(v2->title);
278 g_strstrip(v2->artist);
279 g_strstrip(v2->album);
280 g_strstrip(v2->comment);
281 g_strstrip(v2->genre);
282 g_strstrip(v2->year);
283 g_strstrip(v2->track);
286 const char *local__get_id3_genre(unsigned char genre_code)
288 if (genre_code < GENRE_MAX)
289 return gettext(id3_genres[genre_code]);
293 #endif /* ifndef FLAC__HAS_ID3LIB */
296 * Function local__extname (filename)
298 * Return pointer within filename to its extenstion, or NULL if
299 * filename has no extension.
302 gchar *local__extname(const char *filename)
304 gchar *ext = strrchr(filename, '.');
312 char* local__getstr(char* str)
314 if (str && strlen(str) > 0)
319 int local__getnum(char* str)
321 if (str && strlen(str) > 0)
326 int local__vcentry_matches(const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry *entry)
328 const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
329 const unsigned field_name_length = strlen(field_name);
330 return (0 != eq && (unsigned)(eq-entry->entry) == field_name_length && 0 == strncasecmp(field_name, entry->entry, field_name_length));
333 void local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, gchar **dest)
335 const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
340 const unsigned value_length = entry->length - (unsigned)((++eq) - entry->entry);
342 *dest = g_malloc(value_length + 1);
344 memcpy(*dest, eq, value_length);
345 (*dest)[value_length] = '\0';