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

#include "UserSampleQueue.h"

namespace Audio {

void UserSampleQueue::append(FixedArray<Sample>&& samples)
{
    Threading::MutexLocker lock(m_sample_mutex);
    if (m_samples_to_discard != 0)
        m_backing_samples = m_backing_samples.release_slice(m_samples_to_discard);
    m_backing_samples.append(move(samples));
    fix_spans();
}

void UserSampleQueue::clear()
{
    discard_samples(size());
}

void UserSampleQueue::fix_spans()
{
    Threading::MutexLocker lock(m_sample_mutex);
    m_enqueued_samples = m_backing_samples.spans();
    m_samples_to_discard = 0;
}

Sample UserSampleQueue::operator[](size_t index)
{
    Threading::MutexLocker lock(m_sample_mutex);
    return m_enqueued_samples[index];
}

void UserSampleQueue::discard_samples(size_t count)
{
    Threading::MutexLocker lock(m_sample_mutex);
    m_samples_to_discard += count;
    m_enqueued_samples = m_enqueued_samples.slice(count);
}

size_t UserSampleQueue::size()
{
    Threading::MutexLocker lock(m_sample_mutex);
    return m_enqueued_samples.size();
}

size_t UserSampleQueue::remaining_samples()
{
    Threading::MutexLocker lock(m_sample_mutex);
    VERIFY(m_backing_samples.size() >= m_samples_to_discard);
    return m_backing_samples.size() - m_samples_to_discard;
}

bool UserSampleQueue::is_empty()
{
    Threading::MutexLocker lock(m_sample_mutex);
    return m_enqueued_samples.is_empty();
}

}