// This effect Copyright (C) 2004 and later Cockos Incorporated
// License: LGPL - http://www.gnu.org/licenses/lgpl.html

desc: MDCT Filter
//tags: filter
//author: Cockos

slider1:128<32,512,153.6>Bands
slider2:4<0,512,1>Start Band
slider3:8<0,512,1>End Band
slider4:0.0<-120,120,1>Adjust (dB)

in_pin:input
out_pin:output (mono)
out_pin:output (mono)

@init
ext_tail_size = 2048;

@slider
  slider1=2 ^ (0|(0.5+log10(slider1)/log10(2)))|0; 
  slider1=min(512,max(slider1,32));
  fftsize != slider1*2 ? (fftsize=slider1*2; bufpos=bi1=0; bi2=fftsize*2; halfsize=fftsize*0.5);
  slider2=min(slider3,max(slider2,0)); 
  slider3=max(slider2,min(slider3,slider1));
  adj=2 ^ (slider4/6);

@sample
// bi2 was the previously transformed buffer, and by the time it
// is bi2 we only touch the second half (the first was replaced when
// it was bi1)

// bi1 is the most recently transformed buffer, we only touch the first
// half, because the second will be used for the next overlap


t=bi1+bufpos;
p0=t[0];
t[0]=spl0;

t=bi2+halfsize+bufpos;
p1=t[0];
t[0]=spl0;

spl0 = p0 + p1; // our mdct handles windowing, so we just add

bufpos+=1;

bufpos >= halfsize ? (
  // swap bi1 and bi2
  t=bi1; bi1=bi2; bi2=t;
  // we hit our FFT size here

  mdct(bi1,fftsize);
  bufpos=bi1+slider2;
  loop(slider3-slider2, bufpos[0]*=adj; bufpos+=1);
  imdct(bi1,fftsize);
  bufpos=0;
);

spl1=spl0;
