Commit c75339ed authored by Max Kellermann's avatar Max Kellermann

pcm/Format: change parameters/return values to ConstBuffer

parent b0b7244b
......@@ -50,9 +50,6 @@ PcmFormatConverter::Close()
ConstBuffer<void>
PcmFormatConverter::Convert(ConstBuffer<void> src, Error &error)
{
const void *result = nullptr;
size_t result_size = 0;
switch (dest_format) {
case SampleFormat::UNDEFINED:
assert(false);
......@@ -60,45 +57,33 @@ PcmFormatConverter::Convert(ConstBuffer<void> src, Error &error)
case SampleFormat::S8:
case SampleFormat::DSD:
result = nullptr;
break;
error.Format(pcm_domain,
"PCM conversion from %s to %s is not implemented",
sample_format_to_string(src_format),
sample_format_to_string(dest_format));
return nullptr;
case SampleFormat::S16:
result = pcm_convert_to_16(buffer, dither,
src_format,
src.data, src.size,
&result_size);
break;
return pcm_convert_to_16(buffer, dither,
src_format,
src).ToVoid();
case SampleFormat::S24_P32:
result = pcm_convert_to_24(buffer,
src_format,
src.data, src.size,
&result_size);
break;
return pcm_convert_to_24(buffer,
src_format,
src).ToVoid();
case SampleFormat::S32:
result = pcm_convert_to_32(buffer,
src_format,
src.data, src.size,
&result_size);
break;
return pcm_convert_to_32(buffer,
src_format,
src).ToVoid();
case SampleFormat::FLOAT:
result = pcm_convert_to_float(buffer,
src_format,
src.data, src.size,
&result_size);
break;
}
if (result == nullptr) {
error.Format(pcm_domain,
"PCM conversion from %s to %s is not implemented",
sample_format_to_string(src_format),
sample_format_to_string(dest_format));
return nullptr;
return pcm_convert_to_float(buffer,
src_format,
src).ToVoid();
}
return { result, result_size };
assert(false);
gcc_unreachable();
}
......@@ -25,6 +25,7 @@
#include <stdint.h>
#include <stddef.h>
template<typename T> struct ConstBuffer;
class PcmBuffer;
class PcmDither;
......@@ -36,14 +37,12 @@ class PcmDither;
* @param dither a pcm_dither object for 24-to-16 conversion
* @param bits the number of in the source buffer
* @param src the source PCM buffer
* @param src_size the size of #src in bytes
* @param dest_size_r returns the number of bytes of the destination buffer
* @return the destination buffer
*/
const int16_t *
gcc_pure
ConstBuffer<int16_t>
pcm_convert_to_16(PcmBuffer &buffer, PcmDither &dither,
SampleFormat src_format, const void *src,
size_t src_size, size_t *dest_size_r);
SampleFormat src_format, ConstBuffer<void> src);
/**
* Converts PCM samples to 24 bit (32 bit alignment).
......@@ -51,14 +50,12 @@ pcm_convert_to_16(PcmBuffer &buffer, PcmDither &dither,
* @param buffer a PcmBuffer object
* @param bits the number of in the source buffer
* @param src the source PCM buffer
* @param src_size the size of #src in bytes
* @param dest_size_r returns the number of bytes of the destination buffer
* @return the destination buffer
*/
const int32_t *
gcc_pure
ConstBuffer<int32_t>
pcm_convert_to_24(PcmBuffer &buffer,
SampleFormat src_format, const void *src,
size_t src_size, size_t *dest_size_r);
SampleFormat src_format, ConstBuffer<void> src);
/**
* Converts PCM samples to 32 bit.
......@@ -66,14 +63,12 @@ pcm_convert_to_24(PcmBuffer &buffer,
* @param buffer a PcmBuffer object
* @param bits the number of in the source buffer
* @param src the source PCM buffer
* @param src_size the size of #src in bytes
* @param dest_size_r returns the number of bytes of the destination buffer
* @return the destination buffer
*/
const int32_t *
gcc_pure
ConstBuffer<int32_t>
pcm_convert_to_32(PcmBuffer &buffer,
SampleFormat src_format, const void *src,
size_t src_size, size_t *dest_size_r);
SampleFormat src_format, ConstBuffer<void> src);
/**
* Converts PCM samples to 32 bit floating point.
......@@ -85,9 +80,9 @@ pcm_convert_to_32(PcmBuffer &buffer,
* @param dest_size_r returns the number of bytes of the destination buffer
* @return the destination buffer
*/
const float *
gcc_pure
ConstBuffer<float>
pcm_convert_to_float(PcmBuffer &buffer,
SampleFormat src_format, const void *src,
size_t src_size, size_t *dest_size_r);
SampleFormat src_format, ConstBuffer<void> src);
#endif
......@@ -29,86 +29,72 @@
void
PcmFormatTest::TestFormat8to16()
{
constexpr unsigned N = 256;
constexpr size_t N = 256;
const auto src = TestDataBuffer<int8_t, N>();
PcmBuffer buffer;
size_t d_size;
PcmDither dither;
auto d = pcm_convert_to_16(buffer, dither, SampleFormat::S8,
src, sizeof(src), &d_size);
auto d_end = pcm_end_pointer(d, d_size);
CPPUNIT_ASSERT_EQUAL(N, unsigned(d_end - d));
auto d = pcm_convert_to_16(buffer, dither, SampleFormat::S8, src);
CPPUNIT_ASSERT_EQUAL(N, d.size);
for (size_t i = 0; i < N; ++i)
CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 8);
CPPUNIT_ASSERT_EQUAL(int(src[i]), d.data[i] >> 8);
}
void
PcmFormatTest::TestFormat16to24()
{
constexpr unsigned N = 256;
constexpr size_t N = 256;
const auto src = TestDataBuffer<int16_t, N>();
PcmBuffer buffer;
size_t d_size;
auto d = pcm_convert_to_24(buffer, SampleFormat::S16,
src, sizeof(src), &d_size);
auto d_end = pcm_end_pointer(d, d_size);
CPPUNIT_ASSERT_EQUAL(N, unsigned(d_end - d));
auto d = pcm_convert_to_24(buffer, SampleFormat::S16, src);
CPPUNIT_ASSERT_EQUAL(N, d.size);
for (size_t i = 0; i < N; ++i)
CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 8);
CPPUNIT_ASSERT_EQUAL(int(src[i]), d.data[i] >> 8);
}
void
PcmFormatTest::TestFormat16to32()
{
constexpr unsigned N = 256;
constexpr size_t N = 256;
const auto src = TestDataBuffer<int16_t, N>();
PcmBuffer buffer;
size_t d_size;
auto d = pcm_convert_to_32(buffer, SampleFormat::S16,
src, sizeof(src), &d_size);
auto d_end = pcm_end_pointer(d, d_size);
CPPUNIT_ASSERT_EQUAL(N, unsigned(d_end - d));
auto d = pcm_convert_to_32(buffer, SampleFormat::S16, src);
CPPUNIT_ASSERT_EQUAL(N, d.size);
for (size_t i = 0; i < N; ++i)
CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 16);
CPPUNIT_ASSERT_EQUAL(int(src[i]), d.data[i] >> 16);
}
void
PcmFormatTest::TestFormatFloat()
{
constexpr unsigned N = 256;
constexpr size_t N = 256;
const auto src = TestDataBuffer<int16_t, N>();
PcmBuffer buffer1, buffer2;
size_t f_size;
auto f = pcm_convert_to_float(buffer1, SampleFormat::S16,
src, sizeof(src), &f_size);
auto f_end = pcm_end_pointer(f, f_size);
CPPUNIT_ASSERT_EQUAL(N, unsigned(f_end - f));
auto f = pcm_convert_to_float(buffer1, SampleFormat::S16, src);
CPPUNIT_ASSERT_EQUAL(N, f.size);
for (auto i = f; i != f_end; ++i) {
CPPUNIT_ASSERT(*i >= -1.);
CPPUNIT_ASSERT(*i <= 1.);
for (size_t i = 0; i != f.size; ++i) {
CPPUNIT_ASSERT(f.data[i] >= -1.);
CPPUNIT_ASSERT(f.data[i] <= 1.);
}
PcmDither dither;
size_t d_size;
auto d = pcm_convert_to_16(buffer2, dither,
SampleFormat::FLOAT,
f, f_size, &d_size);
auto d_end = pcm_end_pointer(d, d_size);
CPPUNIT_ASSERT_EQUAL(N, unsigned(d_end - d));
f.ToVoid());
CPPUNIT_ASSERT_EQUAL(N, d.size);
for (size_t i = 0; i < N; ++i)
CPPUNIT_ASSERT_EQUAL(src[i], d[i]);
CPPUNIT_ASSERT_EQUAL(src[i], d.data[i]);
}
......@@ -17,6 +17,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "util/ConstBuffer.hxx"
#include <array>
#include <random>
......@@ -76,6 +78,14 @@ public:
operator typename std::array<T, N>::const_pointer() const {
return begin();
}
operator ConstBuffer<T>() const {
return { begin(), size() };
}
operator ConstBuffer<void>() const {
return { begin(), size() * sizeof(T) };
}
};
template<typename T>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment