1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <qwidget.h>
#include <qstring.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qbrush.h>
#include <qsizepolicy.h>
#include <qsize.h>
#include <qtimer.h>
#include <math.h>
#include "filter.h"
Filter::Filter(float *p_cutoffRef, float *p_resonanceRef, float *p_risingRef, float *p_fallingRef,
float *p_hwidthRef, float *p_smoothnessRef,
QWidget* parent, const char *name, SynthData *p_synthdata)
: QWidget (parent, name)
{
synthdata = p_synthdata;
qtimer = new QTimer(this);
connect(qtimer, SIGNAL(timeout()), this, SLOT(repaintFilter()));
cutoffRef = p_cutoffRef;
resonanceRef = p_resonanceRef;
risingRef = p_risingRef;
fallingRef = p_fallingRef;
hwidthRef = p_hwidthRef;
smoothnessRef = p_smoothnessRef;
setPalette(QPalette(QColor(0, 20, 100), QColor(0, 20, 100)));
setMinimumHeight(140);
}
Filter::~Filter()
{
}
float Filter::logfilt(float logf, float cutoff, float resonance, float rising,
float falling, float hwidth, float smoothness) {
float response;
float sr, sf, hw, sn, rn;
float log2, c1, c2, c3, c4, r1, r2, r3, r4;
float logf1, logf2, logf3, logf4, logf5, logf6, logf7;
log2 = log(2.0);
sr = rising / 48.0;
sf = falling / 48.0;
sn = smoothness;
rn = resonance;
hw = hwidth;
c1 = sr / (2.0 * sn);
c2 = - sr / (2.0 * hw);
c3 = - sf / (2.0 * hw);
c4 = sf / (2.0 * sn);
r1 = 1.0 - rn + c1 * sn * sn;
if (r1 > 1.0 - rn / 2.0) {
r1 = 1.0 - rn / 2.0;
c1 = (r1 + rn - 1.0) / (sn * sn);
}
r2 = 1.0 + c2 * hw * hw;
if (r2 < 1.0 - rn / 2.0) {
r2 = 1.0 - rn / 2.0;
c2 = (r2 - 1.0) / (hw * hw);
}
r3 = 1.0 + c3 * hw * hw;
if (r3 < 0.5) {
r3 = 0.5;
c3 = (r3 - 1.0) / (hw * hw);
}
r4 = c4 * sn * sn;
if (r4 > 0.5) {
r4 = 0.5;
c4 = r4 / (sn * sn);
}
logf4 = log(cutoff) / log2;
logf3 = logf4 - hw;
logf5 = logf4 + hw;
logf2 = logf3 + (r1 - r2) / sr;
logf1 = logf2 - sn;
logf6 = logf5 + (r3 - r4) / sf;
logf7 = logf6 + sn;
if (logf < logf1) {
response = 1.0 - rn;
}
if ((logf >= logf1) && (logf < logf2)) {
response = 1.0 - rn + c1 * (logf - logf1) * (logf - logf1);
}
if ((logf >= logf2) && (logf < logf3)) {
response = r1 + sr * (logf - logf2);
}
if ((logf >= logf3) && (logf < logf4)) {
response = 1.0 + c2 * (logf - logf4) * (logf - logf4);
}
if ((logf >= logf4) && (logf < logf5)) {
response = 1.0 + c3 * (logf - logf4) * (logf - logf4);
}
if ((logf >= logf5) && (logf < logf6)) {
response = r3 - sf * (logf - logf5);
}
if ((logf >= logf6) && (logf < logf7)) {
response = c4 * (logf - logf7) * (logf - logf7);
}
if (logf >= logf7) {
response = 0;
}
return(response);
}
float Filter::filt(float f, float cutoff, float resonance, float rising,
float falling, float hwidth, float smoothness) {
float response;
response = logfilt(log(f)/log(2.0), cutoff, resonance, rising, falling, hwidth, smoothness);
return((exp(log(10.0)/10.0 * response)-1.0) / (exp(log(10.0)/10.0)-1.0));
// return((exp(response)-1.0) / (exp(1.0)-1.0));
}
void Filter::paintEvent(QPaintEvent *) {
QPixmap pm(width(), height());
QPainter p(&pm);
int l1;
float x1, y1, x2, y2, xscale, yscale;
pm.fill(QColor(0, 20, 100));
p.setViewport(0, 0, width(), height());
p.setWindow(0, 0, width(), height());
p.setPen(QColor(220, 100, 0));
p.drawRect(0, 0, width(), height());
xscale = (float)width() * log(2.0) / log((float)synthdata->rate / 2.0);
yscale = (float)(height()-1);
for (l1 = 0; l1 < width()-1; l1++) {
x1 = (float)l1 / xscale;
x2 = (float)(l1 + 1) / xscale;
y1 = yscale * (1.0 - logfilt(x1, *cutoffRef, *resonanceRef, *risingRef, *fallingRef, *hwidthRef, *smoothnessRef));
y2 = yscale * (1.0 - logfilt(x2, *cutoffRef, *resonanceRef, *risingRef, *fallingRef, *hwidthRef, *smoothnessRef));
p.drawLine(l1, y1, l1 + 1, y2);
}
xscale = (float)width() / (float)(synthdata->rate / 2.0);
p.setPen(QColor(0, 200, 100));
for (l1 = 0; l1 < width()-1; l1++) {
x1 = (float)l1 / xscale;
x2 = (float)(l1 + 1) / xscale;
y1 = yscale * (1.0 - filt(x1, *cutoffRef, *resonanceRef, *risingRef, *fallingRef, *hwidthRef, *smoothnessRef));
y2 = yscale * (1.0 - filt(x2, *cutoffRef, *resonanceRef, *risingRef, *fallingRef, *hwidthRef, *smoothnessRef));
p.drawLine(l1, y1, l1 + 1, y2);
}
bitBlt(this, 0, 0, &pm);
}
void Filter::repaintFilter() {
repaint(false);
}
void Filter::updateFilter(int value) {
qtimer->start(10, true);
}
QSize Filter::sizeHint() const {
return QSize(FILTER_MINIMUM_WIDTH, FILTER_MINIMUM_HEIGHT);
}
QSizePolicy Filter::sizePolicy() const {
return QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
}
void Filter::resizeEvent (QResizeEvent* )
{
repaint(true);
}
|