From 4ce1dae673e050f7e06270e4dc9484738114beef Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 4 Jul 2022 15:27:03 +0200 Subject: [PATCH] pcm/*: use std::span instead of ConstBuffer --- src/decoder/Bridge.cxx | 6 +- src/decoder/plugins/VorbisDecoderPlugin.cxx | 3 +- src/filter/plugins/ConvertFilterPlugin.cxx | 4 +- src/filter/plugins/ReplayGainFilterPlugin.cxx | 2 +- src/lib/ffmpeg/Interleave.cxx | 3 +- src/output/plugins/AlsaOutputPlugin.cxx | 8 +- src/output/plugins/OssOutputPlugin.cxx | 6 +- src/pcm/ChannelsConverter.cxx | 30 ++-- src/pcm/ChannelsConverter.hxx | 6 +- src/pcm/Convert.cxx | 20 +-- src/pcm/Convert.hxx | 8 +- src/pcm/Dop.cxx | 5 +- src/pcm/Dop.hxx | 5 +- src/pcm/Dsd16.cxx | 7 +- src/pcm/Dsd16.hxx | 5 +- src/pcm/Dsd32.cxx | 7 +- src/pcm/Dsd32.hxx | 5 +- src/pcm/Export.cxx | 50 +++--- src/pcm/Export.hxx | 10 +- src/pcm/FallbackResampler.cxx | 25 +-- src/pcm/FallbackResampler.hxx | 5 +- src/pcm/FormatConverter.cxx | 29 ++-- src/pcm/FormatConverter.hxx | 6 +- src/pcm/GlueResampler.cxx | 8 +- src/pcm/GlueResampler.hxx | 8 +- src/pcm/Interleave.cxx | 25 ++- src/pcm/Interleave.hxx | 15 +- src/pcm/LibsamplerateResampler.cxx | 17 ++- src/pcm/LibsamplerateResampler.hxx | 4 +- src/pcm/MixRampAnalyzer.cxx | 13 +- src/pcm/MixRampAnalyzer.hxx | 4 +- src/pcm/MixRampGlue.cxx | 4 +- src/pcm/Order.cxx | 63 ++++---- src/pcm/Order.hxx | 7 +- src/pcm/PcmChannels.cxx | 69 ++++----- src/pcm/PcmChannels.hxx | 18 +-- src/pcm/PcmDsd.cxx | 27 ++-- src/pcm/PcmDsd.hxx | 11 +- src/pcm/PcmFormat.cxx | 144 +++++++++--------- src/pcm/PcmFormat.hxx | 18 +-- src/pcm/ReplayGainAnalyzer.cxx | 37 +++-- src/pcm/ReplayGainAnalyzer.hxx | 9 +- src/pcm/Resampler.hxx | 10 +- src/pcm/RestBuffer.hxx | 44 +++--- src/pcm/SoxrResampler.cxx | 16 +- src/pcm/SoxrResampler.hxx | 4 +- src/pcm/Volume.cxx | 35 +++-- src/pcm/Volume.hxx | 7 +- test/RunMixRampAnalyzer.cxx | 1 - test/RunReplayGainAnalyzer.cxx | 1 - test/run_convert.cxx | 9 +- test/software_volume.cxx | 5 +- test/test_pcm_channels.cxx | 37 +++-- test/test_pcm_export.cxx | 139 +++++++++-------- test/test_pcm_format.cxx | 34 ++--- test/test_pcm_interleave.cxx | 12 +- test/test_pcm_util.hxx | 9 +- test/test_pcm_volume.cxx | 48 +++--- 58 files changed, 572 insertions(+), 595 deletions(-) diff --git a/src/decoder/Bridge.cxx b/src/decoder/Bridge.cxx index d273cde8a..33f06adda 100644 --- a/src/decoder/Bridge.cxx +++ b/src/decoder/Bridge.cxx @@ -511,9 +511,9 @@ DecoderBridge::SubmitData(InputStream *is, assert(dc.in_audio_format != dc.out_audio_format); try { - auto result = convert->Convert({data, length}); - data = result.data; - length = result.size; + auto result = convert->Convert({(const std::byte *)data, length}); + data = result.data(); + length = result.size(); } catch (...) { /* the PCM conversion has failed - stop playback, since we have no better way to diff --git a/src/decoder/plugins/VorbisDecoderPlugin.cxx b/src/decoder/plugins/VorbisDecoderPlugin.cxx index 9ed642cb1..aaf37ad0a 100644 --- a/src/decoder/plugins/VorbisDecoderPlugin.cxx +++ b/src/decoder/plugins/VorbisDecoderPlugin.cxx @@ -227,8 +227,7 @@ VorbisDecoder::SubmitSomePcm() } #else PcmInterleaveFloat(buffer, - ConstBuffer(pcm, - channels), + {pcm, channels}, n_frames); #endif diff --git a/src/filter/plugins/ConvertFilterPlugin.cxx b/src/filter/plugins/ConvertFilterPlugin.cxx index 5dcca8f99..77915766b 100644 --- a/src/filter/plugins/ConvertFilterPlugin.cxx +++ b/src/filter/plugins/ConvertFilterPlugin.cxx @@ -55,7 +55,7 @@ public: ConstBuffer Flush() override { return state ? state->Flush() - : nullptr; + : std::span{}; } }; @@ -108,7 +108,7 @@ ConvertFilter::FilterPCM(ConstBuffer src) return state ? state->Convert(src) /* optimized special case: no-op */ - : src; + : std::span{src}; } std::unique_ptr diff --git a/src/filter/plugins/ReplayGainFilterPlugin.cxx b/src/filter/plugins/ReplayGainFilterPlugin.cxx index 032cc23b7..4bb0ec2f0 100644 --- a/src/filter/plugins/ReplayGainFilterPlugin.cxx +++ b/src/filter/plugins/ReplayGainFilterPlugin.cxx @@ -201,7 +201,7 @@ ConstBuffer ReplayGainFilter::FilterPCM(ConstBuffer src) { return mixer != nullptr - ? src + ? std::span{src} : pv.Apply(src); } diff --git a/src/lib/ffmpeg/Interleave.cxx b/src/lib/ffmpeg/Interleave.cxx index 4e901315f..bb83081ed 100644 --- a/src/lib/ffmpeg/Interleave.cxx +++ b/src/lib/ffmpeg/Interleave.cxx @@ -57,8 +57,7 @@ InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer) throw std::bad_alloc(); PcmInterleave(output_buffer, - ConstBuffer((const void *const*)frame.extended_data, - channels), + {(const void *const*)frame.extended_data, channels}, n_frames, av_get_bytes_per_sample(format)); } else { diff --git a/src/output/plugins/AlsaOutputPlugin.cxx b/src/output/plugins/AlsaOutputPlugin.cxx index 5b191ee47..7ebc3c9a3 100644 --- a/src/output/plugins/AlsaOutputPlugin.cxx +++ b/src/output/plugins/AlsaOutputPlugin.cxx @@ -1231,13 +1231,13 @@ AlsaOutput::Play(const void *chunk, size_t size) if (size > max_size) size = max_size; - const auto e = pcm_export->Export({chunk, size}); + const auto e = pcm_export->Export({(const std::byte *)chunk, size}); if (e.empty()) return size; - size_t bytes_written = ring_buffer->push((const uint8_t *)e.data, - e.size); - assert(bytes_written == e.size); + size_t bytes_written = ring_buffer->push((const uint8_t *)e.data(), + e.size()); + assert(bytes_written == e.size()); (void)bytes_written; return size; diff --git a/src/output/plugins/OssOutputPlugin.cxx b/src/output/plugins/OssOutputPlugin.cxx index 6e310a9d1..b79aaa548 100644 --- a/src/output/plugins/OssOutputPlugin.cxx +++ b/src/output/plugins/OssOutputPlugin.cxx @@ -691,12 +691,12 @@ OssOutput::Play(const void *chunk, size_t size) if (!fd.IsDefined()) Reopen(); - const auto e = pcm_export->Export({chunk, size}); + const auto e = pcm_export->Export({(const std::byte *)chunk, size}); if (e.empty()) return size; - chunk = e.data; - size = e.size; + chunk = e.data(); + size = e.size(); while (true) { ret = fd.Write(chunk, size); diff --git a/src/pcm/ChannelsConverter.cxx b/src/pcm/ChannelsConverter.cxx index 84147070d..c06fd8fba 100644 --- a/src/pcm/ChannelsConverter.cxx +++ b/src/pcm/ChannelsConverter.cxx @@ -19,8 +19,8 @@ #include "ChannelsConverter.hxx" #include "PcmChannels.hxx" -#include "util/ConstBuffer.hxx" #include "util/RuntimeError.hxx" +#include "util/SpanCast.hxx" #include @@ -55,8 +55,8 @@ PcmChannelsConverter::Close() noexcept #endif } -ConstBuffer -PcmChannelsConverter::Convert(ConstBuffer src) noexcept +std::span +PcmChannelsConverter::Convert(std::span src) noexcept { switch (format) { case SampleFormat::UNDEFINED: @@ -66,24 +66,24 @@ PcmChannelsConverter::Convert(ConstBuffer src) noexcept gcc_unreachable(); case SampleFormat::S16: - return pcm_convert_channels_16(buffer, dest_channels, - src_channels, - ConstBuffer::FromVoid(src)).ToVoid(); + return std::as_bytes(pcm_convert_channels_16(buffer, dest_channels, + src_channels, + FromBytesStrict(src))); case SampleFormat::S24_P32: - return pcm_convert_channels_24(buffer, dest_channels, - src_channels, - ConstBuffer::FromVoid(src)).ToVoid(); + return std::as_bytes(pcm_convert_channels_24(buffer, dest_channels, + src_channels, + FromBytesStrict(src))); case SampleFormat::S32: - return pcm_convert_channels_32(buffer, dest_channels, - src_channels, - ConstBuffer::FromVoid(src)).ToVoid(); + return std::as_bytes(pcm_convert_channels_32(buffer, dest_channels, + src_channels, + FromBytesStrict(src))); case SampleFormat::FLOAT: - return pcm_convert_channels_float(buffer, dest_channels, - src_channels, - ConstBuffer::FromVoid(src)).ToVoid(); + return std::as_bytes(pcm_convert_channels_float(buffer, dest_channels, + src_channels, + FromBytesStrict(src))); } assert(false); diff --git a/src/pcm/ChannelsConverter.hxx b/src/pcm/ChannelsConverter.hxx index 74ea75157..bca717943 100644 --- a/src/pcm/ChannelsConverter.hxx +++ b/src/pcm/ChannelsConverter.hxx @@ -23,12 +23,12 @@ #include "SampleFormat.hxx" #include "Buffer.hxx" +#include + #ifndef NDEBUG #include #endif -template struct ConstBuffer; - /** * A class that converts samples from one format to another. */ @@ -74,7 +74,7 @@ public: * @return the destination buffer */ [[gnu::pure]] - ConstBuffer Convert(ConstBuffer src) noexcept; + std::span Convert(std::span src) noexcept; }; #endif diff --git a/src/pcm/Convert.cxx b/src/pcm/Convert.cxx index 4b553edd3..a261d3dd2 100644 --- a/src/pcm/Convert.cxx +++ b/src/pcm/Convert.cxx @@ -19,7 +19,7 @@ #include "Convert.hxx" #include "ConfiguredResampler.hxx" -#include "util/ConstBuffer.hxx" +#include "util/SpanCast.hxx" #include #include @@ -111,16 +111,16 @@ PcmConvert::Reset() noexcept #endif } -ConstBuffer -PcmConvert::Convert(ConstBuffer buffer) +std::span +PcmConvert::Convert(std::span buffer) { #ifdef ENABLE_DSD if (src_format.format == SampleFormat::DSD) { - auto s = ConstBuffer::FromVoid(buffer); + auto s = FromBytesStrict(buffer); auto d = dsd2pcm_float - ? dsd.ToFloat(src_format.channels, s).ToVoid() - : dsd.ToS24(src_format.channels, s).ToVoid(); - if (d.IsNull()) + ? std::as_bytes(dsd.ToFloat(src_format.channels, s)) + : std::as_bytes(dsd.ToS24(src_format.channels, s)); + if (d.data() == nullptr) throw std::runtime_error("DSD to PCM conversion failed"); buffer = d; @@ -139,12 +139,12 @@ PcmConvert::Convert(ConstBuffer buffer) return buffer; } -ConstBuffer +std::span PcmConvert::Flush() { if (enable_resampler) { auto buffer = resampler.Flush(); - if (!buffer.IsNull()) { + if (buffer.data() != nullptr) { if (enable_format) buffer = format_converter.Convert(buffer); @@ -155,5 +155,5 @@ PcmConvert::Flush() } } - return nullptr; + return {}; } diff --git a/src/pcm/Convert.hxx b/src/pcm/Convert.hxx index 81be5790d..e83aca2c3 100644 --- a/src/pcm/Convert.hxx +++ b/src/pcm/Convert.hxx @@ -30,7 +30,9 @@ #include "PcmDsd.hxx" #endif -template struct ConstBuffer; +#include +#include + struct ConfigData; /** @@ -77,13 +79,13 @@ public: * @param src the source PCM buffer * @return the destination buffer */ - ConstBuffer Convert(ConstBuffer src); + std::span Convert(std::span src); /** * Flush pending data and return it. This should be called * repepatedly until it returns nullptr. */ - ConstBuffer Flush(); + std::span Flush(); }; void diff --git a/src/pcm/Dop.cxx b/src/pcm/Dop.cxx index d62af9c74..721917a7b 100644 --- a/src/pcm/Dop.cxx +++ b/src/pcm/Dop.cxx @@ -19,7 +19,6 @@ #include "Dop.hxx" #include "ChannelDefs.hxx" -#include "util/ConstBuffer.hxx" #include #include @@ -88,8 +87,8 @@ DsdToDopConverter::Open(unsigned _channels) noexcept rest_buffer.Open(channels); } -ConstBuffer -DsdToDopConverter::Convert(ConstBuffer src) noexcept +std::span +DsdToDopConverter::Convert(std::span src) noexcept { return rest_buffer.Process(buffer, src, 2 * channels, [this](auto && arg1, auto && arg2, auto && arg3) { return DsdToDop(arg1, arg2, arg3, channels); }); diff --git a/src/pcm/Dop.hxx b/src/pcm/Dop.hxx index b84a9658e..5e53c77c5 100644 --- a/src/pcm/Dop.hxx +++ b/src/pcm/Dop.hxx @@ -24,8 +24,7 @@ #include "RestBuffer.hxx" #include - -template struct ConstBuffer; +#include /** * Pack DSD 1 bit samples into (padded) 24 bit PCM samples for @@ -60,7 +59,7 @@ public: return 2 * GetInputBlockSize(); } - ConstBuffer Convert(ConstBuffer src) noexcept; + std::span Convert(std::span src) noexcept; }; #endif diff --git a/src/pcm/Dsd16.cxx b/src/pcm/Dsd16.cxx index d6c6f2244..90245d5ca 100644 --- a/src/pcm/Dsd16.cxx +++ b/src/pcm/Dsd16.cxx @@ -18,9 +18,6 @@ */ #include "Dsd16.hxx" -#include "util/ConstBuffer.hxx" - -#include /** * Construct a 16 bit integer from two bytes. @@ -60,8 +57,8 @@ Dsd16Converter::Open(unsigned _channels) noexcept rest_buffer.Open(channels); } -ConstBuffer -Dsd16Converter::Convert(ConstBuffer src) noexcept +std::span +Dsd16Converter::Convert(std::span src) noexcept { return rest_buffer.Process(buffer, src, channels, [this](auto && arg1, auto && arg2, auto && arg3) { return Dsd8To16(arg1, arg2, arg3, channels); }); diff --git a/src/pcm/Dsd16.hxx b/src/pcm/Dsd16.hxx index e5bb0cc15..130225a13 100644 --- a/src/pcm/Dsd16.hxx +++ b/src/pcm/Dsd16.hxx @@ -24,8 +24,7 @@ #include "RestBuffer.hxx" #include - -template struct ConstBuffer; +#include /** * Convert DSD_U8 to DSD_U16 (native endian, oldest bits in MSB). @@ -58,7 +57,7 @@ public: return GetInputBlockSize(); } - ConstBuffer Convert(ConstBuffer src) noexcept; + std::span Convert(std::span src) noexcept; }; #endif diff --git a/src/pcm/Dsd32.cxx b/src/pcm/Dsd32.cxx index b13b6ba32..4297afe01 100644 --- a/src/pcm/Dsd32.cxx +++ b/src/pcm/Dsd32.cxx @@ -18,9 +18,6 @@ */ #include "Dsd32.hxx" -#include "util/ConstBuffer.hxx" - -#include /** * Construct a 32 bit integer from four bytes. @@ -62,8 +59,8 @@ Dsd32Converter::Open(unsigned _channels) noexcept rest_buffer.Open(channels); } -ConstBuffer -Dsd32Converter::Convert(ConstBuffer src) noexcept +std::span +Dsd32Converter::Convert(std::span src) noexcept { return rest_buffer.Process(buffer, src, channels, [this](auto && arg1, auto && arg2, auto && arg3) { return Dsd8To32(arg1, arg2, arg3, channels); }); diff --git a/src/pcm/Dsd32.hxx b/src/pcm/Dsd32.hxx index 5dd43cd0c..c0cb8b1b2 100644 --- a/src/pcm/Dsd32.hxx +++ b/src/pcm/Dsd32.hxx @@ -24,8 +24,7 @@ #include "RestBuffer.hxx" #include - -template struct ConstBuffer; +#include /** * Convert DSD_U8 to DSD_U32 (native endian, oldest bits in MSB). @@ -58,7 +57,7 @@ public: return GetInputBlockSize(); } - ConstBuffer Convert(ConstBuffer src) noexcept; + std::span Convert(std::span src) noexcept; }; #endif diff --git a/src/pcm/Export.cxx b/src/pcm/Export.cxx index 5f5858ae8..d71202979 100644 --- a/src/pcm/Export.cxx +++ b/src/pcm/Export.cxx @@ -22,12 +22,11 @@ #include "Pack.hxx" #include "Silence.hxx" #include "util/ByteReverse.hxx" -#include "util/ConstBuffer.hxx" +#include "util/SpanCast.hxx" +#include #include -#include - void PcmExport::Open(SampleFormat sample_format, unsigned _channels, Params params) noexcept @@ -98,9 +97,9 @@ PcmExport::Open(SampleFormat sample_format, unsigned _channels, assert(buffer_size < sizeof(buffer)); PcmSilence({buffer, buffer_size}, src_sample_format); auto s = Export({buffer, buffer_size}); - assert(s.size < sizeof(silence_buffer)); - silence_size = s.size; - memcpy(silence_buffer, s.data, s.size); + assert(s.size() < sizeof(silence_buffer)); + silence_size = s.size(); + std::copy(s.begin(), s.end(), silence_buffer); } void @@ -200,7 +199,7 @@ PcmExport::GetOutputBlockSize() const noexcept return GetOutputFrameSize(); } -ConstBuffer +std::span PcmExport::GetSilence() const noexcept { return {silence_buffer, silence_size}; @@ -262,8 +261,8 @@ PcmExport::Params::CalcInputSampleRate(unsigned sample_rate) const noexcept return sample_rate; } -ConstBuffer -PcmExport::Export(ConstBuffer data) noexcept +std::span +PcmExport::Export(std::span data) noexcept { if (alsa_channel_order) data = ToAlsaChannelOrder(order_buffer, data, @@ -275,38 +274,34 @@ PcmExport::Export(ConstBuffer data) noexcept break; case DsdMode::U16: - data = dsd16_converter.Convert(ConstBuffer::FromVoid(data)) - .ToVoid(); + data = std::as_bytes(dsd16_converter.Convert(FromBytesStrict(data))); break; case DsdMode::U32: - data = dsd32_converter.Convert(ConstBuffer::FromVoid(data)) - .ToVoid(); + data = std::as_bytes(dsd32_converter.Convert(FromBytesStrict(data))); break; case DsdMode::DOP: - data = dop_converter.Convert(ConstBuffer::FromVoid(data)) - .ToVoid(); + data = std::as_bytes(dop_converter.Convert(FromBytesStrict(data))); break; } #endif if (pack24) { - const auto src = ConstBuffer::FromVoid(data); - const size_t num_samples = src.size; + const auto src = FromBytesStrict(data); + const size_t num_samples = src.size(); const size_t dest_size = num_samples * 3; auto *dest = (uint8_t *)pack_buffer.Get(dest_size); assert(dest != nullptr); - pcm_pack_24(dest, src.begin(), src.end()); + pcm_pack_24(dest, src.data(), src.data() + src.size()); - data.data = dest; - data.size = dest_size; + data = std::as_bytes(std::span{dest, dest_size}); } else if (shift8) { - const auto src = ConstBuffer::FromVoid(data); + const auto src = FromBytesStrict(data); - auto *dest = (uint32_t *)pack_buffer.Get(data.size); - data.data = dest; + auto *dest = (uint32_t *)pack_buffer.Get(data.size()); + data = {(const std::byte *)dest, data.size()}; for (auto i : src) *dest++ = i << 8; @@ -315,13 +310,14 @@ PcmExport::Export(ConstBuffer data) noexcept if (reverse_endian > 0) { assert(reverse_endian >= 2); - const auto src = ConstBuffer::FromVoid(data); + const auto src = FromBytesStrict(data); - auto *dest = (uint8_t *)reverse_buffer.Get(data.size); + auto *dest = (uint8_t *)reverse_buffer.Get(data.size()); assert(dest != nullptr); - data.data = dest; + data = {(const std::byte *)dest, data.size()}; - reverse_bytes(dest, src.begin(), src.end(), reverse_endian); + reverse_bytes(dest, src.data(), src.data() + src.size(), + reverse_endian); } return data; diff --git a/src/pcm/Export.hxx b/src/pcm/Export.hxx index 518196946..f0906ad26 100644 --- a/src/pcm/Export.hxx +++ b/src/pcm/Export.hxx @@ -30,9 +30,9 @@ #include "Dop.hxx" #endif +#include #include - -template struct ConstBuffer; +#include /** * An object that handles export of PCM samples to some instance @@ -80,7 +80,7 @@ class PcmExport { size_t silence_size; - uint8_t silence_buffer[64]; /* worst-case size */ + std::byte silence_buffer[64]; /* worst-case size */ /** * The sample format of input data. @@ -227,7 +227,7 @@ public: * this #PcmExport object exists and until the next Open() * call */ - ConstBuffer GetSilence() const noexcept; + std::span GetSilence() const noexcept; /** * Export a PCM buffer. @@ -236,7 +236,7 @@ public: * @return the destination buffer; may be empty (and may be a * pointer to the source buffer) */ - ConstBuffer Export(ConstBuffer src) noexcept; + std::span Export(std::span src) noexcept; /** * Converts the number of consumed bytes from the Export() diff --git a/src/pcm/FallbackResampler.cxx b/src/pcm/FallbackResampler.cxx index 8dc81f1eb..1a8acdf74 100644 --- a/src/pcm/FallbackResampler.cxx +++ b/src/pcm/FallbackResampler.cxx @@ -18,6 +18,7 @@ */ #include "FallbackResampler.hxx" +#include "util/SpanCast.hxx" #include @@ -61,22 +62,22 @@ FallbackPcmResampler::Close() noexcept } template -static ConstBuffer +static std::span pcm_resample_fallback(PcmBuffer &buffer, unsigned channels, unsigned src_rate, - ConstBuffer src, + std::span src, unsigned dest_rate) noexcept { unsigned dest_pos = 0; - unsigned src_frames = src.size / channels; + unsigned src_frames = src.size() / channels; unsigned dest_frames = (src_frames * dest_rate + src_rate - 1) / src_rate; unsigned dest_samples = dest_frames * channels; - size_t dest_size = dest_samples * sizeof(*src.data); + size_t dest_size = dest_samples * sizeof(T); T *dest_buffer = (T *)buffer.Get(dest_size); - assert((src.size % channels) == 0); + assert((src.size() % channels) == 0); switch (channels) { case 1: @@ -101,20 +102,20 @@ pcm_resample_fallback(PcmBuffer &buffer, } template -static ConstBuffer +static std::span pcm_resample_fallback_void(PcmBuffer &buffer, unsigned channels, unsigned src_rate, - ConstBuffer src, + std::span src, unsigned dest_rate) noexcept { - const auto typed_src = ConstBuffer::FromVoid(src); - return pcm_resample_fallback(buffer, channels, src_rate, typed_src, - dest_rate).ToVoid(); + const auto typed_src = FromBytesStrict(src); + return std::as_bytes(pcm_resample_fallback(buffer, channels, src_rate, typed_src, + dest_rate)); } -ConstBuffer -FallbackPcmResampler::Resample(ConstBuffer src) +std::span +FallbackPcmResampler::Resample(std::span src) { switch (format.format) { case SampleFormat::UNDEFINED: diff --git a/src/pcm/FallbackResampler.hxx b/src/pcm/FallbackResampler.hxx index 26e41ba04..2dd080a04 100644 --- a/src/pcm/FallbackResampler.hxx +++ b/src/pcm/FallbackResampler.hxx @@ -24,6 +24,9 @@ #include "Buffer.hxx" #include "AudioFormat.hxx" +#include +#include + /** * A naive resampler that is used when no external library was found * (or when the user explicitly asks for bad quality). @@ -37,7 +40,7 @@ class FallbackPcmResampler final : public PcmResampler { public: AudioFormat Open(AudioFormat &af, unsigned new_sample_rate) override; void Close() noexcept override; - ConstBuffer Resample(ConstBuffer src) override; + std::span Resample(std::span src) override; }; #endif diff --git a/src/pcm/FormatConverter.cxx b/src/pcm/FormatConverter.cxx index 9220f68c2..43a1c590a 100644 --- a/src/pcm/FormatConverter.cxx +++ b/src/pcm/FormatConverter.cxx @@ -19,7 +19,6 @@ #include "FormatConverter.hxx" #include "PcmFormat.hxx" -#include "util/ConstBuffer.hxx" #include "util/RuntimeError.hxx" #include @@ -61,8 +60,8 @@ PcmFormatConverter::Close() noexcept #endif } -ConstBuffer -PcmFormatConverter::Convert(ConstBuffer src) noexcept +std::span +PcmFormatConverter::Convert(std::span src) noexcept { switch (dest_format) { case SampleFormat::UNDEFINED: @@ -72,24 +71,24 @@ PcmFormatConverter::Convert(ConstBuffer src) noexcept gcc_unreachable(); case SampleFormat::S16: - return pcm_convert_to_16(buffer, dither, - src_format, - src).ToVoid(); + return std::as_bytes(pcm_convert_to_16(buffer, dither, + src_format, + src)); case SampleFormat::S24_P32: - return pcm_convert_to_24(buffer, - src_format, - src).ToVoid(); + return std::as_bytes(pcm_convert_to_24(buffer, + src_format, + src)); case SampleFormat::S32: - return pcm_convert_to_32(buffer, - src_format, - src).ToVoid(); + return std::as_bytes(pcm_convert_to_32(buffer, + src_format, + src)); case SampleFormat::FLOAT: - return pcm_convert_to_float(buffer, - src_format, - src).ToVoid(); + return std::as_bytes(pcm_convert_to_float(buffer, + src_format, + src)); } assert(false); diff --git a/src/pcm/FormatConverter.hxx b/src/pcm/FormatConverter.hxx index 84c0e7de7..865fbdbce 100644 --- a/src/pcm/FormatConverter.hxx +++ b/src/pcm/FormatConverter.hxx @@ -24,12 +24,12 @@ #include "Buffer.hxx" #include "Dither.hxx" +#include + #ifndef NDEBUG #include #endif -template struct ConstBuffer; - /** * A class that converts samples from one format to another. */ @@ -73,7 +73,7 @@ public: * @return the destination buffer */ [[gnu::pure]] - ConstBuffer Convert(ConstBuffer src) noexcept; + std::span Convert(std::span src) noexcept; }; #endif diff --git a/src/pcm/GlueResampler.cxx b/src/pcm/GlueResampler.cxx index 2621ad899..e379acd50 100644 --- a/src/pcm/GlueResampler.cxx +++ b/src/pcm/GlueResampler.cxx @@ -71,18 +71,16 @@ GluePcmResampler::Reset() noexcept resampler->Reset(); } -ConstBuffer -GluePcmResampler::Resample(ConstBuffer src) +std::span +GluePcmResampler::Resample(std::span src) { - assert(!src.IsNull()); - if (requested_sample_format != src_sample_format) src = format_converter.Convert(src); return resampler->Resample(src); } -ConstBuffer +std::span GluePcmResampler::Flush() { return resampler->Flush(); diff --git a/src/pcm/GlueResampler.hxx b/src/pcm/GlueResampler.hxx index 8dedf55a0..580a9bc44 100644 --- a/src/pcm/GlueResampler.hxx +++ b/src/pcm/GlueResampler.hxx @@ -23,9 +23,11 @@ #include "SampleFormat.hxx" #include "FormatConverter.hxx" +#include +#include + struct AudioFormat; class PcmResampler; -template struct ConstBuffer; /** * A glue class that integrates a #PcmResampler and automatically @@ -60,9 +62,9 @@ public: */ void Reset() noexcept; - ConstBuffer Resample(ConstBuffer src); + std::span Resample(std::span src); - ConstBuffer Flush(); + std::span Flush(); }; #endif diff --git a/src/pcm/Interleave.cxx b/src/pcm/Interleave.cxx index f14cfa9e4..9c350fece 100644 --- a/src/pcm/Interleave.cxx +++ b/src/pcm/Interleave.cxx @@ -23,11 +23,11 @@ static void GenericPcmInterleave(uint8_t *gcc_restrict dest, - ConstBuffer src, + std::span src, size_t n_frames, size_t sample_size) noexcept { for (size_t frame = 0; frame < n_frames; ++frame) { - for (size_t channel = 0; channel < src.size; ++channel) { + for (size_t channel = 0; channel < src.size(); ++channel) { memcpy(dest, src[channel] + frame * sample_size, sample_size); dest += sample_size; @@ -51,10 +51,10 @@ PcmInterleaveStereo(T *gcc_restrict dest, template static void PcmInterleaveT(T *gcc_restrict dest, - const ConstBuffer src, + const std::span src, size_t n_frames) noexcept { - switch (src.size) { + switch (src.size()) { case 2: PcmInterleaveStereo(dest, src[0], src[1], n_frames); return; @@ -64,14 +64,14 @@ PcmInterleaveT(T *gcc_restrict dest, auto *d = dest++; for (const auto *const s_end = s + n_frames; - s != s_end; ++s, d += src.size) + s != s_end; ++s, d += src.size()) *d = *s; } } static void PcmInterleave16(int16_t *gcc_restrict dest, - const ConstBuffer src, + const std::span src, size_t n_frames) noexcept { PcmInterleaveT(dest, src, n_frames); @@ -79,7 +79,7 @@ PcmInterleave16(int16_t *gcc_restrict dest, void PcmInterleave32(int32_t *gcc_restrict dest, - const ConstBuffer src, + const std::span src, size_t n_frames) noexcept { PcmInterleaveT(dest, src, n_frames); @@ -87,28 +87,25 @@ PcmInterleave32(int32_t *gcc_restrict dest, void PcmInterleave(void *gcc_restrict dest, - ConstBuffer src, + std::span src, size_t n_frames, size_t sample_size) noexcept { switch (sample_size) { case 2: PcmInterleave16((int16_t *)dest, - ConstBuffer((const int16_t *const*)src.data, - src.size), + {(const int16_t *const*)src.data(), src.size()}, n_frames); break; case 4: PcmInterleave32((int32_t *)dest, - ConstBuffer((const int32_t *const*)src.data, - src.size), + {(const int32_t *const*)src.data(), src.size()}, n_frames); break; default: GenericPcmInterleave((uint8_t *)dest, - ConstBuffer((const uint8_t *const*)src.data, - src.size), + {(const uint8_t *const*)src.data(), src.size()}, n_frames, sample_size); } } diff --git a/src/pcm/Interleave.hxx b/src/pcm/Interleave.hxx index fbffc7d5e..0ea599c85 100644 --- a/src/pcm/Interleave.hxx +++ b/src/pcm/Interleave.hxx @@ -21,15 +21,16 @@ #define MPD_PCM_INTERLEAVE_HXX #include "util/Compiler.h" -#include "util/ConstBuffer.hxx" +#include #include +#include /** * Interleave planar PCM samples from #src to #dest. */ void -PcmInterleave(void *gcc_restrict dest, ConstBuffer src, +PcmInterleave(void *gcc_restrict dest, std::span src, size_t n_frames, size_t sample_size) noexcept; /** @@ -37,16 +38,18 @@ PcmInterleave(void *gcc_restrict dest, ConstBuffer src, * per sample). */ void -PcmInterleave32(int32_t *gcc_restrict dest, ConstBuffer src, +PcmInterleave32(int32_t *gcc_restrict dest, + std::span src, size_t n_frames) noexcept; static inline void -PcmInterleaveFloat(float *gcc_restrict dest, ConstBuffer src, +PcmInterleaveFloat(float *gcc_restrict dest, + std::span src, size_t n_frames) noexcept { PcmInterleave32((int32_t *)dest, - ConstBuffer((const int32_t *const*)src.data, - src.size), + std::span((const int32_t *const*)src.data(), + src.size()), n_frames); } diff --git a/src/pcm/LibsamplerateResampler.cxx b/src/pcm/LibsamplerateResampler.cxx index a09189163..b0bd44549 100644 --- a/src/pcm/LibsamplerateResampler.cxx +++ b/src/pcm/LibsamplerateResampler.cxx @@ -22,6 +22,7 @@ #include "util/ASCII.hxx" #include "util/RuntimeError.hxx" #include "util/Domain.hxx" +#include "util/SpanCast.hxx" #include "Log.hxx" #include @@ -120,17 +121,17 @@ LibsampleratePcmResampler::Reset() noexcept src_reset(state); } -inline ConstBuffer -LibsampleratePcmResampler::Resample2(ConstBuffer src) +inline std::span +LibsampleratePcmResampler::Resample2(std::span src) { - assert(src.size % channels == 0); + assert(src.size() % channels == 0); - const unsigned src_frames = src.size / channels; + const unsigned src_frames = src.size() / channels; const unsigned dest_frames = (src_frames * dest_rate + src_rate - 1) / src_rate; size_t data_out_size = dest_frames * sizeof(float) * channels; - data.data_in = const_cast(src.data); + data.data_in = const_cast(src.data()); data.data_out = (float *)buffer.Get(data_out_size); data.input_frames = src_frames; data.output_frames = dest_frames; @@ -143,8 +144,8 @@ LibsampleratePcmResampler::Resample2(ConstBuffer src) return {data.data_out, size_t(data.output_frames_gen * channels)}; } -ConstBuffer -LibsampleratePcmResampler::Resample(ConstBuffer src) +std::span +LibsampleratePcmResampler::Resample(std::span src) { - return Resample2(ConstBuffer::FromVoid(src)).ToVoid(); + return std::as_bytes(Resample2(FromBytesStrict(src))); } diff --git a/src/pcm/LibsamplerateResampler.hxx b/src/pcm/LibsamplerateResampler.hxx index e3bb40914..cc13a2ab2 100644 --- a/src/pcm/LibsamplerateResampler.hxx +++ b/src/pcm/LibsamplerateResampler.hxx @@ -44,10 +44,10 @@ public: AudioFormat Open(AudioFormat &af, unsigned new_sample_rate) override; void Close() noexcept override; void Reset() noexcept override; - ConstBuffer Resample(ConstBuffer src) override; + std::span Resample(std::span src) override; private: - ConstBuffer Resample2(ConstBuffer src); + std::span Resample2(std::span src); }; void diff --git a/src/pcm/MixRampAnalyzer.cxx b/src/pcm/MixRampAnalyzer.cxx index 939d0dbe6..bde98fffb 100644 --- a/src/pcm/MixRampAnalyzer.cxx +++ b/src/pcm/MixRampAnalyzer.cxx @@ -18,7 +18,8 @@ */ #include "MixRampAnalyzer.hxx" -#include "util/ConstBuffer.hxx" + +#include inline void MixRampData::Add(MixRampItem item) noexcept @@ -35,20 +36,20 @@ MixRampData::Add(MixRampItem item) noexcept } void -MixRampAnalyzer::Process(ConstBuffer src) noexcept +MixRampAnalyzer::Process(std::span src) noexcept { while (!src.empty()) { std::size_t chunk_remaining = chunk_frames - chunk_fill; assert(chunk_remaining > 0); - if (chunk_remaining > src.size) { + if (chunk_remaining > src.size()) { gain_analyzer.Process(src); - chunk_fill += src.size; + chunk_fill += src.size(); return; } - gain_analyzer.Process({src.data, chunk_remaining}); - src.skip_front(chunk_remaining); + gain_analyzer.Process({src.data(), chunk_remaining}); + src = src.subspan(chunk_remaining); gain_analyzer.Flush(); diff --git a/src/pcm/MixRampAnalyzer.hxx b/src/pcm/MixRampAnalyzer.hxx index 779640d27..3eb05e63f 100644 --- a/src/pcm/MixRampAnalyzer.hxx +++ b/src/pcm/MixRampAnalyzer.hxx @@ -22,6 +22,8 @@ #include "ReplayGainAnalyzer.hxx" #include "Chrono.hxx" +#include + constexpr auto mixramp_volumes = std::array{ -90., -60., -40., -30., -24., -21., -18., -15., -12., -9., -6., -3., 0., 3., 6., @@ -74,7 +76,7 @@ class MixRampAnalyzer { std::size_t chunk_fill = 0; public: - void Process(ConstBuffer src) noexcept; + void Process(std::span src) noexcept; FloatDuration GetTime() const noexcept { return chunk_number * chunk_duration; diff --git a/src/pcm/MixRampGlue.cxx b/src/pcm/MixRampGlue.cxx index ab1670b0a..1cdc9bef3 100644 --- a/src/pcm/MixRampGlue.cxx +++ b/src/pcm/MixRampGlue.cxx @@ -23,7 +23,7 @@ #include "MusicPipe.hxx" #include "MusicChunk.hxx" #include "util/Compiler.h" -#include "util/ConstBuffer.hxx" +#include "util/SpanCast.hxx" #include @@ -99,7 +99,7 @@ AnalyzeMixRamp(const MusicPipe &pipe, const AudioFormat &audio_format, MixRampAnalyzer a; do { - a.Process(ConstBuffer::FromVoid({chunk->data, chunk->length})); + a.Process(FromBytesStrict({chunk->data, chunk->length})); } while ((chunk = chunk->next.get()) != nullptr); return ToString(a.GetResult(), a.GetTime(), direction); diff --git a/src/pcm/Order.cxx b/src/pcm/Order.cxx index 1fdaa4561..a6057222f 100644 --- a/src/pcm/Order.cxx +++ b/src/pcm/Order.cxx @@ -19,8 +19,7 @@ #include "Order.hxx" #include "Buffer.hxx" -#include "util/ConstBuffer.hxx" - +#include "util/SpanCast.hxx" /* * According to: @@ -109,12 +108,12 @@ ToAlsaChannelOrder50(V *dest, const V *src, size_t n) noexcept } template -static inline ConstBuffer -ToAlsaChannelOrder50(PcmBuffer &buffer, ConstBuffer src) noexcept +static inline std::span +ToAlsaChannelOrder50(PcmBuffer &buffer, std::span src) noexcept { - auto dest = buffer.GetT(src.size); - ToAlsaChannelOrder50(dest, src.data, src.size / 5); - return { dest, src.size }; + auto dest = buffer.GetT(src.size()); + ToAlsaChannelOrder50(dest, src.data(), src.size() / 5); + return { dest, src.size() }; } template @@ -127,12 +126,12 @@ ToAlsaChannelOrder51(V *dest, const V *src, size_t n) noexcept } template -static inline ConstBuffer -ToAlsaChannelOrder51(PcmBuffer &buffer, ConstBuffer src) noexcept +static inline std::span +ToAlsaChannelOrder51(PcmBuffer &buffer, std::span src) noexcept { - auto dest = buffer.GetT(src.size); - ToAlsaChannelOrder51(dest, src.data, src.size / 6); - return { dest, src.size }; + auto dest = buffer.GetT(src.size()); + ToAlsaChannelOrder51(dest, src.data(), src.size() / 6); + return { dest, src.size() }; } template @@ -145,12 +144,12 @@ ToAlsaChannelOrder70(V *dest, const V *src, size_t n) noexcept } template -static inline ConstBuffer -ToAlsaChannelOrder70(PcmBuffer &buffer, ConstBuffer src) noexcept +static inline std::span +ToAlsaChannelOrder70(PcmBuffer &buffer, std::span src) noexcept { - auto dest = buffer.GetT(src.size); - ToAlsaChannelOrder70(dest, src.data, src.size / 7); - return { dest, src.size }; + auto dest = buffer.GetT(src.size()); + ToAlsaChannelOrder70(dest, src.data(), src.size() / 7); + return { dest, src.size() }; } template @@ -163,17 +162,17 @@ ToAlsaChannelOrder71(V *dest, const V *src, size_t n) noexcept } template -static inline ConstBuffer -ToAlsaChannelOrder71(PcmBuffer &buffer, ConstBuffer src) noexcept +static inline std::span +ToAlsaChannelOrder71(PcmBuffer &buffer, std::span src) noexcept { - auto dest = buffer.GetT(src.size); - ToAlsaChannelOrder71(dest, src.data, src.size / 8); - return { dest, src.size }; + auto dest = buffer.GetT(src.size()); + ToAlsaChannelOrder71(dest, src.data(), src.size() / 8); + return { dest, src.size() }; } template -static ConstBuffer -ToAlsaChannelOrderT(PcmBuffer &buffer, ConstBuffer src, +static std::span +ToAlsaChannelOrderT(PcmBuffer &buffer, std::span src, unsigned channels) noexcept { switch (channels) { @@ -194,8 +193,8 @@ ToAlsaChannelOrderT(PcmBuffer &buffer, ConstBuffer src, } } -ConstBuffer -ToAlsaChannelOrder(PcmBuffer &buffer, ConstBuffer src, +std::span +ToAlsaChannelOrder(PcmBuffer &buffer, std::span src, SampleFormat sample_format, unsigned channels) noexcept { switch (sample_format) { @@ -205,16 +204,16 @@ ToAlsaChannelOrder(PcmBuffer &buffer, ConstBuffer src, return src; case SampleFormat::S16: - return ToAlsaChannelOrderT(buffer, - ConstBuffer::FromVoid(src), - channels).ToVoid(); + return std::as_bytes(ToAlsaChannelOrderT(buffer, + FromBytesStrict(src), + channels)); case SampleFormat::S24_P32: case SampleFormat::S32: case SampleFormat::FLOAT: - return ToAlsaChannelOrderT(buffer, - ConstBuffer::FromVoid(src), - channels).ToVoid(); + return std::as_bytes(ToAlsaChannelOrderT(buffer, + FromBytesStrict(src), + channels)); } gcc_unreachable(); diff --git a/src/pcm/Order.hxx b/src/pcm/Order.hxx index 195b0d748..4cc8da383 100644 --- a/src/pcm/Order.hxx +++ b/src/pcm/Order.hxx @@ -22,15 +22,16 @@ #include "SampleFormat.hxx" +#include + class PcmBuffer; -template struct ConstBuffer; /** * Convert the given buffer from FLAC channel order * (https://xiph.org/flac/format.html) to ALSA channel order. */ -ConstBuffer -ToAlsaChannelOrder(PcmBuffer &buffer, ConstBuffer src, +std::span +ToAlsaChannelOrder(PcmBuffer &buffer, std::span src, SampleFormat sample_format, unsigned channels) noexcept; #endif diff --git a/src/pcm/PcmChannels.cxx b/src/pcm/PcmChannels.cxx index 91189ab60..e467a15cc 100644 --- a/src/pcm/PcmChannels.cxx +++ b/src/pcm/PcmChannels.cxx @@ -22,7 +22,6 @@ #include "Buffer.hxx" #include "Silence.hxx" #include "Traits.hxx" -#include "util/ConstBuffer.hxx" #include #include @@ -30,11 +29,9 @@ template static void -MonoToStereo(D dest, S src, S end) noexcept +MonoToStereo(D dest, S src) noexcept { - while (src != end) { - const auto value = *src++; - + for (const auto value : src) { *dest++ = value; *dest++ = value; } @@ -55,10 +52,9 @@ StereoToMono(typename Traits::value_type _a, template> static typename Traits::pointer StereoToMono(typename Traits::pointer dest, - typename Traits::const_pointer src, - typename Traits::const_pointer end) noexcept + std::span _src) noexcept { - while (src != end) { + for (auto src = _src.begin(), end = _src.end(); src != end;) { const auto a = *src++; const auto b = *src++; @@ -72,12 +68,11 @@ template> static typename Traits::pointer NToStereo(typename Traits::pointer dest, unsigned src_channels, - typename Traits::const_pointer src, - typename Traits::const_pointer end) noexcept + std::span _src) noexcept { - assert((end - src) % src_channels == 0); + assert(_src.size() % src_channels == 0); - while (src != end) { + for (auto src = _src.begin(), end = _src.end(); src != end;) { typename Traits::sum_type sum = *src++; for (unsigned c = 1; c < src_channels; ++c) sum += *src++; @@ -101,16 +96,15 @@ template> static typename Traits::pointer StereoToN(typename Traits::pointer dest, unsigned dest_channels, - typename Traits::const_pointer src, - typename Traits::const_pointer end) noexcept + std::span _src) noexcept { assert(dest_channels > 2); - assert((end - src) % 2 == 0); + assert(_src.size() % 2 == 0); std::array silence; PcmSilence(std::as_writable_bytes(std::span{silence}), F); - while (src != end) { + for (auto src = _src.begin(), end = _src.end(); src != end;) { /* copy left/right to front-left/front-right, which is the first two channels in all multi-channel configurations **/ @@ -129,12 +123,11 @@ static typename Traits::pointer NToM(typename Traits::pointer dest, unsigned dest_channels, unsigned src_channels, - typename Traits::const_pointer src, - typename Traits::const_pointer end) noexcept + std::span _src) noexcept { - assert((end - src) % src_channels == 0); + assert(_src.size() % src_channels == 0); - while (src != end) { + for (auto src = _src.begin(), end = _src.end(); src != end;) { typename Traits::sum_type sum = *src++; for (unsigned c = 1; c < src_channels; ++c) sum += *src++; @@ -150,68 +143,66 @@ NToM(typename Traits::pointer dest, } template> -static ConstBuffer +static std::span ConvertChannels(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept + std::span src) noexcept { - assert(src.size % src_channels == 0); + assert(src.size() % src_channels == 0); - const size_t dest_size = src.size / src_channels * dest_channels; + const size_t dest_size = src.size() / src_channels * dest_channels; auto dest = buffer.GetT(dest_size); if (src_channels == 1 && dest_channels == 2) - MonoToStereo(dest, src.begin(), src.end()); + MonoToStereo(dest, src); else if (src_channels == 2 && dest_channels == 1) - StereoToMono(dest, src.begin(), src.end()); + StereoToMono(dest, src); else if (dest_channels == 2) - NToStereo(dest, src_channels, src.begin(), src.end()); + NToStereo(dest, src_channels, src); else if (src_channels == 2 && dest_channels > 2) - StereoToN(dest, dest_channels, - src.begin(), src.end()); + StereoToN(dest, dest_channels, src); else - NToM(dest, dest_channels, - src_channels, src.begin(), src.end()); + NToM(dest, dest_channels, src_channels, src); return { dest, dest_size }; } -ConstBuffer +std::span pcm_convert_channels_16(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept + std::span src) noexcept { return ConvertChannels(buffer, dest_channels, src_channels, src); } -ConstBuffer +std::span pcm_convert_channels_24(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept + std::span src) noexcept { return ConvertChannels(buffer, dest_channels, src_channels, src); } -ConstBuffer +std::span pcm_convert_channels_32(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept + std::span src) noexcept { return ConvertChannels(buffer, dest_channels, src_channels, src); } -ConstBuffer +std::span pcm_convert_channels_float(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept + std::span src) noexcept { return ConvertChannels(buffer, dest_channels, src_channels, src); diff --git a/src/pcm/PcmChannels.hxx b/src/pcm/PcmChannels.hxx index 02d901abc..bc8a54283 100644 --- a/src/pcm/PcmChannels.hxx +++ b/src/pcm/PcmChannels.hxx @@ -21,9 +21,9 @@ #define MPD_PCM_CHANNELS_HXX #include +#include class PcmBuffer; -template struct ConstBuffer; /** * Changes the number of channels in 16 bit PCM data. @@ -34,11 +34,11 @@ template struct ConstBuffer; * @param src the source PCM buffer * @return the destination buffer */ -ConstBuffer +std::span pcm_convert_channels_16(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept; + std::span src) noexcept; /** * Changes the number of channels in 24 bit PCM data (aligned at 32 @@ -50,11 +50,11 @@ pcm_convert_channels_16(PcmBuffer &buffer, * @param src the source PCM buffer * @return the destination buffer */ -ConstBuffer +std::span pcm_convert_channels_24(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept; + std::span src) noexcept; /** * Changes the number of channels in 32 bit PCM data. @@ -65,11 +65,11 @@ pcm_convert_channels_24(PcmBuffer &buffer, * @param src the source PCM buffer * @return the destination buffer */ -ConstBuffer +std::span pcm_convert_channels_32(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept; + std::span src) noexcept; /** * Changes the number of channels in 32 bit float PCM data. @@ -80,10 +80,10 @@ pcm_convert_channels_32(PcmBuffer &buffer, * @param src the source PCM buffer * @return the destination buffer */ -ConstBuffer +std::span pcm_convert_channels_float(PcmBuffer &buffer, unsigned dest_channels, unsigned src_channels, - ConstBuffer src) noexcept; + std::span src) noexcept; #endif diff --git a/src/pcm/PcmDsd.cxx b/src/pcm/PcmDsd.cxx index db673b634..1fa5473d4 100644 --- a/src/pcm/PcmDsd.cxx +++ b/src/pcm/PcmDsd.cxx @@ -19,38 +19,35 @@ #include "PcmDsd.hxx" #include "Dsd2Pcm.hxx" -#include "util/ConstBuffer.hxx" #include -ConstBuffer -PcmDsd::ToFloat(unsigned channels, ConstBuffer src) noexcept +std::span +PcmDsd::ToFloat(unsigned channels, std::span src) noexcept { - assert(!src.IsNull()); assert(!src.empty()); - assert(src.size % channels == 0); + assert(src.size() % channels == 0); - const size_t num_samples = src.size; - const size_t num_frames = src.size / channels; + const size_t num_samples = src.size(); + const size_t num_frames = src.size() / channels; auto *dest = buffer.GetT(num_samples); - dsd2pcm.Translate(channels, num_frames, src.data, dest); + dsd2pcm.Translate(channels, num_frames, src.data(), dest); return { dest, num_samples }; } -ConstBuffer -PcmDsd::ToS24(unsigned channels, ConstBuffer src) noexcept +std::span +PcmDsd::ToS24(unsigned channels, std::span src) noexcept { - assert(!src.IsNull()); assert(!src.empty()); - assert(src.size % channels == 0); + assert(src.size() % channels == 0); - const size_t num_samples = src.size; - const size_t num_frames = src.size / channels; + const size_t num_samples = src.size(); + const size_t num_frames = src.size() / channels; auto *dest = buffer.GetT(num_samples); - dsd2pcm.TranslateS24(channels, num_frames, src.data, dest); + dsd2pcm.TranslateS24(channels, num_frames, src.data(), dest); return { dest, num_samples }; } diff --git a/src/pcm/PcmDsd.hxx b/src/pcm/PcmDsd.hxx index 8bfaf0948..122f73f45 100644 --- a/src/pcm/PcmDsd.hxx +++ b/src/pcm/PcmDsd.hxx @@ -24,8 +24,7 @@ #include "Dsd2Pcm.hxx" #include - -template struct ConstBuffer; +#include /** * Wrapper for the dsd2pcm library. @@ -40,11 +39,11 @@ public: dsd2pcm.Reset(); } - ConstBuffer ToFloat(unsigned channels, - ConstBuffer src) noexcept; + std::span ToFloat(unsigned channels, + std::span src) noexcept; - ConstBuffer ToS24(unsigned channels, - ConstBuffer src) noexcept; + std::span ToS24(unsigned channels, + std::span src) noexcept; }; #endif diff --git a/src/pcm/PcmFormat.cxx b/src/pcm/PcmFormat.cxx index 5178fad65..667fc1932 100644 --- a/src/pcm/PcmFormat.cxx +++ b/src/pcm/PcmFormat.cxx @@ -22,7 +22,7 @@ #include "Traits.hxx" #include "FloatConvert.hxx" #include "ShiftConvert.hxx" -#include "util/ConstBuffer.hxx" +#include "util/SpanCast.hxx" #include "util/TransformN.hxx" #include "Dither.cxx" // including the .cxx file to get inlined templates @@ -115,51 +115,51 @@ struct FloatToInteger> #endif template -static ConstBuffer +static std::span AllocateConvert(PcmBuffer &buffer, C convert, - ConstBuffer src) + std::span src) { - auto dest = buffer.GetT(src.size); - convert.Convert(dest, src.data, src.size); - return { dest, src.size }; + auto dest = buffer.GetT(src.size()); + convert.Convert(dest, src.data(), src.size()); + return { dest, src.size() }; } template> -static ConstBuffer -AllocateFromFloat(PcmBuffer &buffer, ConstBuffer src) +static std::span +AllocateFromFloat(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, FloatToInteger(), src); } -static ConstBuffer -pcm_allocate_8_to_16(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_8_to_16(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert8To16(), src); } -static ConstBuffer +static std::span pcm_allocate_24p32_to_16(PcmBuffer &buffer, PcmDither &dither, - ConstBuffer src) + std::span src) { return AllocateConvert(buffer, Convert24To16(dither), src); } -static ConstBuffer +static std::span pcm_allocate_32_to_16(PcmBuffer &buffer, PcmDither &dither, - ConstBuffer src) + std::span src) { return AllocateConvert(buffer, Convert32To16(dither), src); } -static ConstBuffer -pcm_allocate_float_to_16(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_float_to_16(PcmBuffer &buffer, std::span src) { return AllocateFromFloat(buffer, src); } -ConstBuffer +std::span pcm_convert_to_16(PcmBuffer &buffer, PcmDither &dither, - SampleFormat src_format, ConstBuffer src) noexcept + SampleFormat src_format, std::span src) noexcept { switch (src_format) { case SampleFormat::UNDEFINED: @@ -168,25 +168,25 @@ pcm_convert_to_16(PcmBuffer &buffer, PcmDither &dither, case SampleFormat::S8: return pcm_allocate_8_to_16(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S16: - return ConstBuffer::FromVoid(src); + return FromBytesStrict(src); case SampleFormat::S24_P32: return pcm_allocate_24p32_to_16(buffer, dither, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S32: return pcm_allocate_32_to_16(buffer, dither, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::FLOAT: return pcm_allocate_float_to_16(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); } - return nullptr; + return {}; } struct Convert8To24 @@ -197,14 +197,14 @@ struct Convert16To24 : PerSampleConvert> {}; -static ConstBuffer -pcm_allocate_8_to_24(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_8_to_24(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert8To24(), src); } -static ConstBuffer -pcm_allocate_16_to_24(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_16_to_24(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert16To24(), src); } @@ -213,21 +213,21 @@ struct Convert32To24 : PerSampleConvert> {}; -static ConstBuffer -pcm_allocate_32_to_24(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_32_to_24(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert32To24(), src); } -static ConstBuffer -pcm_allocate_float_to_24(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_float_to_24(PcmBuffer &buffer, std::span src) { return AllocateFromFloat(buffer, src); } -ConstBuffer +std::span pcm_convert_to_24(PcmBuffer &buffer, - SampleFormat src_format, ConstBuffer src) noexcept + SampleFormat src_format, std::span src) noexcept { switch (src_format) { case SampleFormat::UNDEFINED: @@ -236,25 +236,25 @@ pcm_convert_to_24(PcmBuffer &buffer, case SampleFormat::S8: return pcm_allocate_8_to_24(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S16: return pcm_allocate_16_to_24(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S24_P32: - return ConstBuffer::FromVoid(src); + return FromBytesStrict(src); case SampleFormat::S32: return pcm_allocate_32_to_24(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::FLOAT: return pcm_allocate_float_to_24(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); } - return nullptr; + return {}; } struct Convert8To32 @@ -269,33 +269,33 @@ struct Convert24To32 : PerSampleConvert> {}; -static ConstBuffer -pcm_allocate_8_to_32(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_8_to_32(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert8To32(), src); } -static ConstBuffer -pcm_allocate_16_to_32(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_16_to_32(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert16To32(), src); } -static ConstBuffer -pcm_allocate_24p32_to_32(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_24p32_to_32(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert24To32(), src); } -static ConstBuffer -pcm_allocate_float_to_32(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_float_to_32(PcmBuffer &buffer, std::span src) { return AllocateFromFloat(buffer, src); } -ConstBuffer +std::span pcm_convert_to_32(PcmBuffer &buffer, - SampleFormat src_format, ConstBuffer src) noexcept + SampleFormat src_format, std::span src) noexcept { switch (src_format) { case SampleFormat::UNDEFINED: @@ -304,25 +304,25 @@ pcm_convert_to_32(PcmBuffer &buffer, case SampleFormat::S8: return pcm_allocate_8_to_32(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S16: return pcm_allocate_16_to_32(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S24_P32: return pcm_allocate_24p32_to_32(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S32: - return ConstBuffer::FromVoid(src); + return FromBytesStrict(src); case SampleFormat::FLOAT: return pcm_allocate_float_to_32(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); } - return nullptr; + return {}; } struct Convert8ToFloat @@ -337,33 +337,33 @@ struct Convert24ToFloat struct Convert32ToFloat : PerSampleConvert> {}; -static ConstBuffer -pcm_allocate_8_to_float(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_8_to_float(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert8ToFloat(), src); } -static ConstBuffer -pcm_allocate_16_to_float(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_16_to_float(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert16ToFloat(), src); } -static ConstBuffer -pcm_allocate_24p32_to_float(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_24p32_to_float(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert24ToFloat(), src); } -static ConstBuffer -pcm_allocate_32_to_float(PcmBuffer &buffer, ConstBuffer src) +static std::span +pcm_allocate_32_to_float(PcmBuffer &buffer, std::span src) { return AllocateConvert(buffer, Convert32ToFloat(), src); } -ConstBuffer +std::span pcm_convert_to_float(PcmBuffer &buffer, - SampleFormat src_format, ConstBuffer src) noexcept + SampleFormat src_format, std::span src) noexcept { switch (src_format) { case SampleFormat::UNDEFINED: @@ -372,23 +372,23 @@ pcm_convert_to_float(PcmBuffer &buffer, case SampleFormat::S8: return pcm_allocate_8_to_float(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S16: return pcm_allocate_16_to_float(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S32: return pcm_allocate_32_to_float(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::S24_P32: return pcm_allocate_24p32_to_float(buffer, - ConstBuffer::FromVoid(src)); + FromBytesStrict(src)); case SampleFormat::FLOAT: - return ConstBuffer::FromVoid(src); + return FromBytesStrict(src); } - return nullptr; + return {}; } diff --git a/src/pcm/PcmFormat.hxx b/src/pcm/PcmFormat.hxx index 41d67ebae..392a4bc2d 100644 --- a/src/pcm/PcmFormat.hxx +++ b/src/pcm/PcmFormat.hxx @@ -23,8 +23,8 @@ #include "SampleFormat.hxx" #include +#include -template struct ConstBuffer; class PcmBuffer; class PcmDither; @@ -38,9 +38,9 @@ class PcmDither; * @return the destination buffer */ [[gnu::pure]] -ConstBuffer +std::span pcm_convert_to_16(PcmBuffer &buffer, PcmDither &dither, - SampleFormat src_format, ConstBuffer src) noexcept; + SampleFormat src_format, std::span src) noexcept; /** * Converts PCM samples to 24 bit (32 bit alignment). @@ -50,9 +50,9 @@ pcm_convert_to_16(PcmBuffer &buffer, PcmDither &dither, * @return the destination buffer */ [[gnu::pure]] -ConstBuffer +std::span pcm_convert_to_24(PcmBuffer &buffer, - SampleFormat src_format, ConstBuffer src) noexcept; + SampleFormat src_format, std::span src) noexcept; /** * Converts PCM samples to 32 bit. @@ -62,9 +62,9 @@ pcm_convert_to_24(PcmBuffer &buffer, * @return the destination buffer */ [[gnu::pure]] -ConstBuffer +std::span pcm_convert_to_32(PcmBuffer &buffer, - SampleFormat src_format, ConstBuffer src) noexcept; + SampleFormat src_format, std::span src) noexcept; /** * Converts PCM samples to 32 bit floating point. @@ -74,8 +74,8 @@ pcm_convert_to_32(PcmBuffer &buffer, * @return the destination buffer */ [[gnu::pure]] -ConstBuffer +std::span pcm_convert_to_float(PcmBuffer &buffer, - SampleFormat src_format, ConstBuffer src) noexcept; + SampleFormat src_format, std::span src) noexcept; #endif diff --git a/src/pcm/ReplayGainAnalyzer.cxx b/src/pcm/ReplayGainAnalyzer.cxx index 10476f2c3..feaa11b5f 100644 --- a/src/pcm/ReplayGainAnalyzer.cxx +++ b/src/pcm/ReplayGainAnalyzer.cxx @@ -25,7 +25,6 @@ #include "ReplayGainAnalyzer.hxx" #include "util/Compiler.h" -#include "util/ConstBuffer.hxx" #include #include @@ -85,7 +84,7 @@ SquareHypot(ReplayGainAnalyzer::Frame f) noexcept */ [[gnu::hot]] static double -CalcStereoRMS(ConstBuffer src) noexcept +CalcStereoRMS(std::span src) noexcept { #if GCC_OLDER_THAN(10,0) /* GCC 8 doesn't have std::transform_reduce() */ @@ -100,7 +99,7 @@ CalcStereoRMS(ConstBuffer src) noexcept SquareHypot); #endif - return 10 * std::log10(sum / src.size) + 90.0 - 3.0; + return 10 * std::log10(sum / src.size()) + 90.0 - 3.0; } static constexpr bool @@ -118,7 +117,7 @@ IsSilentFrame(ReplayGainAnalyzer::Frame frame) noexcept [[gnu::pure]] static bool -IsSilentBuffer(ConstBuffer buffer) noexcept +IsSilentBuffer(std::span buffer) noexcept { return std::all_of(buffer.begin(), buffer.end(), IsSilentFrame); } @@ -261,20 +260,20 @@ ReplayGainAnalyzer::Butter::Filter(Frame *gcc_restrict samples, } void -ReplayGainAnalyzer::Process(ConstBuffer src) noexcept +ReplayGainAnalyzer::Process(std::span src) noexcept { assert(!src.empty()); float new_peak = FindPeak(src.front().data(), - src.size * src.front().size()); + src.size() * src.front().size()); if (new_peak > peak) peak = new_peak; - Frame *tmp = buffer.GetT(src.size); - yule.Filter(src.data, tmp, src.size); - butter.Filter(tmp, src.size); + Frame *tmp = buffer.GetT(src.size()); + yule.Filter(src.data(), tmp, src.size()); + butter.Filter(tmp, src.size()); - const long level = std::lrint(std::floor(STEPS_PER_DB * CalcStereoRMS({tmp, src.size}))); + const long level = std::lrint(std::floor(STEPS_PER_DB * CalcStereoRMS({tmp, src.size()}))); const std::size_t level_index = std::clamp(level, 0L, (long)histogram.size() - 1L); histogram[level_index]++; } @@ -318,37 +317,37 @@ ReplayGainAnalyzer::GetGain() const noexcept } void -WindowReplayGainAnalyzer::CopyToBuffer(ConstBuffer src) noexcept +WindowReplayGainAnalyzer::CopyToBuffer(std::span src) noexcept { std::copy(src.begin(), src.end(), window_buffer.data() + window_fill); - window_fill += src.size; + window_fill += src.size(); } void -WindowReplayGainAnalyzer::Process(ConstBuffer src) noexcept +WindowReplayGainAnalyzer::Process(std::span src) noexcept { assert(window_fill < WINDOW_FRAMES); if (window_fill > 0) { std::size_t window_space = WINDOW_FRAMES - window_fill; - if (src.size < window_space) { + if (src.size() < window_space) { CopyToBuffer(src); return; } - CopyToBuffer({src.data, window_space}); + CopyToBuffer({src.data(), window_space}); Flush(); - src.skip_front(window_space); + src = src.subspan(window_space); if (src.empty()) return; } - while (src.size >= WINDOW_FRAMES) { - ReplayGainAnalyzer::Process({src.data, WINDOW_FRAMES}); - src.skip_front(WINDOW_FRAMES); + while (src.size() >= WINDOW_FRAMES) { + ReplayGainAnalyzer::Process({src.data(), WINDOW_FRAMES}); + src = src.subspan(WINDOW_FRAMES); } CopyToBuffer(src); diff --git a/src/pcm/ReplayGainAnalyzer.hxx b/src/pcm/ReplayGainAnalyzer.hxx index fcba9ad4b..1902181d0 100644 --- a/src/pcm/ReplayGainAnalyzer.hxx +++ b/src/pcm/ReplayGainAnalyzer.hxx @@ -29,8 +29,7 @@ #include #include - -template struct ConstBuffer; +#include /** * Analyze a 44.1 kHz / stereo / float32 audio stream and calculate @@ -112,7 +111,7 @@ private: public: ReplayGainAnalyzer() noexcept; - void Process(ConstBuffer src) noexcept; + void Process(std::span src) noexcept; float GetPeak() const noexcept { return peak; @@ -133,10 +132,10 @@ class WindowReplayGainAnalyzer : public ReplayGainAnalyzer { std::size_t window_fill = 0; public: - void Process(ConstBuffer src) noexcept; + void Process(std::span src) noexcept; void Flush() noexcept; private: - void CopyToBuffer(ConstBuffer src) noexcept; + void CopyToBuffer(std::span src) noexcept; }; diff --git a/src/pcm/Resampler.hxx b/src/pcm/Resampler.hxx index f14faf924..baa420537 100644 --- a/src/pcm/Resampler.hxx +++ b/src/pcm/Resampler.hxx @@ -20,9 +20,11 @@ #ifndef MPD_PCM_RESAMPLER_HXX #define MPD_PCM_RESAMPLER_HXX -#include "util/ConstBuffer.hxx" #include "util/Compiler.h" +#include +#include + struct AudioFormat; /** @@ -68,14 +70,14 @@ public: * @return the destination buffer (will be invalidated by * filter_close() or filter_filter()) */ - virtual ConstBuffer Resample(ConstBuffer src) = 0; + virtual std::span Resample(std::span src) = 0; /** * Flush pending data and return it. This should be called * repepatedly until it returns nullptr. */ - virtual ConstBuffer Flush() { - return nullptr; + virtual std::span Flush() { + return {}; } }; diff --git a/src/pcm/RestBuffer.hxx b/src/pcm/RestBuffer.hxx index a8d0f9ef4..b31747287 100644 --- a/src/pcm/RestBuffer.hxx +++ b/src/pcm/RestBuffer.hxx @@ -25,8 +25,8 @@ #include #include #include +#include -template struct ConstBuffer; class PcmBuffer; /** @@ -65,33 +65,33 @@ public: } private: - ConstBuffer Complete(ConstBuffer &src) noexcept { + std::span Complete(std::span &src) noexcept { assert(audio_valid_channel_count(GetChannelCount())); - assert(src.size % GetChannelCount() == 0); + assert(src.size() % GetChannelCount() == 0); if (size == 0) - return nullptr; + return {}; size_t missing = capacity - size; - size_t n = std::min(missing, src.size); + size_t n = std::min(missing, src.size()); std::copy_n(src.begin(), n, &data[size]); - src.skip_front(n); + src = src.subspan(n); size += n; if (size < capacity) - return nullptr; + return {}; size = 0; return {data, capacity}; } - void Append(ConstBuffer src) noexcept { + void Append(std::span src) noexcept { assert(audio_valid_channel_count(GetChannelCount())); - assert(src.size % GetChannelCount() == 0); - assert(size + src.size < capacity); + assert(src.size() % GetChannelCount() == 0); + assert(size + src.size() < capacity); - std::copy_n(src.begin(), src.size, &data[size]); - size += src.size; + std::copy(src.begin(), src.end(), &data[size]); + size += src.size(); } public: @@ -109,18 +109,18 @@ public: * may be empty */ template - ConstBuffer Process(PcmBuffer &buffer, ConstBuffer src, - size_t dest_block_size, - F &&f) { + std::span Process(PcmBuffer &buffer, std::span src, + size_t dest_block_size, + F &&f) { assert(dest_block_size % GetChannelCount() == 0); const auto previous_rest = Complete(src); - assert(previous_rest.size == 0 || - previous_rest.size == capacity); + assert(previous_rest.size() == 0 || + previous_rest.size() == capacity); const size_t previous_rest_blocks = !previous_rest.empty(); - const size_t src_blocks = src.size / capacity; - const size_t next_rest_samples = src.size % capacity; + const size_t src_blocks = src.size() / capacity; + const size_t next_rest_samples = src.size() % capacity; const size_t dest_blocks = previous_rest_blocks + src_blocks; const size_t dest_samples = dest_blocks * dest_block_size; @@ -128,14 +128,14 @@ public: auto dest = dest0; if (!previous_rest.empty()) { - f(dest, previous_rest.data, previous_rest_blocks); + f(dest, previous_rest.data(), previous_rest_blocks); dest += dest_block_size; } - f(dest, src.data, src_blocks); + f(dest, src.data(), src_blocks); if (next_rest_samples > 0) - Append({src.data + src_blocks * capacity, + Append({src.data() + src_blocks * capacity, next_rest_samples}); return { dest0, dest_samples }; diff --git a/src/pcm/SoxrResampler.cxx b/src/pcm/SoxrResampler.cxx index ea839c1f3..ad5a788a4 100644 --- a/src/pcm/SoxrResampler.cxx +++ b/src/pcm/SoxrResampler.cxx @@ -267,13 +267,13 @@ SoxrPcmResampler::Reset() noexcept #endif } -ConstBuffer -SoxrPcmResampler::Resample(ConstBuffer src) +std::span +SoxrPcmResampler::Resample(std::span src) { const size_t frame_size = channels * sizeof(float); - assert(src.size % frame_size == 0); + assert(src.size() % frame_size == 0); - const size_t n_frames = src.size / frame_size; + const size_t n_frames = src.size() / frame_size; /* always round up: worst case output buffer size */ const size_t o_frames = size_t(n_frames * ratio) + 1; @@ -281,15 +281,15 @@ SoxrPcmResampler::Resample(ConstBuffer src) auto *output_buffer = (float *)buffer.Get(o_frames * frame_size); size_t i_done, o_done; - soxr_error_t e = soxr_process(soxr, src.data, n_frames, &i_done, + soxr_error_t e = soxr_process(soxr, src.data(), n_frames, &i_done, output_buffer, o_frames, &o_done); if (e != nullptr) throw FormatRuntimeError("soxr error: %s", e); - return { output_buffer, o_done * frame_size }; + return { (const std::byte *)output_buffer, o_done * frame_size }; } -ConstBuffer +std::span SoxrPcmResampler::Flush() { const size_t frame_size = channels * sizeof(float); @@ -307,5 +307,5 @@ SoxrPcmResampler::Flush() /* flush complete */ output_buffer = nullptr; - return { output_buffer, o_done * frame_size }; + return { (const std::byte *)output_buffer, o_done * frame_size }; } diff --git a/src/pcm/SoxrResampler.hxx b/src/pcm/SoxrResampler.hxx index 746d1524a..0f00c0178 100644 --- a/src/pcm/SoxrResampler.hxx +++ b/src/pcm/SoxrResampler.hxx @@ -41,8 +41,8 @@ public: AudioFormat Open(AudioFormat &af, unsigned new_sample_rate) override; void Close() noexcept override; void Reset() noexcept override; - ConstBuffer Resample(ConstBuffer src) override; - ConstBuffer Flush() override; + std::span Resample(std::span src) override; + std::span Flush() override; }; void diff --git a/src/pcm/Volume.cxx b/src/pcm/Volume.cxx index 415ac3ec4..8592573b4 100644 --- a/src/pcm/Volume.cxx +++ b/src/pcm/Volume.cxx @@ -20,7 +20,6 @@ #include "Volume.hxx" #include "Silence.hxx" #include "Traits.hxx" -#include "util/ConstBuffer.hxx" #include "util/RuntimeError.hxx" #include "util/TransformN.hxx" @@ -185,13 +184,13 @@ PcmVolume::Open(SampleFormat _format, bool allow_convert) return format = _format; } -ConstBuffer -PcmVolume::Apply(ConstBuffer src) noexcept +std::span +PcmVolume::Apply(std::span src) noexcept { if (volume == PCM_VOLUME_1 && !convert) return src; - size_t dest_size = src.size; + size_t dest_size = src.size(); if (convert) { assert(format == SampleFormat::S16); @@ -205,7 +204,7 @@ PcmVolume::Apply(ConstBuffer src) noexcept /* optimized special case: 0% volume = memset(0) */ PcmSilence(std::span{(std::byte *)data, dest_size}, format); - return { data, dest_size }; + return { (const std::byte *)data, dest_size }; } switch (format) { @@ -215,42 +214,42 @@ PcmVolume::Apply(ConstBuffer src) noexcept case SampleFormat::S8: pcm_volume_change_8(dither, (int8_t *)data, - (const int8_t *)src.data, - src.size / sizeof(int8_t), + (const int8_t *)src.data(), + src.size() / sizeof(int8_t), volume); break; case SampleFormat::S16: if (convert) PcmVolumeChange16to32((int32_t *)data, - (const int16_t *)src.data, - src.size / sizeof(int16_t), + (const int16_t *)src.data(), + src.size() / sizeof(int16_t), volume); else pcm_volume_change_16(dither, (int16_t *)data, - (const int16_t *)src.data, - src.size / sizeof(int16_t), + (const int16_t *)src.data(), + src.size() / sizeof(int16_t), volume); break; case SampleFormat::S24_P32: pcm_volume_change_24(dither, (int32_t *)data, - (const int32_t *)src.data, - src.size / sizeof(int32_t), + (const int32_t *)src.data(), + src.size() / sizeof(int32_t), volume); break; case SampleFormat::S32: pcm_volume_change_32(dither, (int32_t *)data, - (const int32_t *)src.data, - src.size / sizeof(int32_t), + (const int32_t *)src.data(), + src.size() / sizeof(int32_t), volume); break; case SampleFormat::FLOAT: pcm_volume_change_float((float *)data, - (const float *)src.data, - src.size / sizeof(float), + (const float *)src.data(), + src.size() / sizeof(float), pcm_volume_to_float(volume)); break; @@ -259,5 +258,5 @@ PcmVolume::Apply(ConstBuffer src) noexcept return src; } - return { data, dest_size }; + return { (const std::byte *)data, dest_size }; } diff --git a/src/pcm/Volume.hxx b/src/pcm/Volume.hxx index af1964ec3..c0a36641c 100644 --- a/src/pcm/Volume.hxx +++ b/src/pcm/Volume.hxx @@ -24,12 +24,13 @@ #include "Buffer.hxx" #include "Dither.hxx" +#include +#include + #ifndef NDEBUG #include #endif -template struct ConstBuffer; - /** * Number of fractional bits for a fixed-point volume value. */ @@ -121,7 +122,7 @@ public: * Apply the volume level. */ [[gnu::pure]] - ConstBuffer Apply(ConstBuffer src) noexcept; + std::span Apply(std::span src) noexcept; }; #endif diff --git a/test/RunMixRampAnalyzer.cxx b/test/RunMixRampAnalyzer.cxx index 652ba98f2..ad5dd4604 100644 --- a/test/RunMixRampAnalyzer.cxx +++ b/test/RunMixRampAnalyzer.cxx @@ -20,7 +20,6 @@ #include "ReadFrames.hxx" #include "pcm/MixRampAnalyzer.hxx" #include "io/FileDescriptor.hxx" -#include "util/ConstBuffer.hxx" #include "util/PrintException.hxx" #include diff --git a/test/RunReplayGainAnalyzer.cxx b/test/RunReplayGainAnalyzer.cxx index f179b17ed..b6ae2ea9e 100644 --- a/test/RunReplayGainAnalyzer.cxx +++ b/test/RunReplayGainAnalyzer.cxx @@ -21,7 +21,6 @@ #include "pcm/ReplayGainAnalyzer.hxx" #include "io/FileDescriptor.hxx" #include "system/Error.hxx" -#include "util/ConstBuffer.hxx" #include "util/PrintException.hxx" #include diff --git a/test/run_convert.cxx b/test/run_convert.cxx index b78d159b4..263522cf3 100644 --- a/test/run_convert.cxx +++ b/test/run_convert.cxx @@ -30,7 +30,6 @@ #include "fs/Path.hxx" #include "fs/NarrowPath.hxx" #include "io/FileDescriptor.hxx" -#include "util/ConstBuffer.hxx" #include "util/StaticFifoBuffer.hxx" #include "util/OptionDef.hxx" #include "util/OptionParser.hxx" @@ -132,16 +131,16 @@ RunConvert(PcmConvert &convert, size_t in_frame_size, buffer.Consume(src.size()); - auto output = convert.Convert({src.data(), src.size()}); - out_fd.FullWrite(output.data, output.size); + auto output = convert.Convert(src); + out_fd.FullWrite(output.data(), output.size()); } while (true) { auto output = convert.Flush(); - if (output.IsNull()) + if (output.data() == nullptr) break; - out_fd.FullWrite(output.data, output.size); + out_fd.FullWrite(output.data(), output.size()); } } diff --git a/test/software_volume.cxx b/test/software_volume.cxx index cce0b7289..1fdfca132 100644 --- a/test/software_volume.cxx +++ b/test/software_volume.cxx @@ -26,7 +26,6 @@ #include "pcm/Volume.hxx" #include "pcm/AudioParser.hxx" #include "pcm/AudioFormat.hxx" -#include "util/ConstBuffer.hxx" #include "util/PrintException.hxx" #include @@ -37,7 +36,7 @@ int main(int argc, char **argv) try { - static char buffer[4096]; + static std::byte buffer[4096]; ssize_t nbytes; if (argc > 2) { @@ -58,7 +57,7 @@ try { while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) { auto dest = pv.Apply({buffer, size_t(nbytes)}); - [[maybe_unused]] ssize_t ignored = write(1, dest.data, dest.size); + [[maybe_unused]] ssize_t ignored = write(1, dest.data(), dest.size()); } pv.Close(); diff --git a/test/test_pcm_channels.cxx b/test/test_pcm_channels.cxx index 07835a824..2f5561a1a 100644 --- a/test/test_pcm_channels.cxx +++ b/test/test_pcm_channels.cxx @@ -20,7 +20,6 @@ #include "test_pcm_util.hxx" #include "pcm/PcmChannels.hxx" #include "pcm/Buffer.hxx" -#include "util/ConstBuffer.hxx" #include @@ -33,18 +32,18 @@ TEST(PcmTest, Channels16) /* stereo to mono */ - auto dest = pcm_convert_channels_16(buffer, 1, 2, { src, N * 2 }); - EXPECT_FALSE(dest.IsNull()); - EXPECT_EQ(N, dest.size); + auto dest = pcm_convert_channels_16(buffer, 1, 2, src); + EXPECT_NE(dest.data(), nullptr); + EXPECT_EQ(N, dest.size()); for (unsigned i = 0; i < N; ++i) EXPECT_EQ(int16_t((src[i * 2] + src[i * 2 + 1]) / 2), dest[i]); /* mono to stereo */ - dest = pcm_convert_channels_16(buffer, 2, 1, { src, N * 2 }); - EXPECT_FALSE(dest.IsNull()); - EXPECT_EQ(N * 4, dest.size); + dest = pcm_convert_channels_16(buffer, 2, 1, src); + EXPECT_NE(dest.data(), nullptr); + EXPECT_EQ(N * 4, dest.size()); for (unsigned i = 0; i < N; ++i) { EXPECT_EQ(src[i], dest[i * 2]); EXPECT_EQ(src[i], dest[i * 2 + 1]); @@ -52,9 +51,9 @@ TEST(PcmTest, Channels16) /* stereo to 5.1 */ - dest = pcm_convert_channels_16(buffer, 6, 2, { src, N * 2 }); - EXPECT_FALSE(dest.IsNull()); - EXPECT_EQ(N * 6, dest.size); + dest = pcm_convert_channels_16(buffer, 6, 2, src); + EXPECT_NE(dest.data(), nullptr); + EXPECT_EQ(N * 6, dest.size()); constexpr int16_t silence = 0; for (unsigned i = 0; i < N; ++i) { EXPECT_EQ(src[i * 2], dest[i * 6]); @@ -75,18 +74,18 @@ TEST(PcmTest, Channels32) /* stereo to mono */ - auto dest = pcm_convert_channels_32(buffer, 1, 2, { src, N * 2 }); - EXPECT_FALSE(dest.IsNull()); - EXPECT_EQ(N, dest.size); + auto dest = pcm_convert_channels_32(buffer, 1, 2, src); + EXPECT_NE(dest.data(), nullptr); + EXPECT_EQ(N, dest.size()); for (unsigned i = 0; i < N; ++i) EXPECT_EQ(int32_t(((int64_t)src[i * 2] + (int64_t)src[i * 2 + 1]) / 2), dest[i]); /* mono to stereo */ - dest = pcm_convert_channels_32(buffer, 2, 1, { src, N * 2 }); - EXPECT_FALSE(dest.IsNull()); - EXPECT_EQ(N * 4, dest.size); + dest = pcm_convert_channels_32(buffer, 2, 1, src); + EXPECT_NE(dest.data(), nullptr); + EXPECT_EQ(N * 4, dest.size()); for (unsigned i = 0; i < N; ++i) { EXPECT_EQ(src[i], dest[i * 2]); EXPECT_EQ(src[i], dest[i * 2 + 1]); @@ -94,9 +93,9 @@ TEST(PcmTest, Channels32) /* stereo to 5.1 */ - dest = pcm_convert_channels_32(buffer, 6, 2, { src, N * 2 }); - EXPECT_FALSE(dest.IsNull()); - EXPECT_EQ(N * 6, dest.size); + dest = pcm_convert_channels_32(buffer, 6, 2, src); + EXPECT_NE(dest.data(), nullptr); + EXPECT_EQ(N * 6, dest.size()); constexpr int32_t silence = 0; for (unsigned i = 0; i < N; ++i) { EXPECT_EQ(src[i * 2], dest[i * 6]); diff --git a/test/test_pcm_export.cxx b/test/test_pcm_export.cxx index c25f53dcd..234120a34 100644 --- a/test/test_pcm_export.cxx +++ b/test/test_pcm_export.cxx @@ -21,7 +21,6 @@ #include "pcm/Export.hxx" #include "pcm/Traits.hxx" #include "util/ByteOrder.hxx" -#include "util/ConstBuffer.hxx" #include @@ -46,14 +45,14 @@ TEST(PcmTest, ExportShift8) EXPECT_EQ(e.GetInputBlockSize(), 8u); EXPECT_EQ(e.GetOutputBlockSize(), 8u); - auto dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(expected), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); + auto dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(expected), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected, dest.size()) == 0); const auto silence = e.GetSilence(); constexpr uint8_t expected_silence[8]{}; - EXPECT_EQ(silence.size, sizeof(expected_silence)); - EXPECT_EQ(memcmp(silence.data, expected_silence, + EXPECT_EQ(silence.size(), sizeof(expected_silence)); + EXPECT_EQ(memcmp(silence.data(), expected_silence, sizeof(expected_silence)), 0); } @@ -95,14 +94,14 @@ TEST(PcmTest, ExportPack24) EXPECT_EQ(e.GetInputBlockSize(), 8u); EXPECT_EQ(e.GetOutputBlockSize(), 6u); - auto dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(expected_size, dest.size); - EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); + auto dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(expected_size, dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected, dest.size()) == 0); const auto silence = e.GetSilence(); constexpr uint8_t expected_silence[6]{}; - EXPECT_EQ(silence.size, sizeof(expected_silence)); - EXPECT_EQ(memcmp(silence.data, expected_silence, + EXPECT_EQ(silence.size(), sizeof(expected_silence)); + EXPECT_EQ(memcmp(silence.data(), expected_silence, sizeof(expected_silence)), 0); } @@ -134,9 +133,9 @@ TEST(PcmTest, ExportReverseEndian) EXPECT_EQ(e.GetInputBlockSize(), 2u); EXPECT_EQ(e.GetOutputBlockSize(), 2u); - auto dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(src), dest.size); - EXPECT_TRUE(memcmp(dest.data, src, dest.size) == 0); + auto dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(src), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), src, dest.size()) == 0); e.Open(SampleFormat::S16, 2, params); @@ -145,9 +144,9 @@ TEST(PcmTest, ExportReverseEndian) EXPECT_EQ(e.GetInputBlockSize(), 4u); EXPECT_EQ(e.GetOutputBlockSize(), 4u); - dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(expected2), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected2, dest.size) == 0); + dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(expected2), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected2, dest.size()) == 0); e.Open(SampleFormat::S32, 2, params); @@ -156,14 +155,14 @@ TEST(PcmTest, ExportReverseEndian) EXPECT_EQ(e.GetInputBlockSize(), 8u); EXPECT_EQ(e.GetOutputBlockSize(), 8u); - dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(expected4), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected4, dest.size) == 0); + dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(expected4), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected4, dest.size()) == 0); const auto silence = e.GetSilence(); constexpr uint8_t expected_silence[8]{}; - EXPECT_EQ(silence.size, sizeof(expected_silence)); - EXPECT_EQ(memcmp(silence.data, expected_silence, + EXPECT_EQ(silence.size(), sizeof(expected_silence)); + EXPECT_EQ(memcmp(silence.data(), expected_silence, sizeof(expected_silence)), 0); } @@ -199,33 +198,33 @@ TEST(PcmTest, ExportDsdU16) EXPECT_EQ(e.GetInputBlockSize(), 4u); EXPECT_EQ(e.GetOutputBlockSize(), 4u); - auto dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(expected), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); + auto dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(expected), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected, dest.size()) == 0); /* no output, 2/4 remains */ static constexpr uint8_t src2[] = { 0x11, 0x22 }; - dest = e.Export({src2, sizeof(src2)}); + dest = e.Export(std::as_bytes(std::span{src2})); EXPECT_TRUE(dest.empty()); /* one full frame and 2/4 remains */ static constexpr uint8_t src3[] = { 0x33, 0x44, 0x55, 0x66 }; static constexpr uint16_t expected3[] = { 0x1133, 0x2244 }; - dest = e.Export({src3, sizeof(src3)}); - EXPECT_EQ(sizeof(expected3), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected3, dest.size) == 0); + dest = e.Export(std::as_bytes(std::span{src3})); + EXPECT_EQ(sizeof(expected3), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected3, dest.size()) == 0); /* two full frames and 2/4 remains again */ static constexpr uint8_t src4[] = { 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee }; static constexpr uint16_t expected4[] = { 0x5577, 0x6688, 0x99bb, 0xaacc }; - dest = e.Export({src4, sizeof(src4)}); - EXPECT_EQ(sizeof(expected4), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected4, dest.size) == 0); + dest = e.Export(std::as_bytes(std::span{src4})); + EXPECT_EQ(sizeof(expected4), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected4, dest.size()) == 0); const auto silence = e.GetSilence(); constexpr uint8_t expected_silence[]{0x69, 0x69, 0x69, 0x69}; - EXPECT_EQ(silence.size, sizeof(expected_silence)); - EXPECT_EQ(memcmp(silence.data, expected_silence, + EXPECT_EQ(silence.size(), sizeof(expected_silence)); + EXPECT_EQ(memcmp(silence.data(), expected_silence, sizeof(expected_silence)), 0); } @@ -259,33 +258,33 @@ TEST(PcmTest, ExportDsdU32) EXPECT_EQ(e.GetInputBlockSize(), 8u); EXPECT_EQ(e.GetOutputBlockSize(), 8u); - auto dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(expected), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); + auto dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(expected), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected, dest.size()) == 0); /* no output, 4/8 remains */ static constexpr uint8_t src2[] = { 0x11, 0x22, 0x33, 0x44 }; - dest = e.Export({src2, sizeof(src2)}); + dest = e.Export(std::as_bytes(std::span{src2})); EXPECT_TRUE(dest.empty()); /* one full frame and 4/8 remains */ static constexpr uint8_t src3[] = { 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc }; static constexpr uint32_t expected3[] = { 0x11335577, 0x22446688 }; - dest = e.Export({src3, sizeof(src3)}); - EXPECT_EQ(sizeof(expected3), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected3, dest.size) == 0); + dest = e.Export(std::as_bytes(std::span{src3})); + EXPECT_EQ(sizeof(expected3), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected3, dest.size()) == 0); /* two full frames and 2/4 remains again */ static constexpr uint8_t src4[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; static constexpr uint32_t expected4[] = { 0x99bb0103, 0xaacc0204 }; - dest = e.Export({src4, sizeof(src4)}); - EXPECT_EQ(sizeof(expected4), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected4, dest.size) == 0); + dest = e.Export(std::as_bytes(std::span{src4})); + EXPECT_EQ(sizeof(expected4), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected4, dest.size()) == 0); const auto silence = e.GetSilence(); constexpr uint8_t expected_silence[]{0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69}; - EXPECT_EQ(silence.size, sizeof(expected_silence)); - EXPECT_EQ(memcmp(silence.data, expected_silence, + EXPECT_EQ(silence.size(), sizeof(expected_silence)); + EXPECT_EQ(memcmp(silence.data(), expected_silence, sizeof(expected_silence)), 0); } @@ -317,43 +316,43 @@ TEST(PcmTest, ExportDop) EXPECT_EQ(e.GetInputBlockSize(), 8u); EXPECT_EQ(e.GetOutputBlockSize(), 16u); - auto dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(expected), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); + auto dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(expected), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected, dest.size()) == 0); /* not enough data: 2/8 */ static constexpr uint8_t src2[] = { 0x12, 0x34 }; - dest = e.Export({src2, sizeof(src2)}); - ASSERT_EQ(dest.size, 0u); + dest = e.Export(std::as_bytes(std::span{src2})); + ASSERT_EQ(dest.size(), 0u); /* not enough data: 6/8 */ static constexpr uint8_t src3[] = { 0x56, 0x78, 0x9a, 0xbc }; - dest = e.Export({src3, sizeof(src3)}); - ASSERT_EQ(dest.size, 0u); + dest = e.Export(std::as_bytes(std::span{src3})); + ASSERT_EQ(dest.size(), 0u); /* just enough data: 8/8 */ static constexpr uint8_t src4[] = { 0xde, 0xf0 }; static constexpr uint32_t expected4[] = { 0xff051256, 0xff053478, 0xfffa9ade, 0xfffabcf0 }; - dest = e.Export({src4, sizeof(src4)}); - ASSERT_EQ(sizeof(expected4), dest.size); - ASSERT_TRUE(memcmp(dest.data, expected4, dest.size) == 0); + dest = e.Export(std::as_bytes(std::span{src4})); + ASSERT_EQ(sizeof(expected4), dest.size()); + ASSERT_TRUE(memcmp(dest.data(), expected4, dest.size()) == 0); /* not enough data: 6/8 */ static constexpr uint8_t src5[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; - dest = e.Export({src5, sizeof(src5)}); + dest = e.Export(std::as_bytes(std::span{src5})); ASSERT_TRUE(dest.empty()); /* two quads returned, not enough data for more: 2/8 */ static constexpr uint8_t src6[] = { 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x10, 0x20 }; static constexpr uint32_t expected6[] = { 0xff051133, 0xff052244, 0xfffa5577, 0xfffa6688, 0xff0599bb, 0xff05aacc, 0xfffaddff, 0xfffaee00 }; - dest = e.Export({src6, sizeof(src6)}); - ASSERT_EQ(sizeof(expected6), dest.size); - ASSERT_TRUE(memcmp(dest.data, expected6, dest.size) == 0); + dest = e.Export(std::as_bytes(std::span{src6})); + ASSERT_EQ(sizeof(expected6), dest.size()); + ASSERT_TRUE(memcmp(dest.data(), expected6, dest.size()) == 0); const auto silence = e.GetSilence(); constexpr uint32_t expected_silence[]{0xff056969, 0xff056969, 0xfffa6969, 0xfffa6969}; - EXPECT_EQ(silence.size, sizeof(expected_silence)); - EXPECT_EQ(memcmp(silence.data, expected_silence, + EXPECT_EQ(silence.size(), sizeof(expected_silence)); + EXPECT_EQ(memcmp(silence.data(), expected_silence, sizeof(expected_silence)), 0); } @@ -384,9 +383,9 @@ TestAlsaChannelOrder51() PcmExport e; e.Open(F, 6, params); - auto dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(expected), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); + auto dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(expected), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected, dest.size()) == 0); } template> @@ -414,14 +413,14 @@ TestAlsaChannelOrder71() PcmExport e; e.Open(F, 8, params); - auto dest = e.Export({src, sizeof(src)}); - EXPECT_EQ(sizeof(expected), dest.size); - EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); + auto dest = e.Export(std::as_bytes(std::span{src})); + EXPECT_EQ(sizeof(expected), dest.size()); + EXPECT_TRUE(memcmp(dest.data(), expected, dest.size()) == 0); const auto silence = e.GetSilence(); constexpr value_type expected_silence[8]{}; - EXPECT_EQ(silence.size, sizeof(expected_silence)); - EXPECT_EQ(memcmp(silence.data, expected_silence, + EXPECT_EQ(silence.size(), sizeof(expected_silence)); + EXPECT_EQ(memcmp(silence.data(), expected_silence, sizeof(expected_silence)), 0); } diff --git a/test/test_pcm_format.cxx b/test/test_pcm_format.cxx index 4ecd19270..aedeec23d 100644 --- a/test/test_pcm_format.cxx +++ b/test/test_pcm_format.cxx @@ -34,7 +34,7 @@ TEST(PcmTest, Format8To16) PcmDither dither; auto d = pcm_convert_to_16(buffer, dither, SampleFormat::S8, src); - EXPECT_EQ(N, d.size); + EXPECT_EQ(N, d.size()); for (size_t i = 0; i < N; ++i) EXPECT_EQ(int(src[i]), d[i] >> 8); @@ -48,7 +48,7 @@ TEST(PcmTest, Format16To24) PcmBuffer buffer; auto d = pcm_convert_to_24(buffer, SampleFormat::S16, src); - EXPECT_EQ(N, d.size); + EXPECT_EQ(N, d.size()); for (size_t i = 0; i < N; ++i) EXPECT_EQ(int(src[i]), d[i] >> 8); @@ -62,7 +62,7 @@ TEST(PcmTest, Format16To32) PcmBuffer buffer; auto d = pcm_convert_to_32(buffer, SampleFormat::S16, src); - EXPECT_EQ(N, d.size); + EXPECT_EQ(N, d.size()); for (size_t i = 0; i < N; ++i) EXPECT_EQ(int(src[i]), d[i] >> 16); @@ -76,9 +76,9 @@ TEST(PcmTest, FormatFloat16) PcmBuffer buffer1, buffer2; auto f = pcm_convert_to_float(buffer1, SampleFormat::S16, src); - EXPECT_EQ(N, f.size); + EXPECT_EQ(N, f.size()); - for (size_t i = 0; i != f.size; ++i) { + for (size_t i = 0; i != f.size(); ++i) { EXPECT_GE(f[i], -1.f); EXPECT_LE(f[i], 1.f); } @@ -87,14 +87,14 @@ TEST(PcmTest, FormatFloat16) auto d = pcm_convert_to_16(buffer2, dither, SampleFormat::FLOAT, - f.ToVoid()); - EXPECT_EQ(N, d.size); + std::as_bytes(f)); + EXPECT_EQ(N, d.size()); for (size_t i = 0; i < N; ++i) EXPECT_EQ(src[i], d[i]); /* check if clamping works */ - auto *writable = const_cast(f.data); + auto *writable = const_cast(f.data()); *writable++ = 1.01; *writable++ = 10; *writable++ = -1.01; @@ -102,8 +102,8 @@ TEST(PcmTest, FormatFloat16) d = pcm_convert_to_16(buffer2, dither, SampleFormat::FLOAT, - f.ToVoid()); - EXPECT_EQ(N, d.size); + std::as_bytes(f)); + EXPECT_EQ(N, d.size()); EXPECT_EQ(32767, int(d[0])); EXPECT_EQ(32767, int(d[1])); @@ -122,17 +122,17 @@ TEST(PcmTest, FormatFloat32) PcmBuffer buffer1, buffer2; auto f = pcm_convert_to_float(buffer1, SampleFormat::S32, src); - EXPECT_EQ(N, f.size); + EXPECT_EQ(N, f.size()); - for (size_t i = 0; i != f.size; ++i) { + for (size_t i = 0; i != f.size(); ++i) { EXPECT_GE(f[i], -1.f); EXPECT_LE(f[i], 1.f); } auto d = pcm_convert_to_32(buffer2, SampleFormat::FLOAT, - f.ToVoid()); - EXPECT_EQ(N, d.size); + std::as_bytes(f)); + EXPECT_EQ(N, d.size()); constexpr int error = 64; @@ -140,7 +140,7 @@ TEST(PcmTest, FormatFloat32) EXPECT_NEAR(src[i], d[i], error); /* check if clamping works */ - auto *writable = const_cast(f.data); + auto *writable = const_cast(f.data()); *writable++ = 1.01; *writable++ = 10; *writable++ = -1.01; @@ -148,8 +148,8 @@ TEST(PcmTest, FormatFloat32) d = pcm_convert_to_32(buffer2, SampleFormat::FLOAT, - f.ToVoid()); - EXPECT_EQ(N, d.size); + std::as_bytes(f)); + EXPECT_EQ(N, d.size()); EXPECT_EQ(2147483647, int(d[0])); EXPECT_EQ(2147483647, int(d[1])); diff --git a/test/test_pcm_interleave.cxx b/test/test_pcm_interleave.cxx index 038bda717..5b424bc18 100644 --- a/test/test_pcm_interleave.cxx +++ b/test/test_pcm_interleave.cxx @@ -35,8 +35,10 @@ TestInterleaveN() static constexpr size_t n_frames = std::size(src1); static constexpr unsigned channels = std::size(src_all); - static const ConstBuffer src((const void *const*)src_all, - channels); + static const std::span src{ + (const void *const*)src_all, + channels, + }; static constexpr T poison = T(0xdeadbeef); T dest[n_frames * channels + 1]; @@ -77,8 +79,10 @@ TEST(PcmTest, Interleave24) static constexpr size_t n_frames = std::size(src1) / 3; static constexpr unsigned channels = std::size(src_all); - static const ConstBuffer src((const void *const*)src_all, - channels); + static const std::span src{ + (const void *const*)src_all, + channels, + }; static constexpr T poison = 0xff; T dest[n_frames * channels * 3 + 1]; diff --git a/test/test_pcm_util.hxx b/test/test_pcm_util.hxx index 8d50618c0..4bce71b8b 100644 --- a/test/test_pcm_util.hxx +++ b/test/test_pcm_util.hxx @@ -17,12 +17,11 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "util/ConstBuffer.hxx" - #include #include #include +#include #include #include @@ -81,12 +80,12 @@ public: return begin(); } - operator ConstBuffer() const { + operator std::span() const { return { begin(), size() }; } - operator ConstBuffer() const { - return { begin(), size() * sizeof(T) }; + operator std::span() const { + return { (const std::byte *)begin(), size() * sizeof(T) }; } }; diff --git a/test/test_pcm_volume.cxx b/test/test_pcm_volume.cxx index 11ea75303..59388b144 100644 --- a/test/test_pcm_volume.cxx +++ b/test/test_pcm_volume.cxx @@ -19,7 +19,7 @@ #include "pcm/Volume.hxx" #include "pcm/Traits.hxx" -#include "util/ConstBuffer.hxx" +#include "util/SpanCast.hxx" #include "test_pcm_util.hxx" #include @@ -41,23 +41,23 @@ TestVolume(G g=G()) constexpr size_t N = 509; static value_type zero[N]; const auto _src = TestDataBuffer(g); - const ConstBuffer src(_src, sizeof(_src)); + const std::span src = _src; pv.SetVolume(0); auto dest = pv.Apply(src); - EXPECT_EQ(src.size, dest.size); - EXPECT_EQ(0, memcmp(dest.data, zero, sizeof(zero))); + EXPECT_EQ(src.size(), dest.size()); + EXPECT_EQ(0, memcmp(dest.data(), zero, sizeof(zero))); pv.SetVolume(PCM_VOLUME_1); dest = pv.Apply(src); - EXPECT_EQ(src.size, dest.size); - EXPECT_EQ(0, memcmp(dest.data, src.data, src.size)); + EXPECT_EQ(src.size(), dest.size()); + EXPECT_EQ(0, memcmp(dest.data(), src.data(), src.size())); pv.SetVolume(PCM_VOLUME_1 / 2); dest = pv.Apply(src); - EXPECT_EQ(src.size, dest.size); + EXPECT_EQ(src.size(), dest.size()); - const auto _dest = ConstBuffer::FromVoid(dest); + const auto _dest = FromBytesStrict(dest); for (unsigned i = 0; i < N; ++i) { const auto expected = (_src[i] + 1) / 2; EXPECT_GE(_dest[i], expected - 4); @@ -89,27 +89,27 @@ TEST(PcmTest, Volume16to32) constexpr size_t N = 509; static value_type zero[N]; const auto _src = TestDataBuffer(g); - const ConstBuffer src(_src, sizeof(_src)); + const std::span src = _src; pv.SetVolume(0); auto dest = pv.Apply(src); - EXPECT_EQ(src.size * 2, dest.size); - EXPECT_EQ(0, memcmp(dest.data, zero, sizeof(zero))); + EXPECT_EQ(src.size() * 2, dest.size()); + EXPECT_EQ(0, memcmp(dest.data(), zero, sizeof(zero))); pv.SetVolume(PCM_VOLUME_1); dest = pv.Apply(src); - EXPECT_EQ(src.size * 2, dest.size); - auto s = ConstBuffer::FromVoid(src); - auto d = ConstBuffer::FromVoid(dest); + EXPECT_EQ(src.size() * 2, dest.size()); + auto s = FromBytesStrict(src); + auto d = FromBytesStrict(dest); for (size_t i = 0; i < N; ++i) EXPECT_EQ(d[i], s[i] << 8); pv.SetVolume(PCM_VOLUME_1 / 2); dest = pv.Apply(src); - EXPECT_EQ(src.size * 2, dest.size); + EXPECT_EQ(src.size() * 2, dest.size()); - s = ConstBuffer::FromVoid(src); - d = ConstBuffer::FromVoid(dest); + s = FromBytesStrict(src); + d = FromBytesStrict(dest); for (unsigned i = 0; i < N; ++i) { const int32_t expected = (s[i] << 8) / 2; EXPECT_EQ(d[i], expected); @@ -136,23 +136,23 @@ TEST(PcmTest, VolumeFloat) constexpr size_t N = 509; static float zero[N]; const auto _src = TestDataBuffer(RandomFloat()); - const ConstBuffer src(_src, sizeof(_src)); + const std::span src = _src; pv.SetVolume(0); auto dest = pv.Apply(src); - EXPECT_EQ(src.size, dest.size); - EXPECT_EQ(0, memcmp(dest.data, zero, sizeof(zero))); + EXPECT_EQ(src.size(), dest.size()); + EXPECT_EQ(0, memcmp(dest.data(), zero, sizeof(zero))); pv.SetVolume(PCM_VOLUME_1); dest = pv.Apply(src); - EXPECT_EQ(src.size, dest.size); - EXPECT_EQ(0, memcmp(dest.data, src.data, src.size)); + EXPECT_EQ(src.size(), dest.size()); + EXPECT_EQ(0, memcmp(dest.data(), src.data(), src.size())); pv.SetVolume(PCM_VOLUME_1 / 2); dest = pv.Apply(src); - EXPECT_EQ(src.size, dest.size); + EXPECT_EQ(src.size(), dest.size()); - const auto _dest = ConstBuffer::FromVoid(dest); + const auto _dest = FromBytesStrict(dest); for (unsigned i = 0; i < N; ++i) EXPECT_NEAR((double)_src[i] / 2., (double)_dest[i], 1.);