logScale: reverse logScale logic (to the expected one) and allow exponential if logScale == -1

eq: reimplement using native audio node biquad filters
sidepanel-right: prevent 1px border-right from messing with scrollbar interaction
This commit is contained in:
bordun 2020-03-21 19:21:08 +01:00
parent b07158008e
commit 81f187c088
8 changed files with 93 additions and 170 deletions

View file

@ -1,10 +1,12 @@
var {clip} = require('../utils'),
var {clip, mapToScale} = require('../utils'),
Plot = require('./plot'),
Widget = require('../common/widget'),
StaticProperties = require('../mixins/static_properties')
StaticProperties = require('../mixins/static_properties'),
audioContext = new AudioContext(),
MAXFREQ = 0.5 * audioContext.sampleRate
class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22000}, logScaleX: false}) {
class Eq extends StaticProperties(Plot, {logScaleX: false, logScaleY:false}) {
static description() {
@ -20,16 +22,17 @@ class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22000}, logScale
filters: {type: 'array', value: '', help: [
'Each item must be an object with the following properties',
'- `type`: string ("highpass", "highshelf", "lowpass", "lowshelf", "peak" or "notch")',
'- `type`: string ("highpass", "highshelf", "lowpass", "lowshelf", "peaking", "bandpass" or "notch")',
'- `freq`: number (filter\'s resonant frequency)',
'- `q`: number (Q factor)',
'- `gain`: number',
'- `on`: boolean',
'See https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode'
]},
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'},
rangeX: {type: 'object', value: {min: 20, max: 22000}, help: 'Defines the min and max values for the x axis (in Hz, logarithmic scale)'},
rangeY: {type: 'object', value: {min:-6, max:6}, help: 'Defines the min and max values for the y axis (in dB)'},
origin: {type: 'number|boolean', value: 'auto', help: 'Defines the y axis origin. Set to `false` to disable it'},
}, ['interaction', 'decimals', 'typeTags', 'bypass'])
@ -40,39 +43,84 @@ class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22000}, logScale
super(options)
this.calcFilters()
setTimeout(()=>{
this.createFilters()
this.calcResponse()
})
}
calcFilters() {
createFilters() {
this.filters = []
var filters = this.getProp('filters')
for (let i in filters) {
this.filters[i] = new BiquadFilterNode(audioContext, {
Q: filters[i].q,
type: filters[i].type,
frequency: filters[i].freq,
gain: filters[i].gain,
})
this.filters[i]._on = filters[i].on
}
}
calcResponse() {
/*
Based on view-source:http://webaudio-io2012.appspot.com/frames/frequency-response.html © Google 2010
*/
var resolution = this.width,
frequencyHz = new Float32Array(resolution),
phaseResponse = new Float32Array(resolution),
nOctaves = 11,
rangeXIn = [MAXFREQ * Math.pow(2.0, nOctaves * -1.0), MAXFREQ],
rangeXOut = [this.getProp('rangeX').min, this.getProp('rangeX').max]
for (let i = 0; i < resolution; ++i) {
var f = i / resolution
// Convert to log frequency scale (octaves).
// f = MAXFREQ * Math.pow(2.0, nOctaves * (f - 1.0))
f = mapToScale(i, [0,this.width], rangeXOut, -1, true)
frequencyHz[i] = f
}
var filters = this.getProp('filters'),
resolution = Math.min(parseInt(this.getProp('resolution')) || 64, 1024),
responses = [],
eqResponse = []
for (let i in filters) {
for (let filter of this.filters) {
var filterResponse
if (filter._on === false) continue
if (!filters[i].type) filters[i].type = 'peak'
var filterResponse = new Float32Array(resolution);
if (!filters[i].on) {
filterResponse = calcBiquad({type:'peak',freq:1,gain:0,q:1}, resolution)
} else {
filterResponse = calcBiquad(filters[i], resolution)
}
filter.getFrequencyResponse(frequencyHz, filterResponse, phaseResponse);
for (var k in filterResponse) {
if (eqResponse[k] === undefined) {
eqResponse[k] = [0,0]
}
responses.push(filterResponse)
eqResponse[k] = [filterResponse[k][0], eqResponse[k][1]+filterResponse[k][1]]
}
for (let i = 0; i < resolution; ++i) {
eqResponse[i] = 0
for (let r of responses) {
eqResponse[i] += 20.0 * Math.log(r[i]) / Math.LN10
}
}
this.setValue(eqResponse, {sync: true})
this.setValue(eqResponse)
}
@ -82,10 +130,9 @@ class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22000}, logScale
super.onPropChanged(...arguments)
switch (propName) {
case 'resolution':
case 'filters':
this.calcFilters()
this.createFilters()
this.calcResponse()
return
}
@ -95,139 +142,7 @@ class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22000}, logScale
}
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,
minVal, maxVal,
len = resolution
var V = Math.pow(10, Math.abs(gain) / 20)
var K = Math.tan(Math.PI * freq / 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 (gain >= 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 (gain >= 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 (gain >= 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 magPlot = []
for (var i = 0; i < len; i++) {
var w, phi, y
// if (linear) w = i / (len - 1) * Math.PI // 0 to pi, linear scale
w = Math.exp(Math.log(1 / 0.001) * i / (len - 1)) * 0.001 * Math.PI // 0.001 to 1, times pi, log scale
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
magPlot.push([i / (len - 1) * Fs / 2, y])
if (i == 0)
minVal = maxVal = y
else if (y < minVal)
minVal = y
else if (y > maxVal)
maxVal = y
}
return magPlot
}

View file

@ -63,7 +63,6 @@ module.exports = class Plot extends StaticProperties(Canvas, {bypass: true, inte
max: Math.abs(this.rangeY.max)>=1000?this.rangeY.max/1000+'k':this.rangeY.max
}
}
this.smooth = this.getProp('smooth') === true ? 0.25 : this.getProp('smooth') === false ? 0 : parseFloat(this.getProp('smooth')) || 0
}

View file

@ -38,8 +38,8 @@ module.exports = class MultiXy extends Pad {
pips: {type: 'boolean', value: true, help: 'Set to `false` to hide the scale'},
rangeX: {type: 'object', value: {min:0,max:1}, help: 'Defines the min and max values for the x axis'},
rangeY: {type: 'object', value: {min:0,max:1}, help: 'Defines the min and max values for the y axis'},
logScaleX: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the x axis (base 10). Set to a `number` to define the logarithm\'s base.'},
logScaleY: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the y axis (base 10). Set to a `number` to define the logarithm\'s base.'},
logScaleX: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the x axis. Set to `-1` for exponential scale.'},
logScaleY: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the y axis. Set to `-1` for exponential scale.'},
sensitivity: {type: 'number', value: 1, help: 'Defines the pad\'s sensitivity when `snap` is `false` '},
}, [], {

View file

@ -28,8 +28,8 @@ module.exports = class Xy extends Pad {
pips: {type: 'boolean', value: true, help: 'Set to `false` to hide the scale'},
rangeX: {type: 'object', value: {min:0,max:1}, help: 'Defines the min and max values for the x axis'},
rangeY: {type: 'object', value: {min:0,max:1}, help: 'Defines the min and max values for the y axis'},
logScaleX: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the x axis (base 10). Set to a `number` to define the logarithm\'s base.'},
logScaleY: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the y axis (base 10). Set to a `number` to define the logarithm\'s base.'},
logScaleX: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the x axis. Set to `-1` for exponential scale.'},
logScaleY: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the y axis. Set to `-1` for exponential scale.'},
doubleTap: {type: 'boolean|string', value: false, help: [
'Set to `true` to make the fader reset to its default value when receiving a double tap.',
'Can also be an osc address, which case the widget will just send an osc message: `/<doubleTap> <preArgs>`'
@ -81,7 +81,7 @@ module.exports = class Xy extends Pad {
})
this.value = []
touchstate(this, {element: this.canvas})
this.active = false

View file

@ -40,7 +40,7 @@ module.exports = class Fader extends Slider {
'- values can be `number` or `object` if a custom label is needed',
'Example: (`{min:{"-inf": 0}, "50%": 0.25, max: {"+inf": 1}}`)'
]},
logScale: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale (base 10). Set to a `number` to define the logarithm\'s base.'},
logScale: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale. Set to `-1` for exponential scale.'},
sensitivity: {type: 'number', value: 1, help: 'Defines the fader\'s sensitivity when `snap` is `false` '},
steps: {type: 'string|number|array', value: '', help: [
'Restricts the widget\'s value:',

View file

@ -35,7 +35,7 @@ module.exports = class Knob extends Slider {
'- values can be `number` or `object` if a custom label is needed',
'Example: (`{min:{"-inf": 0}, "50%": 0.25, max: {"+inf": 1}}`)'
]},
logScale: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale (base 10). Set to a `number` to define the logarithm\'s base.'},
logScale: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale. Set to `-1` for exponential scale.'},
sensitivity: {type: 'number', value: 1, help: 'Defines the knob\'s sensitivity when `mode` is not `snap` '},
steps: {type: 'string|number|array', value: '', help: [
'Restricts the widget\'s value:',

View file

@ -24,15 +24,22 @@ module.exports = {
// clip in
value = module.exports.clip(value,[rangeIn[0], rangeIn[1]])
// normalize
value = (value - rangeIn[0]) / (rangeIn[1] - rangeIn[0])
// log scale
if (log) {
if (log === true) log = 10
if (log < 0) revertlog = !revertlog
var logScale = revertlog ? Math.abs(rangeIn[1] - rangeIn[0]) / 10 :
Math.abs(rangeOut[1] - rangeOut[0]) / 10
value = revertlog ?
Math.pow(log, value) / (log - 1) - 1 / (log - 1) :
Math.log(value * (log - 1) + 1) / Math.log(log)
Math.log(value * (logScale - 1) + 1) / Math.log(logScale) :
Math.pow(logScale, value) / (logScale - 1) - 1 / (logScale - 1)
}
// scale out

View file

@ -302,7 +302,9 @@ osc-panel-content {
overflow-y: auto;
&.left, &.right {
border-right: 1rem solid var(--color-ui-background);
&::-webkit-scrollbar-thumb, &::-webkit-scrollbar-track-piece {
border-right: 1rem solid var(--color-ui-background);
}
}