summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibAudio/UserSampleQueue.h
blob: f7c2303775e879879dd90e328205f10de1243b90 (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
/*
 * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/DisjointChunks.h>
#include <AK/FixedArray.h>
#include <AK/Noncopyable.h>
#include <AK/Vector.h>
#include <LibAudio/Sample.h>
#include <LibThreading/Mutex.h>

namespace Audio {

// A sample queue providing synchronized access to efficiently-stored segmented user-provided audio data.
class UserSampleQueue {
    AK_MAKE_NONCOPYABLE(UserSampleQueue);
    AK_MAKE_NONMOVABLE(UserSampleQueue);

public:
    UserSampleQueue() = default;

    void append(FixedArray<Sample>&& samples);
    void clear();
    // Slice off some amount of samples from the beginning.
    void discard_samples(size_t count);
    Sample operator[](size_t index);

    // The number of samples in the span.
    size_t size();
    bool is_empty();
    size_t remaining_samples();

private:
    // Re-initialize the spans after a vector resize.
    void fix_spans();

    Threading::Mutex m_sample_mutex;
    // Sample data view to keep track of what to play next.
    DisjointSpans<Sample> m_enqueued_samples;
    // The number of samples that were played from the backing store since last discarding its start.
    size_t m_samples_to_discard { 0 };
    // The backing store for the enqueued sample view.
    DisjointChunks<Sample, FixedArray<Sample>> m_backing_samples {};
};

}