osmocom-analog/src/lib/libcompandor/compandor.c

142 lines
4.0 KiB
C
Raw Normal View History

2017-06-24 08:09:22 +00:00
/* Compandor to use various networks like C-Netz / NMT / AMPS / TACS
2016-03-13 15:18:42 +00:00
*
* (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2016-04-23 12:33:02 +00:00
#include <stdio.h>
2016-03-13 15:18:42 +00:00
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "../libsample/sample.h"
2016-06-20 17:37:56 +00:00
#include "compandor.h"
2016-03-13 15:18:42 +00:00
2016-04-23 12:33:02 +00:00
//#define db2level(db) pow(10, (double)db / 20.0)
2016-03-13 15:18:42 +00:00
2016-04-23 12:33:02 +00:00
/* factor is the gain (raise and fall) after given attack/recovery time */
#define COMPRESS_ATTACK_FACTOR 1.83 /* about 1.5 after 12 dB step up */
#define COMPRESS_RECOVERY_FACTOR 0.44 /* about 0.75 after 12 dB step down */
#define EXPAND_ATTACK_FACTOR 1.145 /* about 0.57 after 6 dB step up */
#define EXPAND_RECOVERY_FACTOR 0.753 /* about 1.51 after 6 dB step down */
2016-03-13 15:18:42 +00:00
/* Minimum level value to keep state (-60 dB) */
2016-04-23 12:33:02 +00:00
#define ENVELOPE_MIN 0.001
2016-03-13 15:18:42 +00:00
/* Maximum level, to prevent sqrt_tab to overflow */
#define ENVELOPE_MAX 9.990
2016-03-13 15:18:42 +00:00
static double sqrt_tab[10000];
/*
2016-06-20 17:37:56 +00:00
* Init compandor according to ITU-T G.162 specification
2016-03-13 15:18:42 +00:00
*
* Hopefully this is correct
*
*/
void init_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms)
2016-03-13 15:18:42 +00:00
{
int i;
memset(state, 0, sizeof(*state));
2016-04-23 12:33:02 +00:00
state->c.peak = 1.0;
state->c.envelope = 1.0;
state->e.peak = 1.0;
state->e.envelope = 1.0;
2017-06-24 08:09:22 +00:00
state->c.step_up = pow(COMPRESS_ATTACK_FACTOR, 1000.0 / attack_ms / samplerate);
state->c.step_down = pow(COMPRESS_RECOVERY_FACTOR, 1000.0 / recovery_ms / samplerate);
state->e.step_up = pow(EXPAND_ATTACK_FACTOR, 1000.0 / attack_ms / samplerate);
state->e.step_down = pow(EXPAND_RECOVERY_FACTOR, 1000.0 / recovery_ms / samplerate);
2016-03-13 15:18:42 +00:00
// FIXME: make global, not at instance
for (i = 0; i < 10000; i++)
sqrt_tab[i] = sqrt(i * 0.001);
}
void compress_audio(compandor_t *state, sample_t *samples, int num)
2016-03-13 15:18:42 +00:00
{
double value, peak, envelope, step_up, step_down;
2016-03-13 15:18:42 +00:00
int i;
2016-04-23 12:33:02 +00:00
step_up = state->c.step_up;
step_down = state->c.step_down;
peak = state->c.peak;
envelope = state->c.envelope;
2016-03-13 15:18:42 +00:00
2016-04-23 12:33:02 +00:00
// printf("envelope=%.4f\n", envelope);
2016-03-13 15:18:42 +00:00
for (i = 0; i < num; i++) {
value = *samples;
2016-03-13 15:18:42 +00:00
2016-04-23 12:33:02 +00:00
/* 'peak' is the level that raises directly with the signal
* level, but falls with specified recovery rate. */
if (fabs(value) > peak)
peak = fabs(value);
2016-03-13 15:18:42 +00:00
else
2016-04-23 12:33:02 +00:00
peak *= step_down;
/* 'evelope' is the level that raises with the specified attack
* rate to 'peak', but falls with specified recovery rate. */
if (peak > envelope)
envelope *= step_up;
else
envelope = peak;
if (envelope < ENVELOPE_MIN)
envelope = ENVELOPE_MIN;
if (envelope > ENVELOPE_MAX)
envelope = ENVELOPE_MAX;
2016-03-13 15:18:42 +00:00
*samples++ = value / sqrt_tab[(int)(envelope / 0.001)];
2016-04-23 12:33:02 +00:00
//if (i > 47000.0 && i < 48144)
//printf("time=%.4f envelope=%.4fdb, value=%.4f\n", (double)i/48000.0, 20*log10(envelope), value);
2016-03-13 15:18:42 +00:00
}
2016-04-23 12:33:02 +00:00
//exit(0);
2016-03-13 15:18:42 +00:00
2016-04-23 12:33:02 +00:00
state->c.envelope = envelope;
state->c.peak = peak;
2016-03-13 15:18:42 +00:00
}
void expand_audio(compandor_t *state, sample_t *samples, int num)
2016-03-13 15:18:42 +00:00
{
double value, peak, envelope, step_up, step_down;
2016-03-13 15:18:42 +00:00
int i;
2016-04-23 12:33:02 +00:00
step_up = state->e.step_up;
step_down = state->e.step_down;
peak = state->e.peak;
envelope = state->e.envelope;
2016-03-13 15:18:42 +00:00
for (i = 0; i < num; i++) {
value = *samples;
2016-03-13 15:18:42 +00:00
2016-04-23 12:33:02 +00:00
/* for comments: see compress_audio() */
if (fabs(value) > peak)
peak = fabs(value);
else
peak *= step_down;
if (peak > envelope)
envelope *= step_up;
2016-03-13 15:18:42 +00:00
else
2016-04-23 12:33:02 +00:00
envelope = peak;
if (envelope < ENVELOPE_MIN)
envelope = ENVELOPE_MIN;
2016-03-13 15:18:42 +00:00
*samples++ = value * envelope;
2016-03-13 15:18:42 +00:00
}
2016-04-23 12:33:02 +00:00
state->e.envelope = envelope;
state->e.peak = peak;
2016-03-13 15:18:42 +00:00
}