Recognizes if input is ogg or not.
[xiph.git] / postfish / deverb.c
blobfc9ac4076567ed2880ea549de8ea674742c6a452
1 /*
3 * postfish
4 *
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)
10 * any later version.
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.
24 #include "postfish.h"
25 #include "feedback.h"
26 #include <fftw3.h>
27 #include "subband.h"
28 #include "bessel.h"
29 #include "deverb.h"
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
35 scale).
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;
46 extern int input_ch;
48 typedef struct {
49 subband_state ss;
51 iir_filter smooth;
52 iir_filter smoothlimit;
53 iir_filter release;
55 iir_state *iirS[deverb_freqs];
56 iir_state *iirR[deverb_freqs];
58 float prevratio[deverb_freqs];
60 int inactive_delay[deverb_freqs];
62 } deverb_state;
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){
69 int j;
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));
82 void deverb_reset(){
83 int i;
85 subband_reset(&channel_state.ss);
87 for(i=0;i<deverb_freqs;i++)
88 deverb_reset_band(i);
91 static void filter_set(subband_state *ss,
92 float msec,
93 iir_filter *filter,
94 int attackp,
95 int order){
96 float alpha;
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 &&
102 attackp)
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);
107 filter->alpha=alpha;
108 filter->Hz=alpha*input_rate;
109 filter->ms=msec;
112 int deverb_load(void){
113 int i;
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;
127 return 0;
130 static void deverb_work_helper(void *vs, deverb_settings *sset){
131 deverb_state *sss=(deverb_state *)vs;
132 subband_state *ss=&sss->ss;
133 int i,j,k,l;
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;
139 int ahead;
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++){
150 int firstlink=0;
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
161 band. */
163 deverb_reset_band(i);
165 }else{
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]);
174 if(!active){
175 deverb_reset_one(i,j);
176 }else{
178 /* run the filters */
180 if(sset->linkp){
181 if(!firstlink){
182 firstlink++;
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++)
188 fast[k]+=x[k]*x[k];
190 for(k=0;k<input_size;k++)
191 fast[k]*=scale;
195 }else{
196 float *x=sss->ss.lap[i][j]+ahead;
197 for(k=0;k<input_size;k++)
198 fast[k]=x[k]*x[k];
202 if(sset->linkp==0 || firstlink==1){
204 compute_iir_freefall_limited(fast, input_size, &sss->iirS[i][j],
205 smooth,smoothlimit);
207 memcpy(slow,fast,sizeof(slow));
208 compute_iir_freefallonly1(slow, input_size, &sss->iirR[i][j],
209 release);
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);
218 }else{
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++){
234 if(l!=j){
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));
241 firstlink++;
246 float *x=sss->ss.lap[i][j];
247 for(k=0;k<input_size;k++)
248 if(fast[k]<1.)
249 x[k]*=fast[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];
269 int i;
271 for(i=0;i<input_ch;i++){
272 visible[i]=0;
273 active[i]=deverb_channel_set.active[i];
274 w[i]=&sw;
277 return subband_read(in, &channel_state.ss, w, visible, active,
278 deverb_work_channel, &channel_state);