5 * Copyright (C) 2002-2005 Monty and Xiph.Org
7 * Postfish is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * Postfish is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Postfish; see the file COPYING. If not, write to the
19 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 /* (since this one is kinda unique) The Deverberation Filter....
33 Reverberation in a measurably live environment displays
34 log amplitude decay with time (linear decay when plotted on a dB
37 In its simplest form, the deverber follows actual RMS amplitude
38 attacks but chooses a slower-than-actual decay, then expands
39 according to the dB distance between the slow and actual decay.
41 Thus, the deverber can be used to 'dry out' a very 'wet'
42 reverberative track. */
44 extern int input_size
;
45 extern int input_rate
;
52 iir_filter smoothlimit
;
55 iir_state
*iirS
[deverb_freqs
];
56 iir_state
*iirR
[deverb_freqs
];
58 float prevratio
[deverb_freqs
];
60 int inactive_delay
[deverb_freqs
];
64 deverb_settings deverb_channel_set
;
65 static deverb_state channel_state
;
66 static subband_window sw
;
68 static void deverb_reset_band(int freq
){
70 for(j
=0;j
<input_ch
;j
++)
71 memset(&channel_state
.iirS
[freq
][j
],0,sizeof(iir_state
));
72 for(j
=0;j
<input_ch
;j
++)
73 memset(&channel_state
.iirR
[freq
][j
],0,sizeof(iir_state
));
74 channel_state
.inactive_delay
[freq
]=2;
77 static void deverb_reset_one(int freq
,int ch
){
78 memset(&channel_state
.iirS
[freq
][ch
],0,sizeof(iir_state
));
79 memset(&channel_state
.iirR
[freq
][ch
],0,sizeof(iir_state
));
85 subband_reset(&channel_state
.ss
);
87 for(i
=0;i
<deverb_freqs
;i
++)
91 static void filter_set(subband_state
*ss
,
97 float corner_freq
= 500./msec
;
99 /* make sure the chosen frequency doesn't require a lookahead
100 greater than what's available */
101 if(impulse_freq2(input_size
*2-ss
->qblocksize
*3)*1.01>corner_freq
&&
103 corner_freq
=impulse_freq2(input_size
*2-ss
->qblocksize
*3);
105 alpha
=corner_freq
/input_rate
;
106 filter
->g
=mkbessel(alpha
,order
,filter
->c
);
108 filter
->Hz
=alpha
*input_rate
;
112 int deverb_load(void){
114 int qblocksize
=input_size
/16;
115 memset(&channel_state
,0,sizeof(channel_state
));
117 deverb_channel_set
.active
=calloc(input_ch
,sizeof(*deverb_channel_set
.active
));
119 subband_load(&channel_state
.ss
,deverb_freqs
,qblocksize
,input_ch
);
120 subband_load_freqs(&channel_state
.ss
,&sw
,deverb_freq_list
,deverb_freqs
);
122 for(i
=0;i
<deverb_freqs
;i
++){
123 channel_state
.iirS
[i
]=calloc(input_ch
,sizeof(iir_state
));
124 channel_state
.iirR
[i
]=calloc(input_ch
,sizeof(iir_state
));
125 channel_state
.inactive_delay
[i
]=2;
130 static void deverb_work_helper(void *vs
, deverb_settings
*sset
){
131 deverb_state
*sss
=(deverb_state
*)vs
;
132 subband_state
*ss
=&sss
->ss
;
134 float smoothms
=sset
->smooth
*.1;
135 float releasems
=sset
->release
*.1;
136 iir_filter
*smooth
=&sss
->smooth
;
137 iir_filter
*smoothlimit
=&sss
->smoothlimit
;
138 iir_filter
*release
=&sss
->release
;
141 if(smoothms
!=smooth
->ms
){
142 filter_set(ss
,smoothms
,smooth
,1,2);
143 filter_set(ss
,smoothms
,smoothlimit
,0,1);
145 if(releasems
!=release
->ms
)filter_set(ss
,releasems
,release
,0,1);
147 ahead
=impulse_ahead2(smooth
->alpha
);
149 for(i
=0;i
<deverb_freqs
;i
++){
151 float fast
[input_size
];
152 float slow
[input_size
];
153 int ratio
=sset
->ratio
[i
];
154 float multiplier
= (sss
->inactive_delay
[i
]>0 ? 0. : 1.-1000./ratio
);
156 if(ratio
==1000 && sss
->prevratio
[i
]==0.){
158 /* although the effect is active, this band is set to unity.
159 Save CPU: Do not run the filters. This is not tied into the
160 subbanding 'active' indicator as that's by channel, not by
163 deverb_reset_band(i
);
166 sss
->inactive_delay
[i
]--;
167 if(sss
->inactive_delay
[i
]<0)sss
->inactive_delay
[i
]=0;
169 for(j
=0;j
<input_ch
;j
++){
170 int active
=(ss
->effect_active1
[j
] ||
171 ss
->effect_active0
[j
] ||
172 ss
->effect_activeC
[j
]);
175 deverb_reset_one(i
,j
);
178 /* run the filters */
183 memset(fast
,0,sizeof(fast
));
184 float scale
=1./input_ch
;
185 for(l
=0;l
<input_ch
;l
++){
186 float *x
=sss
->ss
.lap
[i
][l
]+ahead
;
187 for(k
=0;k
<input_size
;k
++)
190 for(k
=0;k
<input_size
;k
++)
196 float *x
=sss
->ss
.lap
[i
][j
]+ahead
;
197 for(k
=0;k
<input_size
;k
++)
202 if(sset
->linkp
==0 || firstlink
==1){
204 compute_iir_freefall_limited(fast
, input_size
, &sss
->iirS
[i
][j
],
207 memcpy(slow
,fast
,sizeof(slow
));
208 compute_iir_freefallonly1(slow
, input_size
, &sss
->iirR
[i
][j
],
211 //_analysis("fast3",i,fast,input_size,1,offset);
213 if(multiplier
==sss
->prevratio
[i
]){
215 for(k
=0;k
<input_size
;k
++)
216 fast
[k
]=fromdB_a((todB_a(slow
[k
])-todB_a(fast
[k
]))*.5*multiplier
);
219 float multiplier_add
=(multiplier
-sss
->prevratio
[i
])/input_size
;
220 multiplier
=sss
->prevratio
[i
];
222 for(k
=0;k
<input_size
;k
++){
223 fast
[k
]=fromdB_a((todB_a(slow
[k
])-todB_a(fast
[k
]))*.5*multiplier
);
224 multiplier
+=multiplier_add
;
229 //_analysis("adj3",i,fast,input_size,1,offset);
231 if(sset
->linkp
&& firstlink
==1){
233 for(l
=0;l
<input_ch
;l
++){
235 memcpy(&sss
->iirS
[i
][l
],&sss
->iirS
[i
][j
],sizeof(iir_state
));
236 memcpy(&sss
->iirR
[i
][l
],&sss
->iirR
[i
][j
],sizeof(iir_state
));
246 float *x
=sss
->ss
.lap
[i
][j
];
247 for(k
=0;k
<input_size
;k
++)
255 sss
->prevratio
[i
]=multiplier
;
261 static void deverb_work_channel(void *vs
){
262 deverb_work_helper(vs
,&deverb_channel_set
);
265 time_linkage
*deverb_read_channel(time_linkage
*in
){
266 int visible
[input_ch
];
267 int active
[input_ch
];
268 subband_window
*w
[input_ch
];
271 for(i
=0;i
<input_ch
;i
++){
273 active
[i
]=deverb_channel_set
.active
[i
];
277 return subband_read(in
, &channel_state
.ss
, w
, visible
, active
,
278 deverb_work_channel
, &channel_state
);