From ab49fcfb7c9ea591d9ec53c3a4bd590d695dad3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sat, 23 Apr 2022 12:30:36 +0200 Subject: [PATCH] LibAudio+Userland: Remove Audio::LegacyBuffer The file is now renamed to Queue.h, and the Resampler APIs with LegacyBuffer are also removed. These changes look large because nobody actually needs Buffer.h (or Queue.h). It was mostly transitive dependencies on the massive list of includes in that header, which are now almost all gone. Instead, we include common things like Sample.h directly, which should give faster compile times as very few files actually need Queue.h. --- Userland/Applications/Piano/AudioPlayerLoop.h | 3 +- Userland/Applications/Piano/Track.h | 1 - Userland/Applications/Piano/main.cpp | 1 - .../AlbumCoverVisualizationWidget.h | 1 - .../SoundPlayer/BarsVisualizationWidget.h | 1 - .../SoundPlayer/PlaybackManager.h | 2 +- .../Applications/SoundPlayer/SampleWidget.cpp | 1 - .../SoundPlayer/VisualizationWidget.h | 2 +- Userland/Libraries/LibAudio/Buffer.cpp | 139 ------------------ Userland/Libraries/LibAudio/Buffer.h | 104 ------------- Userland/Libraries/LibAudio/CMakeLists.txt | 2 - .../Libraries/LibAudio/ConnectionFromClient.h | 2 +- Userland/Libraries/LibAudio/FlacLoader.cpp | 3 +- Userland/Libraries/LibAudio/FlacLoader.h | 1 - Userland/Libraries/LibAudio/FlacTypes.h | 3 +- Userland/Libraries/LibAudio/Loader.h | 4 +- Userland/Libraries/LibAudio/MP3Loader.h | 2 +- Userland/Libraries/LibAudio/Queue.h | 21 +++ Userland/Libraries/LibAudio/Resampler.cpp | 28 ---- Userland/Libraries/LibAudio/Resampler.h | 3 - Userland/Libraries/LibDSP/Music.h | 2 +- Userland/Libraries/LibDSP/Synthesizers.cpp | 1 + Userland/Services/AudioServer/AudioServer.ipc | 2 +- .../AudioServer/ConnectionFromClient.cpp | 2 +- .../AudioServer/ConnectionFromClient.h | 2 +- Userland/Services/AudioServer/Mixer.h | 2 +- Userland/Utilities/asctl.cpp | 1 - 27 files changed, 39 insertions(+), 297 deletions(-) delete mode 100644 Userland/Libraries/LibAudio/Buffer.cpp delete mode 100644 Userland/Libraries/LibAudio/Buffer.h create mode 100644 Userland/Libraries/LibAudio/Queue.h delete mode 100644 Userland/Libraries/LibAudio/Resampler.cpp diff --git a/Userland/Applications/Piano/AudioPlayerLoop.h b/Userland/Applications/Piano/AudioPlayerLoop.h index 208adf8cd7..ecba88036a 100644 --- a/Userland/Applications/Piano/AudioPlayerLoop.h +++ b/Userland/Applications/Piano/AudioPlayerLoop.h @@ -8,8 +8,9 @@ #pragma once #include "Music.h" -#include #include +#include +#include #include #include #include diff --git a/Userland/Applications/Piano/Track.h b/Userland/Applications/Piano/Track.h index bd785586a1..9c239cd75a 100644 --- a/Userland/Applications/Piano/Track.h +++ b/Userland/Applications/Piano/Track.h @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include diff --git a/Userland/Applications/Piano/main.cpp b/Userland/Applications/Piano/main.cpp index 8c18b6467f..6d3ab24b2e 100644 --- a/Userland/Applications/Piano/main.cpp +++ b/Userland/Applications/Piano/main.cpp @@ -11,7 +11,6 @@ #include "MainWidget.h" #include "TrackManager.h" #include -#include #include #include #include diff --git a/Userland/Applications/SoundPlayer/AlbumCoverVisualizationWidget.h b/Userland/Applications/SoundPlayer/AlbumCoverVisualizationWidget.h index 29fa5b5c6e..904dd35976 100644 --- a/Userland/Applications/SoundPlayer/AlbumCoverVisualizationWidget.h +++ b/Userland/Applications/SoundPlayer/AlbumCoverVisualizationWidget.h @@ -8,7 +8,6 @@ #pragma once #include "VisualizationWidget.h" -#include #include class AlbumCoverVisualizationWidget final : public VisualizationWidget { diff --git a/Userland/Applications/SoundPlayer/BarsVisualizationWidget.h b/Userland/Applications/SoundPlayer/BarsVisualizationWidget.h index b8739d1dca..1857fd1d3c 100644 --- a/Userland/Applications/SoundPlayer/BarsVisualizationWidget.h +++ b/Userland/Applications/SoundPlayer/BarsVisualizationWidget.h @@ -11,7 +11,6 @@ #include #include #include -#include #include class BarsVisualizationWidget final : public VisualizationWidget { diff --git a/Userland/Applications/SoundPlayer/PlaybackManager.h b/Userland/Applications/SoundPlayer/PlaybackManager.h index d34c35a6fc..9dcca04191 100644 --- a/Userland/Applications/SoundPlayer/PlaybackManager.h +++ b/Userland/Applications/SoundPlayer/PlaybackManager.h @@ -10,9 +10,9 @@ #include #include #include -#include #include #include +#include #include #include diff --git a/Userland/Applications/SoundPlayer/SampleWidget.cpp b/Userland/Applications/SoundPlayer/SampleWidget.cpp index 3abf2eb886..9517c45771 100644 --- a/Userland/Applications/SoundPlayer/SampleWidget.cpp +++ b/Userland/Applications/SoundPlayer/SampleWidget.cpp @@ -7,7 +7,6 @@ #include "SampleWidget.h" #include -#include #include SampleWidget::SampleWidget() diff --git a/Userland/Applications/SoundPlayer/VisualizationWidget.h b/Userland/Applications/SoundPlayer/VisualizationWidget.h index cab2e8d3d8..9261c76ae5 100644 --- a/Userland/Applications/SoundPlayer/VisualizationWidget.h +++ b/Userland/Applications/SoundPlayer/VisualizationWidget.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include diff --git a/Userland/Libraries/LibAudio/Buffer.cpp b/Userland/Libraries/LibAudio/Buffer.cpp deleted file mode 100644 index b313b500fe..0000000000 --- a/Userland/Libraries/LibAudio/Buffer.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * Copyright (c) 2021, kleines Filmröllchen - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include "Buffer.h" -#include -#include -#include -#include - -namespace Audio { - -i32 LegacyBuffer::allocate_id() -{ - static Atomic next_id; - return next_id++; -} - -template -static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector& samples, int num_channels) -{ - double left_channel_sample = 0; - double right_channel_sample = 0; - - switch (num_channels) { - case 1: - for (;;) { - left_channel_sample = read_sample(stream); - samples.append(Sample(left_channel_sample)); - - if (stream.handle_any_error()) { - break; - } - } - break; - case 2: - for (;;) { - left_channel_sample = read_sample(stream); - right_channel_sample = read_sample(stream); - samples.append(Sample(left_channel_sample, right_channel_sample)); - - if (stream.handle_any_error()) { - break; - } - } - break; - default: - VERIFY_NOT_REACHED(); - } -} - -static double read_float_sample_64(InputMemoryStream& stream) -{ - LittleEndian sample; - stream >> sample; - return double(sample); -} - -static double read_float_sample_32(InputMemoryStream& stream) -{ - LittleEndian sample; - stream >> sample; - return double(sample); -} - -static double read_norm_sample_24(InputMemoryStream& stream) -{ - u8 byte = 0; - stream >> byte; - u32 sample1 = byte; - stream >> byte; - u32 sample2 = byte; - stream >> byte; - u32 sample3 = byte; - - i32 value = 0; - value = sample1 << 8; - value |= (sample2 << 16); - value |= (sample3 << 24); - return double(value) / NumericLimits::max(); -} - -static double read_norm_sample_16(InputMemoryStream& stream) -{ - LittleEndian sample; - stream >> sample; - return double(sample) / NumericLimits::max(); -} - -static double read_norm_sample_8(InputMemoryStream& stream) -{ - u8 sample = 0; - stream >> sample; - return double(sample) / NumericLimits::max(); -} - -ErrorOr> 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> LegacyBuffer::from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples) -{ - Vector fdata; - fdata.ensure_capacity(num_samples); - - switch (sample_format) { - case PcmSampleFormat::Uint8: - read_samples_from_stream(stream, read_norm_sample_8, fdata, num_channels); - break; - case PcmSampleFormat::Int16: - read_samples_from_stream(stream, read_norm_sample_16, fdata, num_channels); - break; - case PcmSampleFormat::Int24: - read_samples_from_stream(stream, read_norm_sample_24, fdata, num_channels); - break; - case PcmSampleFormat::Float32: - read_samples_from_stream(stream, read_float_sample_32, fdata, num_channels); - break; - case PcmSampleFormat::Float64: - read_samples_from_stream(stream, read_float_sample_64, fdata, num_channels); - break; - default: - VERIFY_NOT_REACHED(); - } - - // We should handle this in a better way above, but for now -- - // just make sure we're good. Worst case we just write some 0s where they - // don't belong. - VERIFY(!stream.handle_any_error()); - - return LegacyBuffer::create_with_samples(move(fdata)); -} - -} diff --git a/Userland/Libraries/LibAudio/Buffer.h b/Userland/Libraries/LibAudio/Buffer.h deleted file mode 100644 index e998de46df..0000000000 --- a/Userland/Libraries/LibAudio/Buffer.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * Copyright (c) 2021, kleines Filmröllchen - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include "AK/TypedTransfer.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -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, AUDIO_BUFFERS_COUNT>; - -using namespace AK::Exponentials; - -// A buffer of audio samples. -class LegacyBuffer : public RefCounted { -public: - static ErrorOr> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format); - static ErrorOr> from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples); - template ArrayT> - static ErrorOr> create_with_samples(ArrayT&& samples) - { - return adopt_nonnull_ref_or_enomem(new (nothrow) LegacyBuffer(move(samples))); - } - static ErrorOr> create_with_anonymous_buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count) - { - return adopt_nonnull_ref_or_enomem(new (nothrow) LegacyBuffer(move(buffer), buffer_id, sample_count)); - } - static NonnullRefPtr 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) LegacyBuffer)); - } - - Sample const* samples() const { return (Sample const*)data(); } - - ErrorOr> to_sample_array() const - { - FixedArray samples = TRY(FixedArray::try_create(m_sample_count)); - AK::TypedTransfer::copy(samples.data(), this->samples(), m_sample_count); - return samples; - } - - int sample_count() const { return m_sample_count; } - void const* data() const { return m_buffer.data(); } - int size_in_bytes() const { return m_sample_count * (int)sizeof(Sample); } - int id() const { return m_id; } - Core::AnonymousBuffer const& anonymous_buffer() const { return m_buffer; } - -private: - template ArrayT> - 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()) - { - memcpy(m_buffer.data(), samples.data(), samples.size() * sizeof(Sample)); - } - - explicit LegacyBuffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count) - : m_buffer(move(buffer)) - , m_id(buffer_id) - , m_sample_count(sample_count) - { - } - - // Empty Buffer representation, to avoid tiny anonymous buffers in EOF states - LegacyBuffer() = default; - - static i32 allocate_id(); - - Core::AnonymousBuffer m_buffer; - const i32 m_id { -1 }; - int const m_sample_count { 0 }; -}; - -// This only works for double resamplers, and therefore cannot be part of the class -ErrorOr> resample_buffer(ResampleHelper& resampler, LegacyBuffer const& to_resample); - -} diff --git a/Userland/Libraries/LibAudio/CMakeLists.txt b/Userland/Libraries/LibAudio/CMakeLists.txt index 3a8a7f4f98..06c3dad5ac 100644 --- a/Userland/Libraries/LibAudio/CMakeLists.txt +++ b/Userland/Libraries/LibAudio/CMakeLists.txt @@ -1,6 +1,4 @@ set(SOURCES - Buffer.cpp - Resampler.cpp SampleFormats.cpp ConnectionFromClient.cpp Loader.cpp diff --git a/Userland/Libraries/LibAudio/ConnectionFromClient.h b/Userland/Libraries/LibAudio/ConnectionFromClient.h index cefd5ba331..b74e553bc8 100644 --- a/Userland/Libraries/LibAudio/ConnectionFromClient.h +++ b/Userland/Libraries/LibAudio/ConnectionFromClient.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index be583f350c..f479ccec5a 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -13,14 +13,13 @@ #include #include #include -#include #include #include #include -#include #include #include #include +#include #include #include diff --git a/Userland/Libraries/LibAudio/FlacLoader.h b/Userland/Libraries/LibAudio/FlacLoader.h index 23f1fba00f..a4abc59833 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.h +++ b/Userland/Libraries/LibAudio/FlacLoader.h @@ -6,7 +6,6 @@ #pragma once -#include "Buffer.h" #include "FlacTypes.h" #include "Loader.h" #include diff --git a/Userland/Libraries/LibAudio/FlacTypes.h b/Userland/Libraries/LibAudio/FlacTypes.h index 0b1f6e5431..681ced306a 100644 --- a/Userland/Libraries/LibAudio/FlacTypes.h +++ b/Userland/Libraries/LibAudio/FlacTypes.h @@ -6,7 +6,8 @@ #pragma once -#include "Buffer.h" +#include "Queue.h" +#include "SampleFormats.h" #include #include #include diff --git a/Userland/Libraries/LibAudio/Loader.h b/Userland/Libraries/LibAudio/Loader.h index 302ea13260..4f08aa9120 100644 --- a/Userland/Libraries/LibAudio/Loader.h +++ b/Userland/Libraries/LibAudio/Loader.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -14,8 +15,9 @@ #include #include #include -#include #include +#include +#include #include namespace Audio { diff --git a/Userland/Libraries/LibAudio/MP3Loader.h b/Userland/Libraries/LibAudio/MP3Loader.h index bf6c8fe514..bc88a4e7dc 100644 --- a/Userland/Libraries/LibAudio/MP3Loader.h +++ b/Userland/Libraries/LibAudio/MP3Loader.h @@ -6,9 +6,9 @@ #pragma once -#include "Buffer.h" #include "Loader.h" #include "MP3Types.h" +#include #include #include #include diff --git a/Userland/Libraries/LibAudio/Queue.h b/Userland/Libraries/LibAudio/Queue.h new file mode 100644 index 0000000000..fd103819b3 --- /dev/null +++ b/Userland/Libraries/LibAudio/Queue.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2018-2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +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, AUDIO_BUFFERS_COUNT>; + +} diff --git a/Userland/Libraries/LibAudio/Resampler.cpp b/Userland/Libraries/LibAudio/Resampler.cpp deleted file mode 100644 index cfcd4589d1..0000000000 --- a/Userland/Libraries/LibAudio/Resampler.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2022, kleines Filmröllchen . - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include "Resampler.h" -#include "Buffer.h" -#include "Sample.h" - -namespace Audio { - -ErrorOr> resample_buffer(ResampleHelper& resampler, LegacyBuffer const& to_resample) -{ - Vector resampled; - resampled.ensure_capacity(to_resample.sample_count() * ceil_div(resampler.source(), resampler.target())); - for (size_t i = 0; i < static_cast(to_resample.sample_count()); ++i) { - auto sample = to_resample.samples()[i]; - resampler.process_sample(sample.left, sample.right); - - while (resampler.read_sample(sample.left, sample.right)) - resampled.append(sample); - } - - return LegacyBuffer::create_with_samples(move(resampled)); -} - -} diff --git a/Userland/Libraries/LibAudio/Resampler.h b/Userland/Libraries/LibAudio/Resampler.h index 2b48dbf888..ed3f49739a 100644 --- a/Userland/Libraries/LibAudio/Resampler.h +++ b/Userland/Libraries/LibAudio/Resampler.h @@ -84,7 +84,4 @@ private: SampleType m_last_sample_r {}; }; -class LegacyBuffer; -ErrorOr> resample_buffer(ResampleHelper& resampler, LegacyBuffer const& to_resample); - } diff --git a/Userland/Libraries/LibDSP/Music.h b/Userland/Libraries/LibDSP/Music.h index 9553a9bf46..9b76960f3e 100644 --- a/Userland/Libraries/LibDSP/Music.h +++ b/Userland/Libraries/LibDSP/Music.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include namespace LibDSP { diff --git a/Userland/Libraries/LibDSP/Synthesizers.cpp b/Userland/Libraries/LibDSP/Synthesizers.cpp index 38761af048..5c8b1dd4b8 100644 --- a/Userland/Libraries/LibDSP/Synthesizers.cpp +++ b/Userland/Libraries/LibDSP/Synthesizers.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include diff --git a/Userland/Services/AudioServer/AudioServer.ipc b/Userland/Services/AudioServer/AudioServer.ipc index 890642d23e..9d612a56b5 100644 --- a/Userland/Services/AudioServer/AudioServer.ipc +++ b/Userland/Services/AudioServer/AudioServer.ipc @@ -1,5 +1,5 @@ #include -#include +#include endpoint AudioServer { diff --git a/Userland/Services/AudioServer/ConnectionFromClient.cpp b/Userland/Services/AudioServer/ConnectionFromClient.cpp index 4c3d6862c4..ec0959edea 100644 --- a/Userland/Services/AudioServer/ConnectionFromClient.cpp +++ b/Userland/Services/AudioServer/ConnectionFromClient.cpp @@ -7,7 +7,7 @@ #include "ConnectionFromClient.h" #include "Mixer.h" #include -#include +#include namespace AudioServer { diff --git a/Userland/Services/AudioServer/ConnectionFromClient.h b/Userland/Services/AudioServer/ConnectionFromClient.h index 2962ecbf37..2116c5fc55 100644 --- a/Userland/Services/AudioServer/ConnectionFromClient.h +++ b/Userland/Services/AudioServer/ConnectionFromClient.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include diff --git a/Userland/Services/AudioServer/Mixer.h b/Userland/Services/AudioServer/Mixer.h index ca59225be5..840952b8f1 100644 --- a/Userland/Services/AudioServer/Mixer.h +++ b/Userland/Services/AudioServer/Mixer.h @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Userland/Utilities/asctl.cpp b/Userland/Utilities/asctl.cpp index 0d41f03e53..2d9aaae8f6 100644 --- a/Userland/Utilities/asctl.cpp +++ b/Userland/Utilities/asctl.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include