summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/dsp/surround.c
blob: 1a24d615fc780c81746f8a8bda0ae00e7451bbb5 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***************************************************************************
 *             __________               __   ___.
 *   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 "surround.h"
#include "config.h"
#include "fixedpoint.h"
#include "fracmul.h"
#include "settings.h"
#include "dsp_proc_entry.h"
#include "dsp_filter.h"
#include "core_alloc.h"

static int surround_balance = 0;
static bool surround_side_only = false;
static int surround_mix = 100;
static int surround_strength = 0;
/*1 sample ~ 11ns */
#define DLY_5MS  454
#define DLY_8MS  727
#define DLY_10MS 909
#define DLY_15MS 1363
#define DLY_30MS 2727
#define MAX_DLY DLY_30MS

#define B0_DLY  (MAX_DLY/8 + 1)
#define B2_DLY  (MAX_DLY   + 1)
#define BB_DLY  (MAX_DLY/4 + 1)
#define HH_DLY  (MAX_DLY/2 + 1)
#define CL_DLY  B2_DLY

/*voice from 300hz - 3400hz ?*/
static int32_t tcoef1,tcoef2,bcoef,hcoef;

static int dly_size = MAX_DLY;
static int cutoff_l = 320;
static int cutoff_h = 3400;

static int b0_r=0,b0_w=0,
           b2_r=0,b2_w=0,
           bb_r=0,bb_w=0,
           hh_r=0,hh_w=0,
           cl_r=0,cl_w=0;
static int handle = -1;

#define SURROUND_BUFSIZE ((B0_DLY + B2_DLY + BB_DLY + HH_DLY + CL_DLY)*sizeof (int32_t))

static int surround_buffer_alloc(void)
{
    handle = core_alloc("dsp_surround_buffer", SURROUND_BUFSIZE);
    return handle;
}

static void surround_buffer_free(void)
{
    if (handle < 0)
        return;

    core_free(handle);
    handle = -1;
}

static void dsp_surround_flush(void)
{
    memset(core_get_data(handle), 0, SURROUND_BUFSIZE);
}

static void surround_update_filter(unsigned int fout)
{
    tcoef1 = fp_div(cutoff_l, fout, 31);
    tcoef2 = fp_div(cutoff_h, fout, 31);
    bcoef  = fp_div(cutoff_l / 2, fout, 31);
    hcoef  = fp_div(cutoff_h * 2, fout, 31);
}

void dsp_surround_set_balance(int var)
{
    surround_balance = var;
}

void dsp_surround_side_only(bool var)
{
    surround_side_only = var;
}

void dsp_surround_mix(int var)
{
    surround_mix = var;
}

void dsp_surround_set_cutoff(int frq_l, int frq_h)
{
    if (cutoff_l == frq_l && cutoff_h == frq_h)
        return; /* No settings change */

    cutoff_l = frq_l;/*fx2*/
    cutoff_h = frq_h;/*fx1*/

    struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO);
    if (!dsp_proc_enabled(dsp, DSP_PROC_SURROUND))
        return;

    surround_update_filter(dsp_get_output_frequency(dsp));
}

static void surround_set_stepsize(int surround_strength)
{
    if (handle > 0)
        dsp_surround_flush();

    switch(surround_strength)
    {
    case 1:
        dly_size =  DLY_5MS;
        break;
    case 2:
        dly_size =  DLY_8MS;
        break;
    case 3:
        dly_size =  DLY_10MS;
        break;
    case 4:
        dly_size =  DLY_15MS;
        break;
    case 5:
        dly_size =  DLY_30MS;
        break;
    }
}

void dsp_surround_enable(int var)
{
    if (var == surround_strength)
        return; /* No setting change */

    surround_strength = var;

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

    if (was_enabled == now_enabled)
        return; /* No change in enabled status */

    if (now_enabled)
        surround_set_stepsize(var);

    /* 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_SURROUND, now_enabled);
}

static void surround_process(struct dsp_proc_entry *this,
                               struct dsp_buffer **buf_p)
{

    struct dsp_buffer *buf = *buf_p;
    int count = buf->remcount;

    int dly_shift3 = dly_size/8;
    int dly_shift2 = dly_size/4;
    int dly_shift1 = dly_size/2;
    int dly = dly_size;
    int i;
    int32_t x;

    /*only need to buffer right channel */
    static int32_t *b0, *b2, *bb, *hh, *cl;

    b0 = core_get_data(handle);
    b2 = b0 + B0_DLY;
    bb = b2 + B2_DLY;
    hh = bb + BB_DLY;
    cl = hh + HH_DLY;

    for (i = 0; i < count; i++)
    {
        int32_t mid = buf->p32[0][i] / 2 + buf->p32[1][i] / 2;
        int32_t side = buf->p32[0][i] - buf->p32[1][i];
        int32_t temp0, temp1;

        if (!surround_side_only)
        {
            /*clone the left channal*/
            temp0 = buf->p32[0][i];
            /*keep the middle band of right channel*/
            temp1 = FRACMUL(buf->p32[1][i], tcoef1) -
                    FRACMUL(buf->p32[1][i], tcoef2);
        }
        else /* apply haas to side only*/
        {
            temp0 = side / 2;
            temp1 = FRACMUL(-side, tcoef1) / 2 -
                    FRACMUL(-side, tcoef2) / 2;
        }

        /* inverted crossfeed delay (left channel) to make sound wider*/
        x = temp1/100 * 35;
        temp0 += dequeue(cl, &cl_r, dly);
        enqueue(-x, cl, &cl_w, dly);

        /* apply 1/8 delay to frequency below fx2 */
        x = buf->p32[1][i] - FRACMUL(buf->p32[1][i], tcoef1);
        temp1 += dequeue(b0, &b0_r, dly_shift3);
        enqueue(x, b0, &b0_w, dly_shift3 );

        /* cut frequency below half fx2*/
        temp1 = FRACMUL(temp1, bcoef);

        /* apply 1/4 delay to frequency below half fx2 */
        /* use different delay to fake the sound direction*/
        x = buf->p32[1][i] - FRACMUL(buf->p32[1][i], bcoef);
        temp1 += dequeue(bb, &bb_r, dly_shift2);
        enqueue(x, bb, &bb_w, dly_shift2 );

        /* apply full delay to higher band */
        x = FRACMUL(buf->p32[1][i], tcoef2);
        temp1 += dequeue(b2, &b2_r, dly);
        enqueue(x, b2, &b2_w, dly );

        /* do the same direction trick again */
        temp1 -= FRACMUL(temp1, hcoef);

        x = FRACMUL(buf->p32[1][i], hcoef);
        temp1 += dequeue(hh, &hh_r, dly_shift1);
        enqueue(x, hh, &hh_w, dly_shift1 );
        /*balance*/
        if (surround_balance > 0  && !surround_side_only)
        {
            temp0 -= temp0/200  * surround_balance;
            temp1 += temp1/200  * surround_balance;
        }
        else if (surround_balance > 0)
        {
            temp0 += temp0/200  * surround_balance;
            temp1 -= temp1/200  * surround_balance;
        }

        if  (surround_side_only)
        {
            temp0 += mid;
            temp1 += mid;
        }

        if (surround_mix == 100)
        {
            buf->p32[0][i] = temp0;
            buf->p32[1][i] = temp1;
        }
        else
        {
            /*dry wet mix*/
            buf->p32[0][i] = buf->p32[0][i]/100 * (100-surround_mix) +
                             temp0/100 * surround_mix;
            buf->p32[1][i] = buf->p32[1][i]/100 * (100-surround_mix) +
                             temp1/100 * surround_mix;
        }
    }
    (void)this;
}

/* Handle format changes and verify the format compatibility */
static intptr_t surround_new_format(struct dsp_proc_entry *this,
                                    struct dsp_config *dsp,
                                    struct sample_format *format)
{
    DSP_PRINT_FORMAT(DSP_PROC_SURROUND, *format);

    /* Stereo mode only */
    bool was_active = dsp_proc_active(dsp, DSP_PROC_SURROUND);
    bool now_active = format->num_channels > 1;
    dsp_proc_activate(dsp, DSP_PROC_SURROUND, now_active);

    if (now_active)
    {
        if (!was_active)
            dsp_surround_flush(); /* Going online */

        return PROC_NEW_FORMAT_OK;
    }

    /* Can't do this. Sleep until next change. */
    DEBUGF("  DSP_PROC_SURROUND- deactivated\n");
    return PROC_NEW_FORMAT_DEACTIVATED;

    (void)this;
}

/* DSP message hook */
static intptr_t surround_configure(struct dsp_proc_entry *this,
                                     struct dsp_config *dsp,
                                     unsigned int setting,
                                     intptr_t value)
{
    intptr_t retval = 0;

    switch (setting)
    {
    case DSP_PROC_INIT:
        /* Coming online; was disabled */
        retval = surround_buffer_alloc();
        if (retval < 0)
            break;

        this->process = surround_process;

        dsp_surround_flush();

        /* Wouldn't have been getting frequency updates */
        surround_update_filter(dsp_get_output_frequency(dsp));
        break;

    case DSP_PROC_CLOSE:
        /* Being disabled (called also if init fails) */
        surround_buffer_free();
        break;

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

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

    case DSP_PROC_NEW_FORMAT:
        /* Source buffer format is changing (also sent when first enabled) */
        retval = surround_new_format(this, dsp, (struct sample_format *)value);
        break;
    }

    return retval;
}

/* Database entry */
DSP_PROC_DB_ENTRY(
    SURROUND,
    surround_configure);