summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/dsp/afr.c
blob: d9b6ef97a4ec0557c706d80ed8eaeb69e07adca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2014 by Chiwen Chang
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "afr.h"
#include "config.h"
#include "fixedpoint.h"
#include "fracmul.h"
#include "settings.h"
#include "dsp_proc_entry.h"
#include "dsp_filter.h"

/* Auditory fatigue reduction */

static int afr_strength = 0;
static struct dsp_filter afr_filters[4];

static void dsp_afr_flush(void)
{
    for (int i = 0 ;i < 4;i++)
        filter_flush(&afr_filters[i]);
}

static void strength_update(unsigned int fout)
{
    int hs=0, ls=0, drop3k=0;

    switch (afr_strength)
    {
    /* not called if afr_strength == 0 (disabled) */
    case 1:
        hs=-29; ls=0; drop3k=-13;
        break;
    case 2:
        hs=-58; ls=-5; drop3k=-29;
        break;
    case 3:
        hs=-72; ls=-5; drop3k=-48;
        break;
    }

    filter_bishelf_coefs(fp_div(8000, fout, 32),
                         fp_div(20000, fout, 32),
                         0, (hs/2), 0,
                         &afr_filters[0]);

    filter_pk_coefs(fp_div(8000, fout, 32), 28, hs,
                     &afr_filters[1]);

    filter_pk_coefs(fp_div(3150, fout, 32), 28, drop3k,
                     &afr_filters[2]);

    filter_bishelf_coefs(fp_div(200, fout, 32),
                         fp_div(1280, fout, 32),
                         ls, 0, 0,
                         &afr_filters[3]);
}

void dsp_afr_enable(int var)
{
    if (var == afr_strength)
        return; /* No setting change */

    afr_strength = var;

    struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO);
    bool was_enabled = dsp_proc_enabled(dsp, DSP_PROC_AFR);
    bool now_enabled = var > 0;

    if (was_enabled == now_enabled && !now_enabled)
        return;

    /* If changing status, enable or disable it; if already enabled push
       additional DSP_PROC_INIT messages with value = 1 to force-update the
       filters */
    dsp_proc_enable(dsp, DSP_PROC_AFR, now_enabled);
}

static void afr_reduce_process(struct dsp_proc_entry *this,
                               struct dsp_buffer **buf_p)
{
    struct dsp_buffer *buf = *buf_p;

    for (int i = 0; i < 4; i++)
        filter_process(&afr_filters[i], buf->p32, buf->remcount,
                       buf->format.num_channels);

    (void)this;
}

/* DSP message hook */
static intptr_t afr_configure(struct dsp_proc_entry *this,
                              struct dsp_config *dsp,
                              unsigned int setting,
                              intptr_t value)
{
    /* This only attaches to the audio (codec) DSP */

    switch (setting)
    {
    case DSP_PROC_INIT:
        if (value == 0)
        {
            /* Coming online; was disabled */
            this->process = afr_reduce_process;
            dsp_afr_flush();
            dsp_proc_activate(dsp, DSP_PROC_AFR, true);
        }
        /* else additional forced messages */

        strength_update(dsp_get_output_frequency(dsp));
        break;

    case DSP_FLUSH:
        /* Discontinuity; clear filters */
        dsp_afr_flush();
        break;

    case DSP_SET_OUT_FREQUENCY:
        /* New output frequency */
        strength_update(value);
        break;
    }

    return 1;
}

/* Database entry */
DSP_PROC_DB_ENTRY(
    AFR,
    afr_configure);