2 * Copyright (C) 2008 by INdT
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * @author Andre Moreira Magalhaes <andre.magalhaes@openbossa.org>
27 #include <lightmediascanner_plugin.h>
28 #include <lightmediascanner_db.h>
33 #include <FLAC/metadata.h>
36 struct lms_plugin plugin;
37 lms_db_audio_t *audio_db;
40 static const char _name[] = "flac";
41 static const struct lms_string_size _codec = LMS_STATIC_STRING_SIZE("flac");
42 static const struct lms_string_size _exts[] = {
43 LMS_STATIC_STRING_SIZE(".flac")
45 static const char *_cats[] = {
50 static const char *_authors[] = {
51 "Andre Moreira Magalhaes",
56 _match(struct plugin *p, const char *path, int len, int base)
60 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
64 return (void*)(i + 1);
68 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
70 struct lms_audio_info info = { };
71 FLAC__StreamMetadata *tags = NULL;
72 FLAC__StreamMetadata si;
77 if (!FLAC__metadata_get_streaminfo(finfo->path, &si)) {
78 fprintf(stderr, "ERROR: cannot retrieve file %s STREAMINFO block\n",
83 info.channels = si.data.stream_info.channels;
84 info.sampling_rate = si.data.stream_info.sample_rate;
86 if (si.data.stream_info.sample_rate) {
87 info.length = si.data.stream_info.total_samples / si.data.stream_info.sample_rate;
88 info.bitrate = (finfo->size * 8) / info.length;
92 info.container = _codec;
94 if (!FLAC__metadata_get_tags(finfo->path, &tags)) {
95 fprintf(stderr, "ERROR: cannot retrieve file %s tags\n", finfo->path);
99 for (i = 0; i < tags->data.vorbis_comment.num_comments; i++) {
100 str = (char *) tags->data.vorbis_comment.comments[i].entry;
101 len = tags->data.vorbis_comment.comments[i].length;
102 if (strncmp(str, "TITLE=", 6) == 0)
103 lms_string_size_strndup(&info.title, str + 6, len - 6);
104 else if (strncmp(str, "ARTIST=", 7) == 0)
105 lms_string_size_strndup(&info.artist, str + 7, len - 7);
106 else if (strncmp(str, "ALBUM=", 6) == 0)
107 lms_string_size_strndup(&info.album, str + 7, len - 7);
108 else if (strncmp(str, "GENRE=", 6) == 0)
109 lms_string_size_strndup(&info.genre, str + 6, len - 6);
110 else if (strncmp(str, "TRACKNUMBER=", 12) == 0)
111 info.trackno = atoi(str + 12);
114 FLAC__metadata_object_delete(tags);
116 lms_string_size_strip_and_free(&info.title);
117 lms_string_size_strip_and_free(&info.artist);
118 lms_string_size_strip_and_free(&info.album);
119 lms_string_size_strip_and_free(&info.genre);
123 lms_name_from_path(&info.title, finfo->path, finfo->path_len,
124 finfo->base, _exts[((long) match) - 1].len,
127 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
130 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
132 lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
134 lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
137 fprintf(stderr, "file %s info\n", finfo->path);
138 fprintf(stderr, "\ttitle='%s'\n", info.title.str);
139 fprintf(stderr, "\tartist='%s'\n", info.artist.str);
140 fprintf(stderr, "\talbum='%s'\n", info.album.str);
141 fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
142 fprintf(stderr, "\ttrack number='%d'\n", info.trackno);
146 r = lms_db_audio_add(plugin->audio_db, &info);
148 free(info.title.str);
149 free(info.artist.str);
150 free(info.album.str);
151 free(info.genre.str);
157 _setup(struct plugin *plugin, struct lms_context *ctxt)
159 plugin->audio_db = lms_db_audio_new(ctxt->db);
160 if (!plugin->audio_db)
166 _start(struct plugin *plugin, struct lms_context *ctxt)
168 return lms_db_audio_start(plugin->audio_db);
172 _finish(struct plugin *plugin, struct lms_context *ctxt)
174 if (plugin->audio_db)
175 lms_db_audio_free(plugin->audio_db);
180 _close(struct plugin *plugin)
186 API struct lms_plugin *
187 lms_plugin_open(void)
189 struct plugin *plugin;
191 plugin = (struct plugin *)malloc(sizeof(*plugin));
192 plugin->plugin.name = _name;
193 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
194 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
195 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
196 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
197 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
198 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
200 return (struct lms_plugin *)plugin;
203 API const struct lms_plugin_info *
204 lms_plugin_info(void)
206 static struct lms_plugin_info info = {
212 "http://lms.garage.maemo.org"