1 /*
2 |  Copyright (C) 2007 Jorg Schuler <jcsjcs at users sourceforge net>
3 |  Part of the gtkpod project.
4 |
5 |  URL: http://www.gtkpod.org/
6 |  URL: http://gtkpod.sourceforge.net/
7 |
8 |  Gtkpod is free software; you can redistribute it and/or modify
9 |  it under the terms of the GNU General Public License as published by
10 |  the Free Software Foundation; either version 2 of the License, or
11 |  (at your option) any later version.
12 |
13 |  Gtkpod is distributed in the hope that it will be useful,
14 |  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 |  GNU General Public License for more details.
17 |
18 |  You should have received a copy of the GNU General Public License
19 |  along with gtkpod; if not, write to the Free Software
20 |  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 |
22 |  iTunes and iPod are trademarks of Apple
23 |
24 |  This product is not supported/written/published by Apple!
25 |
26 |  $Id$
27 */
28 
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32 
33 #include "charset.h"
34 #include "itdb.h"
35 #include "misc.h"
36 #include "flacfile.h"
37 #include "mp3file.h"
38 
39 /* Info on how to implement new file formats: see mp3file.c for more info */
40 
41 #ifdef HAVE_FLAC
42 
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <inttypes.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <FLAC/metadata.h>
49 #include "prefs.h"
50 
flac_get_file_info(gchar * flacFileName)51 Track *flac_get_file_info (gchar *flacFileName)
52 {
53     Track *track = NULL;
54     FLAC__StreamMetadata stream_data;
55     FLAC__StreamMetadata *tags;
56 
57     if(!FLAC__metadata_get_streaminfo (flacFileName, &stream_data))
58     {
59         gchar *filename = NULL;
60         filename = charset_to_utf8 (flacFileName);
61 
62 	gtkpod_warning (_("'%s' does not appear to be an FLAC audio file.\n"),
63 	                filename);
64         g_free (filename);
65     }
66     else
67     {
68 	gboolean flac_metadata_ok = FALSE;
69 
70         track = gp_track_new ();
71 
72         if (prefs_get_int("readtags"))
73         {
74             if (!FLAC__metadata_get_tags (flacFileName, &tags))
75             {
76                 gchar *filename = NULL;
77                 filename = charset_to_utf8 (flacFileName);
78                 gtkpod_warning (_("Error retrieving tags for '%s'.\n"),
79 	                        filename);
80                 g_free (filename);
81                 /* FIXME: should NULL be returned if no tags? */
82             }
83             else {
84                 gint i;
85 
86 		if (tags->data.vorbis_comment.num_comments > 0)
87 		{
88 		    flac_metadata_ok = TRUE;
89 		}
90 
91                 for (i = 0 ; i < tags->data.vorbis_comment.num_comments ; i++)
92                 {
93                     gchar *tag = (gchar*)tags->data.vorbis_comment.comments[i].entry;
94 
95                     if (g_ascii_strncasecmp("ARTIST=", tag, 7) == 0) {
96                         track->artist = charset_to_utf8 (tag + 7);
97                     }
98                     if (g_ascii_strncasecmp("ALBUM=", tag, 6) == 0) {
99                         track->album = charset_to_utf8 (tag + 6);
100                     }
101                     if (g_ascii_strncasecmp("TITLE=", tag, 6) == 0) {
102                         track->title = charset_to_utf8 (tag + 6);
103                     }
104                     if (g_ascii_strncasecmp("GENRE=", tag, 6) == 0) {
105                         track->genre = charset_to_utf8 (tag + 6);
106                     }
107                     if (g_ascii_strncasecmp("YEAR=", tag, 5) == 0) {
108                         track->year = atoi (tag + 5);
109                     }
110                     if (g_ascii_strncasecmp("DATE=", tag, 5) == 0) {
111 			/* The date field is supposed to be of the
112 			   format YYYY-MM-DD */
113                         track->year = atoi (tag + 5);
114                     }
115                     /* track_nr/tracks tag handling */
116                     if (g_ascii_strncasecmp("TRACKNUMBER=", tag, 12) == 0) {
117                         gchar* string;
118                         gchar* string2;
119                         string = tag + 12;
120 			string2 = strchr(string,'/');
121 			if (string2)
122 			{
123 			    track->tracks = atoi (string2 + 1);
124 			    *string2 = '\0';
125 			}
126 			track->track_nr = atoi (string);
127                     }
128                     /* cd_nr/cds tag handling */
129                     if (g_ascii_strncasecmp("DISCNUMBER=", tag, 11) == 0) {
130                         gchar* string;
131                         gchar* string2;
132                         string = tag + 11;
133 			string2 = strchr(string,'/');
134 			if (string2)
135 			{
136 			    track->cds = atoi (string2 + 1);
137 			    *string2 = '\0';
138 			}
139 			track->cd_nr = atoi (string);
140                     }
141                     if (g_ascii_strncasecmp("COMPOSER=", tag, 9) == 0) {
142                         track->composer = charset_to_utf8 (tag + 9);
143                     }
144                     if (g_ascii_strncasecmp("COMMENT=", tag, 8) == 0) {
145                         track->comment = charset_to_utf8 (tag + 8);
146                     }
147                     if (g_ascii_strncasecmp("TRACKS=", tag, 7) == 0) {
148                         track->tracks = atoi (tag  + 7);
149                     }
150                     if (g_ascii_strncasecmp("CDNR=", tag, 5) == 0) {
151                         track->cd_nr = atoi (tag + 5);
152                     }
153                     if (g_ascii_strncasecmp("CDS=", tag, 4) == 0) {
154                         track->cds = atoi (tag  + 4);
155 		    }
156 		    /* I'm not sure if "BPM=" is correct */
157                     if (g_ascii_strncasecmp("BPM=", tag, 4) == 0) {
158                         track->BPM = atoi (tag  + 4);
159                     }
160                 }
161             }
162 
163             FLAC__metadata_object_delete (tags);
164 
165 	    if (!flac_metadata_ok)
166 	    {   /* fall back on ID3 */
167 		id3_read_tags (flacFileName, track);
168 	    }
169 	}
170 
171 	g_free (track->description);
172 	track->description = g_strdup ("FLAC audio file");
173 
174         track->bitrate = stream_data.data.stream_info.bits_per_sample/1000;
175         track->samplerate = stream_data.data.stream_info.sample_rate;
176         track->tracklen = stream_data.data.stream_info.total_samples / (stream_data.data.stream_info.sample_rate / 1000);
177     }
178 
179     return track;
180 }
181 
flac_write_file_info(gchar * flacFileName,Track * track)182 gboolean flac_write_file_info (gchar *flacFileName, Track *track)
183 {
184     gboolean result = FALSE;
185     gtkpod_warning ("Not supported yet\n"); /* FIXME: */
186     return result;
187 }
188 
189 #else
190 /* We don't support FLAC without the FLAC library */
flac_get_file_info(gchar * name)191 Track *flac_get_file_info (gchar *name)
192 {
193     gtkpod_warning (_("Import of '%s' failed: FLAC not supported without the FLAC library. You must compile the gtkpod source together with the FLAC library.\n"), name);
194     return NULL;
195 }
196 
flac_write_file_info(gchar * filename,Track * track)197 gboolean flac_write_file_info (gchar *filename, Track *track)
198 {
199     gtkpod_warning (_("FLAC metadata update for '%s' failed: FLAC not supported without the FLAC library. You must compile the gtkpod source together with the FLAC library.\n"), filename);
200     return FALSE;
201 }
202 #endif
203