From f66a011ec13b3f4d03cceef361aaf59afcb1bec6 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 25 Aug 2018 13:05:19 +0200 Subject: [PATCH] plots: normalize a few things and fix visualizer's value sync (#352) --- .../js/app/widgets/plots/_plots_base.js | 32 +++++++---------- src/browser/js/app/widgets/plots/eq.js | 34 ++++++++++++------- .../js/app/widgets/plots/visualizer.js | 18 ++++++---- 3 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/browser/js/app/widgets/plots/_plots_base.js b/src/browser/js/app/widgets/plots/_plots_base.js index 0c3cb365..c3637072 100644 --- a/src/browser/js/app/widgets/plots/_plots_base.js +++ b/src/browser/js/app/widgets/plots/_plots_base.js @@ -14,7 +14,7 @@ module.exports = class _plots_base extends Canvas { super({...options, html: html}) - this.data = [] + this.value = [] this.rangeX = this.getProp('rangeX') || {min:0,max:1} this.rangeY = this.getProp('rangeY') || {min:0,max:1} this.logScaleX = this.getProp('logScaleX') @@ -94,14 +94,14 @@ module.exports = class _plots_base extends Canvas { var points = [] - for (let i in this.data) { + for (let i in this.value) { - if (this.data[i].length) { - points.push(mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[0,this.width],0,this.logScaleX,true)) - points.push(mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true)) + if (this.value[i].length) { + points.push(mapToScale(this.value[i][0],[this.rangeX.min,this.rangeX.max],[0,this.width],0,this.logScaleX,true)) + points.push(mapToScale(this.value[i][1],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true)) } else { - points.push(mapToScale(i,[0,this.data.length-1],[0,this.width],0,this.logScaleX,true)) - points.push(mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true)) + points.push(mapToScale(i,[0,this.value.length-1],[0,this.width],0,this.logScaleX,true)) + points.push(mapToScale(this.value[i],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true)) } } @@ -151,15 +151,15 @@ module.exports = class _plots_base extends Canvas { draw_bars() { - var barWidth = Math.round(this.width / this.data.length), - offset = Math.round((this.width - barWidth * this.data.length) / 2) + var barWidth = Math.round(this.width / this.value.length), + offset = Math.round((this.width - barWidth * this.value.length) / 2) var origin = mapToScale(this.getProp('origin') || this.rangeY.min,[this.rangeY.min,this.rangeY.max],[this.height,0],0,this.getProp('logScaleY'),true) this.ctx.beginPath() - for (let i in this.data) { - var y = mapToScale(this.data[i].length ? this.data[i][1] : this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true) + for (let i in this.value) { + var y = mapToScale(this.value[i].length ? this.value[i][1] : this.value[i],[this.rangeY.min,this.rangeY.max],[this.height-2*PXSCALE,2*PXSCALE],0,this.logScaleY,true) this.ctx.rect(offset + i * barWidth, Math.min(y, origin), barWidth - PXSCALE, Math.abs(Math.min(y - origin))) } @@ -182,12 +182,12 @@ module.exports = class _plots_base extends Canvas { if (Array.isArray(v)) { - this.data = v + this.value = v } else { for (var i in v) { - if (!isNaN(i)) this.data[i] = v[i] + if (!isNaN(i)) this.value[i] = v[i] } } @@ -201,12 +201,6 @@ module.exports = class _plots_base extends Canvas { } - getValue() { - - return deepCopy(this.data) - - } - updateData() { // update the coordinates to draw diff --git a/src/browser/js/app/widgets/plots/eq.js b/src/browser/js/app/widgets/plots/eq.js index 9ec7ee34..54401c46 100644 --- a/src/browser/js/app/widgets/plots/eq.js +++ b/src/browser/js/app/widgets/plots/eq.js @@ -72,29 +72,37 @@ module.exports = class Eq extends _plots_base { } catch(err) {} } - if (typeof v == 'object' && v !== null) { + if (Array.isArray(v)) { var filters = v, eqResponse = [] - for (let i in filters) { + if (v.length === this.resolution) { - var filterResponse + eqResponse = v - if (!filters[i].type) filters[i].type = 'peak' + } else { - if (!filters[i].on) { - filterResponse = _biquad_response({type:'peak',freq:1,gain:0,q:1},!this.getProp('logScaleX'), this.resolution) - } else { - filterResponse = _biquad_response(filters[i],!this.getProp('logScaleX'), this.resolution) - } + for (let i in filters) { - for (var k in filterResponse) { - if (eqResponse[k]===undefined) { - eqResponse[k]=[0,0] + var filterResponse + + if (!filters[i].type) filters[i].type = 'peak' + + if (!filters[i].on) { + filterResponse = _biquad_response({type:'peak',freq:1,gain:0,q:1},!this.getProp('logScaleX'), this.resolution) + } else { + filterResponse = _biquad_response(filters[i],!this.getProp('logScaleX'), 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]] } - eqResponse[k] = [filterResponse[k][0], eqResponse[k][1]+filterResponse[k][1]] } } diff --git a/src/browser/js/app/widgets/plots/visualizer.js b/src/browser/js/app/widgets/plots/visualizer.js index ff810fbc..6c8f8b36 100644 --- a/src/browser/js/app/widgets/plots/visualizer.js +++ b/src/browser/js/app/widgets/plots/visualizer.js @@ -61,8 +61,7 @@ module.exports = class Visualizer extends _plots_base { this.rangeY = this.getProp('range') || {min:0,max:1} this.logScaleY = this.getProp('logScale') this.length = Math.round(clip(this.fps * this.getProp('duration'), [8, 4096])) - this.data = new Array(this.length).fill(this.rangeY.min) - this.value = this.getProp('range').min + this.value = new Array(this.length).fill(this.rangeY.min) this.cancel = false this.looping = false this.clock = 0 @@ -112,18 +111,23 @@ module.exports = class Visualizer extends _plots_base { shiftData(n) { for (var i=0; i