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