summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibAudio/Metadata.cpp
blob: 394760a4d269adc7f648446b09efcead690c1e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "Metadata.h"
#include <AK/Assertions.h>
#include <LibCore/Version.h>

namespace Audio {

bool Person::is_artist() const
{
    return role == Person::Role::Artist
        || role == Person::Role::Composer
        || role == Person::Role::Conductor
        || role == Person::Role::Lyricist
        || role == Person::Role::Performer;
}

Optional<StringView> Person::name_for_role() const
{
    switch (role) {
    case Role::Artist:
    case Role::Performer:
        return {};
    case Role::Lyricist:
        return "Lyricist"sv;
    case Role::Conductor:
        return "Conductor"sv;
    case Role::Publisher:
        return "Publisher"sv;
    case Role::Engineer:
        return "Engineer"sv;
    case Role::Composer:
        return "Composer"sv;
    }
    VERIFY_NOT_REACHED();
}

void Metadata::replace_encoder_with_serenity()
{
    auto version_or_error = Core::Version::read_long_version_string();
    // Unset the encoder field in this case; we definitely want to replace the existing encoder field.
    if (version_or_error.is_error())
        encoder = {};
    auto encoder_string = String::formatted("SerenityOS LibAudio {}", version_or_error.release_value());
    if (encoder_string.is_error())
        encoder = {};
    encoder = encoder_string.release_value();
}

Optional<String> Metadata::first_artist() const
{
    auto artist = people.find_if([](auto const& person) { return person.is_artist(); });
    if (artist.is_end())
        return {};
    return artist->name;
}

ErrorOr<Optional<String>> Metadata::all_artists(StringView concatenate_with) const
{
    // FIXME: This entire function could be similar to TRY(TRY(people.filter(...).try_map(...)).join(concatenate_with)) if these functional iterator transformers existed :^)
    Vector<String> artist_texts;
    TRY(artist_texts.try_ensure_capacity(people.size()));
    for (auto const& person : people) {
        if (!person.is_artist())
            continue;
        if (auto role_name = person.name_for_role(); role_name.has_value())
            artist_texts.unchecked_append(TRY(String::formatted("{} ({})", person.name, role_name.release_value())));
        else
            artist_texts.unchecked_append(person.name);
    }
    if (artist_texts.is_empty())
        return Optional<String> {};
    return String::join(concatenate_with, artist_texts);
}

ErrorOr<void> Metadata::add_miscellaneous(String const& field, String value)
{
    // FIXME: Since try_ensure does not return a reference to the contained value, we have to retrieve it separately.
    //        This is a try_ensure bug that should be fixed.
    (void)TRY(miscellaneous.try_ensure(field, []() { return Vector<String> {}; }));
    auto& values_for_field = miscellaneous.get(field).release_value();
    return values_for_field.try_append(move(value));
}

ErrorOr<void> Metadata::add_person(Person::Role role, String name)
{
    return people.try_append(Person { role, move(name) });
}

}