eq: easy optimisation (don't compute response more than needed)
This commit is contained in:
parent
3407568b5f
commit
892cbd6886
2 changed files with 45 additions and 33 deletions
|
|
@ -5,6 +5,7 @@
|
|||
-widgets
|
||||
- file: remember last visited directy per widget if `directory` property is not set
|
||||
- knob: add `horizontal` mode
|
||||
- eq: filter responses are now only computed when needed; the widget's value is no longer exposed / sync'ed.
|
||||
|
||||
## 1.30.2
|
||||
|
||||
|
|
|
|||
|
|
@ -53,20 +53,31 @@ class Eq extends StaticProperties(Plot, {logScaleX: false, logScaleY:false, smoo
|
|||
|
||||
super(options)
|
||||
|
||||
this.calcResponse()
|
||||
this.setupArrays()
|
||||
this.calcPending = true
|
||||
this.batchDraw()
|
||||
|
||||
}
|
||||
|
||||
setupArrays() {
|
||||
|
||||
this.freqs = new Float32Array(this.width)
|
||||
this.value = new Float32Array(this.width).fill(0)
|
||||
|
||||
var i,
|
||||
min = this.getProp('rangeX').min,
|
||||
max = this.getProp('rangeX').max,
|
||||
width = this.width
|
||||
|
||||
for (i = 0; i < width; ++i) {
|
||||
this.freqs[i] = mapToScale(i, [0, width], [min, max], -1, true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
calcResponse() {
|
||||
|
||||
var resolution = this.width,
|
||||
frequencyHz = new Float32Array(resolution),
|
||||
filterResponse = new Float32Array(resolution).fill(0),
|
||||
Fs = this.getProp('rangeX').max * 2
|
||||
|
||||
for (let i = 0; i < resolution; ++i) {
|
||||
frequencyHz[i] = mapToScale(i, [0, this.width], [this.getProp('rangeX').min, this.getProp('rangeX').max], -1, true)
|
||||
}
|
||||
this.value.fill(0)
|
||||
|
||||
for (let filter of this.getProp('filters')) {
|
||||
|
||||
|
|
@ -85,31 +96,29 @@ class Eq extends StaticProperties(Plot, {logScaleX: false, logScaleY:false, smoo
|
|||
gain: filter.gain || 0,
|
||||
frequency: filter.freq || 1000,
|
||||
q: filter.q || 1,
|
||||
poles, Fs
|
||||
}, frequencyHz, filterResponse)
|
||||
poles,
|
||||
Fs: this.getProp('rangeX').max * 2
|
||||
}, this.freqs, this.value)
|
||||
}
|
||||
|
||||
this.setValue(filterResponse, {sync: true})
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
setValue(v, options={}) {
|
||||
|
||||
if (!(v instanceof Float32Array) && !(Array.isArray(v)) || v.length !== this.width) return
|
||||
|
||||
this.value = v
|
||||
this.batchDraw()
|
||||
|
||||
if (options.sync) this.changed(options)
|
||||
|
||||
draw() {
|
||||
if (this.calcPending) {
|
||||
this.calcResponse()
|
||||
this.calcPending = false
|
||||
}
|
||||
super.draw()
|
||||
}
|
||||
|
||||
resizeHandle(event){
|
||||
|
||||
super.resizeHandle(event)
|
||||
if (this.width !== this.value.length) this.calcResponse()
|
||||
if (this.width !== this.value.length) {
|
||||
this.setupArrays()
|
||||
this.calcPending = true
|
||||
this.batchDraw()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -119,7 +128,8 @@ class Eq extends StaticProperties(Plot, {logScaleX: false, logScaleY:false, smoo
|
|||
|
||||
switch (propName) {
|
||||
case 'filters':
|
||||
this.calcResponse()
|
||||
this.calcPending = true
|
||||
this.batchDraw()
|
||||
return
|
||||
|
||||
}
|
||||
|
|
@ -141,7 +151,8 @@ function biquadResponse(options, frequencyHz, filterResponse) {
|
|||
|
||||
var {type, frequency, q, gain, Fs, poles} = options,
|
||||
len = frequencyHz.length,
|
||||
a0, a1, a2, b1, b2,norm
|
||||
a0, a1, a2, b1, b2,norm,
|
||||
w, phi, y, i
|
||||
|
||||
var V = Math.pow(10, Math.abs(gain) / 20)
|
||||
var K = Math.tan(Math.PI * frequency / Fs)
|
||||
|
|
@ -264,15 +275,15 @@ function biquadResponse(options, frequencyHz, filterResponse) {
|
|||
break
|
||||
}
|
||||
|
||||
for (var i = 0; i < len; i++) {
|
||||
|
||||
var w = mapToScale(frequencyHz[i], [0, Fs / 2], [0, Math.PI], -1, false),
|
||||
phi = Math.pow(Math.sin(w / 2), 2),
|
||||
y = Math.log(Math.pow(a0 + a1 + a2, 2) - 4 * (a0 * a1 + 4 * a0 * a2 + a1 * a2) * phi + 16 * a0 * a2 * phi * phi) -
|
||||
Math.log(Math.pow(1 + b1 + b2, 2) - 4 * (b1 + 4 * b2 + b1 * b2) * phi + 16 * b2 * phi *phi)
|
||||
for (i = 0; i < len; i++) {
|
||||
|
||||
w = mapToScale(frequencyHz[i], [0, Fs / 2], [0, Math.PI], -1, false),
|
||||
phi = Math.pow(Math.sin(w / 2), 2),
|
||||
y = Math.log(Math.pow(a0 + a1 + a2, 2) - 4 * (a0 * a1 + 4 * a0 * a2 + a1 * a2) * phi + 16 * a0 * a2 * phi * phi) -
|
||||
Math.log(Math.pow(1 + b1 + b2, 2) - 4 * (b1 + 4 * b2 + b1 * b2) * phi + 16 * b2 * phi * phi)
|
||||
|
||||
y = y * 10 / Math.LN10
|
||||
// if (y == -Infinity) y = -200
|
||||
|
||||
filterResponse[i] += y * poles
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue