summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibAudio/Loader.cpp
blob: 5904cfb25493c9b1e2a89e9879ceba66cd275491 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2018-2023, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/TypedTransfer.h>
#include <LibAudio/FlacLoader.h>
#include <LibAudio/Loader.h>
#include <LibAudio/MP3Loader.h>
#include <LibAudio/QOALoader.h>
#include <LibAudio/WavLoader.h>

namespace Audio {

LoaderPlugin::LoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
    : m_stream(move(stream))
{
}

Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
    : m_plugin(move(plugin))
{
}

Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::create_plugin(StringView path)
{
    {
        auto plugin = WavLoaderPlugin::create(path);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    {
        auto plugin = FlacLoaderPlugin::create(path);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    {
        auto plugin = MP3LoaderPlugin::create(path);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    {
        auto plugin = QOALoaderPlugin::create(path);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    return LoaderError { "No loader plugin available" };
}

Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::create_plugin(Bytes buffer)
{
    {
        auto plugin = WavLoaderPlugin::create(buffer);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    {
        auto plugin = FlacLoaderPlugin::create(buffer);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    {
        auto plugin = MP3LoaderPlugin::create(buffer);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    {
        auto plugin = QOALoaderPlugin::create(buffer);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    return LoaderError { "No loader plugin available" };
}

LoaderSamples Loader::get_more_samples(size_t samples_to_read_from_input)
{
    size_t remaining_samples = total_samples() - loaded_samples();
    size_t samples_to_read = min(remaining_samples, samples_to_read_from_input);
    auto samples = LOADER_TRY(FixedArray<Sample>::create(samples_to_read));

    size_t sample_index = 0;

    if (m_buffer.size() > 0) {
        size_t to_transfer = min(m_buffer.size(), samples_to_read);
        AK::TypedTransfer<Sample>::move(samples.data(), m_buffer.data(), to_transfer);
        if (to_transfer < m_buffer.size())
            m_buffer.remove(0, to_transfer);
        else
            m_buffer.clear_with_capacity();

        sample_index += to_transfer;
    }

    while (sample_index < samples_to_read) {
        auto chunk_data = TRY(m_plugin->load_chunks(samples_to_read - sample_index));
        chunk_data.remove_all_matching([](auto& chunk) { return chunk.is_empty(); });
        if (chunk_data.is_empty())
            break;
        for (auto& chunk : chunk_data) {
            if (sample_index < samples_to_read) {
                auto count = min(samples_to_read - sample_index, chunk.size());
                AK::TypedTransfer<Sample>::move(samples.span().offset(sample_index), chunk.data(), count);
                // We didn't read all of the chunk; transfer the rest into the buffer.
                if (count < chunk.size()) {
                    auto remaining_samples_count = chunk.size() - count;
                    // We will always have an empty buffer at this point!
                    LOADER_TRY(m_buffer.try_append(chunk.span().offset(count), remaining_samples_count));
                }
            } else {
                // We're now past what the user requested. Transfer the entirety of the data into the buffer.
                LOADER_TRY(m_buffer.try_append(chunk.data(), chunk.size()));
            }
            sample_index += chunk.size();
        }
    }

    return samples;
}

}