aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-01-31 22:43:28 +0100
committerMax Kellermann <max@duempel.org>2013-01-31 22:54:10 +0100
commitf2491c88c86707cc35c7dfc30a1b5a0a29886540 (patch)
treed7b2079f3122aa486b8717c916b596c421944f82
parent1b175025fecb1c10e6719d4ab79c188d473fccc4 (diff)
PcmDither: convert struct to a class
-rw-r--r--src/PcmConvert.cxx3
-rw-r--r--src/PcmConvert.hxx2
-rw-r--r--src/PcmDither.cxx40
-rw-r--r--src/PcmDither.hxx27
-rw-r--r--src/PcmFormat.cxx14
-rw-r--r--src/PcmFormat.hxx4
-rw-r--r--test/test_pcm_dither.cxx12
7 files changed, 47 insertions, 55 deletions
diff --git a/src/PcmConvert.cxx b/src/PcmConvert.cxx
index ed260d07..9618b964 100644
--- a/src/PcmConvert.cxx
+++ b/src/PcmConvert.cxx
@@ -38,7 +38,6 @@ PcmConvert::PcmConvert()
pcm_dsd_init(&dsd);
pcm_resample_init(&resample);
- pcm_dither_24_init(&dither);
pcm_buffer_init(&format_buffer);
pcm_buffer_init(&channels_buffer);
@@ -71,7 +70,7 @@ PcmConvert::Convert16(const audio_format *src_format,
assert(dest_format->format == SAMPLE_FORMAT_S16);
- buf = pcm_convert_to_16(&format_buffer, &dither,
+ buf = pcm_convert_to_16(&format_buffer, dither,
sample_format(src_format->format),
src_buffer, src_size,
&len);
diff --git a/src/PcmConvert.hxx b/src/PcmConvert.hxx
index d76ecc12..f08188a9 100644
--- a/src/PcmConvert.hxx
+++ b/src/PcmConvert.hxx
@@ -42,7 +42,7 @@ class PcmConvert {
struct pcm_resample_state resample;
- struct pcm_dither dither;
+ PcmDither dither;
/** the buffer for converting the sample format */
struct pcm_buffer format_buffer;
diff --git a/src/PcmDither.cxx b/src/PcmDither.cxx
index e1811908..2560a1aa 100644
--- a/src/PcmDither.cxx
+++ b/src/PcmDither.cxx
@@ -21,11 +21,9 @@
#include "PcmDither.hxx"
#include "PcmPrng.hxx"
-static int16_t
-pcm_dither_sample_24_to_16(int32_t sample, struct pcm_dither *dither)
+inline int16_t
+PcmDither::Dither24To16(int_fast32_t sample)
{
- int32_t output, rnd;
-
enum {
from_bits = 24,
to_bits = 16,
@@ -37,18 +35,18 @@ pcm_dither_sample_24_to_16(int32_t sample, struct pcm_dither *dither)
MAX = ONE - 1
};
- sample += dither->error[0] - dither->error[1] + dither->error[2];
+ sample += error[0] - error[1] + error[2];
- dither->error[2] = dither->error[1];
- dither->error[1] = dither->error[0] / 2;
+ error[2] = error[1];
+ error[1] = error[0] / 2;
/* round */
- output = sample + round;
+ int_fast32_t output = sample + round;
- rnd = pcm_prng(dither->random);
- output += (rnd & mask) - (dither->random & mask);
+ int_fast32_t rnd = pcm_prng(random);
+ output += (rnd & mask) - (random & mask);
- dither->random = rnd;
+ random = rnd;
/* clip */
if (output > MAX) {
@@ -65,29 +63,29 @@ pcm_dither_sample_24_to_16(int32_t sample, struct pcm_dither *dither)
output &= ~mask;
- dither->error[0] = sample - output;
+ error[0] = sample - output;
return (int16_t)(output >> scale_bits);
}
void
-pcm_dither_24_to_16(struct pcm_dither *dither,
- int16_t *dest, const int32_t *src, const int32_t *src_end)
+PcmDither::Dither24To16(int16_t *dest, const int32_t *src,
+ const int32_t *src_end)
{
while (src < src_end)
- *dest++ = pcm_dither_sample_24_to_16(*src++, dither);
+ *dest++ = Dither24To16(*src++);
}
-static int16_t
-pcm_dither_sample_32_to_16(int32_t sample, struct pcm_dither *dither)
+inline int16_t
+PcmDither::Dither32To16(int_fast32_t sample)
{
- return pcm_dither_sample_24_to_16(sample >> 8, dither);
+ return Dither24To16(sample >> 8);
}
void
-pcm_dither_32_to_16(struct pcm_dither *dither,
- int16_t *dest, const int32_t *src, const int32_t *src_end)
+PcmDither::Dither32To16(int16_t *dest, const int32_t *src,
+ const int32_t *src_end)
{
while (src < src_end)
- *dest++ = pcm_dither_sample_32_to_16(*src++, dither);
+ *dest++ = Dither32To16(*src++);
}
diff --git a/src/PcmDither.hxx b/src/PcmDither.hxx
index 59affa08..10638230 100644
--- a/src/PcmDither.hxx
+++ b/src/PcmDither.hxx
@@ -22,24 +22,23 @@
#include <stdint.h>
-struct pcm_dither {
+class PcmDither {
int32_t error[3];
int32_t random;
-};
-static inline void
-pcm_dither_24_init(struct pcm_dither *dither)
-{
- dither->error[0] = dither->error[1] = dither->error[2] = 0;
- dither->random = 0;
-}
+public:
+ constexpr PcmDither()
+ :error{0, 0, 0}, random(0) {}
+
+ void Dither24To16(int16_t *dest, const int32_t *src,
+ const int32_t *src_end);
-void
-pcm_dither_24_to_16(struct pcm_dither *dither,
- int16_t *dest, const int32_t *src, const int32_t *src_end);
+ void Dither32To16(int16_t *dest, const int32_t *src,
+ const int32_t *src_end);
-void
-pcm_dither_32_to_16(struct pcm_dither *dither,
- int16_t *dest, const int32_t *src, const int32_t *src_end);
+private:
+ int16_t Dither24To16(int_fast32_t sample);
+ int16_t Dither32To16(int_fast32_t sample);
+};
#endif
diff --git a/src/PcmFormat.cxx b/src/PcmFormat.cxx
index cf601684..50e15c91 100644
--- a/src/PcmFormat.cxx
+++ b/src/PcmFormat.cxx
@@ -33,17 +33,17 @@ pcm_convert_8_to_16(int16_t *out, const int8_t *in, const int8_t *in_end)
}
static void
-pcm_convert_24_to_16(struct pcm_dither *dither,
+pcm_convert_24_to_16(PcmDither &dither,
int16_t *out, const int32_t *in, const int32_t *in_end)
{
- pcm_dither_24_to_16(dither, out, in, in_end);
+ dither.Dither24To16(out, in, in_end);
}
static void
-pcm_convert_32_to_16(struct pcm_dither *dither,
+pcm_convert_32_to_16(PcmDither &dither,
int16_t *out, const int32_t *in, const int32_t *in_end)
{
- pcm_dither_32_to_16(dither, out, in, in_end);
+ dither.Dither32To16(out, in, in_end);
}
static void
@@ -70,7 +70,7 @@ pcm_allocate_8_to_16(struct pcm_buffer *buffer,
}
static int16_t *
-pcm_allocate_24p32_to_16(struct pcm_buffer *buffer, struct pcm_dither *dither,
+pcm_allocate_24p32_to_16(struct pcm_buffer *buffer, PcmDither &dither,
const int32_t *src, size_t src_size,
size_t *dest_size_r)
{
@@ -84,7 +84,7 @@ pcm_allocate_24p32_to_16(struct pcm_buffer *buffer, struct pcm_dither *dither,
}
static int16_t *
-pcm_allocate_32_to_16(struct pcm_buffer *buffer, struct pcm_dither *dither,
+pcm_allocate_32_to_16(struct pcm_buffer *buffer, PcmDither &dither,
const int32_t *src, size_t src_size,
size_t *dest_size_r)
{
@@ -112,7 +112,7 @@ pcm_allocate_float_to_16(struct pcm_buffer *buffer,
}
const int16_t *
-pcm_convert_to_16(struct pcm_buffer *buffer, struct pcm_dither *dither,
+pcm_convert_to_16(struct pcm_buffer *buffer, PcmDither &dither,
enum sample_format src_format, const void *src,
size_t src_size, size_t *dest_size_r)
{
diff --git a/src/PcmFormat.hxx b/src/PcmFormat.hxx
index d739f60d..a5970b2d 100644
--- a/src/PcmFormat.hxx
+++ b/src/PcmFormat.hxx
@@ -26,7 +26,7 @@
#include <stddef.h>
struct pcm_buffer;
-struct pcm_dither;
+class PcmDither;
/**
* Converts PCM samples to 16 bit. If the source format is 24 bit,
@@ -41,7 +41,7 @@ struct pcm_dither;
* @return the destination buffer
*/
const int16_t *
-pcm_convert_to_16(struct pcm_buffer *buffer, struct pcm_dither *dither,
+pcm_convert_to_16(struct pcm_buffer *buffer, PcmDither &dither,
enum sample_format src_format, const void *src,
size_t src_size, size_t *dest_size_r);
diff --git a/test/test_pcm_dither.cxx b/test/test_pcm_dither.cxx
index 44830746..6d1ed686 100644
--- a/test/test_pcm_dither.cxx
+++ b/test/test_pcm_dither.cxx
@@ -37,9 +37,7 @@ random24()
void
test_pcm_dither_24()
{
- struct pcm_dither dither;
-
- pcm_dither_24_init(&dither);
+ PcmDither dither;
enum { N = 256 };
int32_t src[N];
@@ -48,7 +46,7 @@ test_pcm_dither_24()
int16_t dest[N];
- pcm_dither_24_to_16(&dither, dest, src, src + N);
+ dither.Dither24To16(dest, src, src + N);
for (unsigned i = 0; i < N; ++i) {
g_assert_cmpint(dest[i], >=, (src[i] >> 8) - 8);
@@ -59,9 +57,7 @@ test_pcm_dither_24()
void
test_pcm_dither_32()
{
- struct pcm_dither dither;
-
- pcm_dither_24_init(&dither);
+ PcmDither dither;
enum { N = 256 };
int32_t src[N];
@@ -70,7 +66,7 @@ test_pcm_dither_32()
int16_t dest[N];
- pcm_dither_32_to_16(&dither, dest, src, src + N);
+ dither.Dither32To16(dest, src, src + N);
for (unsigned i = 0; i < N; ++i) {
g_assert_cmpint(dest[i], >=, (src[i] >> 16) - 8);