From b6fb8ee9d87fc82ead9e7cf646f91f3d2f97143a Mon Sep 17 00:00:00 2001 From: emeric Date: Sun, 18 Jan 2026 18:45:56 +0100 Subject: [PATCH] Extracted PCM struct to ease reuse --- src/libs/audio/impl/ffmpeg/PcmDecoder.cpp | 16 ++++---- src/libs/audio/impl/ffmpeg/PcmDecoder.hpp | 4 +- src/libs/audio/include/audio/IPcmDecoder.hpp | 22 ++-------- src/libs/audio/include/audio/PcmTypes.hpp | 42 ++++++++++++++++++++ 4 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 src/libs/audio/include/audio/PcmTypes.hpp diff --git a/src/libs/audio/impl/ffmpeg/PcmDecoder.cpp b/src/libs/audio/impl/ffmpeg/PcmDecoder.cpp index 5ba8d33b..0fa8e312 100644 --- a/src/libs/audio/impl/ffmpeg/PcmDecoder.cpp +++ b/src/libs/audio/impl/ffmpeg/PcmDecoder.cpp @@ -41,7 +41,7 @@ extern "C" namespace lms::audio { - std::unique_ptr createPcmDecoder(const std::filesystem::path& filePath, const PcmOutputParameters& parameters) + std::unique_ptr createPcmDecoder(const std::filesystem::path& filePath, const PcmParameters& parameters) { return std::make_unique(filePath, parameters); } @@ -51,25 +51,25 @@ namespace lms::audio::ffmpeg { namespace { - ::AVSampleFormat toAvSampleFormat(PcmDecodeSampleType type, bool planar) + ::AVSampleFormat toAvSampleFormat(PcmSampleType type, bool planar) { switch (type) { - case PcmDecodeSampleType::Signed16: + case PcmSampleType::Signed16: return planar ? AV_SAMPLE_FMT_S16P : AV_SAMPLE_FMT_S16; - case PcmDecodeSampleType::Signed32: + case PcmSampleType::Signed32: return planar ? AV_SAMPLE_FMT_S32P : AV_SAMPLE_FMT_S32; - case PcmDecodeSampleType::Float32: + case PcmSampleType::Float32: return planar ? AV_SAMPLE_FMT_FLTP : AV_SAMPLE_FMT_FLT; - case PcmDecodeSampleType::Float64: + case PcmSampleType::Float64: return planar ? AV_SAMPLE_FMT_DBLP : AV_SAMPLE_FMT_DBL; } - throw Exception("Unsupported PcmDecodeSampleType"); + throw Exception("Unsupported PcmSampleType"); } } // namespace - PcmDecoder::PcmDecoder(const std::filesystem::path& filePath, const PcmOutputParameters& parameters) + PcmDecoder::PcmDecoder(const std::filesystem::path& filePath, const PcmParameters& parameters) : _parameters{ parameters } { if (_parameters.channelCount > AV_NUM_DATA_POINTERS) diff --git a/src/libs/audio/impl/ffmpeg/PcmDecoder.hpp b/src/libs/audio/impl/ffmpeg/PcmDecoder.hpp index 9ae3f4e4..2f90bae4 100644 --- a/src/libs/audio/impl/ffmpeg/PcmDecoder.hpp +++ b/src/libs/audio/impl/ffmpeg/PcmDecoder.hpp @@ -28,7 +28,7 @@ namespace lms::audio::ffmpeg class PcmDecoder : public IPcmDecoder { public: - PcmDecoder(const std::filesystem::path& filePath, const PcmOutputParameters& parameters); + PcmDecoder(const std::filesystem::path& filePath, const PcmParameters& parameters); ~PcmDecoder() override; PcmDecoder(const PcmDecoder&) = delete; @@ -43,7 +43,7 @@ namespace lms::audio::ffmpeg std::size_t drainResampler(std::span outputChannelBuffers, std::size_t maxSamplesPerChannel); std::size_t getEstimatedResamplerAvailableSamples() const; - const PcmOutputParameters _parameters; + const PcmParameters _parameters; bool _finished{}; bool _eof{}; diff --git a/src/libs/audio/include/audio/IPcmDecoder.hpp b/src/libs/audio/include/audio/IPcmDecoder.hpp index 7c285697..022ce23b 100644 --- a/src/libs/audio/include/audio/IPcmDecoder.hpp +++ b/src/libs/audio/include/audio/IPcmDecoder.hpp @@ -19,31 +19,15 @@ #pragma once -#include #include #include #include #include +#include "audio/PcmTypes.hpp" + namespace lms::audio { - enum class PcmDecodeSampleType - { - Signed16, - Signed32, - Float32, - Float64, - }; - - struct PcmOutputParameters - { - unsigned channelCount; - unsigned sampleRate; - PcmDecodeSampleType sampleType; - std::endian byteOrder; - bool planar; - }; - class IPcmDecoder { public: @@ -61,5 +45,5 @@ namespace lms::audio }; // Throw on error - std::unique_ptr createPcmDecoder(const std::filesystem::path& filePath, const PcmOutputParameters& parameters); + std::unique_ptr createPcmDecoder(const std::filesystem::path& filePath, const PcmParameters& parameters); } // namespace lms::audio \ No newline at end of file diff --git a/src/libs/audio/include/audio/PcmTypes.hpp b/src/libs/audio/include/audio/PcmTypes.hpp new file mode 100644 index 00000000..3d60c6d6 --- /dev/null +++ b/src/libs/audio/include/audio/PcmTypes.hpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2026 Emeric Poupon + * + * This file is part of LMS. + * + * LMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LMS. If not, see . + */ + +#pragma once + +#include + +namespace lms::audio +{ + enum class PcmSampleType + { + Signed16, + Signed32, + Float32, + Float64, + }; + + struct PcmParameters + { + unsigned channelCount; + unsigned sampleRate; + PcmSampleType sampleType; + std::endian byteOrder; + bool planar; + }; +} // namespace lms::audio \ No newline at end of file