Extracted PCM struct to ease reuse

This commit is contained in:
emeric
2026-02-17 23:15:44 +01:00
parent cea3cad6e1
commit b6fb8ee9d8
4 changed files with 55 additions and 29 deletions
+8 -8
View File
@@ -41,7 +41,7 @@ extern "C"
namespace lms::audio
{
std::unique_ptr<IPcmDecoder> createPcmDecoder(const std::filesystem::path& filePath, const PcmOutputParameters& parameters)
std::unique_ptr<IPcmDecoder> createPcmDecoder(const std::filesystem::path& filePath, const PcmParameters& parameters)
{
return std::make_unique<ffmpeg::PcmDecoder>(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)
+2 -2
View File
@@ -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<WritableBuffer> outputChannelBuffers, std::size_t maxSamplesPerChannel);
std::size_t getEstimatedResamplerAvailableSamples() const;
const PcmOutputParameters _parameters;
const PcmParameters _parameters;
bool _finished{};
bool _eof{};
+3 -19
View File
@@ -19,31 +19,15 @@
#pragma once
#include <bit>
#include <cstddef>
#include <filesystem>
#include <memory>
#include <span>
#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<IPcmDecoder> createPcmDecoder(const std::filesystem::path& filePath, const PcmOutputParameters& parameters);
std::unique_ptr<IPcmDecoder> createPcmDecoder(const std::filesystem::path& filePath, const PcmParameters& parameters);
} // namespace lms::audio
+42
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <bit>
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