diff --git a/browser/js/app/widgets.js b/browser/js/app/widgets.js index a8d05ad7..9f927ce9 100644 --- a/browser/js/app/widgets.js +++ b/browser/js/app/widgets.js @@ -20,6 +20,7 @@ var widgets = { // plots led: require('./widgets/led'), plot: require('./widgets/plot'), + eq: require('./widgets/eq'), visualizer: require('./widgets/visualizer'), meter: require('./widgets/meter'), text: require('./widgets/text'), @@ -34,7 +35,7 @@ module.exports.categories = { 'Buttons':['toggle','push','switch'], 'Pads':['xy','rgb'], 'Matrices':['multifader','multitoggle','multipush'], - 'Plots':['plot','visualizer','led','meter','text'], + 'Plots':['plot','eq','visualizer','led','meter','text'], 'Containers':['panel','strip'] } diff --git a/browser/js/app/widgets/eq.js b/browser/js/app/widgets/eq.js new file mode 100644 index 00000000..b6e2a2ea --- /dev/null +++ b/browser/js/app/widgets/eq.js @@ -0,0 +1,211 @@ +var utils = require('./utils'), + mapToScale = utils.mapToScale, + calcBiquad = require('./filter') + +module.exports.options = { + type:'eq', + id:'auto', + + separator1:'style', + + label:'auto', + left:'auto', + top:'auto', + width:'auto', + height:'auto', + color:'auto', + css:'', + + separator2:'plot', + + filters:[], + rangeX: {min:0,max:22050}, + rangeY: {min:-20,max:20}, + logScaleX: false, + // logScaleY: false, + + separator3:'osc', + + path:'auto', + preArgs:[], + +} +module.exports.create = function(widgetData,container) { + + var widget = $(` +
+ +
+ `), + canvas = widget.find('canvas'), + ctx = canvas[0].getContext('2d') + + widget.data = [] + widget.height = undefined + widget.width = undefined + widget.linkedWidgets = [] + widget.visible = false + widget.textColor = getComputedStyle(document.documentElement).getPropertyValue('--color-text-fade') + widget.pips = { + x : { + min: '20', + max: '22k' + }, + y : { + min: Math.abs(widgetData.rangeY.min)>=1000?widgetData.rangeY.min/1000+'k':widgetData.rangeY.min, + max: Math.abs(widgetData.rangeY.max)>=1000?widgetData.rangeY.max/1000+'k':widgetData.rangeY.max + } + } + + + for (i in widgetData.filters) { + + for (j in widgetData.filters[i]) { + if (typeof widgetData.filters[i][j]=='string') { + widget.linkedWidgets.push(widgetData.filters[i][j]) + } + } + + } + + canvas.resize(function(){ + var width = canvas.width(), + height = canvas.height() + + if (height==100 && width==100) return + + if (!self.visible) { + widget.visible = true + widget.lineColor = getComputedStyle(widget[0]).getPropertyValue('--color-accent') + } + + + widget.height=height + widget.width=width + + canvas[0].setAttribute('width',width) + canvas[0].setAttribute('height',height) + + widget.fetchValues() + requestAnimationFrame(widget.draw) + + }) + + + widget.handleSync = function(e,id,w) { + if (widget.linkedWidgets.indexOf(id)==-1 || !WIDGETS[id]) return + widget.fetchValues() + requestAnimationFrame(widget.draw) + } + + $('body').on('sync',widget.handleSync) + + widget.draw = function(){ + + ctx.clearRect(0,0,widget.width,widget.height) + ctx.beginPath() + + var first = true + var point = [] + + for (i in widget.data) { + var newpoint = widget.data[i].length? + [ + mapToScale(widget.data[i][0],[widgetData.rangeX.min,widgetData.rangeX.max],[15*PXSCALE,widget.width-15*PXSCALE],0,widgetData.logScaleX,true), + mapToScale(widget.data[i][1],[widgetData.rangeY.min,widgetData.rangeY.max],[widget.height-15*PXSCALE,15*PXSCALE],0,widgetData.logScaleY,true), + ] + : + [ + mapToScale(i,[0,widget.data.length-1],[15*PXSCALE,widget.width-15*PXSCALE],0,widgetData.logScaleX,true), + mapToScale(widget.data[i],[widgetData.rangeY.min,widgetData.rangeY.max],[widget.height-15*PXSCALE,15*PXSCALE],0,widgetData.logScaleY,true), + ] + + + + if (first) { + ctx.moveTo(newpoint[0],newpoint[1]) + first = false + } else { + ctx.moveTo(point[0],point[1]) + ctx.lineTo(newpoint[0],newpoint[1]) + } + point = newpoint + } + + ctx.lineWidth = 1.5*PXSCALE + ctx.strokeStyle= widget.lineColor + ctx.stroke() + + ctx.font = PXSCALE * 10 + 'px sans-serif' + ctx.fillStyle = widget.textColor + + + ctx.textBaseline = "bottom" + ctx.textAlign = 'left' + ctx.fillText(widget.pips.x.min,12*PXSCALE,widget.height) + ctx.textAlign = 'right' + ctx.fillText(widget.pips.x.max,widget.width-10,widget.height) + + ctx.save() + ctx.translate(0, widget.height) + ctx.rotate(-Math.PI/2) + + ctx.textBaseline = "top" + ctx.textAlign = 'left' + ctx.fillText(widget.pips.y.min,12*PXSCALE,2*PXSCALE) + ctx.textAlign = 'right' + ctx.fillText(widget.pips.y.max,widget.height-10*PXSCALE,2*PXSCALE) + ctx.rotate(Math.PI/2) + ctx.restore() + + } + + + + widget.fetchValues = function(){ + var filterparams = [], + filters = widgetData.filters, + data = [] + + + for (i in filters) { + var filter = filters[i] + + filterparams[i] = {} + + for (j in filter) { + + if (typeof filter[j]=='string' && WIDGETS[filter[j]]) { + + filterparams[i][j] = WIDGETS[filter[j]][WIDGETS[filter[j]].length-1].getValue() + + } else { + + filterparams[i][j] = filter[j] + + } + + } + + } + + for (i in filterparams) { + filterPoints = calcBiquad(filterparams[i]) + for (k in filterPoints) { + if (data[k]===undefined) { + data[k]=[0,0] + } + data[k] = [filterPoints[k][0],data[k][1] + filterPoints[k][1]] + } + } + if (data.length) widget.data = data + + } + + widget.setValue = function(v) { + widget.data = v + requestAnimationFrame(widget.draw) + } + + return widget +} diff --git a/browser/js/app/widgets/filter.js b/browser/js/app/widgets/filter.js new file mode 100644 index 00000000..e73b94b2 --- /dev/null +++ b/browser/js/app/widgets/filter.js @@ -0,0 +1,187 @@ +/* + Biquad coefficient calculator + + copyright 2010 Nigel Redmon +*/ + +/* +switch (type) { + case "lowpass": + case "highpass": + case "bandpass": + case "notch": + QField.disabled = false; + gainField.disabled = true; + break; + case "peak": + QField.disabled = false; + gainField.disabled = false; + break + default: + QField.disabled = true; + gainField.disabled = false; +} +*/ + + +// +// calcBiquad +// +// Dec 14, 2010 njr +// +module.exports = function(options) { + var {type, Fc, Fs, Q, peakGain, linear} = options + var a0,a1,a2,b1,b2,norm; + var ymin, ymax, minVal, maxVal; + + var V = Math.pow(10, Math.abs(peakGain) / 20); + var K = Math.tan(Math.PI * Fc / Fs); + switch (type) { + case "lowpass": + norm = 1 / (1 + K / Q + K * K); + a0 = K * K * norm; + a1 = 2 * a0; + a2 = a0; + b1 = 2 * (K * K - 1) * norm; + b2 = (1 - K / Q + K * K) * norm; + break; + + case "highpass": + norm = 1 / (1 + K / Q + K * K); + a0 = 1 * norm; + a1 = -2 * a0; + a2 = a0; + b1 = 2 * (K * K - 1) * norm; + b2 = (1 - K / Q + K * K) * norm; + break; + + case "bandpass": + norm = 1 / (1 + K / Q + K * K); + a0 = K / Q * norm; + a1 = 0; + a2 = -a0; + b1 = 2 * (K * K - 1) * norm; + b2 = (1 - K / Q + K * K) * norm; + break; + + case "notch": + norm = 1 / (1 + K / Q + K * K); + a0 = (1 + K * K) * norm; + a1 = 2 * (K * K - 1) * norm; + a2 = a0; + b1 = a1; + b2 = (1 - K / Q + K * K) * norm; + break; + + case "peak": + if (peakGain >= 0) { + norm = 1 / (1 + 1/Q * K + K * K); + a0 = (1 + V/Q * K + K * K) * norm; + a1 = 2 * (K * K - 1) * norm; + a2 = (1 - V/Q * K + K * K) * norm; + b1 = a1; + b2 = (1 - 1/Q * K + K * K) * norm; + } + else { + norm = 1 / (1 + V/Q * K + K * K); + a0 = (1 + 1/Q * K + K * K) * norm; + a1 = 2 * (K * K - 1) * norm; + a2 = (1 - 1/Q * K + K * K) * norm; + b1 = a1; + b2 = (1 - V/Q * K + K * K) * norm; + } + break; + case "lowShelf": + if (peakGain >= 0) { + norm = 1 / (1 + Math.SQRT2 * K + K * K); + a0 = (1 + Math.sqrt(2*V) * K + V * K * K) * norm; + a1 = 2 * (V * K * K - 1) * norm; + a2 = (1 - Math.sqrt(2*V) * K + V * K * K) * norm; + b1 = 2 * (K * K - 1) * norm; + b2 = (1 - Math.SQRT2 * K + K * K) * norm; + } + else { + norm = 1 / (1 + Math.sqrt(2*V) * K + V * K * K); + a0 = (1 + Math.SQRT2 * K + K * K) * norm; + a1 = 2 * (K * K - 1) * norm; + a2 = (1 - Math.SQRT2 * K + K * K) * norm; + b1 = 2 * (V * K * K - 1) * norm; + b2 = (1 - Math.sqrt(2*V) * K + V * K * K) * norm; + } + break; + case "highShelf": + if (peakGain >= 0) { + norm = 1 / (1 + Math.SQRT2 * K + K * K); + a0 = (V + Math.sqrt(2*V) * K + K * K) * norm; + a1 = 2 * (K * K - V) * norm; + a2 = (V - Math.sqrt(2*V) * K + K * K) * norm; + b1 = 2 * (K * K - 1) * norm; + b2 = (1 - Math.SQRT2 * K + K * K) * norm; + } + else { + norm = 1 / (V + Math.sqrt(2*V) * K + K * K); + a0 = (1 + Math.SQRT2 * K + K * K) * norm; + a1 = 2 * (K * K - 1) * norm; + a2 = (1 - Math.SQRT2 * K + K * K) * norm; + b1 = 2 * (K * K - V) * norm; + b2 = (V - Math.sqrt(2*V) * K + K * K) * norm; + } + break; + } + + var len = 512; + var magPlot = []; + for (var idx = 0; idx < len; idx++) { + var w; + if (linear) + w = idx / (len - 1) * Math.PI; // 0 to pi, linear scale + else + w = Math.exp(Math.log(1 / 0.001) * idx / (len - 1)) * 0.001 * Math.PI; // 0.001 to 1, times pi, log scale + + var phi = Math.pow(Math.sin(w/2), 2); + var 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; + + if (linear) + magPlot.push([idx / (len - 1) * Fs / 2, y]); + else + magPlot.push([idx / (len - 1) * Fs / 2, y]); + + if (idx == 0) + minVal = maxVal = y; + else if (y < minVal) + minVal = y; + else if (y > maxVal) + maxVal = y; + } + + // configure y-axis + switch (type) { + default: + case "lowpass": + case "highpass": + case "bandpass": + case "notch": + ymin = -100; + ymax = 0; + if (maxVal > ymax) + ymax = maxVal; + break; + case "peak": + case "lowShelf": + case "highShelf": + ymin = -10; + ymax = 10; + if (maxVal > ymax) + ymax = maxVal; + else if (minVal < ymin) + ymin = minVal; + break; + } + + return magPlot + // magPlot = [[freq,magnitude],etc] + // ymax / ymin = max/min magnitude +} diff --git a/browser/scss/widgets/_plots.scss b/browser/scss/widgets/_plots.scss index ea78b062..73700e09 100644 --- a/browser/scss/widgets/_plots.scss +++ b/browser/scss/widgets/_plots.scss @@ -38,7 +38,7 @@ } } -.plot-container, .visualizer-container { +.plot-container, .visualizer-container, .eq-container { --color-custom:var(--color-accent); height: 200rem;