1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2020 Hans Petter Selasky
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/queue.h>
29
30 #include <stdint.h>
31 #include <string.h>
32
33 #include "int.h"
34 #include "virtual_oss.h"
35
36 struct virtual_compressor voss_output_compressor_param = {
37 .knee = 85,
38 .attack = 3,
39 .decay = 20,
40 };
41 double voss_output_compressor_gain[VMAX_CHAN];
42
43 void
voss_compressor(int64_t * buffer,double * p_ch_gain,const struct virtual_compressor * p_param,const unsigned samples,const unsigned maxchan,const int64_t fmt_max)44 voss_compressor(int64_t *buffer, double *p_ch_gain,
45 const struct virtual_compressor *p_param, const unsigned samples,
46 const unsigned maxchan, const int64_t fmt_max)
47 {
48 int64_t knee_amp;
49 int64_t sample;
50 unsigned ch;
51 unsigned i;
52 double amp;
53
54 /* check if compressor is enabled */
55 if (p_param->enabled != 1)
56 return;
57
58 knee_amp = (fmt_max * p_param->knee) / VIRTUAL_OSS_KNEE_MAX;
59
60 for (ch = i = 0; i != samples; i++) {
61 sample = buffer[i];
62 if (sample < 0)
63 sample = -sample;
64
65 amp = p_ch_gain[ch];
66 if (sample > knee_amp) {
67 const double gain = (double)knee_amp / (double)sample;
68 if (gain < amp)
69 amp += (gain - amp) / (1LL << p_param->attack);
70 }
71 buffer[i] *= amp;
72 amp += (1.0 - amp) / (1LL << p_param->decay);
73 p_ch_gain[ch] = amp;
74
75 if (++ch == maxchan)
76 ch = 0;
77 }
78 }
79