package com.codefixia.audio;
import com.codefixia.drumcloud.DrumCloud;
/** https://github.com/micknoise/Maximilian/blob/master/maximilian.cpp */
class MickFilter implements Filter {
private float f, res;
private float cutoff, z, c, x, y, out;
private float sampleRate;
MickFilter(float sampleRate) {
this.sampleRate = sampleRate;
}
public void setFilter(float f, float r) {
f = DrumCloud.constrain(f, 0, 1);
res = DrumCloud.constrain(r, 0, 1);
f = DrumCloud.map(f, 0, 1, 25, sampleRate / 4);
r = DrumCloud.map(r, 0, 1, 1, 25);
this.f = f;
this.res = r;
//println("mickF: f "+f+" r "+r);
}
public float applyFilter(float in) {
return lores(in, f, res);
}
public float lores(float input, float cutoff1, float resonance) {
//cutoff=cutoff1*0.5;
//if (cutoff<10) cutoff=10;
//if (cutoff>(sampleRate*0.5)) cutoff=(sampleRate*0.5);
//if (resonance<1.) resonance = 1.;
//if (resonance>2.4) resonance = 2.4;
z=DrumCloud.cos(DrumCloud.TWO_PI*cutoff/sampleRate);
c=2-2*z;
float r=(DrumCloud.sqrt(2.0f)*DrumCloud.sqrt(-DrumCloud.pow((z-1.0f), 3.0f))+resonance*(z-1))/(resonance*(z-1));
x=x+(input-y)*c;
y=y+x;
x=x*r;
out=y;
return out;
}
}
/*
* Copyright (C) 2011 Jacquet Wong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/