2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-07-25 15:54:09 -03:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
|
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"
|
2021-09-02 15:05:21 -03:00
|
|
|
#include "AK/Format.h"
|
2020-09-20 07:33:13 -03:00
|
|
|
#include <AK/Array.h>
|
|
|
|
|
#include <AK/MemoryStream.h>
|
2020-04-15 11:52:14 -03:00
|
|
|
#include <AK/NumericLimits.h>
|
2020-06-12 12:00:03 -03:00
|
|
|
#include <AudioServer/ClientConnection.h>
|
|
|
|
|
#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-06-16 09:04:40 -03:00
|
|
|
u8 Mixer::m_zero_filled_buffer[4096];
|
|
|
|
|
|
2021-07-25 15:54:09 -03:00
|
|
|
Mixer::Mixer(NonnullRefPtr<Core::ConfigFile> config)
|
2020-02-02 08:34:39 -03:00
|
|
|
: m_device(Core::File::construct("/dev/audio", 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;
|
|
|
|
|
},
|
2020-12-31 04:55:56 -03:00
|
|
|
"AudioServer[mixer]"))
|
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
|
|
|
}
|
|
|
|
|
|
2020-06-12 12:00:03 -03:00
|
|
|
Mixer::~Mixer()
|
2019-08-18 07:56:36 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 18:52:43 -03:00
|
|
|
NonnullRefPtr<ClientAudioStream> Mixer::create_queue(ClientConnection& 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(); });
|
|
|
|
|
|
2021-03-19 18:15:54 -03:00
|
|
|
Audio::Frame mixed_buffer[1024];
|
|
|
|
|
auto mixed_buffer_length = (int)(sizeof(mixed_buffer) / sizeof(Audio::Frame));
|
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();
|
|
|
|
|
|
|
|
|
|
int active_queues = 0;
|
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:47:09 -03:00
|
|
|
++active_queues;
|
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
|
|
|
|
2019-07-28 16:27:18 -03:00
|
|
|
for (int i = 0; i < mixed_buffer_length; ++i) {
|
|
|
|
|
auto& mixed_sample = mixed_buffer[i];
|
2021-03-19 18:15:54 -03:00
|
|
|
Audio::Frame sample;
|
2019-07-28 16:27:18 -03:00
|
|
|
if (!queue->get_next_sample(sample))
|
|
|
|
|
break;
|
2021-08-27 18:47:09 -03:00
|
|
|
sample.log_multiply(SAMPLE_HEADROOM);
|
2021-08-27 18:57:02 -03:00
|
|
|
sample.log_multiply(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) {
|
2021-06-16 09:04:40 -03:00
|
|
|
m_device->write(m_zero_filled_buffer, sizeof(m_zero_filled_buffer));
|
2020-09-20 07:33:13 -03:00
|
|
|
} else {
|
|
|
|
|
Array<u8, 4096> buffer;
|
|
|
|
|
OutputMemoryStream stream { buffer };
|
2019-11-22 17:44:02 -03:00
|
|
|
|
|
|
|
|
for (int i = 0; i < mixed_buffer_length; ++i) {
|
|
|
|
|
auto& mixed_sample = mixed_buffer[i];
|
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)
|
|
|
|
|
mixed_sample = { 0 };
|
|
|
|
|
else
|
|
|
|
|
mixed_sample.log_multiply(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;
|
2020-04-15 11:52:14 -03:00
|
|
|
out_sample = 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
|
|
|
|
2020-04-15 11:52:14 -03:00
|
|
|
out_sample = 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());
|
2020-09-20 07:33:13 -03:00
|
|
|
m_device->write(stream.data(), 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();
|
|
|
|
|
|
2021-04-23 20:06:09 -03:00
|
|
|
ClientConnection::for_each([&](ClientConnection& 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();
|
|
|
|
|
|
2020-06-12 12:00:03 -03:00
|
|
|
ClientConnection::for_each([muted](ClientConnection& client) {
|
2019-11-23 13:21:12 -03:00
|
|
|
client.did_change_muted_state({}, muted);
|
|
|
|
|
});
|
2019-11-22 17:44:02 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-18 19:07:29 -03:00
|
|
|
int Mixer::audiodevice_set_sample_rate(u16 sample_rate)
|
|
|
|
|
{
|
|
|
|
|
int code = ioctl(m_device->fd(), SOUNDCARD_IOCTL_SET_SAMPLE_RATE, sample_rate);
|
|
|
|
|
if (code != 0)
|
|
|
|
|
dbgln("Error while setting sample rate to {}: ioctl returned with {}", sample_rate, strerror(code));
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u16 Mixer::audiodevice_get_sample_rate() const
|
|
|
|
|
{
|
|
|
|
|
u16 sample_rate = 0;
|
|
|
|
|
int code = ioctl(m_device->fd(), SOUNDCARD_IOCTL_GET_SAMPLE_RATE, &sample_rate);
|
|
|
|
|
if (code != 0)
|
|
|
|
|
dbgln("Error while getting sample rate: ioctl returned with {}", strerror(code));
|
|
|
|
|
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] {
|
|
|
|
|
m_config->sync();
|
|
|
|
|
},
|
|
|
|
|
this);
|
|
|
|
|
m_config_write_timer->start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 18:52:43 -03:00
|
|
|
ClientAudioStream::ClientAudioStream(ClientConnection& 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
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 18:52:43 -03:00
|
|
|
void ClientAudioStream::enqueue(NonnullRefPtr<Audio::Buffer>&& buffer)
|
2019-07-27 12:20:41 -03:00
|
|
|
{
|
2019-10-19 14:10:53 -03:00
|
|
|
m_remaining_samples += buffer->sample_count();
|
2019-07-28 16:27:18 -03:00
|
|
|
m_queue.enqueue(move(buffer));
|
2019-07-27 12:20:41 -03:00
|
|
|
}
|
2020-06-12 12:00:03 -03:00
|
|
|
}
|