Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 00c25009 authored by Antti S. Lankila's avatar Antti S. Lankila
Browse files

Add audio dithering for intermediate quantizations

Mask quantization effects under classic high-pass triangular
dithering.

Change-Id: I567ad3a7ae31002f04569ae0dec3685de2846d24
parent 76a4c6df
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ int32_t Effect::command(uint32_t cmdCode, uint32_t cmdSize, void *pCmdData, uint
    switch (cmdCode) {
    case EFFECT_CMD_ENABLE:
    case EFFECT_CMD_DISABLE: {
	enable = cmdCode == EFFECT_CMD_ENABLE;
	mEnable = cmdCode == EFFECT_CMD_ENABLE;
	int32_t *replyData = (int32_t *) pReplyData;
	*replyData = 0;
	break;
@@ -110,7 +110,7 @@ int32_t Effect::command(uint32_t cmdCode, uint32_t cmdSize, void *pCmdData, uint

int32_t Effect::process(audio_buffer_t *in, audio_buffer_t *out)
{
    if (! enable) {
    if (! mEnable) {
        for (uint32_t i = 0; i < in->frameCount; i ++) {
	    int32_t tmpL = read(in, i * 2);
	    int32_t tmpR = read(in, i * 2 + 1);
+19 −5
Original line number Diff line number Diff line
@@ -3,6 +3,12 @@
#include <stdint.h>
#include <media/EffectApi.h>

static inline uint16_t prng() {
    static uint32_t seed;
    seed = seed * 1664525 + 1013904223;
    return seed >> 14;
}

class Effect {
    private:
    audio_format_e mFormatIn;
@@ -10,9 +16,18 @@ class Effect {
    effect_buffer_access_e mAccessMode;

    protected:
    bool enable;
    bool mEnable;
    float mSamplingRate;
    uint32_t mChannels;
    uint16_t mPreviousRandom;

    /* High-passed triangular probability density function. */
    inline int32_t triangularDither16() {
        uint16_t newRandom = prng();
        int32_t rnd = mPreviousRandom - newRandom;
        mPreviousRandom = newRandom;
        return rnd;
    }

    inline int32_t read(audio_buffer_t *in, int32_t idx) {
	switch (mFormatIn) {
@@ -51,10 +66,9 @@ class Effect {
	    }
	}

	/* I should probably apply dithering for S15 / U8. */
	switch (mFormatOut) {
	case SAMPLE_FORMAT_PCM_S15:
	    sample >>= 8;
	    sample = (sample + (triangularDither16() >> 8)) >> 8;
	    if (sample > 32767) {
		sample = 32767;
	    }
@@ -65,7 +79,7 @@ class Effect {
	    break;

	case SAMPLE_FORMAT_PCM_U8:
	    sample >>= 16;
	    sample = (sample + triangularDither16()) >> 16;
	    sample += 128;
	    if (sample > 255) {
		sample = 255;