SAF
Loading...
Searching...
No Matches
ambi_drc_internal.c
Go to the documentation of this file.
1/*
2 * Copyright 2017-2018 Leo McCormack
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
40#include "ambi_drc.h"
41#include "ambi_drc_internal.h"
42
43/* Adapted from:
44 * D. Giannoulis, M. Massberg, and J. D. Reiss, “Digital dynamic range compressor design: Tutorial and analysis,”
45 * Journal of the Audio Engineering Society, vol. 60, no. 6, pp. 399–408, June 2012. */
47(
48 float xG,
49 float T,
50 float R,
51 float W
52)
53{
54 float yG;
55 if (2.0f*(xG - T) < -W)
56 yG = xG;
57 else if (2.0f*(fabsf(xG - T)) <= W)
58 yG = xG + (1.0f / R - 1.0f) * powf(xG - T + W / 2.0f, 2.0f) / (2.0f*W);
59 else if (2.0f*(xG - T) > W)
60 yG = T + (xG - T) / R;
61 else
62 yG = 0.0f;
63
64 return yG;
65}
66
67/* Adapted from:
68 * D. Giannoulis, M. Massberg, and J. D. Reiss, “Digital dynamic range compressor design: Tutorial and analysis,”
69 * Journal of the Audio Engineering Society, vol. 60, no. 6, pp. 399–408, June 2012. */
71(
72 float xL,
73 float yL_z1,
74 float alpha_a,
75 float alpha_r
76)
77{
78 float yL;
79 if (xL > yL_z1)
80 yL = alpha_a*yL_z1 + (1.0f - alpha_a) * xL;
81 else
82 yL = alpha_r*yL_z1 + (1.0f - alpha_r) * xL;
83
84 return yL;
85}
86
88(
89 void* const hAmbi
90)
91{
92 ambi_drc_data *pData = (ambi_drc_data*)(hAmbi);
93
94 /* Initialise afSTFT */
95 if (pData->hSTFT == NULL)
96 afSTFT_create(&(pData->hSTFT), pData->new_nSH, pData->new_nSH, HOP_SIZE, 0, 1, AFSTFT_BANDS_CH_TIME);
97 else if(pData->nSH!=pData->new_nSH){/* Or change the number of channels */
98 afSTFT_channelChange(pData->hSTFT, pData->new_nSH, pData->new_nSH);
100 }
101 pData->nSH = pData->new_nSH;
102}
103
104void ambi_drc_setInputOrder(SH_ORDERS inOrder, int* nSH)
105{
106 switch(inOrder){
107 case SH_ORDER_FIRST:
108 (*nSH) = 4;
109 break;
110 case SH_ORDER_SECOND:
111 (*nSH) = 9;
112 break;
113 case SH_ORDER_THIRD:
114 (*nSH) = 16;
115 break;
116 case SH_ORDER_FOURTH:
117 (*nSH) = 25;
118 break;
119 case SH_ORDER_FIFTH:
120 (*nSH) = 36;
121 break;
122 case SH_ORDER_SIXTH:
123 (*nSH) = 49;
124 break;
125 case SH_ORDER_SEVENTH:
126 (*nSH) = 64;
127 break;
128 case SH_ORDER_EIGHTH:
129 (*nSH) = 81;
130 break;
131 case SH_ORDER_NINTH:
132 (*nSH) = 100;
133 break;
134 case SH_ORDER_TENTH:
135 (*nSH) = 121;
136 break;
137 }
138}
SH_ORDERS
Available spherical harmonic (SH) input/output order options.
Definition _common.h:38
@ SH_ORDER_SECOND
Second-order (9 channels)
Definition _common.h:40
@ SH_ORDER_FOURTH
Fourth-order (25 channels)
Definition _common.h:42
@ SH_ORDER_FIRST
First-order (4 channels)
Definition _common.h:39
@ SH_ORDER_THIRD
Third-order (16 channels)
Definition _common.h:41
@ SH_ORDER_FIFTH
Fifth-order (36 channels)
Definition _common.h:43
@ SH_ORDER_EIGHTH
Eighth-order (81 channels)
Definition _common.h:46
@ SH_ORDER_NINTH
Ninth-order (100 channels)
Definition _common.h:47
@ SH_ORDER_TENTH
Tenth-order (121 channels)
Definition _common.h:48
@ SH_ORDER_SIXTH
Sixth-order (49 channels)
Definition _common.h:44
@ SH_ORDER_SEVENTH
Seventh-order (64 channels)
Definition _common.h:45
void afSTFT_clearBuffers(void *const hSTFT)
Flushes time-domain buffers with zeros.
Definition afSTFTlib.c:519
void afSTFT_create(void **const phSTFT, int nCHin, int nCHout, int hopsize, int lowDelayMode, int hybridmode, AFSTFT_FDDATA_FORMAT format)
Creates an instance of afSTFT.
Definition afSTFTlib.c:143
void afSTFT_channelChange(void *const hSTFT, int new_nCHin, int new_nCHout)
Re-allocates memory to support a change in the number of input/output channels.
Definition afSTFTlib.c:477
@ AFSTFT_BANDS_CH_TIME
nBands x nChannels x nTimeHops
Definition afSTFTlib.h:80
#define HOP_SIZE
STFT hop size.
A frequency-dependent Ambisonic sound scene dynamic range compressor (DRC)
void ambi_drc_setInputOrder(SH_ORDERS inOrder, int *nSH)
Sets the internal input order.
float ambi_drc_gainComputer(float xG, float T, float R, float W)
The DRC gain computer.
void ambi_drc_initTFT(void *const hAmbi)
Initialise the filterbank used by ambi_drc.
float ambi_drc_smoothPeakDetector(float xL, float yL_z1, float alpha_a, float alpha_r)
The envelope detector.
A frequency-dependent Ambisonic sound scene dynamic range compressor (DRC)
Main structure for ambi_drc.
void * hSTFT
Time-frequency transform handle.
int nSH
Current number of SH signals.
int new_nSH
New number of SH signals (current value will be replaced by this after next re-init)