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>
29 #include <shared/util.h>
34 #include <FLAC/metadata.h>
37 struct lms_plugin plugin;
38 lms_db_audio_t *audio_db;
41 static const char _name[] = "flac";
42 static const struct lms_string_size _codec = LMS_STATIC_STRING_SIZE("flac");
43 static const struct lms_string_size _exts[] = {
44 LMS_STATIC_STRING_SIZE(".flac")
46 static const char *_cats[] = {
51 static const char *_authors[] = {
52 "Andre Moreira Magalhaes",
57 _match(struct plugin *p, const char *path, int len, int base)
61 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
65 return (void*)(i + 1);
69 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
71 struct lms_audio_info info = { };
72 FLAC__StreamMetadata *tags = NULL;
73 FLAC__StreamMetadata si;
78 if (!FLAC__metadata_get_streaminfo(finfo->path, &si)) {
79 fprintf(stderr, "ERROR: cannot retrieve file %s STREAMINFO block\n",
84 info.channels = si.data.stream_info.channels;
85 info.sampling_rate = si.data.stream_info.sample_rate;
87 if (si.data.stream_info.sample_rate) {
88 info.length = si.data.stream_info.total_samples / si.data.stream_info.sample_rate;
89 info.bitrate = (finfo->size * 8) / info.length;
93 info.container = _codec;
95 if (!FLAC__metadata_get_tags(finfo->path, &tags)) {
96 fprintf(stderr, "ERROR: cannot retrieve file %s tags\n", finfo->path);
100 for (i = 0; i < tags->data.vorbis_comment.num_comments; i++) {
101 str = (char *) tags->data.vorbis_comment.comments[i].entry;
102 len = tags->data.vorbis_comment.comments[i].length;
103 if (strncmp(str, "TITLE=", 6) == 0) {
104 info.title.str = malloc((len - 6 + 1) * sizeof(char));
105 strcpy(info.title.str, str + 6);
106 info.title.len = len - 6;
108 else if (strncmp(str, "ARTIST=", 7) == 0) {
109 info.artist.str = malloc((len - 7 + 1) * sizeof(char));
110 strcpy(info.artist.str, str + 7);
111 info.artist.len = len - 7;
113 else if (strncmp(str, "ALBUM=", 6) == 0) {
114 info.album.str = malloc((len - 6 + 1) * sizeof(char));
115 strcpy(info.album.str, str + 6);
116 info.album.len = len - 6;
118 else if (strncmp(str, "GENRE=", 6) == 0) {
119 info.genre.str = malloc((len - 6 + 1) * sizeof(char));
120 strcpy(info.genre.str, str + 6);
121 info.genre.len = len - 6;
123 else if (strncmp(str, "TRACKNUMBER=", 12) == 0)
124 info.trackno = atoi(str + 12);
127 FLAC__metadata_object_delete(tags);
129 lms_string_size_strip_and_free(&info.title);
130 lms_string_size_strip_and_free(&info.artist);
131 lms_string_size_strip_and_free(&info.album);
132 lms_string_size_strip_and_free(&info.genre);
136 info.title = str_extract_name_from_path(finfo->path, finfo->path_len,
138 &_exts[((long) match) - 1],
141 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
144 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
146 lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
148 lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
151 fprintf(stderr, "file %s info\n", finfo->path);
152 fprintf(stderr, "\ttitle='%s'\n", info.title.str);
153 fprintf(stderr, "\tartist='%s'\n", info.artist.str);
154 fprintf(stderr, "\talbum='%s'\n", info.album.str);
155 fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
156 fprintf(stderr, "\ttrack number='%d'\n", info.trackno);
160 r = lms_db_audio_add(plugin->audio_db, &info);
162 free(info.title.str);
163 free(info.artist.str);
164 free(info.album.str);
165 free(info.genre.str);
171 _setup(struct plugin *plugin, struct lms_context *ctxt)
173 plugin->audio_db = lms_db_audio_new(ctxt->db);
174 if (!plugin->audio_db)
180 _start(struct plugin *plugin, struct lms_context *ctxt)
182 return lms_db_audio_start(plugin->audio_db);
186 _finish(struct plugin *plugin, struct lms_context *ctxt)
188 if (plugin->audio_db)
189 lms_db_audio_free(plugin->audio_db);
194 _close(struct plugin *plugin)
200 API struct lms_plugin *
201 lms_plugin_open(void)
203 struct plugin *plugin;
205 plugin = (struct plugin *)malloc(sizeof(*plugin));
206 plugin->plugin.name = _name;
207 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
208 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
209 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
210 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
211 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
212 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
214 return (struct lms_plugin *)plugin;
217 API const struct lms_plugin_info *
218 lms_plugin_info(void)
220 static struct lms_plugin_info info = {
226 "http://lms.garage.maemo.org"