From d9c32382bc093561ef51278bb2f34e3b38293bcc Mon Sep 17 00:00:00 2001 From: bordun Date: Sat, 21 Mar 2020 11:42:20 +0100 Subject: [PATCH] eq: define filters in "filters" property plot: var declaration fixes inspector-field: smart quote fix --- src/client/ui/ui-inspector.js | 2 +- src/client/widgets/graphs/eq.js | 108 +++++++++++++++--------------- src/client/widgets/graphs/plot.js | 6 +- 3 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/client/ui/ui-inspector.js b/src/client/ui/ui-inspector.js index 3b90c563..cab6a699 100644 --- a/src/client/ui/ui-inspector.js +++ b/src/client/ui/ui-inspector.js @@ -203,7 +203,7 @@ class UiInspector extends UiWidget { input.blur() var v - var str = input.value.replace(/[‟”]/g, '"').replace(/[‘’]/g, '\'') // convert invalid quote characters + var str = input.value.replace(/[‟“”]/g, '"').replace(/[‘’]/g, '\'') // convert invalid quote characters try { v = JSON.parseFlex(str) diff --git a/src/client/widgets/graphs/eq.js b/src/client/widgets/graphs/eq.js index 2cefc054..5d73c1d2 100644 --- a/src/client/widgets/graphs/eq.js +++ b/src/client/widgets/graphs/eq.js @@ -4,11 +4,11 @@ var {clip} = require('../utils'), StaticProperties = require('../mixins/static_properties') -module.exports = class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22000}, logScaleX: true}) { +class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22000}, logScaleX: false}) { static description() { - return 'Draws frequency response between from from an array of filter objects.' + return 'Draws logarithmic frequency response from an array of filter objects.' } @@ -18,14 +18,7 @@ module.exports = class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: _eq:'eq', - pips: {type: 'boolean', value: true, help: 'Set to false to hide the scale'}, - resolution: {type: 'number', value: 128, help: 'Defines the number of points used to compute the frequency response'}, - rangeY: {type: 'object', value: {min:0,max:1}, help: 'Defines the min and max values for the y axis'}, - origin: {type: 'number|boolean', value: 'auto', help: 'Defines the y axis origin. Set to `false` to disable it'}, - - }, ['interaction', 'decimals', 'typeTags', 'bypass'], { - - value: {type: 'array', value: '', help: [ + filters: {type: 'array', value: '', help: [ 'Each item must be an object with the following properties', '- `type`: string ("highpass", "highshelf", "lowpass", "lowshelf", "peak" or "notch")', '- `freq`: number (filter\'s resonant frequency)', @@ -33,9 +26,13 @@ module.exports = class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: '- `gain`: number', '- `on`: boolean', - ]} + ]}, + pips: {type: 'boolean', value: true, help: 'Set to false to hide the scale'}, + resolution: {type: 'number', value: 128, help: 'Defines the number of points used to compute the frequency response'}, + rangeY: {type: 'object', value: {min:0,max:1}, help: 'Defines the min and max values for the y axis'}, + origin: {type: 'number|boolean', value: 'auto', help: 'Defines the y axis origin. Set to `false` to disable it'}, - }) + }, ['interaction', 'decimals', 'typeTags', 'bypass']) } @@ -43,54 +40,53 @@ module.exports = class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: super(options) - this.resolution = clip(this.getProp('resolution'),[64,1024]) + this.calcFilters() } - setValue(v, options={}) { + calcFilters() { - if (typeof v == 'string') { - try { - v = JSON.parseFlex(v) - } catch(err) {} - } + var filters = this.getProp('filters'), + resolution = Math.min(parseInt(this.getProp('resolution')) || 64, 1024), + eqResponse = [] - if (Array.isArray(v)) { + for (let i in filters) { - var filters = v, - eqResponse = [] + var filterResponse - if (v.length === this.resolution) { - - eqResponse = v + if (!filters[i].type) filters[i].type = 'peak' + if (!filters[i].on) { + filterResponse = calcBiquad({type:'peak',freq:1,gain:0,q:1}, resolution) } else { - - for (let i in filters) { - - var filterResponse - - if (!filters[i].type) filters[i].type = 'peak' - - if (!filters[i].on) { - filterResponse = calcBiquad({type:'peak',freq:1,gain:0,q:1}, this.resolution) - } else { - filterResponse = calcBiquad(filters[i], this.resolution) - } - - for (var k in filterResponse) { - if (eqResponse[k]===undefined) { - eqResponse[k]=[0,0] - } - - eqResponse[k] = [filterResponse[k][0], eqResponse[k][1]+filterResponse[k][1]] - } - - } - + filterResponse = calcBiquad(filters[i], resolution) } - if (eqResponse.length) super.setValue(eqResponse, options) + for (var k in filterResponse) { + if (eqResponse[k] === undefined) { + eqResponse[k] = [0,0] + } + + eqResponse[k] = [filterResponse[k][0], eqResponse[k][1]+filterResponse[k][1]] + } + + } + + this.setValue(eqResponse, {sync: true}) + + } + + + onPropChanged(propName, options, oldPropValue) { + + super.onPropChanged(...arguments) + + switch (propName) { + + case 'resolution': + case 'filters': + this.calcFilters() + return } @@ -98,13 +94,17 @@ module.exports = class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: } -// -// calcBiquad -// -// Dec 14, 2010 njr -// original @ Nigel Redmon +Eq.dynamicProps = Eq.prototype.constructor.dynamicProps.concat( + 'resolution', + 'filters' +) + +module.exports = Eq + function calcBiquad(options, resolution) { + // Dec 14, 2010 njr (Nigel Redmon) + var {type, freq, q, gain} = options, Fs = 44100, a0,a1,a2,b1,b2,norm, diff --git a/src/client/widgets/graphs/plot.js b/src/client/widgets/graphs/plot.js index a1fb402f..0064ac41 100644 --- a/src/client/widgets/graphs/plot.js +++ b/src/client/widgets/graphs/plot.js @@ -116,9 +116,9 @@ module.exports = class Plot extends StaticProperties(Canvas, {bypass: true, inte var points = [], padding = this.cssVars.padding + PXSCALE, - x, y + x, y, i - for (let i in this.value) { + for (i in this.value) { if (this.value[i].length) { points.push(mapToScale(this.value[i][0], [this.rangeX.min, this.rangeX.max], [padding, this.width - padding], 0, this.logScaleX, true)) @@ -141,7 +141,7 @@ module.exports = class Plot extends StaticProperties(Canvas, {bypass: true, inte this.ctx.beginPath() this.ctx.moveTo(points[0], points[1]) - for (let i = 2; i < points.length - 2; i += 2) { + for (i = 2; i < points.length - 2; i += 2) { this.ctx.lineTo(points[i], points[i + 1]) } this.ctx.lineTo(points[i], points[i + 1])