[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 4/6] audio: change mixing engine float range to [-1.f, 1.f]
From: |
Gerd Hoffmann |
Subject: |
[PULL 4/6] audio: change mixing engine float range to [-1.f, 1.f] |
Date: |
Mon, 16 Mar 2020 12:30:13 +0100 |
From: Volker RĂ¼melin <address@hidden>
Currently the internal float range of the mixing engine is
[-.5f, .5f]. PulseAudio, SDL2 and libasound use a [-1.f, 1.f]
range. This means with float samples the audio playback volume
is 6dB too low and audio recording signals will be clipped in
most cases.
To avoid another scaling factor in the conv_natural_float_* and
clip_natural_float_* functions with FLOAT_MIXENG defined this
patch changes the mixing engine float range to [-1.f, 1.f].
Signed-off-by: Volker RĂ¼melin <address@hidden>
Message-id: address@hidden
Signed-off-by: Gerd Hoffmann <address@hidden>
---
audio/mixeng_template.h | 17 ++++++++---------
audio/mixeng.c | 4 ++--
2 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/audio/mixeng_template.h b/audio/mixeng_template.h
index 77cc89b9e808..fc8e1d4d9ebf 100644
--- a/audio/mixeng_template.h
+++ b/audio/mixeng_template.h
@@ -41,32 +41,31 @@ static inline mixeng_real glue (conv_, ET) (IN_T v)
#ifdef RECIPROCAL
#ifdef SIGNED
- return nv * (1.f / (mixeng_real) (IN_MAX - IN_MIN));
+ return nv * (2.f / ((mixeng_real)IN_MAX - IN_MIN));
#else
- return (nv - HALF) * (1.f / (mixeng_real) IN_MAX);
+ return (nv - HALF) * (2.f / (mixeng_real)IN_MAX);
#endif
#else /* !RECIPROCAL */
#ifdef SIGNED
- return nv / (mixeng_real) ((mixeng_real) IN_MAX - IN_MIN);
+ return nv / (((mixeng_real)IN_MAX - IN_MIN) / 2.f);
#else
- return (nv - HALF) / (mixeng_real) IN_MAX;
+ return (nv - HALF) / ((mixeng_real)IN_MAX / 2.f);
#endif
#endif
}
static inline IN_T glue (clip_, ET) (mixeng_real v)
{
- if (v >= 0.5) {
+ if (v >= 1.f) {
return IN_MAX;
- }
- else if (v < -0.5) {
+ } else if (v < -1.f) {
return IN_MIN;
}
#ifdef SIGNED
- return ENDIAN_CONVERT ((IN_T) (v * ((mixeng_real) IN_MAX - IN_MIN)));
+ return ENDIAN_CONVERT((IN_T)(v * (((mixeng_real)IN_MAX - IN_MIN) / 2.f)));
#else
- return ENDIAN_CONVERT ((IN_T) ((v * IN_MAX) + HALF));
+ return ENDIAN_CONVERT((IN_T)((v * ((mixeng_real)IN_MAX / 2.f)) + HALF));
#endif
}
diff --git a/audio/mixeng.c b/audio/mixeng.c
index 725b529be7da..739a500449ce 100644
--- a/audio/mixeng.c
+++ b/audio/mixeng.c
@@ -271,11 +271,11 @@ f_sample *mixeng_clip[2][2][2][3] = {
#define CONV_NATURAL_FLOAT(x) (x)
#define CLIP_NATURAL_FLOAT(x) (x)
#else
-static const float float_scale = UINT_MAX;
+static const float float_scale = UINT_MAX / 2.f;
#define CONV_NATURAL_FLOAT(x) ((x) * float_scale)
#ifdef RECIPROCAL
-static const float float_scale_reciprocal = 1.f / UINT_MAX;
+static const float float_scale_reciprocal = 2.f / UINT_MAX;
#define CLIP_NATURAL_FLOAT(x) ((x) * float_scale_reciprocal)
#else
#define CLIP_NATURAL_FLOAT(x) ((x) / float_scale)
--
2.18.2
- [PULL 0/6] Audio 20200316 patches, Gerd Hoffmann, 2020/03/16
- [PULL 1/6] qapi/audio: add documentation for AudioFormat, Gerd Hoffmann, 2020/03/16
- [PULL 4/6] audio: change mixing engine float range to [-1.f, 1.f],
Gerd Hoffmann <=
- [PULL 3/6] audio: consistency changes, Gerd Hoffmann, 2020/03/16
- [PULL 6/6] audio: add audiodev format=f32 option documentation, Gerd Hoffmann, 2020/03/16
- [PULL 5/6] audio: fix saturation nonlinearity in clip_* functions, Gerd Hoffmann, 2020/03/16
- [PULL 2/6] audio: change naming scheme of FLOAT_CONV macros, Gerd Hoffmann, 2020/03/16
- Re: [PULL 0/6] Audio 20200316 patches, Peter Maydell, 2020/03/16