mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 19:27:36 +00:00
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:
This commit is contained in:
parent
cb0e95c928
commit
49b087f3cd
36 changed files with 485 additions and 278 deletions
|
@ -22,9 +22,18 @@
|
|||
#include <LibAudio/Sample.h>
|
||||
#include <LibAudio/SampleFormats.h>
|
||||
#include <LibCore/AnonymousBuffer.h>
|
||||
#include <LibCore/SharedCircularQueue.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Audio {
|
||||
|
||||
static constexpr size_t AUDIO_BUFFERS_COUNT = 128;
|
||||
// The audio buffer size is specifically chosen to be about 1/1000th of a second (1ms).
|
||||
// This has the biggest impact on latency and performance.
|
||||
// The currently chosen value was not put here with much thought and a better choice is surely possible.
|
||||
static constexpr size_t AUDIO_BUFFER_SIZE = 50;
|
||||
using AudioQueue = Core::SharedSingleProducerCircularQueue<Array<Sample, AUDIO_BUFFER_SIZE>, AUDIO_BUFFERS_COUNT>;
|
||||
|
||||
using namespace AK::Exponentials;
|
||||
|
||||
// A buffer of audio samples.
|
||||
|
|
|
@ -8,6 +8,7 @@ set(SOURCES
|
|||
FlacLoader.cpp
|
||||
WavWriter.cpp
|
||||
MP3Loader.cpp
|
||||
UserSampleQueue.cpp
|
||||
)
|
||||
|
||||
set(GENERATED_SOURCES
|
||||
|
@ -16,4 +17,4 @@ set(GENERATED_SOURCES
|
|||
)
|
||||
|
||||
serenity_lib(LibAudio audio)
|
||||
target_link_libraries(LibAudio LibCore LibIPC)
|
||||
target_link_libraries(LibAudio LibCore LibIPC LibThreading)
|
||||
|
|
|
@ -1,48 +1,130 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibAudio/Buffer.h>
|
||||
#include <AK/Atomic.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/Time.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibAudio/ConnectionFromClient.h>
|
||||
#include <LibAudio/UserSampleQueue.h>
|
||||
#include <LibCore/Event.h>
|
||||
#include <LibThreading/Mutex.h>
|
||||
#include <time.h>
|
||||
|
||||
namespace Audio {
|
||||
|
||||
// FIXME: We don't know what is a good value for this.
|
||||
// Real-time audio may be improved with a lower value.
|
||||
static timespec g_enqueue_wait_time { 0, 10'000'000 };
|
||||
|
||||
ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
|
||||
: IPC::ConnectionToServer<AudioClientEndpoint, AudioServerEndpoint>(*this, move(socket))
|
||||
, m_buffer(make<AudioQueue>(MUST(AudioQueue::try_create())))
|
||||
, m_user_queue(make<UserSampleQueue>())
|
||||
, m_background_audio_enqueuer(Threading::Thread::construct([this]() {
|
||||
// All the background thread does is run an event loop.
|
||||
Core::EventLoop enqueuer_loop;
|
||||
m_enqueuer_loop = &enqueuer_loop;
|
||||
enqueuer_loop.exec();
|
||||
m_enqueuer_loop_destruction.lock();
|
||||
m_enqueuer_loop = nullptr;
|
||||
m_enqueuer_loop_destruction.unlock();
|
||||
return (intptr_t) nullptr;
|
||||
}))
|
||||
{
|
||||
m_background_audio_enqueuer->start();
|
||||
set_buffer(*m_buffer);
|
||||
}
|
||||
|
||||
void ConnectionFromClient::enqueue(LegacyBuffer const& buffer)
|
||||
ConnectionFromClient::~ConnectionFromClient()
|
||||
{
|
||||
for (;;) {
|
||||
auto success = enqueue_buffer(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
|
||||
if (success)
|
||||
break;
|
||||
nanosleep(&g_enqueue_wait_time, nullptr);
|
||||
die();
|
||||
}
|
||||
|
||||
void ConnectionFromClient::die()
|
||||
{
|
||||
// We're sometimes getting here after the other thread has already exited and its event loop does no longer exist.
|
||||
m_enqueuer_loop_destruction.lock();
|
||||
if (m_enqueuer_loop != nullptr) {
|
||||
m_enqueuer_loop->wake();
|
||||
m_enqueuer_loop->quit(0);
|
||||
}
|
||||
m_enqueuer_loop_destruction.unlock();
|
||||
(void)m_background_audio_enqueuer->join();
|
||||
}
|
||||
|
||||
void ConnectionFromClient::async_enqueue(LegacyBuffer const& buffer)
|
||||
ErrorOr<void> ConnectionFromClient::async_enqueue(FixedArray<Sample>&& samples)
|
||||
{
|
||||
async_enqueue_buffer(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
|
||||
update_good_sleep_time();
|
||||
m_user_queue->append(move(samples));
|
||||
// Wake the background thread to make sure it starts enqueuing audio.
|
||||
if (!m_audio_enqueuer_active.load())
|
||||
m_enqueuer_loop->post_event(*this, make<Core::CustomEvent>(0), Core::EventLoop::ShouldWake::Yes);
|
||||
async_start_playback();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
bool ConnectionFromClient::try_enqueue(LegacyBuffer const& buffer)
|
||||
void ConnectionFromClient::clear_client_buffer()
|
||||
{
|
||||
return enqueue_buffer(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
|
||||
m_user_queue->clear();
|
||||
}
|
||||
|
||||
void ConnectionFromClient::finished_playing_buffer(i32 buffer_id)
|
||||
void ConnectionFromClient::update_good_sleep_time()
|
||||
{
|
||||
if (on_finish_playing_buffer)
|
||||
on_finish_playing_buffer(buffer_id);
|
||||
auto sample_rate = static_cast<double>(get_sample_rate());
|
||||
auto buffer_play_time_ns = 1'000'000'000.0 / (sample_rate / static_cast<double>(AUDIO_BUFFER_SIZE));
|
||||
// A factor of 1 should be good for now.
|
||||
m_good_sleep_time = Time::from_nanoseconds(static_cast<unsigned>(buffer_play_time_ns)).to_timespec();
|
||||
}
|
||||
|
||||
// Non-realtime audio writing loop
|
||||
void ConnectionFromClient::custom_event(Core::CustomEvent&)
|
||||
{
|
||||
Array<Sample, AUDIO_BUFFER_SIZE> next_chunk;
|
||||
while (true) {
|
||||
m_audio_enqueuer_active.store(true);
|
||||
|
||||
if (m_user_queue->is_empty()) {
|
||||
dbgln("Reached end of provided audio data, going to sleep");
|
||||
break;
|
||||
}
|
||||
|
||||
auto available_samples = min(AUDIO_BUFFER_SIZE, m_user_queue->size());
|
||||
for (size_t i = 0; i < available_samples; ++i)
|
||||
next_chunk[i] = (*m_user_queue)[i];
|
||||
|
||||
m_user_queue->discard_samples(available_samples);
|
||||
|
||||
// FIXME: Could we recieve interrupts in a good non-IPC way instead?
|
||||
auto result = m_buffer->try_blocking_enqueue(next_chunk, [this]() {
|
||||
nanosleep(&m_good_sleep_time, nullptr);
|
||||
});
|
||||
if (result.is_error())
|
||||
dbgln("Error while writing samples to shared buffer: {}", result.error());
|
||||
}
|
||||
m_audio_enqueuer_active.store(false);
|
||||
}
|
||||
|
||||
ErrorOr<void, AudioQueue::QueueStatus> ConnectionFromClient::realtime_enqueue(Array<Sample, AUDIO_BUFFER_SIZE> samples)
|
||||
{
|
||||
return m_buffer->try_enqueue(samples);
|
||||
}
|
||||
|
||||
unsigned ConnectionFromClient::total_played_samples() const
|
||||
{
|
||||
return m_buffer->weak_tail() * AUDIO_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
unsigned ConnectionFromClient::remaining_samples()
|
||||
{
|
||||
return static_cast<unsigned>(m_user_queue->remaining_samples());
|
||||
}
|
||||
|
||||
size_t ConnectionFromClient::remaining_buffers() const
|
||||
{
|
||||
return m_buffer->size() - m_buffer->weak_remaining_capacity();
|
||||
}
|
||||
|
||||
void ConnectionFromClient::main_mix_muted_state_changed(bool muted)
|
||||
|
|
|
@ -1,29 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Concepts.h>
|
||||
#include <AK/FixedArray.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AudioServer/AudioClientEndpoint.h>
|
||||
#include <AudioServer/AudioServerEndpoint.h>
|
||||
#include <LibAudio/Buffer.h>
|
||||
#include <LibAudio/UserSampleQueue.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/Object.h>
|
||||
#include <LibIPC/ConnectionToServer.h>
|
||||
#include <LibThreading/Mutex.h>
|
||||
#include <LibThreading/Thread.h>
|
||||
|
||||
namespace Audio {
|
||||
|
||||
class LegacyBuffer;
|
||||
|
||||
class ConnectionFromClient final
|
||||
: public IPC::ConnectionToServer<AudioClientEndpoint, AudioServerEndpoint>
|
||||
, public AudioClientEndpoint {
|
||||
IPC_CLIENT_CONNECTION(ConnectionFromClient, "/tmp/portal/audio")
|
||||
public:
|
||||
void enqueue(LegacyBuffer const&);
|
||||
bool try_enqueue(LegacyBuffer const&);
|
||||
void async_enqueue(LegacyBuffer const&);
|
||||
virtual ~ConnectionFromClient() override;
|
||||
|
||||
// Both of these APIs are for convenience and when you don't care about real-time behavior.
|
||||
// They will not work properly in conjunction with realtime_enqueue.
|
||||
// If you don't refill the buffer in time with this API, the last shared buffer write is zero-padded to play all of the samples.
|
||||
template<ArrayLike<Sample> Samples>
|
||||
ErrorOr<void> async_enqueue(Samples&& samples)
|
||||
{
|
||||
return async_enqueue(TRY(FixedArray<Sample>::try_create(samples.span())));
|
||||
}
|
||||
|
||||
ErrorOr<void> async_enqueue(FixedArray<Sample>&& samples);
|
||||
|
||||
void clear_client_buffer();
|
||||
|
||||
// Returns immediately with the appropriate status if the buffer is full; use in conjunction with remaining_buffers to get low latency.
|
||||
ErrorOr<void, AudioQueue::QueueStatus> realtime_enqueue(Array<Sample, AUDIO_BUFFER_SIZE> samples);
|
||||
|
||||
// This information can be deducted from the shared audio buffer.
|
||||
unsigned total_played_samples() const;
|
||||
// How many samples remain in m_enqueued_samples.
|
||||
unsigned remaining_samples();
|
||||
// How many buffers (i.e. short sample arrays) the server hasn't played yet.
|
||||
// Non-realtime code needn't worry about this.
|
||||
size_t remaining_buffers() const;
|
||||
|
||||
virtual void die() override;
|
||||
|
||||
Function<void(i32 buffer_id)> on_finish_playing_buffer;
|
||||
Function<void(bool muted)> on_main_mix_muted_state_change;
|
||||
Function<void(double volume)> on_main_mix_volume_change;
|
||||
Function<void(double volume)> on_client_volume_change;
|
||||
|
@ -31,10 +63,31 @@ public:
|
|||
private:
|
||||
ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket>);
|
||||
|
||||
virtual void finished_playing_buffer(i32) override;
|
||||
virtual void main_mix_muted_state_changed(bool) override;
|
||||
virtual void main_mix_volume_changed(double) override;
|
||||
virtual void client_volume_changed(double) override;
|
||||
|
||||
// We use this to perform the audio enqueuing on the background thread's event loop
|
||||
virtual void custom_event(Core::CustomEvent&) override;
|
||||
|
||||
// FIXME: This should be called every time the sample rate changes, but we just cautiously call it on every non-realtime enqueue.
|
||||
void update_good_sleep_time();
|
||||
|
||||
// Shared audio buffer: both server and client constantly read and write to/from this.
|
||||
// This needn't be mutex protected: it's internally multi-threading aware.
|
||||
OwnPtr<AudioQueue> m_buffer;
|
||||
|
||||
// The queue of non-realtime audio provided by the user.
|
||||
NonnullOwnPtr<UserSampleQueue> m_user_queue;
|
||||
|
||||
NonnullRefPtr<Threading::Thread> m_background_audio_enqueuer;
|
||||
Core::EventLoop* m_enqueuer_loop;
|
||||
Threading::Mutex m_enqueuer_loop_destruction;
|
||||
Atomic<bool> m_audio_enqueuer_active { false };
|
||||
|
||||
// A good amount of time to sleep when the queue is full.
|
||||
// (Only used for non-realtime enqueues)
|
||||
timespec m_good_sleep_time {};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -242,7 +242,7 @@ LoaderSamples FlacLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_i
|
|||
{
|
||||
ssize_t remaining_samples = static_cast<ssize_t>(m_total_samples - m_loaded_samples);
|
||||
if (remaining_samples <= 0)
|
||||
return LegacyBuffer::create_empty();
|
||||
return FixedArray<Sample> {};
|
||||
|
||||
// FIXME: samples_to_read is calculated wrong, because when seeking not all samples are loaded.
|
||||
size_t samples_to_read = min(max_bytes_to_read_from_input, remaining_samples);
|
||||
|
@ -267,10 +267,8 @@ LoaderSamples FlacLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_i
|
|||
}
|
||||
|
||||
m_loaded_samples += sample_index;
|
||||
auto maybe_buffer = LegacyBuffer::create_with_samples(move(samples));
|
||||
if (maybe_buffer.is_error())
|
||||
return LoaderError { LoaderError::Category::Internal, m_loaded_samples, "Couldn't allocate sample buffer" };
|
||||
return maybe_buffer.release_value();
|
||||
|
||||
return samples;
|
||||
}
|
||||
|
||||
MaybeLoaderError FlacLoaderPlugin::next_frame(Span<Sample> target_vector)
|
||||
|
|
|
@ -22,7 +22,7 @@ namespace Audio {
|
|||
|
||||
static constexpr StringView no_plugin_error = "No loader plugin available";
|
||||
|
||||
using LoaderSamples = Result<NonnullRefPtr<LegacyBuffer>, LoaderError>;
|
||||
using LoaderSamples = Result<FixedArray<Sample>, LoaderError>;
|
||||
using MaybeLoaderError = Result<void, LoaderError>;
|
||||
|
||||
class LoaderPlugin {
|
||||
|
@ -60,7 +60,7 @@ public:
|
|||
static Result<NonnullRefPtr<Loader>, LoaderError> create(StringView path) { return adopt_ref(*new Loader(TRY(try_create(path)))); }
|
||||
static Result<NonnullRefPtr<Loader>, LoaderError> create(Bytes& buffer) { return adopt_ref(*new Loader(TRY(try_create(buffer)))); }
|
||||
|
||||
LoaderSamples get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) const { return m_plugin->get_more_samples(max_bytes_to_read_from_input); }
|
||||
LoaderSamples get_more_samples(size_t max_samples_to_read_from_input = 128 * KiB) const { return m_plugin->get_more_samples(max_samples_to_read_from_input); }
|
||||
|
||||
MaybeLoaderError reset() const { return m_plugin->reset(); }
|
||||
MaybeLoaderError seek(int const position) const { return m_plugin->seek(position); }
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "MP3Loader.h"
|
||||
#include "MP3HuffmanTables.h"
|
||||
#include "MP3Tables.h"
|
||||
#include <AK/FixedArray.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/FileStream.h>
|
||||
|
||||
|
@ -115,18 +116,17 @@ MaybeLoaderError MP3LoaderPlugin::seek(int const position)
|
|||
return {};
|
||||
}
|
||||
|
||||
LoaderSamples MP3LoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input)
|
||||
LoaderSamples MP3LoaderPlugin::get_more_samples(size_t max_samples_to_read_from_input)
|
||||
{
|
||||
Vector<Sample> samples;
|
||||
FixedArray<Sample> samples = LOADER_TRY(FixedArray<Sample>::try_create(max_samples_to_read_from_input));
|
||||
|
||||
size_t samples_to_read = max_bytes_to_read_from_input;
|
||||
samples.resize(samples_to_read);
|
||||
size_t samples_to_read = max_samples_to_read_from_input;
|
||||
while (samples_to_read > 0) {
|
||||
if (!m_current_frame.has_value()) {
|
||||
auto maybe_frame = read_next_frame();
|
||||
if (maybe_frame.is_error()) {
|
||||
if (m_input_stream->unreliable_eof()) {
|
||||
return LegacyBuffer::create_empty();
|
||||
return FixedArray<Sample> {};
|
||||
}
|
||||
return maybe_frame.release_error();
|
||||
}
|
||||
|
@ -156,10 +156,7 @@ LoaderSamples MP3LoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_in
|
|||
}
|
||||
|
||||
m_loaded_samples += samples.size();
|
||||
auto maybe_buffer = LegacyBuffer::create_with_samples(move(samples));
|
||||
if (maybe_buffer.is_error())
|
||||
return LoaderError { LoaderError::Category::Internal, m_loaded_samples, "Couldn't allocate sample buffer" };
|
||||
return maybe_buffer.release_value();
|
||||
return samples;
|
||||
}
|
||||
|
||||
MaybeLoaderError MP3LoaderPlugin::build_seek_table()
|
||||
|
|
63
Userland/Libraries/LibAudio/UserSampleQueue.cpp
Normal file
63
Userland/Libraries/LibAudio/UserSampleQueue.cpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* 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);
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
51
Userland/Libraries/LibAudio/UserSampleQueue.h
Normal file
51
Userland/Libraries/LibAudio/UserSampleQueue.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* 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/Format.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 {};
|
||||
};
|
||||
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
#include "WavLoader.h"
|
||||
#include "Buffer.h"
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/FixedArray.h>
|
||||
#include <AK/NumericLimits.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/Try.h>
|
||||
|
@ -53,7 +54,7 @@ LoaderSamples WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_in
|
|||
|
||||
int remaining_samples = m_total_samples - m_loaded_samples;
|
||||
if (remaining_samples <= 0)
|
||||
return LegacyBuffer::create_empty();
|
||||
return FixedArray<Sample> {};
|
||||
|
||||
// One "sample" contains data from all channels.
|
||||
// In the Wave spec, this is also called a block.
|
||||
|
@ -88,7 +89,7 @@ LoaderSamples WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_in
|
|||
|
||||
// m_loaded_samples should contain the amount of actually loaded samples
|
||||
m_loaded_samples += samples_to_read;
|
||||
return buffer.release_value();
|
||||
return LOADER_TRY(buffer.value()->to_sample_array());
|
||||
}
|
||||
|
||||
MaybeLoaderError WavLoaderPlugin::seek(int const sample_index)
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#include <LibCore/FileStream.h>
|
||||
|
||||
namespace Audio {
|
||||
class LegacyBuffer;
|
||||
|
||||
// defines for handling the WAV header data
|
||||
#define WAVE_FORMAT_PCM 0x0001 // PCM
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue