logScale: clean code and accept setting a custom log base (#519)

This commit is contained in:
jean-emmanuel 2019-04-18 15:57:46 +02:00
parent 64c0825311
commit a7fd017276
10 changed files with 30 additions and 18 deletions

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', value: false, help: 'Set to `true` to use logarithmic scale for the x axis (log10)'},
logScaleY: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the y axis (log10)'}
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.'}
}, [], {

View file

@ -29,8 +29,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', value: false, help: 'Set to `true` to use logarithmic scale for the x axis (log10)'},
logScaleY: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the y axis (log10)'},
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.'},
input: {type: 'boolean', value: true, help: 'Set to false to hide the built-in input widget'},
doubleTap: {type: 'boolean|string', value: false, help: [
'Set to `true` to make the fader reset to its default value when receiving a double tap.',

View file

@ -20,7 +20,7 @@ module.exports = class Eq extends _plots_base {
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'},
logScaleX: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the x axis (log10)'},
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.'},
smooth: {type: 'boolean|number', value: false, help: 'Set to `true` to make the line smooth. Float values are also acceptable (works fine between `0` and `0.5`)'},
}, ['target', 'precision', 'bypass'], {

View file

@ -11,7 +11,7 @@ module.exports = class Led extends Widget {
_led:'led',
range: {type: 'object', value: {min:0,max:1}, help: 'Value to led intensity mapping range'},
logScale: {type: 'boolean', value: false, help: 'Set to `true` to use a logarithmic mapping scale)'},
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.'},
}, ['target', 'precision', 'bypass'], {})

View file

@ -17,8 +17,8 @@ module.exports = class Plot extends _plots_base {
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', value: false, help: 'Set to `true` to use logarithmic scale for the x axis (log10)'},
logScaleY: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the y axis (log10)'},
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.'},
origin: {type: 'number', value: 'auto', help: 'Defines the y axis origin. Set to `false` to disable it.'},
dots: {type: 'boolean', value: true, help: ''},
bars: {type: 'boolean', value: false, help: 'Set to `true` to use draw bars instead (disables `logScaleX` and forces `x axis` even spacing)'},

View file

@ -18,7 +18,7 @@ module.exports = class Visualizer extends _plots_base {
duration: {type: 'number', value: 1, help: 'Defines visualization duration in seconds'},
range: {type: 'object', value: {min:0,max:1}, help: 'Defines the min and max values for the y axis'},
origin: {type: 'number', value: 'auto', help: 'Defines the y axis origin. Set to `false` to disable it'},
logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the y axis'},
logScale: {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.'},
smooth: {type: 'boolean|number', value: false, help: 'Set to `true` to make the line smooth. Float values are also acceptable (works fine between `0` and `0.5`)'},
pips:{type: 'boolean', value: true, help: 'Set to `false` to hide the scale'},

View file

@ -40,7 +40,7 @@ module.exports = class Fader extends Slider {
'- `number`: define a number of evenly spaced steps',
'- `array`: use arbitrary values',
]},
logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale'},
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.'},
unit: {type: 'string', value: '', help: 'Unit will be appended to the displayed widget\'s value (it doesn\'t affect osc messages)'},
origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the fader\'s gauge'},

View file

@ -31,7 +31,7 @@ module.exports = class Knob extends Slider {
'- `number`: define a number of evenly spaced steps',
'- `array`: use arbitrary values',
]},
logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale'},
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.'},
unit: {type: 'string', value: '', help: 'Unit will be appended to the displayed widget\'s value (it doesn\'t affect osc messages)'},
origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the knob\'s gauge'},

View file

@ -12,19 +12,29 @@ module.exports = {
// map a value from a scale to another input and output must be range arrays
mapToScale: function(value,rangeIn,rangeOut,precision,log,revertlog) {
// clip in
value = module.exports.clip(value,[rangeIn[0],rangeIn[1]])
value = log ?
revertlog ?
(Math.pow(10,((value-rangeIn[0])/(rangeIn[1]-rangeIn[0])))/9-1/9) * (rangeOut[1]-rangeOut[0]) + rangeOut[0]
:Math.log10(((value-rangeIn[0])/(rangeIn[1]-rangeIn[0]))*9+1) * (rangeOut[1]-rangeOut[0]) + rangeOut[0]
:((value-rangeIn[0])/(rangeIn[1]-rangeIn[0])) * (rangeOut[1]-rangeOut[0]) + rangeOut[0]
// normalize
value = (value - rangeIn[0]) / (rangeIn[1] - rangeIn[0])
value = Math.max(Math.min(rangeOut[0],rangeOut[1]),Math.min(value,Math.max(rangeOut[0],rangeOut[1])))
// log scale
if (log) {
if (log === true) log = 10
value = revertlog ?
Math.pow(log, value) / (log - 1) - 1 / (log - 1) :
Math.log(value * (log - 1) + 1) / Math.log(log)
}
// scale out
value = value * (rangeOut[1] - rangeOut[0]) + rangeOut[0]
// clip out
value = module.exports.clip(value,[rangeOut[0],rangeOut[1]])
// precision
if (precision !== false) value = parseFloat(value.toFixed(precision))
return value
},