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