Widgets: use some inheritence pattern, for DRY's sake
This commit is contained in:
parent
387fe6c0a1
commit
1c8cf61430
13 changed files with 3330 additions and 3676 deletions
|
|
@ -18,12 +18,12 @@ var widgets = {
|
|||
multifader: require('./widgets/multifader'),
|
||||
|
||||
// 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'),
|
||||
led: require('./widgets/plots/led'),
|
||||
plot: require('./widgets/plots/plot'),
|
||||
eq: require('./widgets/plots/eq'),
|
||||
visualizer: require('./widgets/plots/visualizer'),
|
||||
meter: require('./widgets/plots/meter'),
|
||||
text: require('./widgets/plots/text'),
|
||||
|
||||
// containers
|
||||
strip: require('./widgets/strip'),
|
||||
|
|
|
|||
|
|
@ -1,226 +0,0 @@
|
|||
var utils = require('./utils'),
|
||||
mapToScale = utils.mapToScale,
|
||||
clip = utils.clip,
|
||||
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:[],
|
||||
resolution:128,
|
||||
rangeY: {min:-20,max:20},
|
||||
logScaleX: false,
|
||||
|
||||
separator3:'osc',
|
||||
|
||||
path:'auto',
|
||||
preArgs:[],
|
||||
|
||||
}
|
||||
module.exports.create = function(widgetData,container) {
|
||||
|
||||
var widget = $(`
|
||||
<div class="plot">
|
||||
<canvas></canvas>
|
||||
</div>
|
||||
`),
|
||||
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.rangeY = widgetData.rangeY
|
||||
widget.rangeX = {min:20,max:22050}
|
||||
widget.pips = {
|
||||
x : {
|
||||
min: '20',
|
||||
max: '22k'
|
||||
},
|
||||
y : {
|
||||
min: Math.abs(widget.rangeY.min)>=1000?widget.rangeY.min/1000+'k':widget.rangeY.min,
|
||||
max: Math.abs(widget.rangeY.max)>=1000?widget.rangeY.max/1000+'k':widget.rangeY.max
|
||||
}
|
||||
}
|
||||
|
||||
widgetData.resolution = clip(widgetData.resolution,[64,1024])
|
||||
|
||||
for (i in widgetData.filters) {
|
||||
|
||||
for (j in widgetData.filters[i]) {
|
||||
if (typeof widgetData.filters[i][j]=='string' && !(j=='type' && widgetData.filters[i][j].match(/peak|notch|highpass|highshelf|lowpass|lowshelf/))) {
|
||||
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-custom')
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
|
||||
if (widget.data[i][1]>widget.rangeY.max || widget.data[i][1]<widget.rangeY.min ||
|
||||
widget.data[i][0]>widget.rangeX.max || widget.data[i][0]<widget.rangeX.min) continue
|
||||
|
||||
var newpoint = widget.data[i].length?
|
||||
[
|
||||
mapToScale(widget.data[i][0],[widget.rangeX.min,widget.rangeX.max],[15*PXSCALE,widget.width-15*PXSCALE],0,widgetData.logScaleX,true),
|
||||
mapToScale(widget.data[i][1],[widget.rangeY.min,widget.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],[widget.rangeY.min,widget.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*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) {
|
||||
|
||||
if (!filterparams[i].type) filterparams[i].type = "peak"
|
||||
|
||||
if (!filterparams[i].on) {
|
||||
filterPoints = calcBiquad({type:"peak",freq:1,gain:0,q:1},!widgetData.logScaleX,widgetData.resolution)
|
||||
} else {
|
||||
filterPoints = calcBiquad(filterparams[i],!widgetData.logScaleX,widgetData.resolution)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
@ -1,214 +0,0 @@
|
|||
var utils = require('./utils'),
|
||||
mapToScale = utils.mapToScale
|
||||
|
||||
module.exports.options = {
|
||||
type:'plot',
|
||||
id:'auto',
|
||||
|
||||
separator1:'style',
|
||||
|
||||
label:'auto',
|
||||
left:'auto',
|
||||
top:'auto',
|
||||
width:'auto',
|
||||
height:'auto',
|
||||
color:'auto',
|
||||
css:'',
|
||||
|
||||
separator2:'plot',
|
||||
|
||||
points:[],
|
||||
rangeX: {min:0,max:1},
|
||||
rangeY: {min:0,max:1},
|
||||
logScaleX: false,
|
||||
logScaleY: false,
|
||||
|
||||
separator3:'osc',
|
||||
|
||||
path:'auto',
|
||||
preArgs:[],
|
||||
|
||||
}
|
||||
module.exports.create = function(widgetData,container) {
|
||||
|
||||
var widget = $(`
|
||||
<div class="plot">
|
||||
<canvas></canvas>
|
||||
</div>
|
||||
`),
|
||||
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: Math.abs(widgetData.rangeX.min)>=1000?widgetData.rangeX.min/1000+'k':widgetData.rangeX.min,
|
||||
max: Math.abs(widgetData.rangeX.max)>=1000?widgetData.rangeX.max/1000+'k':widgetData.rangeX.max
|
||||
},
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (typeof widgetData.points=='string') {
|
||||
|
||||
widget.linkedWidgets.push(widgetData.points)
|
||||
|
||||
} else if (typeof widgetData.points=='object') {
|
||||
|
||||
for (i in widgetData.points) {
|
||||
for (j in widgetData.points[i]) {
|
||||
if (typeof widgetData.points[i][j] == 'string') {
|
||||
widget.linkedWidgets.push(widgetData.points[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-custom')
|
||||
}
|
||||
|
||||
|
||||
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])
|
||||
if (widgetData.logScaleX&&!widgetData.logScaleY) {
|
||||
ctx.quadraticCurveTo(point[0], newpoint[1], newpoint[0], newpoint[1])
|
||||
} else if (widgetData.logScaleY&&!widgetData.logScaleX) {
|
||||
ctx.quadraticCurveTo(newpoint[0], point[1], newpoint[0], newpoint[1])
|
||||
} else {
|
||||
ctx.lineTo(newpoint[0],newpoint[1])
|
||||
}
|
||||
|
||||
}
|
||||
ctx.rect(newpoint[0]-1*PXSCALE,newpoint[1]-1*PXSCALE,2,2)
|
||||
point = newpoint
|
||||
}
|
||||
|
||||
ctx.lineWidth = 1*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 data = [],
|
||||
points = widgetData.points
|
||||
|
||||
if (typeof points=='string' && WIDGETS[points]) {
|
||||
|
||||
data = WIDGETS[points][WIDGETS[points].length-1].getValue()
|
||||
|
||||
} else if (typeof points=='object') {
|
||||
|
||||
for (i in points) {
|
||||
|
||||
data[i] = []
|
||||
for (k in [0,1]) {
|
||||
if (typeof points[i][k] == 'string' && WIDGETS[points[i][k]]) {
|
||||
data[i][k] = WIDGETS[points[i][k]][WIDGETS[points[i][k]].length-1].getValue()
|
||||
} else {
|
||||
data[i][k] = points[i][k]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (data.length) widget.data = data
|
||||
}
|
||||
|
||||
widget.setValue = function(v) {
|
||||
widget.data = v
|
||||
requestAnimationFrame(widget.draw)
|
||||
}
|
||||
|
||||
return widget
|
||||
}
|
||||
|
|
@ -107,25 +107,25 @@ module.exports = function(options,linear,resolution) {
|
|||
}
|
||||
|
||||
var magPlot = []
|
||||
for (var idx = 0; idx < len; idx++) {
|
||||
var w
|
||||
for (var i = 0; i < len; i++) {
|
||||
var w, phi, y
|
||||
if (linear)
|
||||
w = idx / (len - 1) * Math.PI; // 0 to pi, linear scale
|
||||
w = i / (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
|
||||
w = Math.exp(Math.log(1 / 0.001) * i / (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)
|
||||
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
|
||||
if (y == -Infinity) y = -200
|
||||
|
||||
if (linear)
|
||||
magPlot.push([idx / (len - 1) * Fs / 2, y])
|
||||
magPlot.push([i / (len - 1) * Fs / 2, y])
|
||||
else
|
||||
magPlot.push([idx / (len - 1) * Fs / 2, y])
|
||||
magPlot.push([i / (len - 1) * Fs / 2, y])
|
||||
|
||||
if (idx == 0)
|
||||
if (i == 0)
|
||||
minVal = maxVal = y
|
||||
else if (y < minVal)
|
||||
minVal = y
|
||||
|
|
@ -134,5 +134,4 @@ module.exports = function(options,linear,resolution) {
|
|||
}
|
||||
|
||||
return magPlot
|
||||
// magPlot = [[freq,magnitude],etc]
|
||||
}
|
||||
143
browser/js/app/widgets/plots/_plots_base.js
Normal file
143
browser/js/app/widgets/plots/_plots_base.js
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
var {mapToScale} = require('../utils')
|
||||
|
||||
module.exports = function(widgetData){
|
||||
|
||||
this.widget = $(`
|
||||
<div class="plot">
|
||||
<canvas></canvas>
|
||||
</div>
|
||||
`)
|
||||
this.canvas = this.widget.find('canvas')
|
||||
this.ctx = this.canvas[0].getContext('2d')
|
||||
this.data = []
|
||||
this.height = undefined
|
||||
this.width = undefined
|
||||
this.linkedWidgets = []
|
||||
this.visible = false
|
||||
this.textColor = getComputedStyle(document.documentElement).getPropertyValue('--color-text-fade')
|
||||
this.rangeX = widgetData.rangeX || {min:0,max:1}
|
||||
this.rangeY = widgetData.rangeY || {min:0,max:1}
|
||||
this.pips = {
|
||||
x : {
|
||||
min: Math.abs(this.rangeX.min)>=1000?this.rangeY.min/1000+'k':this.rangeX.min,
|
||||
max: Math.abs(this.rangeX.max)>=1000?this.rangeY.max/1000+'k':this.rangeX.max
|
||||
},
|
||||
y : {
|
||||
min: Math.abs(this.rangeY.min)>=1000?this.rangeY.min/1000+'k':this.rangeY.min,
|
||||
max: Math.abs(this.rangeY.max)>=1000?this.rangeY.max/1000+'k':this.rangeY.max
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.canvas.resize(function(){
|
||||
var width = this.canvas.width(),
|
||||
height = this.canvas.height()
|
||||
|
||||
if (height==100 && width==100) return
|
||||
|
||||
if (!self.visible) {
|
||||
this.visible = true
|
||||
this.lineColor = getComputedStyle(this.widget[0]).getPropertyValue('--color-custom')
|
||||
}
|
||||
|
||||
this.height=height
|
||||
this.width=width
|
||||
|
||||
this.canvas[0].setAttribute('width',width)
|
||||
this.canvas[0].setAttribute('height',height)
|
||||
|
||||
this.updateData()
|
||||
requestAnimationFrame(this._draw)
|
||||
|
||||
}.bind(this))
|
||||
|
||||
this._draw = function() {
|
||||
|
||||
this.ctx.clearRect(0,0,this.width,this.height)
|
||||
this.ctx.beginPath()
|
||||
|
||||
this.draw()
|
||||
|
||||
this.ctx.lineWidth = 1*PXSCALE
|
||||
this.ctx.strokeStyle = this.lineColor
|
||||
this.ctx.stroke()
|
||||
|
||||
this.ctx.font = PXSCALE * 10 + 'px sans-serif'
|
||||
this.ctx.fillStyle = this.textColor
|
||||
|
||||
|
||||
this.ctx.textBaseline = "bottom"
|
||||
this.ctx.textAlign = 'left'
|
||||
this.ctx.fillText(this.pips.x.min,12*PXSCALE,this.height)
|
||||
this.ctx.textAlign = 'right'
|
||||
this.ctx.fillText(this.pips.x.max,this.width-10,this.height)
|
||||
|
||||
this.ctx.save()
|
||||
this.ctx.translate(0, this.height)
|
||||
this.ctx.rotate(-Math.PI/2)
|
||||
|
||||
this.ctx.textBaseline = "top"
|
||||
this.ctx.textAlign = 'left'
|
||||
this.ctx.fillText(this.pips.y.min,12*PXSCALE,2*PXSCALE)
|
||||
this.ctx.textAlign = 'right'
|
||||
this.ctx.fillText(this.pips.y.max,this.height-10*PXSCALE,2*PXSCALE)
|
||||
this.ctx.rotate(Math.PI/2)
|
||||
this.ctx.restore()
|
||||
|
||||
}.bind(this)
|
||||
|
||||
this.draw = function() {
|
||||
var first = true
|
||||
var point = []
|
||||
for (i in this.data) {
|
||||
|
||||
if (this.data[i][1]>this.rangeY.max || this.data[i][1]<this.rangeY.min ||
|
||||
this.data[i][0]>this.rangeX.max || this.data[i][0]<this.rangeX.min) continue
|
||||
|
||||
var newpoint = this.data[i].length?
|
||||
[
|
||||
mapToScale(this.data[i][0],[this.rangeX.min,this.rangeX.max],[15*PXSCALE,this.width-15*PXSCALE],0,widgetData.logScaleX,true),
|
||||
mapToScale(this.data[i][1],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,widgetData.logScaleY,true),
|
||||
]
|
||||
:
|
||||
[
|
||||
mapToScale(i,[0,this.data.length-1],[15*PXSCALE,this.width-15*PXSCALE],0,widgetData.logScaleX,true),
|
||||
mapToScale(this.data[i],[this.rangeY.min,this.rangeY.max],[this.height-15*PXSCALE,15*PXSCALE],0,widgetData.logScaleY,true),
|
||||
]
|
||||
|
||||
|
||||
if (first) {
|
||||
this.ctx.moveTo(newpoint[0],newpoint[1])
|
||||
first = false
|
||||
} else {
|
||||
this.ctx.moveTo(point[0],point[1])
|
||||
this.ctx.lineTo(newpoint[0],newpoint[1])
|
||||
}
|
||||
point = newpoint
|
||||
}
|
||||
}.bind(this)
|
||||
|
||||
|
||||
this._handleSync = function(e,id,w){
|
||||
this.handleSync(e,id,w)
|
||||
}.bind(this)
|
||||
|
||||
this.handleSync = function(e,id,w) {
|
||||
if (this.linkedWidgets.indexOf(id)==-1 || !WIDGETS[id]) return
|
||||
this.updateData()
|
||||
requestAnimationFrame(this._draw)
|
||||
}
|
||||
|
||||
$('body').on('sync',this._handleSync)
|
||||
|
||||
|
||||
this.updateData = function(){
|
||||
// update the coordinates to draw
|
||||
}
|
||||
|
||||
this.setValue = function(v) {
|
||||
this.data = v
|
||||
requestAnimationFrame(this._draw)
|
||||
}
|
||||
|
||||
}
|
||||
105
browser/js/app/widgets/plots/eq.js
Normal file
105
browser/js/app/widgets/plots/eq.js
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
var {mapToScale, clip} = require('../utils'),
|
||||
_biquad_response = require('./_biquad_response'),
|
||||
_plots_base = require('./_plots_base')
|
||||
|
||||
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:[],
|
||||
resolution:128,
|
||||
rangeY: {min:-20,max:20},
|
||||
logScaleX: false,
|
||||
|
||||
separator3:'osc',
|
||||
|
||||
path:'auto',
|
||||
preArgs:[],
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports.create = function(widgetData) {
|
||||
|
||||
var eq = new _plots_base(widgetData)
|
||||
|
||||
eq.rangeX = {min:20,max:22050}
|
||||
eq.pips.x.min = '20'
|
||||
eq.pips.x.max = '22k'
|
||||
|
||||
widgetData.resolution = clip(widgetData.resolution,[64,1024])
|
||||
|
||||
for (i in widgetData.filters) {
|
||||
|
||||
for (j in widgetData.filters[i]) {
|
||||
if (typeof widgetData.filters[i][j]=='string' && !(j=='type' && widgetData.filters[i][j].match(/peak|notch|highpass|highshelf|lowpass|lowshelf/))) {
|
||||
eq.linkedWidgets.push(widgetData.filters[i][j])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
eq.updateData = function(){
|
||||
var filters = [],
|
||||
eqResponse = []
|
||||
|
||||
|
||||
for (i in widgetData.filters) {
|
||||
var filter = widgetData.filters[i]
|
||||
|
||||
filters[i] = {}
|
||||
|
||||
for (j in filter) {
|
||||
|
||||
if (typeof filter[j]=='string' && WIDGETS[filter[j]]) {
|
||||
|
||||
filters[i][j] = WIDGETS[filter[j]][WIDGETS[filter[j]].length-1].getValue()
|
||||
|
||||
} else {
|
||||
|
||||
filters[i][j] = filter[j]
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for (i in filters) {
|
||||
|
||||
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},!widgetData.logScaleX,widgetData.resolution)
|
||||
} else {
|
||||
filterResponse = _biquad_response(filters[i],!widgetData.logScaleX,widgetData.resolution)
|
||||
}
|
||||
for (k in filterResponse) {
|
||||
if (eqResponse[k]===undefined) {
|
||||
eqResponse[k]=[0,0]
|
||||
}
|
||||
|
||||
eqResponse[k] = [filterResponse[k][0], eqResponse[k][1]+filterResponse[k][1]]
|
||||
}
|
||||
}
|
||||
if (eqResponse.length) eq.data = eqResponse
|
||||
|
||||
}
|
||||
|
||||
return eq.widget
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
var utils = require('./utils'),
|
||||
mapToScale = utils.mapToScale
|
||||
var {mapToScale} = require('../utils')
|
||||
|
||||
module.exports.options = {
|
||||
type:'led',
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
var utils = require('./utils'),
|
||||
clip = utils.clip,
|
||||
mapToScale = utils.mapToScale
|
||||
var {mapToScale} = require('../utils')
|
||||
|
||||
module.exports.options = {
|
||||
type:'fader',
|
||||
81
browser/js/app/widgets/plots/plot.js
Normal file
81
browser/js/app/widgets/plots/plot.js
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
var {mapToScale} = require('../utils'),
|
||||
_plots_base = require('./_plots_base')
|
||||
|
||||
module.exports.options = {
|
||||
type:'plot',
|
||||
id:'auto',
|
||||
|
||||
separator1:'style',
|
||||
|
||||
label:'auto',
|
||||
left:'auto',
|
||||
top:'auto',
|
||||
width:'auto',
|
||||
height:'auto',
|
||||
color:'auto',
|
||||
css:'',
|
||||
|
||||
separator2:'plot',
|
||||
|
||||
points:[],
|
||||
rangeX: {min:0,max:1},
|
||||
rangeY: {min:0,max:1},
|
||||
logScaleX: false,
|
||||
logScaleY: false,
|
||||
|
||||
separator3:'osc',
|
||||
|
||||
path:'auto',
|
||||
preArgs:[],
|
||||
|
||||
}
|
||||
module.exports.create = function(widgetData) {
|
||||
|
||||
|
||||
var plot = new _plots_base(widgetData)
|
||||
|
||||
if (typeof widgetData.points=='string') {
|
||||
|
||||
plot.linkedWidgets.push(widgetData.points)
|
||||
|
||||
} else if (typeof widgetData.points=='object') {
|
||||
|
||||
for (i in widgetData.points) {
|
||||
for (j in widgetData.points[i]) {
|
||||
if (typeof widgetData.points[i][j] == 'string') {
|
||||
plot.linkedWidgets.push(widgetData.points[i][j])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
plot.updateData = function(){
|
||||
var data = [],
|
||||
points = widgetData.points
|
||||
|
||||
if (typeof points=='string' && WIDGETS[points]) {
|
||||
|
||||
data = WIDGETS[points][WIDGETS[points].length-1].getValue()
|
||||
|
||||
} else if (typeof points=='object') {
|
||||
|
||||
for (i in points) {
|
||||
|
||||
data[i] = []
|
||||
for (k in [0,1]) {
|
||||
if (typeof points[i][k] == 'string' && WIDGETS[points[i][k]]) {
|
||||
data[i][k] = WIDGETS[points[i][k]][WIDGETS[points[i][k]].length-1].getValue()
|
||||
} else {
|
||||
data[i][k] = points[i][k]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (data.length) plot.data = data
|
||||
}
|
||||
|
||||
return plot.widget
|
||||
}
|
||||
|
|
@ -1,6 +1,3 @@
|
|||
var utils = require('./utils'),
|
||||
mapToScale = utils.mapToScale
|
||||
|
||||
module.exports.options = {
|
||||
type:'text',
|
||||
id:'auto',
|
||||
117
browser/js/app/widgets/plots/visualizer.js
Normal file
117
browser/js/app/widgets/plots/visualizer.js
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
var {mapToScale, clip} = require('../utils'),
|
||||
_plots_base = require('./_plots_base')
|
||||
|
||||
module.exports.options = {
|
||||
type:'visualizer',
|
||||
id:'auto',
|
||||
|
||||
separator1:'style',
|
||||
|
||||
label:'auto',
|
||||
left:'auto',
|
||||
top:'auto',
|
||||
width:'auto',
|
||||
height:'auto',
|
||||
color:'auto',
|
||||
css:'',
|
||||
|
||||
separator2:'plot',
|
||||
|
||||
widgetId:'',
|
||||
duration:1,
|
||||
range: {min:0,max:1},
|
||||
logScale: false,
|
||||
|
||||
separator3:'osc',
|
||||
|
||||
path:'auto',
|
||||
preArgs:[],
|
||||
}
|
||||
|
||||
module.exports.create = function(widgetData,container) {
|
||||
|
||||
var visualizer = new _plots_base(widgetData)
|
||||
|
||||
visualizer.pips.x.min = Math.abs(widgetData.range.min)>=1000?widgetData.range.min/1000+'k':widgetData.range.min
|
||||
visualizer.pips.x.max = Math.abs(widgetData.range.max)>=1000?widgetData.range.max/1000+'k':widgetData.range.max
|
||||
visualizer.length = Math.round(clip(60*widgetData.duration,[8,4096]))
|
||||
visualizer.data = new Array(visualizer.length)
|
||||
visualizer.value = widgetData.range.min
|
||||
visualizer.cancel = false
|
||||
visualizer.loop = false
|
||||
|
||||
|
||||
|
||||
visualizer.handleSync = function(e,id,w) {
|
||||
if (widgetData.widgetId!=id || !WIDGETS[id]) return
|
||||
visualizer.startLoop()
|
||||
}
|
||||
|
||||
visualizer.startLoop = function(){
|
||||
|
||||
if (visualizer.cancel) clearTimeout(visualizer.cancel)
|
||||
|
||||
visualizer.cancel = setTimeout(function(){
|
||||
clearInterval(visualizer.loop)
|
||||
visualizer.loop = false
|
||||
visualizer.cancel = false
|
||||
},1000*widgetData.duration)
|
||||
|
||||
if (visualizer.loop) return
|
||||
|
||||
visualizer.loop = setInterval(function(){
|
||||
visualizer.updateData()
|
||||
visualizer._draw()
|
||||
},1000*widgetData.duration/visualizer.length)
|
||||
}
|
||||
|
||||
|
||||
visualizer.draw = function(){
|
||||
|
||||
var first = true
|
||||
var point = []
|
||||
|
||||
for (var i=visualizer.length-1;i>=0;i=i-1) {
|
||||
var newpoint = [
|
||||
mapToScale(i,[0,visualizer.length-1],[15*PXSCALE,visualizer.width-15*PXSCALE],1),
|
||||
mapToScale(visualizer.data[i],[widgetData.range.min,widgetData.range.max],[visualizer.height-15*PXSCALE,15*PXSCALE],1,widgetData.logScale,true),
|
||||
]
|
||||
if (first) {
|
||||
visualizer.ctx.moveTo(newpoint[0],newpoint[1])
|
||||
first = false
|
||||
} else {
|
||||
if (widgetData.logScale) {
|
||||
visualizer.ctx.quadraticCurveTo(newpoint[0], point[1], newpoint[0], newpoint[1])
|
||||
} else {
|
||||
visualizer.ctx.lineTo(newpoint[0],newpoint[1])
|
||||
}
|
||||
|
||||
}
|
||||
point = newpoint
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
visualizer.updateData = function(){
|
||||
var id = widgetData.widgetId
|
||||
|
||||
if (typeof id == 'string' && WIDGETS[id]) {
|
||||
var v = WIDGETS[id][WIDGETS[id].length-1].getValue()
|
||||
visualizer.data.push(v)
|
||||
visualizer.value = v
|
||||
} else {
|
||||
visualizer.data.push(visualizer.value)
|
||||
}
|
||||
|
||||
visualizer.data.splice(0,1)
|
||||
|
||||
}
|
||||
|
||||
visualizer.setValue = function(v) {
|
||||
visualizer.value = v
|
||||
visualizer.startLoop()
|
||||
}
|
||||
|
||||
return visualizer.widget
|
||||
}
|
||||
|
|
@ -1,176 +0,0 @@
|
|||
var utils = require('./utils'),
|
||||
mapToScale = utils.mapToScale,
|
||||
clip = utils.clip
|
||||
|
||||
module.exports.options = {
|
||||
type:'visualizer',
|
||||
id:'auto',
|
||||
|
||||
separator1:'style',
|
||||
|
||||
label:'auto',
|
||||
left:'auto',
|
||||
top:'auto',
|
||||
width:'auto',
|
||||
height:'auto',
|
||||
color:'auto',
|
||||
css:'',
|
||||
|
||||
separator2:'plot',
|
||||
|
||||
widgetId:'',
|
||||
duration:1,
|
||||
range: {min:0,max:1},
|
||||
logScale: false,
|
||||
|
||||
separator3:'osc',
|
||||
|
||||
path:'auto',
|
||||
preArgs:[],
|
||||
}
|
||||
module.exports.create = function(widgetData,container) {
|
||||
|
||||
var widget = $(`
|
||||
<div class="plot">
|
||||
<canvas></canvas>
|
||||
</div>
|
||||
`),
|
||||
canvas = widget.find('canvas'),
|
||||
ctx = canvas[0].getContext('2d')
|
||||
|
||||
widget.height = 0
|
||||
widget.width = 0
|
||||
widget.visible = false
|
||||
widget.textColor = getComputedStyle(document.documentElement).getPropertyValue('--color-text-fade')
|
||||
widget.pips = {
|
||||
min: Math.abs(widgetData.range.min)>=1000?widgetData.range.min/1000+'k':widgetData.range.min,
|
||||
max: Math.abs(widgetData.range.max)>=1000?widgetData.range.max/1000+'k':widgetData.range.max
|
||||
}
|
||||
widget.length = Math.round(clip(60*widgetData.duration,[8,4096]))
|
||||
widget.data = new Array(widget.length)
|
||||
widget.value = widgetData.range.min
|
||||
|
||||
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-custom')
|
||||
}
|
||||
|
||||
widget.height=height
|
||||
widget.width=width
|
||||
|
||||
canvas[0].setAttribute('width',width)
|
||||
canvas[0].setAttribute('height',height)
|
||||
|
||||
requestAnimationFrame(widget.draw)
|
||||
|
||||
})
|
||||
|
||||
|
||||
widget.handleSync = function(e,id,w) {
|
||||
if (widgetData.widgetId!=id || !WIDGETS[id]) return
|
||||
widget.startLoop()
|
||||
}
|
||||
|
||||
$('body').on('sync',widget.handleSync)
|
||||
|
||||
|
||||
widget.cancel = false
|
||||
widget.loop = false
|
||||
|
||||
widget.startLoop = function(){
|
||||
|
||||
if (widget.cancel) clearTimeout(widget.cancel)
|
||||
|
||||
widget.cancel = setTimeout(function(){
|
||||
clearInterval(widget.loop)
|
||||
widget.loop = false
|
||||
widget.cancel = false
|
||||
},1000*widgetData.duration)
|
||||
|
||||
if (widget.loop) return
|
||||
|
||||
widget.loop = setInterval(function(){
|
||||
widget.fetchValue()
|
||||
widget.draw()
|
||||
},1000*widgetData.duration/widget.length)
|
||||
}
|
||||
|
||||
|
||||
widget.draw = function(){
|
||||
ctx.clearRect(0,0,widget.width,widget.height)
|
||||
ctx.beginPath()
|
||||
|
||||
var first = true
|
||||
var point = []
|
||||
|
||||
for (var i=widget.length-1;i>=0;i=i-1) {
|
||||
var newpoint = [
|
||||
mapToScale(i,[0,widget.length-1],[15*PXSCALE,widget.width-15*PXSCALE],1),
|
||||
mapToScale(widget.data[i],[widgetData.range.min,widgetData.range.max],[widget.height-15*PXSCALE,15*PXSCALE],1,widgetData.logScale,true),
|
||||
]
|
||||
if (first) {
|
||||
ctx.moveTo(newpoint[0],newpoint[1])
|
||||
first = false
|
||||
} else {
|
||||
if (widgetData.logScale) {
|
||||
ctx.quadraticCurveTo(newpoint[0], point[1], newpoint[0], newpoint[1])
|
||||
} else {
|
||||
ctx.lineTo(newpoint[0],newpoint[1])
|
||||
}
|
||||
|
||||
}
|
||||
point = newpoint
|
||||
}
|
||||
|
||||
ctx.lineWidth = 1*PXSCALE
|
||||
ctx.strokeStyle= widget.lineColor
|
||||
ctx.stroke()
|
||||
|
||||
ctx.font = PXSCALE * 10 + 'px sans-serif'
|
||||
ctx.fillStyle = widget.textColor
|
||||
|
||||
|
||||
ctx.save()
|
||||
ctx.translate(0, widget.height)
|
||||
ctx.rotate(-Math.PI/2)
|
||||
|
||||
ctx.textBaseline = "top"
|
||||
ctx.textAlign = 'left'
|
||||
ctx.fillText(widget.pips.min,12*PXSCALE,2*PXSCALE)
|
||||
ctx.textAlign = 'right'
|
||||
ctx.fillText(widget.pips.max,widget.height-10*PXSCALE,2*PXSCALE)
|
||||
ctx.rotate(Math.PI/2)
|
||||
ctx.restore()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
widget.fetchValue = function(){
|
||||
var id = widgetData.widgetId
|
||||
|
||||
if (typeof id == 'string' && WIDGETS[id]) {
|
||||
var v = WIDGETS[id][WIDGETS[id].length-1].getValue()
|
||||
widget.data.push(v)
|
||||
widget.value = v
|
||||
} else {
|
||||
widget.data.push(widget.value)
|
||||
}
|
||||
|
||||
widget.data.splice(0,1)
|
||||
|
||||
}
|
||||
|
||||
widget.setValue = function(v) {
|
||||
widget.value = v
|
||||
widget.startLoop()
|
||||
}
|
||||
|
||||
return widget
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue