LibAudio+Everywhere: Rename Audio::Buffer -> Audio::LegacyBuffer

With the following change in how we send audio, the old Buffer type is
not really needed anymore. However, moving WavLoader to the new system
is a bit more involved and out of the scope of this PR. Therefore, we
need to keep Buffer around, but to make it clear that it's the old
buffer type which will be removed soon, we rename it to LegacyBuffer.
Most of the users will be gone after the next commit anyways.
This commit is contained in:
kleines Filmröllchen 2022-02-20 13:18:38 +01:00 committed by Linus Groh
parent fc7d231b00
commit cb0e95c928
Notes: sideshowbarker 2024-07-17 11:37:09 +09:00
21 changed files with 54 additions and 54 deletions

View file

@ -9,8 +9,8 @@
#include "TrackManager.h"
// Converts Piano-internal data to an Audio::Buffer that AudioServer receives
static NonnullRefPtr<Audio::Buffer> music_samples_to_buffer(Array<Sample, sample_count> samples)
// Converts Piano-internal data to an Audio::LegacyBuffer that AudioServer receives
static NonnullRefPtr<Audio::LegacyBuffer> music_samples_to_buffer(Array<Sample, sample_count> samples)
{
Vector<Audio::Sample, sample_count> frames;
frames.ensure_capacity(sample_count);
@ -19,7 +19,7 @@ static NonnullRefPtr<Audio::Buffer> music_samples_to_buffer(Array<Sample, sample
frames.unchecked_append(frame);
}
// FIXME: Handle OOM better.
return MUST(Audio::Buffer::create_with_samples(frames));
return MUST(Audio::LegacyBuffer::create_with_samples(frames));
}
AudioPlayerLoop::AudioPlayerLoop(TrackManager& track_manager, bool& need_to_write_wav, Audio::WavWriter& wav_writer)
@ -42,7 +42,7 @@ AudioPlayerLoop::AudioPlayerLoop(TrackManager& track_manager, bool& need_to_writ
void AudioPlayerLoop::enqueue_audio()
{
m_track_manager.fill_buffer(m_buffer);
NonnullRefPtr<Audio::Buffer> audio_buffer = music_samples_to_buffer(m_buffer);
NonnullRefPtr<Audio::LegacyBuffer> audio_buffer = music_samples_to_buffer(m_buffer);
// FIXME: Handle OOM better.
audio_buffer = MUST(Audio::resample_buffer(m_resampler.value(), *audio_buffer));
m_audio_client->async_enqueue(audio_buffer);

View file

@ -32,12 +32,12 @@ public:
int last_seek() const { return m_last_seek; }
bool is_paused() const { return m_paused; }
float total_length() const { return m_total_length; }
RefPtr<Audio::Buffer> current_buffer() const { return m_current_buffer; }
RefPtr<Audio::LegacyBuffer> current_buffer() const { return m_current_buffer; }
NonnullRefPtr<Audio::ConnectionFromClient> connection() const { return m_connection; }
Function<void()> on_update;
Function<void(Audio::Buffer&)> on_load_sample_buffer;
Function<void(Audio::LegacyBuffer&)> on_load_sample_buffer;
Function<void()> on_finished_playing;
private:
@ -56,7 +56,7 @@ private:
size_t m_source_buffer_size_bytes { 0 };
RefPtr<Audio::Loader> m_loader { nullptr };
NonnullRefPtr<Audio::ConnectionFromClient> m_connection;
RefPtr<Audio::Buffer> m_current_buffer;
RefPtr<Audio::LegacyBuffer> m_current_buffer;
Queue<i32, always_enqueued_buffer_count + 1> m_enqueued_buffers;
Optional<Audio::ResampleHelper<double>> m_resampler;
RefPtr<Core::Timer> m_timer;

View file

@ -72,7 +72,7 @@ public:
virtual void volume_changed(double) = 0;
virtual void mute_changed(bool) = 0;
virtual void total_samples_changed(int) = 0;
virtual void sound_buffer_played(RefPtr<Audio::Buffer>, [[maybe_unused]] int sample_rate, [[maybe_unused]] int samples_played) = 0;
virtual void sound_buffer_played(RefPtr<Audio::LegacyBuffer>, [[maybe_unused]] int sample_rate, [[maybe_unused]] int samples_played) = 0;
protected:
void done_initializing()

View file

@ -216,7 +216,7 @@ void SoundPlayerWidgetAdvancedView::total_samples_changed(int total_samples)
m_playback_progress_slider->set_page_step(total_samples / 10);
}
void SoundPlayerWidgetAdvancedView::sound_buffer_played(RefPtr<Audio::Buffer> buffer, int sample_rate, int samples_played)
void SoundPlayerWidgetAdvancedView::sound_buffer_played(RefPtr<Audio::LegacyBuffer> buffer, int sample_rate, int samples_played)
{
m_visualization->set_buffer(buffer);
m_visualization->set_samplerate(sample_rate);

View file

@ -46,7 +46,7 @@ public:
virtual void volume_changed(double) override;
virtual void mute_changed(bool) override;
virtual void total_samples_changed(int) override;
virtual void sound_buffer_played(RefPtr<Audio::Buffer>, int sample_rate, int samples_played) override;
virtual void sound_buffer_played(RefPtr<Audio::LegacyBuffer>, int sample_rate, int samples_played) override;
protected:
void keydown_event(GUI::KeyEvent&) override;

View file

@ -18,7 +18,7 @@ class VisualizationWidget : public GUI::Frame {
public:
virtual void render(GUI::PaintEvent&, FixedArray<double> const& samples) = 0;
void set_buffer(RefPtr<Audio::Buffer> buffer)
void set_buffer(RefPtr<Audio::LegacyBuffer> buffer)
{
if (buffer.is_null())
return;

View file

@ -13,7 +13,7 @@
namespace Audio {
i32 Buffer::allocate_id()
i32 LegacyBuffer::allocate_id()
{
static Atomic<i32> next_id;
return next_id++;
@ -97,13 +97,13 @@ static double read_norm_sample_8(InputMemoryStream& stream)
return double(sample) / NumericLimits<u8>::max();
}
ErrorOr<NonnullRefPtr<Buffer>> Buffer::from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format)
ErrorOr<NonnullRefPtr<LegacyBuffer>> LegacyBuffer::from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format)
{
InputMemoryStream stream { data };
return from_pcm_stream(stream, num_channels, sample_format, data.size() / (pcm_bits_per_sample(sample_format) / 8));
}
ErrorOr<NonnullRefPtr<Buffer>> Buffer::from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples)
ErrorOr<NonnullRefPtr<LegacyBuffer>> LegacyBuffer::from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples)
{
Vector<Sample> fdata;
fdata.ensure_capacity(num_samples);
@ -133,7 +133,7 @@ ErrorOr<NonnullRefPtr<Buffer>> Buffer::from_pcm_stream(InputMemoryStream& stream
// don't belong.
VERIFY(!stream.handle_any_error());
return Buffer::create_with_samples(move(fdata));
return LegacyBuffer::create_with_samples(move(fdata));
}
}

View file

@ -28,23 +28,23 @@ namespace Audio {
using namespace AK::Exponentials;
// A buffer of audio samples.
class Buffer : public RefCounted<Buffer> {
class LegacyBuffer : public RefCounted<LegacyBuffer> {
public:
static ErrorOr<NonnullRefPtr<Buffer>> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
static ErrorOr<NonnullRefPtr<Buffer>> from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples);
static ErrorOr<NonnullRefPtr<LegacyBuffer>> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
static ErrorOr<NonnullRefPtr<LegacyBuffer>> from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples);
template<ArrayLike<Sample> ArrayT>
static ErrorOr<NonnullRefPtr<Buffer>> create_with_samples(ArrayT&& samples)
static ErrorOr<NonnullRefPtr<LegacyBuffer>> create_with_samples(ArrayT&& samples)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) Buffer(move(samples)));
return adopt_nonnull_ref_or_enomem(new (nothrow) LegacyBuffer(move(samples)));
}
static ErrorOr<NonnullRefPtr<Buffer>> create_with_anonymous_buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
static ErrorOr<NonnullRefPtr<LegacyBuffer>> create_with_anonymous_buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) Buffer(move(buffer), buffer_id, sample_count));
return adopt_nonnull_ref_or_enomem(new (nothrow) LegacyBuffer(move(buffer), buffer_id, sample_count));
}
static NonnullRefPtr<Buffer> create_empty()
static NonnullRefPtr<LegacyBuffer> create_empty()
{
// If we can't allocate an empty buffer, things are in a very bad state.
return MUST(adopt_nonnull_ref_or_enomem(new (nothrow) Buffer));
return MUST(adopt_nonnull_ref_or_enomem(new (nothrow) LegacyBuffer));
}
Sample const* samples() const { return (Sample const*)data(); }
@ -64,7 +64,7 @@ public:
private:
template<ArrayLike<Sample> ArrayT>
explicit Buffer(ArrayT&& samples)
explicit LegacyBuffer(ArrayT&& samples)
: m_buffer(Core::AnonymousBuffer::create_with_size(samples.size() * sizeof(Sample)).release_value())
, m_id(allocate_id())
, m_sample_count(samples.size())
@ -72,7 +72,7 @@ private:
memcpy(m_buffer.data<void>(), samples.data(), samples.size() * sizeof(Sample));
}
explicit Buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
explicit LegacyBuffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
: m_buffer(move(buffer))
, m_id(buffer_id)
, m_sample_count(sample_count)
@ -80,7 +80,7 @@ private:
}
// Empty Buffer representation, to avoid tiny anonymous buffers in EOF states
Buffer() = default;
LegacyBuffer() = default;
static i32 allocate_id();
@ -90,6 +90,6 @@ private:
};
// This only works for double resamplers, and therefore cannot be part of the class
ErrorOr<NonnullRefPtr<Buffer>> resample_buffer(ResampleHelper<double>& resampler, Buffer const& to_resample);
ErrorOr<NonnullRefPtr<LegacyBuffer>> resample_buffer(ResampleHelper<double>& resampler, LegacyBuffer const& to_resample);
}

View file

@ -19,7 +19,7 @@ ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSock
{
}
void ConnectionFromClient::enqueue(Buffer const& buffer)
void ConnectionFromClient::enqueue(LegacyBuffer const& buffer)
{
for (;;) {
auto success = enqueue_buffer(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
@ -29,12 +29,12 @@ void ConnectionFromClient::enqueue(Buffer const& buffer)
}
}
void ConnectionFromClient::async_enqueue(Buffer const& buffer)
void ConnectionFromClient::async_enqueue(LegacyBuffer const& buffer)
{
async_enqueue_buffer(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
}
bool ConnectionFromClient::try_enqueue(Buffer const& buffer)
bool ConnectionFromClient::try_enqueue(LegacyBuffer const& buffer)
{
return enqueue_buffer(buffer.anonymous_buffer(), buffer.id(), buffer.sample_count());
}

View file

@ -12,16 +12,16 @@
namespace Audio {
class Buffer;
class LegacyBuffer;
class ConnectionFromClient final
: public IPC::ConnectionToServer<AudioClientEndpoint, AudioServerEndpoint>
, public AudioClientEndpoint {
IPC_CLIENT_CONNECTION(ConnectionFromClient, "/tmp/portal/audio")
public:
void enqueue(Buffer const&);
bool try_enqueue(Buffer const&);
void async_enqueue(Buffer const&);
void enqueue(LegacyBuffer const&);
bool try_enqueue(LegacyBuffer const&);
void async_enqueue(LegacyBuffer const&);
Function<void(i32 buffer_id)> on_finish_playing_buffer;
Function<void(bool muted)> on_main_mix_muted_state_change;

View file

@ -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 Buffer::create_empty();
return LegacyBuffer::create_empty();
// 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,7 +267,7 @@ LoaderSamples FlacLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_i
}
m_loaded_samples += sample_index;
auto maybe_buffer = Buffer::create_with_samples(move(samples));
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();

View file

@ -22,7 +22,7 @@ namespace Audio {
static constexpr StringView no_plugin_error = "No loader plugin available";
using LoaderSamples = Result<NonnullRefPtr<Buffer>, LoaderError>;
using LoaderSamples = Result<NonnullRefPtr<LegacyBuffer>, LoaderError>;
using MaybeLoaderError = Result<void, LoaderError>;
class LoaderPlugin {

View file

@ -126,7 +126,7 @@ LoaderSamples MP3LoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_in
auto maybe_frame = read_next_frame();
if (maybe_frame.is_error()) {
if (m_input_stream->unreliable_eof()) {
return Buffer::create_empty();
return LegacyBuffer::create_empty();
}
return maybe_frame.release_error();
}
@ -156,7 +156,7 @@ LoaderSamples MP3LoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_in
}
m_loaded_samples += samples.size();
auto maybe_buffer = Buffer::create_with_samples(move(samples));
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();

View file

@ -10,7 +10,7 @@
namespace Audio {
ErrorOr<NonnullRefPtr<Buffer>> resample_buffer(ResampleHelper<double>& resampler, Buffer const& to_resample)
ErrorOr<NonnullRefPtr<LegacyBuffer>> resample_buffer(ResampleHelper<double>& resampler, LegacyBuffer const& to_resample)
{
Vector<Sample> resampled;
resampled.ensure_capacity(to_resample.sample_count() * ceil_div(resampler.source(), resampler.target()));
@ -22,7 +22,7 @@ ErrorOr<NonnullRefPtr<Buffer>> resample_buffer(ResampleHelper<double>& resampler
resampled.append(sample);
}
return Buffer::create_with_samples(move(resampled));
return LegacyBuffer::create_with_samples(move(resampled));
}
}

View file

@ -84,7 +84,7 @@ private:
SampleType m_last_sample_r {};
};
class Buffer;
ErrorOr<NonnullRefPtr<Buffer>> resample_buffer(ResampleHelper<double>& resampler, Buffer const& to_resample);
class LegacyBuffer;
ErrorOr<NonnullRefPtr<LegacyBuffer>> resample_buffer(ResampleHelper<double>& resampler, LegacyBuffer const& to_resample);
}

View file

@ -53,7 +53,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 Buffer::create_empty();
return LegacyBuffer::create_empty();
// One "sample" contains data from all channels.
// In the Wave spec, this is also called a block.
@ -78,7 +78,7 @@ LoaderSamples WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_in
if (m_stream->handle_any_error())
return LoaderError { LoaderError::Category::IO, static_cast<size_t>(m_loaded_samples), "Stream read error" };
auto buffer = Buffer::from_pcm_data(
auto buffer = LegacyBuffer::from_pcm_data(
sample_data.bytes(),
m_num_channels,
m_sample_format);

View file

@ -21,7 +21,7 @@
#include <LibCore/FileStream.h>
namespace Audio {
class Buffer;
class LegacyBuffer;
// defines for handling the WAV header data
#define WAVE_FORMAT_PCM 0x0001 // PCM
@ -30,7 +30,7 @@ class Buffer;
#define WAVE_FORMAT_MULAW 0x0007 // 8-bit ITU-T G.711 µ-law
#define WAVE_FORMAT_EXTENSIBLE 0xFFFE // Determined by SubFormat
// Parses a WAV file and produces an Audio::Buffer.
// Parses a WAV file and produces an Audio::LegacyBuffer.
class WavLoaderPlugin : public LoaderPlugin {
public:
explicit WavLoaderPlugin(StringView path);

View file

@ -94,7 +94,7 @@ Messages::AudioServer::EnqueueBufferResponse ConnectionFromClient::enqueue_buffe
return false;
// There's not a big allocation to worry about here.
m_queue->enqueue(MUST(Audio::Buffer::create_with_anonymous_buffer(buffer, buffer_id, sample_count)));
m_queue->enqueue(MUST(Audio::LegacyBuffer::create_with_anonymous_buffer(buffer, buffer_id, sample_count)));
return true;
}

View file

@ -12,7 +12,7 @@
#include <LibIPC/ConnectionFromClient.h>
namespace Audio {
class Buffer;
class LegacyBuffer;
}
namespace AudioServer {

View file

@ -200,7 +200,7 @@ ClientAudioStream::ClientAudioStream(ConnectionFromClient& client)
{
}
void ClientAudioStream::enqueue(NonnullRefPtr<Audio::Buffer>&& buffer)
void ClientAudioStream::enqueue(NonnullRefPtr<Audio::LegacyBuffer>&& buffer)
{
m_remaining_samples += buffer->sample_count();
m_queue.enqueue(move(buffer));

View file

@ -38,7 +38,7 @@ public:
~ClientAudioStream() = default;
bool is_full() const { return m_queue.size() >= 3; }
void enqueue(NonnullRefPtr<Audio::Buffer>&&);
void enqueue(NonnullRefPtr<Audio::LegacyBuffer>&&);
bool get_next_sample(Audio::Sample& sample)
{
@ -97,8 +97,8 @@ public:
void set_muted(bool muted) { m_muted = muted; }
private:
RefPtr<Audio::Buffer> m_current;
Queue<NonnullRefPtr<Audio::Buffer>> m_queue;
RefPtr<Audio::LegacyBuffer> m_current;
Queue<NonnullRefPtr<Audio::LegacyBuffer>> m_queue;
int m_position { 0 };
int m_remaining_samples { 0 };
int m_played_samples { 0 };