2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-13 08:07:00 -03:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-07-25 15:54:09 -03:00
|
|
|
#include "Mixer.h"
|
2020-09-20 07:33:13 -03:00
|
|
|
#include <AK/Array.h>
|
LibAudio+Userland: Use new audio queue in client-server communication
Previously, we were sending Buffers to the server whenever we had new
audio data for it. This meant that for every audio enqueue action, we
needed to create a new shared memory anonymous buffer, send that
buffer's file descriptor over IPC (+recfd on the other side) and then
map the buffer into the audio server's memory to be able to play it.
This was fine for sending large chunks of audio data, like when playing
existing audio files. However, in the future we want to move to
real-time audio in some applications like Piano. This means that the
size of buffers that are sent need to be very small, as just the size of
a buffer itself is part of the audio latency. If we were to try
real-time audio with the existing system, we would run into problems
really quickly. Dealing with a continuous stream of new anonymous files
like the current audio system is rather expensive, as we need Kernel
help in multiple places. Additionally, every enqueue incurs an IPC call,
which are not optimized for >1000 calls/second (which would be needed
for real-time audio with buffer sizes of ~40 samples). So a fundamental
change in how we handle audio sending in userspace is necessary.
This commit moves the audio sending system onto a shared single producer
circular queue (SSPCQ) (introduced with one of the previous commits).
This queue is intended to live in shared memory and be accessed by
multiple processes at the same time. It was specifically written to
support the audio sending case, so e.g. it only supports a single
producer (the audio client). Now, audio sending follows these general
steps:
- The audio client connects to the audio server.
- The audio client creates a SSPCQ in shared memory.
- The audio client sends the SSPCQ's file descriptor to the audio server
with the set_buffer() IPC call.
- The audio server receives the SSPCQ and maps it.
- The audio client signals start of playback with start_playback().
- At the same time:
- The audio client writes its audio data into the shared-memory queue.
- The audio server reads audio data from the shared-memory queue(s).
Both sides have additional before-queue/after-queue buffers, depending
on the exact application.
- Pausing playback is just an IPC call, nothing happens to the buffer
except that the server stops reading from it until playback is
resumed.
- Muting has nothing to do with whether audio data is read or not.
- When the connection closes, the queues are unmapped on both sides.
This should already improve audio playback performance in a bunch of
places.
Implementation & commit notes:
- Audio loaders don't create LegacyBuffers anymore. LegacyBuffer is kept
for WavLoader, see previous commit message.
- Most intra-process audio data passing is done with FixedArray<Sample>
or Vector<Sample>.
- Improvements to most audio-enqueuing applications. (If necessary I can
try to extract some of the aplay improvements.)
- New APIs on LibAudio/ClientConnection which allows non-realtime
applications to enqueue audio in big chunks like before.
- Removal of status APIs from the audio server connection for
information that can be directly obtained from the shared queue.
- Split the pause playback API into two APIs with more intuitive names.
I know this is a large commit, and you can kinda tell from the commit
message. It's basically impossible to break this up without hacks, so
please forgive me. These are some of the best changes to the audio
subsystem and I hope that that makes up for this :yaktangle: commit.
:yakring:
2022-02-20 09:01:22 -03:00
|
|
|
#include <AK/Format.h>
|
2020-09-20 07:33:13 -03:00
|
|
|
#include <AK/MemoryStream.h>
|
2020-04-15 11:52:14 -03:00
|
|
|
#include <AK/NumericLimits.h>
|
2022-02-25 07:18:30 -03:00
|
|
|
#include <AudioServer/ConnectionFromClient.h>
|
2020-06-12 12:00:03 -03:00
|
|
|
#include <AudioServer/Mixer.h>
|
2021-07-25 15:54:09 -03:00
|
|
|
#include <LibCore/ConfigFile.h>
|
|
|
|
|
#include <LibCore/Timer.h>
|
2019-12-22 17:33:33 -03:00
|
|
|
#include <pthread.h>
|
2021-08-18 19:07:29 -03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
|
2020-06-12 12:00:03 -03:00
|
|
|
namespace AudioServer {
|
|
|
|
|
|
2021-07-25 15:54:09 -03:00
|
|
|
Mixer::Mixer(NonnullRefPtr<Core::ConfigFile> config)
|
2022-02-11 16:50:18 -03:00
|
|
|
// FIXME: Allow AudioServer to use other audio channels as well
|
|
|
|
|
: m_device(Core::File::construct("/dev/audio/0", this))
|
2021-05-22 13:47:42 -03:00
|
|
|
, m_sound_thread(Threading::Thread::construct(
|
2019-12-22 17:33:33 -03:00
|
|
|
[this] {
|
|
|
|
|
mix();
|
|
|
|
|
return 0;
|
|
|
|
|
},
|
2022-07-11 14:32:29 -03:00
|
|
|
"AudioServer[mixer]"sv))
|
2021-07-25 15:54:09 -03:00
|
|
|
, m_config(move(config))
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
{
|
2021-05-12 06:26:43 -03:00
|
|
|
if (!m_device->open(Core::OpenMode::WriteOnly)) {
|
2021-01-10 06:02:20 -03:00
|
|
|
dbgln("Can't open audio device: {}", m_device->error_string());
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-25 15:54:09 -03:00
|
|
|
m_muted = m_config->read_bool_entry("Master", "Mute", false);
|
2021-08-27 18:47:09 -03:00
|
|
|
m_main_volume = static_cast<double>(m_config->read_num_entry("Master", "Volume", 100)) / 100.0;
|
2021-07-25 15:54:09 -03:00
|
|
|
|
2020-12-31 04:55:56 -03:00
|
|
|
m_sound_thread->start();
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
}
|
|
|
|
|
|
2022-02-25 07:18:30 -03:00
|
|
|
NonnullRefPtr<ClientAudioStream> Mixer::create_queue(ConnectionFromClient& client)
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
{
|
2021-08-27 18:52:43 -03:00
|
|
|
auto queue = adopt_ref(*new ClientAudioStream(client));
|
2021-09-02 15:05:21 -03:00
|
|
|
m_pending_mutex.lock();
|
|
|
|
|
|
2019-07-28 16:27:18 -03:00
|
|
|
m_pending_mixing.append(*queue);
|
2021-09-02 15:05:21 -03:00
|
|
|
|
|
|
|
|
m_pending_mutex.unlock();
|
|
|
|
|
// Signal the mixer thread to start back up, in case nobody was connected before.
|
|
|
|
|
m_mixing_necessary.signal();
|
|
|
|
|
|
2019-07-28 16:27:18 -03:00
|
|
|
return queue;
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
}
|
|
|
|
|
|
2020-06-12 12:00:03 -03:00
|
|
|
void Mixer::mix()
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
{
|
2019-07-28 16:27:18 -03:00
|
|
|
decltype(m_pending_mixing) active_mix_queues;
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
|
|
|
|
|
for (;;) {
|
2021-09-02 15:05:21 -03:00
|
|
|
m_pending_mutex.lock();
|
|
|
|
|
// While we have nothing to mix, wait on the condition.
|
|
|
|
|
m_mixing_necessary.wait_while([this, &active_mix_queues]() { return m_pending_mixing.is_empty() && active_mix_queues.is_empty(); });
|
|
|
|
|
if (!m_pending_mixing.is_empty()) {
|
2021-06-12 08:24:45 -03:00
|
|
|
active_mix_queues.extend(move(m_pending_mixing));
|
2021-09-02 15:05:21 -03:00
|
|
|
m_pending_mixing.clear();
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
}
|
2021-09-02 15:05:21 -03:00
|
|
|
m_pending_mutex.unlock();
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
|
2019-12-22 14:30:43 -03:00
|
|
|
active_mix_queues.remove_all_matching([&](auto& entry) { return !entry->client(); });
|
|
|
|
|
|
2022-06-15 16:43:07 -03:00
|
|
|
Array<Audio::Sample, HARDWARE_BUFFER_SIZE> mixed_buffer;
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
|
2021-08-27 18:47:09 -03:00
|
|
|
m_main_volume.advance_time();
|
|
|
|
|
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
// Mix the buffers together into the output
|
2019-07-28 16:27:18 -03:00
|
|
|
for (auto& queue : active_mix_queues) {
|
|
|
|
|
if (!queue->client()) {
|
|
|
|
|
queue->clear();
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-08-27 18:57:02 -03:00
|
|
|
queue->volume().advance_time();
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
|
2022-06-15 16:43:07 -03:00
|
|
|
for (auto& mixed_sample : mixed_buffer) {
|
2021-09-23 16:16:03 -03:00
|
|
|
Audio::Sample sample;
|
2019-07-28 16:27:18 -03:00
|
|
|
if (!queue->get_next_sample(sample))
|
|
|
|
|
break;
|
2021-11-01 22:20:38 -03:00
|
|
|
if (queue->is_muted())
|
|
|
|
|
continue;
|
2021-08-27 18:47:09 -03:00
|
|
|
sample.log_multiply(SAMPLE_HEADROOM);
|
2022-06-15 16:15:58 -03:00
|
|
|
sample.log_multiply(static_cast<float>(queue->volume()));
|
2019-07-28 16:27:18 -03:00
|
|
|
mixed_sample += sample;
|
2019-07-27 12:20:41 -03:00
|
|
|
}
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
}
|
|
|
|
|
|
2020-09-20 07:33:13 -03:00
|
|
|
if (m_muted) {
|
2022-06-15 16:43:07 -03:00
|
|
|
m_device->write(m_zero_filled_buffer.data(), static_cast<int>(m_zero_filled_buffer.size()));
|
2020-09-20 07:33:13 -03:00
|
|
|
} else {
|
2022-06-15 16:43:07 -03:00
|
|
|
Array<u8, HARDWARE_BUFFER_SIZE_BYTES> buffer;
|
2020-09-20 07:33:13 -03:00
|
|
|
OutputMemoryStream stream { buffer };
|
2019-11-22 17:44:02 -03:00
|
|
|
|
2022-06-15 16:43:07 -03:00
|
|
|
for (auto& mixed_sample : mixed_buffer) {
|
2019-07-29 14:06:58 -03:00
|
|
|
|
2021-08-27 18:47:09 -03:00
|
|
|
// Even though it's not realistic, the user expects no sound at 0%.
|
|
|
|
|
if (m_main_volume < 0.01)
|
2021-11-15 18:27:28 -03:00
|
|
|
mixed_sample = Audio::Sample { 0 };
|
2021-08-27 18:47:09 -03:00
|
|
|
else
|
2022-06-15 16:15:58 -03:00
|
|
|
mixed_sample.log_multiply(static_cast<float>(m_main_volume));
|
2019-11-22 17:44:02 -03:00
|
|
|
mixed_sample.clip();
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
|
2020-09-20 07:33:13 -03:00
|
|
|
LittleEndian<i16> out_sample;
|
2022-06-15 16:15:58 -03:00
|
|
|
out_sample = static_cast<i16>(mixed_sample.left * NumericLimits<i16>::max());
|
2019-11-22 17:44:02 -03:00
|
|
|
stream << out_sample;
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
|
2022-06-15 16:15:58 -03:00
|
|
|
out_sample = static_cast<i16>(mixed_sample.right * NumericLimits<i16>::max());
|
2019-11-22 17:44:02 -03:00
|
|
|
stream << out_sample;
|
|
|
|
|
}
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(stream.is_end());
|
|
|
|
|
VERIFY(!stream.has_any_error());
|
2022-06-15 16:43:07 -03:00
|
|
|
m_device->write(stream.data(), static_cast<int>(stream.size()));
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 07:54:52 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-27 12:20:41 -03:00
|
|
|
|
2021-08-27 18:47:09 -03:00
|
|
|
void Mixer::set_main_volume(double volume)
|
2020-07-20 22:16:48 -03:00
|
|
|
{
|
2021-03-25 22:03:51 -03:00
|
|
|
if (volume < 0)
|
|
|
|
|
m_main_volume = 0;
|
2021-08-27 18:47:09 -03:00
|
|
|
else if (volume > 2)
|
|
|
|
|
m_main_volume = 2;
|
2020-11-28 23:56:40 -03:00
|
|
|
else
|
|
|
|
|
m_main_volume = volume;
|
2021-07-25 15:54:09 -03:00
|
|
|
|
2021-08-27 18:47:09 -03:00
|
|
|
m_config->write_num_entry("Master", "Volume", static_cast<int>(volume * 100));
|
2021-07-25 15:54:09 -03:00
|
|
|
request_setting_sync();
|
|
|
|
|
|
2022-02-25 07:18:30 -03:00
|
|
|
ConnectionFromClient::for_each([&](ConnectionFromClient& client) {
|
2021-08-27 18:47:09 -03:00
|
|
|
client.did_change_main_mix_volume({}, main_volume());
|
2020-07-20 22:16:48 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 12:00:03 -03:00
|
|
|
void Mixer::set_muted(bool muted)
|
2019-11-22 17:44:02 -03:00
|
|
|
{
|
2019-11-23 13:21:12 -03:00
|
|
|
if (m_muted == muted)
|
|
|
|
|
return;
|
2019-11-22 17:44:02 -03:00
|
|
|
m_muted = muted;
|
2021-07-25 15:54:09 -03:00
|
|
|
|
|
|
|
|
m_config->write_bool_entry("Master", "Mute", m_muted);
|
|
|
|
|
request_setting_sync();
|
|
|
|
|
|
2022-02-25 07:18:30 -03:00
|
|
|
ConnectionFromClient::for_each([muted](ConnectionFromClient& client) {
|
2021-11-01 21:52:22 -03:00
|
|
|
client.did_change_main_mix_muted_state({}, muted);
|
2019-11-23 13:21:12 -03:00
|
|
|
});
|
2019-11-22 17:44:02 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-24 08:20:20 -03:00
|
|
|
int Mixer::audiodevice_set_sample_rate(u32 sample_rate)
|
2021-08-18 19:07:29 -03:00
|
|
|
{
|
|
|
|
|
int code = ioctl(m_device->fd(), SOUNDCARD_IOCTL_SET_SAMPLE_RATE, sample_rate);
|
|
|
|
|
if (code != 0)
|
2021-11-20 21:56:23 -03:00
|
|
|
dbgln("Error while setting sample rate to {}: ioctl error: {}", sample_rate, strerror(errno));
|
2021-08-18 19:07:29 -03:00
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 08:20:20 -03:00
|
|
|
u32 Mixer::audiodevice_get_sample_rate() const
|
2021-08-18 19:07:29 -03:00
|
|
|
{
|
2021-11-24 08:20:20 -03:00
|
|
|
u32 sample_rate = 0;
|
2021-08-18 19:07:29 -03:00
|
|
|
int code = ioctl(m_device->fd(), SOUNDCARD_IOCTL_GET_SAMPLE_RATE, &sample_rate);
|
|
|
|
|
if (code != 0)
|
2021-11-20 21:56:23 -03:00
|
|
|
dbgln("Error while getting sample rate: ioctl error: {}", strerror(errno));
|
2021-08-18 19:07:29 -03:00
|
|
|
return sample_rate;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-25 15:54:09 -03:00
|
|
|
void Mixer::request_setting_sync()
|
|
|
|
|
{
|
|
|
|
|
if (m_config_write_timer.is_null() || !m_config_write_timer->is_active()) {
|
|
|
|
|
m_config_write_timer = Core::Timer::create_single_shot(
|
|
|
|
|
AUDIO_CONFIG_WRITE_INTERVAL,
|
|
|
|
|
[this] {
|
2022-02-06 11:26:33 -03:00
|
|
|
if (auto result = m_config->sync(); result.is_error())
|
|
|
|
|
dbgln("Failed to write audio mixer config: {}", result.error());
|
2021-07-25 15:54:09 -03:00
|
|
|
},
|
|
|
|
|
this);
|
|
|
|
|
m_config_write_timer->start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 07:18:30 -03:00
|
|
|
ClientAudioStream::ClientAudioStream(ConnectionFromClient& client)
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 19:26:13 -03:00
|
|
|
: m_client(client)
|
2019-07-28 16:27:18 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 12:00:03 -03:00
|
|
|
}
|