blob: 97c4a19e1f6d1b904bc7477ace95c10fb9bffc9c (
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
|
#include <AK/BufferStream.h>
#include <AudioServer/ASClientConnection.h>
#include <AudioServer/ASMixer.h>
#include <limits>
ASMixer::ASMixer()
: m_device(CFile::construct("/dev/audio", this))
, m_sound_thread([this] {
mix();
return 0;
}, "AudioServer[mixer]")
{
if (!m_device->open(CIODevice::WriteOnly)) {
dbgprintf("Can't open audio device: %s\n", m_device->error_string());
return;
}
m_zero_filled_buffer = (u8*)malloc(4096);
bzero(m_zero_filled_buffer, 4096);
m_sound_thread.start();
}
ASMixer::~ASMixer()
{
}
NonnullRefPtr<ASBufferQueue> ASMixer::create_queue(ASClientConnection& client)
{
LOCKER(m_lock);
auto queue = adopt(*new ASBufferQueue(client));
m_pending_mixing.append(*queue);
return queue;
}
void ASMixer::mix()
{
decltype(m_pending_mixing) active_mix_queues;
for (;;) {
{
LOCKER(m_lock);
active_mix_queues.append(move(m_pending_mixing));
}
// ### use a wakeup of some kind rather than this garbage
if (active_mix_queues.size() == 0) {
// nothing to mix yet
usleep(10000);
continue;
}
active_mix_queues.remove_all_matching([&](auto& entry) { return !entry->client(); });
ASample mixed_buffer[1024];
auto mixed_buffer_length = (int)(sizeof(mixed_buffer) / sizeof(ASample));
// Mix the buffers together into the output
for (auto& queue : active_mix_queues) {
if (!queue->client()) {
queue->clear();
continue;
}
for (int i = 0; i < mixed_buffer_length; ++i) {
auto& mixed_sample = mixed_buffer[i];
ASample sample;
if (!queue->get_next_sample(sample))
break;
mixed_sample += sample;
}
}
bool muted = m_muted;
// output the mixed stuff to the device
u8 raw_buffer[4096];
auto buffer = ByteBuffer::wrap(muted ? m_zero_filled_buffer : raw_buffer, sizeof(raw_buffer));
BufferStream stream(buffer);
if (!muted) {
for (int i = 0; i < mixed_buffer_length; ++i) {
auto& mixed_sample = mixed_buffer[i];
mixed_sample.scale(m_main_volume);
mixed_sample.clip();
i16 out_sample;
out_sample = mixed_sample.left * std::numeric_limits<i16>::max();
stream << out_sample;
ASSERT(!stream.at_end()); // we should have enough space for both channels in one buffer!
out_sample = mixed_sample.right * std::numeric_limits<i16>::max();
stream << out_sample;
}
}
if (stream.offset() != 0) {
buffer.trim(stream.offset());
}
m_device->write(buffer);
}
}
void ASMixer::set_muted(bool muted)
{
if (m_muted == muted)
return;
m_muted = muted;
ASClientConnection::for_each([muted](ASClientConnection& client) {
client.did_change_muted_state({}, muted);
});
}
ASBufferQueue::ASBufferQueue(ASClientConnection& client)
: m_client(client.make_weak_ptr())
{
}
void ASBufferQueue::enqueue(NonnullRefPtr<ABuffer>&& buffer)
{
m_remaining_samples += buffer->sample_count();
m_queue.enqueue(move(buffer));
}
|