1 /* GStreamer taglib-based APEv2 muxer
2 * Copyright (C) 2006 Christophe Fergeau <teuf@gnome.org>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-apev2mux
24 * @see_also: #GstTagSetter
26 * This element adds APEv2 tags to the beginning of a stream using the taglib
29 * Applications can set the tags to write using the #GstTagSetter interface.
30 * Tags sent by upstream elements will be picked up automatically (and merged
31 * according to the merge mode set via the tag setter interface).
34 * <title>Example pipelines</title>
36 * gst-launch -v filesrc location=foo.ogg ! decodebin ! audioconvert ! lame ! apev2mux ! filesink location=foo.mp3
37 * ]| A pipeline that transcodes a file from Ogg/Vorbis to mp3 format with an
38 * APEv2 that contains the same as the the Ogg/Vorbis file. Make sure the
39 * Ogg/Vorbis file actually has comments to preserve.
41 * gst-launch -m filesrc location=foo.mp3 ! apedemux ! fakesink silent=TRUE 2> /dev/null | grep taglist
42 * ]| Verify that tags have been written.
50 #include "gstapev2mux.h"
55 #include <gst/tag/tag.h>
57 using namespace TagLib;
59 GST_DEBUG_CATEGORY_STATIC (gst_apev2_mux_debug);
60 #define GST_CAT_DEFAULT gst_apev2_mux_debug
62 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
65 GST_STATIC_CAPS ("application/x-apetag"));
67 GST_BOILERPLATE (GstApev2Mux, gst_apev2_mux, GstTagLibMux,
68 GST_TYPE_TAG_LIB_MUX);
70 static GstBuffer *gst_apev2_mux_render_tag (GstTagLibMux * mux,
71 GstTagList * taglist);
74 gst_apev2_mux_base_init (gpointer g_class)
76 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
78 gst_element_class_add_static_pad_template (element_class, &src_template);
80 gst_element_class_set_details_simple (element_class,
81 "TagLib-based APEv2 Muxer", "Formatter/Metadata",
82 "Adds an APEv2 header to the beginning of files using taglib",
83 "Sebastian Dröge <slomo@circular-chaos.org>");
85 GST_DEBUG_CATEGORY_INIT (gst_apev2_mux_debug, "apev2mux", 0,
86 "taglib-based APEv2 tag muxer");
90 gst_apev2_mux_class_init (GstApev2MuxClass * klass)
92 GST_TAG_LIB_MUX_CLASS (klass)->render_tag =
93 GST_DEBUG_FUNCPTR (gst_apev2_mux_render_tag);
97 gst_apev2_mux_init (GstApev2Mux * apev2mux, GstApev2MuxClass * apev2mux_class)
103 add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
105 APE::Tag * apev2tag = (APE::Tag *) user_data;
108 /* FIXME: if there are several values set for the same tag, this won't
109 * work, only the first value will be taken into account
111 if (strcmp (tag, GST_TAG_TITLE) == 0) {
114 result = gst_tag_list_peek_string_index (list, tag, 0, &title);
115 if (result != FALSE) {
116 GST_DEBUG ("Setting title to %s", title);
117 apev2tag->setTitle (String (title, String::UTF8));
119 } else if (strcmp (tag, GST_TAG_ALBUM) == 0) {
122 result = gst_tag_list_peek_string_index (list, tag, 0, &album);
123 if (result != FALSE) {
124 GST_DEBUG ("Setting album to %s", album);
125 apev2tag->setAlbum (String (album, String::UTF8));
127 } else if (strcmp (tag, GST_TAG_ARTIST) == 0) {
130 result = gst_tag_list_peek_string_index (list, tag, 0, &artist);
131 if (result != FALSE) {
132 GST_DEBUG ("Setting artist to %s", artist);
133 apev2tag->setArtist (String (artist, String::UTF8));
135 } else if (strcmp (tag, GST_TAG_COMPOSER) == 0) {
136 const char *composer;
138 result = gst_tag_list_peek_string_index (list, tag, 0, &composer);
139 if (result != FALSE) {
140 GST_DEBUG ("Setting composer to %s", composer);
141 apev2tag->addValue (String ("COMPOSER", String::UTF8),
142 String (composer, String::UTF8));
144 } else if (strcmp (tag, GST_TAG_GENRE) == 0) {
147 result = gst_tag_list_peek_string_index (list, tag, 0, &genre);
148 if (result != FALSE) {
149 GST_DEBUG ("Setting genre to %s", genre);
150 apev2tag->setGenre (String (genre, String::UTF8));
152 } else if (strcmp (tag, GST_TAG_COMMENT) == 0) {
155 result = gst_tag_list_peek_string_index (list, tag, 0, &comment);
156 if (result != FALSE) {
157 GST_DEBUG ("Setting comment to %s", comment);
158 apev2tag->setComment (String (comment, String::UTF8));
160 } else if (strcmp (tag, GST_TAG_DATE) == 0) {
163 result = gst_tag_list_get_date_index (list, tag, 0, &date);
164 if (result != FALSE) {
167 year = g_date_get_year (date);
168 GST_DEBUG ("Setting track year to %d", year);
169 apev2tag->setYear (year);
172 } else if (strcmp (tag, GST_TAG_TRACK_NUMBER) == 0) {
175 result = gst_tag_list_get_uint_index (list, tag, 0, &track_number);
176 if (result != FALSE) {
179 result = gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT,
184 tag_str = g_strdup_printf ("%d/%d", track_number, total_tracks);
185 GST_DEBUG ("Setting track number to %s", tag_str);
186 apev2tag->addValue (String ("TRACK", String::UTF8),
187 String (tag_str, String::UTF8), true);
190 GST_DEBUG ("Setting track number to %d", track_number);
191 apev2tag->setTrack (track_number);
194 } else if (strcmp (tag, GST_TAG_TRACK_COUNT) == 0) {
197 if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_NUMBER, 0, &n)) {
198 GST_DEBUG ("track-count handled with track-number, skipping");
199 } else if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT, 0, &n)) {
202 tag_str = g_strdup_printf ("0/%d", n);
203 GST_DEBUG ("Setting track number to %s", tag_str);
204 apev2tag->addValue (String ("TRACK", String::UTF8),
205 String (tag_str, String::UTF8), true);
209 } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
212 result = gst_tag_list_get_uint_index (list, tag, 0, &volume_number);
214 if (result != FALSE) {
218 result = gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
222 tag_str = g_strdup_printf ("CD %d/%d", volume_number, volume_count);
224 tag_str = g_strdup_printf ("CD %d", volume_number);
227 GST_DEBUG ("Setting album number to %s", tag_str);
229 apev2tag->addValue (String ("MEDIA", String::UTF8),
230 String (tag_str, String::UTF8), true);
233 } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_COUNT) == 0) {
236 if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_NUMBER, 0, &n)) {
237 GST_DEBUG ("volume-count handled with volume-number, skipping");
238 } else if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
242 tag_str = g_strdup_printf ("CD 0/%u", n);
243 GST_DEBUG ("Setting album volume number/count to %s", tag_str);
245 apev2tag->addValue (String ("MEDIA", String::UTF8),
246 String (tag_str, String::UTF8), true);
250 } else if (strcmp (tag, GST_TAG_COPYRIGHT) == 0) {
251 const gchar *copyright;
253 result = gst_tag_list_peek_string_index (list, tag, 0, ©right);
255 if (result != FALSE) {
256 GST_DEBUG ("Setting copyright to %s", copyright);
257 apev2tag->addValue (String ("COPYRIGHT", String::UTF8),
258 String (copyright, String::UTF8), true);
260 } else if (strcmp (tag, GST_TAG_LOCATION) == 0) {
261 const gchar *location;
263 result = gst_tag_list_peek_string_index (list, tag, 0, &location);
265 if (result != FALSE) {
266 GST_DEBUG ("Setting location to %s", location);
267 apev2tag->addValue (String ("FILE", String::UTF8),
268 String (location, String::UTF8), true);
270 } else if (strcmp (tag, GST_TAG_ISRC) == 0) {
273 result = gst_tag_list_peek_string_index (list, tag, 0, &isrc);
275 if (result != FALSE) {
276 GST_DEBUG ("Setting ISRC to %s", isrc);
277 apev2tag->addValue (String ("ISRC", String::UTF8),
278 String (isrc, String::UTF8), true);
280 } else if (strcmp (tag, GST_TAG_TRACK_GAIN) == 0) {
283 result = gst_tag_list_get_double_index (list, tag, 0, &value);
285 if (result != FALSE) {
286 gchar *track_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
288 track_gain = g_ascii_dtostr (track_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
289 GST_DEBUG ("Setting track gain to %s", track_gain);
290 apev2tag->addValue (String ("REPLAYGAIN_TRACK_GAIN",
291 String::UTF8), String (track_gain, String::UTF8), true);
294 } else if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0) {
297 result = gst_tag_list_get_double_index (list, tag, 0, &value);
299 if (result != FALSE) {
300 gchar *track_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
302 track_peak = g_ascii_dtostr (track_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
303 GST_DEBUG ("Setting track peak to %s", track_peak);
304 apev2tag->addValue (String ("REPLAYGAIN_TRACK_PEAK",
305 String::UTF8), String (track_peak, String::UTF8), true);
308 } else if (strcmp (tag, GST_TAG_ALBUM_GAIN) == 0) {
311 result = gst_tag_list_get_double_index (list, tag, 0, &value);
313 if (result != FALSE) {
314 gchar *album_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
316 album_gain = g_ascii_dtostr (album_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
317 GST_DEBUG ("Setting album gain to %s", album_gain);
318 apev2tag->addValue (String ("REPLAYGAIN_ALBUM_GAIN",
319 String::UTF8), String (album_gain, String::UTF8), true);
322 } else if (strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
325 result = gst_tag_list_get_double_index (list, tag, 0, &value);
327 if (result != FALSE) {
328 gchar *album_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
330 album_peak = g_ascii_dtostr (album_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
331 GST_DEBUG ("Setting album peak to %s", album_peak);
332 apev2tag->addValue (String ("REPLAYGAIN_ALBUM_PEAK",
333 String::UTF8), String (album_peak, String::UTF8), true);
337 GST_WARNING ("Unsupported tag: %s", tag);
342 gst_apev2_mux_render_tag (GstTagLibMux * mux, GstTagList * taglist)
345 ByteVector rendered_tag;
350 gst_tag_list_foreach (taglist, add_one_tag, &apev2tag);
352 rendered_tag = apev2tag.render ();
353 tag_size = rendered_tag.size ();
355 GST_LOG_OBJECT (mux, "tag size = %d bytes", tag_size);
357 /* Create buffer with tag */
358 buf = gst_buffer_new_and_alloc (tag_size);
359 memcpy (GST_BUFFER_DATA (buf), rendered_tag.data (), tag_size);
360 gst_buffer_set_caps (buf, GST_PAD_CAPS (mux->srcpad));
366 gst_apev2_mux_plugin_init (GstPlugin * plugin)
368 if (!gst_element_register (plugin, "apev2mux", GST_RANK_NONE,