NO-OP: whitespace
[ardour.git] / libs / ardour / session_metadata.cc
1 /*
2  * Copyright (C) 2008-2012 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2009 David Robillard <d@drobilla.net>
5  * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015 Colin Fletcher <colin.m.fletcher@googlemail.com>
7  *
8  * This program 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  * This program 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 along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "ardour/session_metadata.h"
24
25 #include <iostream>
26 #include <sstream>
27
28 using namespace std;
29 using namespace Glib;
30 using namespace ARDOUR;
31
32 SessionMetadata *SessionMetadata::_metadata = NULL;  //singleton instance
33
34 SessionMetadata::SessionMetadata ()
35 {
36         /*** General ***/
37         map.insert (Property ("description", ""));
38
39         /*** Track/Song Data ***/
40         map.insert (Property ("comment", ""));
41         map.insert (Property ("copyright", ""));
42         map.insert (Property ("isrc", ""));
43         map.insert (Property ("year", ""));
44
45         /*** Title and friends ***/
46         map.insert (Property ("grouping", ""));
47         map.insert (Property ("title", ""));
48         map.insert (Property ("subtitle", ""));
49
50         /*** People... ***/
51         map.insert (Property ("artist", ""));
52         map.insert (Property ("album_artist", ""));
53         map.insert (Property ("lyricist", ""));
54         map.insert (Property ("composer", ""));
55         map.insert (Property ("conductor", ""));
56         map.insert (Property ("remixer", ""));
57         map.insert (Property ("arranger", ""));
58         map.insert (Property ("engineer", ""));
59         map.insert (Property ("producer", ""));
60         map.insert (Property ("dj_mixer", ""));
61         map.insert (Property ("mixer", ""));
62         //map.insert (Property ("performers", "")); // Multiple values [instrument]
63
64         /*** Education... ***/
65         map.insert (Property ("instructor", ""));
66         map.insert (Property ("course", ""));
67
68         /*** Album info ***/
69         map.insert (Property ("album", ""));
70         map.insert (Property ("compilation", ""));
71         map.insert (Property ("disc_subtitle", ""));
72         map.insert (Property ("disc_number", ""));
73         map.insert (Property ("total_discs", ""));
74         map.insert (Property ("track_number", ""));
75         map.insert (Property ("total_tracks", ""));
76
77         /*** Style ***/
78         map.insert (Property ("genre", ""));
79         //map.insert (Property ("mood", ""));
80         //map.insert (Property ("bpm", ""));
81
82         /*** Other ***/
83         //map.insert (Property ("lyrics", ""));
84         //map.insert (Property ("media", ""));
85         //map.insert (Property ("label", ""));
86         map.insert (Property ("barcode", ""));
87         //map.insert (Property ("encoded_by", ""));
88         //map.insert (Property ("catalog_number", ""));
89
90         /*** Sorting orders ***/
91         //map.insert (Property ("album_sort", ""));
92         //map.insert (Property ("album_artist_sort", ""));
93         //map.insert (Property ("artist_sort", ""));
94         //map.insert (Property ("title_sort", ""));
95
96         /*** Globals ***/
97         user_map.insert (Property ("user_name", ""));
98         user_map.insert (Property ("user_email", ""));
99         user_map.insert (Property ("user_web", ""));
100         user_map.insert (Property ("user_organization", ""));
101         user_map.insert (Property ("user_country", ""));
102 }
103
104 SessionMetadata::~SessionMetadata ()
105 {
106
107 }
108
109 XMLNode *
110 SessionMetadata::get_xml (const string & name)
111 {
112         string value = get_value (name);
113         if (value.empty()) {
114                 return 0;
115         }
116
117         XMLNode val ("value", value);
118         XMLNode * node = new XMLNode (name);
119         node->add_child_copy (val);
120
121         return node;
122 }
123
124 string
125 SessionMetadata::get_value (const string & name) const
126 {
127         PropertyMap::const_iterator it = map.find (name);
128         if (it == map.end()) {
129                 it = user_map.find (name);
130                 if (it == user_map.end()) {
131                         // Should not be reached, except if loading metadata from a newer version with a new type
132                         std::cerr << "Programming error in SessionMetadata::get_value (" << name << ")" << std::endl;
133                         return "";
134                 }
135         }
136
137         return it->second;
138 }
139
140 uint32_t
141 SessionMetadata::get_uint_value (const string & name) const
142 {
143         return atoi (get_value (name).c_str());
144 }
145
146 void
147 SessionMetadata::set_value (const string & name, const string & value)
148 {
149         PropertyMap::iterator it = map.find (name);
150         if (it == map.end()) {
151                 it = user_map.find (name);
152                 if (it == user_map.end()) {
153                         // Should not be reached, except if loading metadata from a newer version with a new type
154                         std::cerr << "Programming error in SessionMetadata::set_value (" << name << ")" << std::endl;
155                         return;
156                 }
157         }
158
159         it->second = value;
160 }
161
162 void
163 SessionMetadata::set_value (const string & name, uint32_t value)
164 {
165         std::ostringstream oss;
166         oss << value;
167         if (oss.str().compare("0")) {
168                 set_value (name, oss.str());
169         } else {
170                 set_value (name, "");
171         }
172 }
173
174 /*** Serialization ***/
175 XMLNode &
176 SessionMetadata::get_state ()
177 {
178         XMLNode * node = new XMLNode ("Metadata");
179         XMLNode * prop;
180
181         for (PropertyMap::const_iterator it = map.begin(); it != map.end(); ++it) {
182                 if ((prop = get_xml (it->first))) {
183                         node->add_child_nocopy (*prop);
184                 }
185         }
186
187         return *node;
188 }
189
190 int
191 SessionMetadata::set_state (const XMLNode & state, int /*version_num*/)
192 {
193         const XMLNodeList & children = state.children();
194         string name;
195         string value;
196         XMLNode * node;
197
198         for (XMLNodeConstIterator it = children.begin(); it != children.end(); it++) {
199                 node = *it;
200                 if (node->children().empty()) {
201                         continue;
202                 }
203
204                 name = node->name();
205                 node = *node->children().begin();
206                 value = node->content();
207
208                 set_value (name, value);
209         }
210
211         return 0;
212 }
213
214
215 XMLNode &
216 SessionMetadata::get_user_state ()
217 {
218         XMLNode * node = new XMLNode ("Metadata");
219         XMLNode * prop;
220
221         for (PropertyMap::const_iterator it = user_map.begin(); it != user_map.end(); ++it) {
222                 if ((prop = get_xml (it->first))) {
223                         node->add_child_nocopy (*prop);
224                 }
225         }
226
227         return *node;
228 }
229
230 /*** Accessing ***/
231 string
232 SessionMetadata::description () const
233 {
234         return get_value("description");
235 }
236
237 string
238 SessionMetadata::comment () const
239 {
240         return get_value("comment");
241 }
242
243 string
244 SessionMetadata::copyright () const
245 {
246         return get_value("copyright");
247 }
248
249 string
250 SessionMetadata::isrc () const
251 {
252         return get_value("isrc");
253 }
254
255 uint32_t
256 SessionMetadata::year () const
257 {
258         return get_uint_value("year");
259 }
260
261 string
262 SessionMetadata::grouping () const
263 {
264         return get_value("grouping");
265 }
266
267 string
268 SessionMetadata::barcode () const
269 {
270         return get_value("barcode");
271 }
272
273 string
274 SessionMetadata::title () const
275 {
276         return get_value("title");
277 }
278
279 string
280 SessionMetadata::subtitle () const
281 {
282         return get_value("subtitle");
283 }
284
285 string
286 SessionMetadata::artist () const
287 {
288         return get_value("artist");
289 }
290
291 string
292 SessionMetadata::album_artist () const
293 {
294         return get_value("album_artist");
295 }
296
297 string
298 SessionMetadata::lyricist () const
299 {
300         return get_value("lyricist");
301 }
302
303 string
304 SessionMetadata::composer () const
305 {
306         return get_value("composer");
307 }
308
309 string
310 SessionMetadata::conductor () const
311 {
312         return get_value("conductor");
313 }
314
315 string
316 SessionMetadata::remixer () const
317 {
318         return get_value("remixer");
319 }
320
321 string
322 SessionMetadata::arranger () const
323 {
324         return get_value("arranger");
325 }
326
327 string
328 SessionMetadata::engineer () const
329 {
330         return get_value("engineer");
331 }
332
333 string
334 SessionMetadata::producer () const
335 {
336         return get_value("producer");
337 }
338
339 string
340 SessionMetadata::dj_mixer () const
341 {
342         return get_value("dj_mixer");
343 }
344
345 string
346 SessionMetadata::mixer () const
347 {
348         return get_value("mixer");
349 }
350
351 string
352 SessionMetadata::album () const
353 {
354         return get_value("album");
355 }
356
357 string
358 SessionMetadata::compilation () const
359 {
360         return get_value("compilation");
361 }
362
363 string
364 SessionMetadata::disc_subtitle () const
365 {
366         return get_value("disc_subtitle");
367 }
368
369 uint32_t
370 SessionMetadata::disc_number () const
371 {
372         return get_uint_value("disc_number");
373 }
374
375 uint32_t
376 SessionMetadata::total_discs () const
377 {
378         return get_uint_value("total_discs");
379 }
380
381 uint32_t
382 SessionMetadata::track_number () const
383 {
384         return get_uint_value("track_number");
385 }
386
387 uint32_t
388 SessionMetadata::total_tracks () const
389 {
390         return get_uint_value("total_tracks");
391 }
392
393 string
394 SessionMetadata::genre () const
395 {
396         return get_value("genre");
397 }
398
399 string
400 SessionMetadata::instructor () const
401 {
402         return get_value("instructor");
403 }
404
405 string
406 SessionMetadata::course () const
407 {
408         return get_value("course");
409 }
410
411
412 string
413 SessionMetadata::user_name () const
414 {
415         return get_value("user_name");
416 }
417
418 string
419 SessionMetadata::user_email () const
420 {
421         return get_value("user_email");
422 }
423
424 string
425 SessionMetadata::user_web () const
426 {
427         return get_value("user_web");
428 }
429
430 string
431 SessionMetadata::organization () const
432 {
433         return get_value("user_organization");
434 }
435
436 string
437 SessionMetadata::country () const
438 {
439         return get_value("user_country");
440 }
441
442
443
444 /*** Editing ***/
445 void
446 SessionMetadata::set_description (const string & v)
447 {
448         set_value ("description", v);
449 }
450
451 void
452 SessionMetadata::set_comment (const string & v)
453 {
454         set_value ("comment", v);
455 }
456
457 void
458 SessionMetadata::set_copyright (const string & v)
459 {
460         set_value ("copyright", v);
461 }
462
463 void
464 SessionMetadata::set_isrc (const string & v)
465 {
466         set_value ("isrc", v);
467 }
468
469 void
470 SessionMetadata::set_year (uint32_t v)
471 {
472         set_value ("year", v);
473 }
474
475 void
476 SessionMetadata::set_grouping (const string & v)
477 {
478         set_value ("grouping", v);
479 }
480
481 void
482 SessionMetadata::set_barcode (const string & v)
483 {
484         set_value ("barcode", v);
485 }
486
487 void
488 SessionMetadata::set_title (const string & v)
489 {
490         set_value ("title", v);
491 }
492
493 void
494 SessionMetadata::set_subtitle (const string & v)
495 {
496         set_value ("subtitle", v);
497 }
498
499 void
500 SessionMetadata::set_artist (const string & v)
501 {
502         set_value ("artist", v);
503 }
504
505 void
506 SessionMetadata::set_album_artist (const string & v)
507 {
508         set_value ("album_artist", v);
509 }
510
511 void
512 SessionMetadata::set_lyricist (const string & v)
513 {
514         set_value ("lyricist", v);
515 }
516
517 void
518 SessionMetadata::set_composer (const string & v)
519 {
520         set_value ("composer", v);
521 }
522
523 void
524 SessionMetadata::set_conductor (const string & v)
525 {
526         set_value ("conductor", v);
527 }
528
529 void
530 SessionMetadata::set_remixer (const string & v)
531 {
532         set_value ("remixer", v);
533 }
534
535 void
536 SessionMetadata::set_arranger (const string & v)
537 {
538         set_value ("arranger", v);
539 }
540
541 void
542 SessionMetadata::set_engineer (const string & v)
543 {
544         set_value ("engineer", v);
545 }
546
547 void
548 SessionMetadata::set_producer (const string & v)
549 {
550         set_value ("producer", v);
551 }
552
553 void
554 SessionMetadata::set_dj_mixer (const string & v)
555 {
556         set_value ("dj_mixer", v);
557 }
558
559 void
560 SessionMetadata::set_mixer (const string & v)
561 {
562         set_value ("mixer", v);
563 }
564
565 void
566 SessionMetadata::set_album (const string & v)
567 {
568         set_value ("album", v);
569 }
570
571 void
572 SessionMetadata::set_compilation (const string & v)
573 {
574         set_value ("compilation", v);
575 }
576
577 void
578 SessionMetadata::set_disc_subtitle (const string & v)
579 {
580         set_value ("disc_subtitle", v);
581 }
582
583 void
584 SessionMetadata::set_disc_number (uint32_t v)
585 {
586         set_value ("disc_number", v);
587 }
588
589 void
590 SessionMetadata::set_total_discs (uint32_t v)
591 {
592         set_value ("total_discs", v);
593 }
594
595 void
596 SessionMetadata::set_track_number (uint32_t v)
597 {
598         set_value ("track_number", v);
599 }
600
601 void
602 SessionMetadata::set_total_tracks (uint32_t v)
603 {
604         set_value ("total_tracks", v);
605 }
606
607 void
608 SessionMetadata::set_genre (const string & v)
609 {
610         set_value ("genre", v);
611 }
612
613 void
614 SessionMetadata::set_instructor (const string & v)
615 {
616         set_value ("instructor", v);
617 }
618
619 void
620 SessionMetadata::set_course (const string & v)
621 {
622         set_value ("course", v);
623 }
624
625 void
626 SessionMetadata::set_user_name (const string & v)
627 {
628         set_value ("user_name", v);
629 }
630
631 void
632 SessionMetadata::set_user_email (const string & v)
633 {
634         set_value ("user_email", v);
635 }
636
637 void
638 SessionMetadata::set_user_web (const string & v)
639 {
640         set_value ("user_web", v);
641 }
642
643 void
644 SessionMetadata::set_organization (const string & v)
645 {
646         set_value ("user_organization", v);
647 }
648 void
649 SessionMetadata::set_country (const string & v)
650 {
651         set_value ("user_country", v);
652 }
653
654 void
655 SessionMetadata::av_export_tag (MetaDataMap& meta) const
656 {
657         /* this is used for ffmpeg/liblame -metadata key=value
658          * (video and mp3 export).
659          * for flac/ogg's vorbis-comment see:
660          * AudiofileTagger::tag_generic()
661          * AudiofileTagger::tag_vorbis_comment()
662          */
663         if (year() > 0) {
664                 std::ostringstream osstream; osstream << year();
665                 meta["year"] = osstream.str();
666         }
667         if (track_number() > 0) {
668                 std::ostringstream osstream; osstream << track_number();
669                 meta["track"] = osstream.str();
670         }
671         if (disc_number() > 0) {
672                 std::ostringstream osstream; osstream << disc_number();
673                 meta["disc"] = osstream.str();
674         }
675         if (!title().empty())        { meta["title"] = title(); }
676         if (!artist().empty())       { meta["author"] = artist(); }
677         if (!album_artist().empty()) { meta["album_artist"] = album_artist(); }
678         if (!album().empty())        { meta["album"] = album(); }
679         if (!genre().empty())        { meta["genre"] = genre(); }
680         if (!composer().empty())     { meta["composer"] = composer(); }
681         if (!comment().empty())      { meta["comment"] = comment(); }
682         if (!copyright().empty())    { meta["copyright"] = copyright(); }
683         if (!subtitle().empty())     { meta["description"] = subtitle(); }
684 }