pcm/Dsd{16,32}: convert public function to stateful class

This commit is contained in:
Max Kellermann 2019-06-17 23:31:52 +02:00
parent d5d5705213
commit 0cc94fe30c
6 changed files with 65 additions and 28 deletions

View File

@ -18,7 +18,6 @@
*/ */
#include "Dsd16.hxx" #include "Dsd16.hxx"
#include "Buffer.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
/** /**
@ -51,9 +50,14 @@ Dsd8To16(uint16_t *dest, const uint8_t *src,
} }
} }
void
Dsd16Converter::Open(unsigned _channels) noexcept
{
channels = _channels;
}
ConstBuffer<uint16_t> ConstBuffer<uint16_t>
Dsd8To16(PcmBuffer &buffer, unsigned channels, Dsd16Converter::Convert(ConstBuffer<uint8_t> _src) noexcept
ConstBuffer<uint8_t> _src) noexcept
{ {
const size_t in_frames = _src.size / channels; const size_t in_frames = _src.size / channels;
const size_t out_frames = in_frames / 2; const size_t out_frames = in_frames / 2;

View File

@ -20,16 +20,27 @@
#ifndef MPD_PCM_DSD_16_HXX #ifndef MPD_PCM_DSD_16_HXX
#define MPD_PCM_DSD_16_HXX #define MPD_PCM_DSD_16_HXX
#include "Buffer.hxx"
#include <stdint.h> #include <stdint.h>
template<typename T> struct ConstBuffer; template<typename T> struct ConstBuffer;
class PcmBuffer;
/** /**
* Convert DSD_U8 to DSD_U16 (native endian, oldest bits in MSB). * Convert DSD_U8 to DSD_U16 (native endian, oldest bits in MSB).
*/ */
ConstBuffer<uint16_t> class Dsd16Converter {
Dsd8To16(PcmBuffer &buffer, unsigned channels, unsigned channels;
ConstBuffer<uint8_t> src) noexcept;
PcmBuffer buffer;
public:
void Open(unsigned _channels) noexcept;
void Reset() noexcept {
}
ConstBuffer<uint16_t> Convert(ConstBuffer<uint8_t> src) noexcept;
};
#endif #endif

View File

@ -53,9 +53,14 @@ Dsd8To32(uint32_t *dest, const uint8_t *src,
} }
} }
void
Dsd32Converter::Open(unsigned _channels) noexcept
{
channels = _channels;
}
ConstBuffer<uint32_t> ConstBuffer<uint32_t>
Dsd8To32(PcmBuffer &buffer, unsigned channels, Dsd32Converter::Convert(ConstBuffer<uint8_t> _src) noexcept
ConstBuffer<uint8_t> _src) noexcept
{ {
const size_t in_frames = _src.size / channels; const size_t in_frames = _src.size / channels;
const size_t out_frames = in_frames / 4; const size_t out_frames = in_frames / 4;

View File

@ -20,16 +20,27 @@
#ifndef MPD_PCM_DSD_32_HXX #ifndef MPD_PCM_DSD_32_HXX
#define MPD_PCM_DSD_32_HXX #define MPD_PCM_DSD_32_HXX
#include "Buffer.hxx"
#include <stdint.h> #include <stdint.h>
template<typename T> struct ConstBuffer; template<typename T> struct ConstBuffer;
class PcmBuffer;
/** /**
* Convert DSD_U8 to DSD_U32 (native endian, oldest bits in MSB). * Convert DSD_U8 to DSD_U32 (native endian, oldest bits in MSB).
*/ */
ConstBuffer<uint32_t> class Dsd32Converter {
Dsd8To32(PcmBuffer &buffer, unsigned channels, unsigned channels;
ConstBuffer<uint8_t> src) noexcept;
PcmBuffer buffer;
public:
void Open(unsigned _channels) noexcept;
void Reset() noexcept {
}
ConstBuffer<uint32_t> Convert(ConstBuffer<uint8_t> src) noexcept;
};
#endif #endif

View File

@ -24,13 +24,6 @@
#include "util/ByteReverse.hxx" #include "util/ByteReverse.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#ifdef ENABLE_DSD
#include "Dsd16.hxx"
#include "Dsd32.hxx"
#include "PcmDsd.hxx"
#include "Dop.hxx"
#endif
#include <assert.h> #include <assert.h>
void void
@ -57,12 +50,16 @@ PcmExport::Open(SampleFormat sample_format, unsigned _channels,
break; break;
case DsdMode::U16: case DsdMode::U16:
dsd16_converter.Open(_channels);
/* after the conversion to DSD_U16, the DSD samples /* after the conversion to DSD_U16, the DSD samples
are stuffed inside fake 16 bit samples */ are stuffed inside fake 16 bit samples */
sample_format = SampleFormat::S16; sample_format = SampleFormat::S16;
break; break;
case DsdMode::U32: case DsdMode::U32:
dsd32_converter.Open(_channels);
/* after the conversion to DSD_U32, the DSD samples /* after the conversion to DSD_U32, the DSD samples
are stuffed inside fake 32 bit samples */ are stuffed inside fake 32 bit samples */
sample_format = SampleFormat::S32; sample_format = SampleFormat::S32;
@ -101,8 +98,14 @@ PcmExport::Reset() noexcept
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
switch (dsd_mode) { switch (dsd_mode) {
case DsdMode::NONE: case DsdMode::NONE:
break;
case DsdMode::U16: case DsdMode::U16:
dsd16_converter.Reset();
break;
case DsdMode::U32: case DsdMode::U32:
dsd32_converter.Reset();
break; break;
case DsdMode::DOP: case DsdMode::DOP:
@ -211,14 +214,12 @@ PcmExport::Export(ConstBuffer<void> data) noexcept
break; break;
case DsdMode::U16: case DsdMode::U16:
data = Dsd8To16(dsd_buffer, channels, data = dsd16_converter.Convert(ConstBuffer<uint8_t>::FromVoid(data))
ConstBuffer<uint8_t>::FromVoid(data))
.ToVoid(); .ToVoid();
break; break;
case DsdMode::U32: case DsdMode::U32:
data = Dsd8To32(dsd_buffer, channels, data = dsd32_converter.Convert(ConstBuffer<uint8_t>::FromVoid(data))
ConstBuffer<uint8_t>::FromVoid(data))
.ToVoid(); .ToVoid();
break; break;

View File

@ -25,6 +25,8 @@
#include "config.h" #include "config.h"
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
#include "Dsd16.hxx"
#include "Dsd32.hxx"
#include "Dop.hxx" #include "Dop.hxx"
#endif #endif
@ -48,11 +50,14 @@ class PcmExport {
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
/** /**
* The buffer is used to convert DSD samples to DSD_U16 or DSD_U32. * @see DsdMode::U16
*
* @see DsdMode::U16, DsdMode::U32
*/ */
PcmBuffer dsd_buffer; Dsd16Converter dsd16_converter;
/**
* @see DsdMode::U32
*/
Dsd32Converter dsd32_converter;
/** /**
* @see DsdMode::DOP * @see DsdMode::DOP