webaudio-controls

June 8, 2017 ยท View on GitHub

WebAudioControls is GUI parts library for Web application using [Polymer] WebComponents.
Especially suitable for audio-applications like VST plugins.

Polymer 1.4 compatible

WebAudioControls is consist of following components

ComponentDescription
webaudio-knobRotating or some other frame-by-frame animation knob
webaudio-sliderVertical or Horizontal slider
webaudio-switchToggle/Kick/Radio switches
webaudio-paramEditable value display field
webaudio-keyboardMouse/Touch playable keyboard. multi-touch support.

Chrome / Firefox / Safari / Opera / IE compatible
iOS and Android touch devices compatible

Live Demo sample1 (with external image-files)
Live Demo sample2 (with code example)
Live Demo sample3 (Knob/Slider/Switch/Param/Keyboard default style)
Live Demo sample4 (webaudio-keyboard to webMIDIAPI)
(need Mac+ChromeCanary+flagEnabled+MIDIdevice or Win+JazzPlugin or Mac+JazzPlugin+MidiDevice)
Renoid : Practical application using webaudio-controls
webaudio-controls Resize Test

Using with external image-files.

Default style with no external image-files.

To Operate

Following user actions are supported.

OperationComponentDescription
ClickSwitch/Paramtoggle / activate the switch or focus the param
DragKnob/Sliderup/right to increase value / down/left to decrease value
Shift+DragKnob/Sliderfine control
Ctrl+Click / Command+Click(Mac)Knob/Slider/Switchset to default value
KeyboardParam/Keyboardedit the param value directly / [ZSXDCV...for lowest visible 'C' octave] and [Q2W3E... one octave higher] as a music keyboard
MouseWheelKnob/Sliderrotate upward to increase value / downward to decrease value
Shift+MouseWheelKnob/Sliderfine control
Mouse Button Press / TouchKeyboardplay keyboard. multi-touch is supported

How to use

  • Install
  • Use command bower install --save Polymer/polymer, if you use bower. Or download zipped file and deploy appropriately.
  • webaudio-controls also can be installed with : bower install --save g200kg/webaudio-controls
  • load webcomponents.js and polymer

    • <script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
      <link rel="import" href="bower_components/polymer/polymer.html">
  • load webaudio-contols

    • <link rel="import" href="bower_components/webaudio-controls/webaudio-controls.html" >
  • insert webaudio-knob/slider/switch/param/keyboard elements. for example...

  • <webaudio-knob src="img/LittlePhatty.png" sprites="100" min="0" max="100"></webaudio-knob>
  • <webaudio-slider src="img/hsliderbody.png"></webaudio-slider>
  • <webaudio-switch src="img/switch_toggle.png" width="32" height="32"></webaudio-switch>
  • <webaudio-param src="" link="knob-1"></webaudio-param>
  • <webaudio-keyboard keys="25" ></webaudio-keyboard>

Attributes

webaudio-knob

AttributeOptionsDefaultDescription
srcstringInternal embedded resource is used if not specifiedurl of the knob image. (single frame or vertical stitched)
valuefloat0The current value. Also used as initial value if specified
defvaluefloatInitial 'value' is used if not specifiedThe default value that will be used when ctrl+click
minfloat0Minimum value of the knob
maxfloat100Maximum value of the knob
stepfloat1Value step of the control. The 'value' is always rounded to multiple of 'step'
logint0If 1, knob scale is logalithmic. In this mode, step is ignored.
unitsstringnullspecified units (e.g. Hz) is added to valuetip
widthint64Knob display width in px
heightint64Knob display height in px
diameterint64Knob display diameter in px. This attribute can be used instead of width / height if the display image is square
spritesint0if 0, the src image should be single frame image that indicate middle position. the image will be rotated -135deg to +135deg. if sprirites is not 0, the src image should be stitched multi-framed image. sprites specify the max frame number in the stitched knob image. Note that this is (number of frames) - 1
sensitivityfloat1Pointing device sensitivity. min-max range correspond to (128 / sensitivity) px
valuetip0,11Enable the overlaid value-tip display.
tooltipstringnullTooltip text that will be shown when mouse hover a while
enable0,11Enable control with the pointing device.
colorsstring"#e00;#000;#000"Semicolon separated 3 colors for 'indicator', 'body' and 'highlight'. These colors are used in default knob (when src is not provided).
midilearnstringnullIf true, MIDI learn function with right-click menu is enabled.
midiccstringnullAssign MIDI control change to this knob. with format ch.cn, here the ch is channel (1-16, ignore channel if 0) and cn is control number (0-119).

webaudio-slider

AttributeOptionsDefaultDescription
srcstringInternal embedded resource is used if not specifiedurl of the slider background image
knobsrcstringInternal embedded resouce is used if not specifiedurl of the slider knob part image
valuefloat0The current value. Also used as initial value if specified
defvaluefloatInitial 'value' is used if not specifiedThe default value that will be used when ctrl+click
minfloat0Minimum value of the slider
maxfloat100Maximum value of the slider
stepfloat1Value step of the control. The 'value' is always rounded to multiple of 'step'
widthint24Slider display width in px
heightint128Slider display height in px
knobwidthintsame as 'width' if 'direction' is vert, or same as 'height' if 'direction' is horzSlider knob part width in px
knobheightintsame as 'width' if 'direction' is vert, or same as 'height' if 'direction' is horzSlider knob part height in px
ditchlengthint('height'-'knobheight') or ('width'-'knobwidth') depends on 'direction'Knob movable length
direction"vert","horz""vert"Slider direction. vertical or horizontal
sensitivityfloat1Pointing device sensitivity. min-max range correspond to (128 / 'sensitivity') px
valuetip0,11Enable the overlaid value-tip display.
tooltipstringnullTooltip text that will be shown when mouse hover a while
enable0, 11Enable control with the pointing device.
colorsstring"#e00;#000;#fff"Semicolon separated 3 colors for 'knob', 'background' and 'highlight'. These colors are used in default knob (when src or knobsrc is not provided).
midilearnstringnullIf true, MIDI learn function with right-click menu is enabled.
midiccstringnullAssign MIDI control change to this slider. with format ch.cn, here the ch is channel (1-16, ignore channel if 0) and cn is control number (0-119).

webaudio-switch

AttributeOptionsDefaultDescription
srcstringInternal embedded resource is used if not specifiedurl of the vertical stitched switch image
value0,10The current value (0 or 1). Also used as initial value of the switch if specified
defvalue0,1Initial 'value' is used if not specifiedThe default value that will be used when ctrl+click
widthint32Switch display width in px
heightint32Switch display height in px
type"toggle","kick","radio""toggle"Switch type. "toggle" switch has so-called 'checkbox' function. "radio" switch is a radio-button and the "kick" switch is a general command button
groupstringnullGroup id string used if the 'type' is "radio". Only one switch will be set to "1" in same group
invert0,10exchange on and off image
tooltipstringnullTooltip text that will be shown when mouse hover a while
enable0,11Enable control with the pointing device.
colorsstring"#e00;#000;#fff"Semicolon separated 3 colors for 'knob', 'background' and 'highlight'. These colors are used in default switch (when src is not provided).
midilearnstringnullIf true, MIDI learn function with right-click menu is enabled.
midiccstringnullAssign MIDI control change to this switch. with format ch.cn, here the ch is channel (1-16, ignore channel if 0) and cn is control number (0-119).

webaudio-param

AttributeOptionsDefaultDescription
srcstringBlack rectangle if not specifiedBackground image or color. Transparent if set to "", or url to background image.
valuefloat0The current value. Usually same as linked control
widthint32Parameter display width in px
heightint16Parameter display height in px
fontsizeint9Font-size of the parameter display
colorsstring"#e00;#000"Semicolon separated 2 colors for text and background. background color is used when src is not defined.
linkstringnullSpecify the linked webaudio-knob/slider/switch by Id

webaudio-keyboard

AttributeOptionsDefaultDescription
valuesint array[]The array of current pressed key numbers. "values" may has more than one element in multi-touch environment.
widthint480Keyboard display width in px
heightint128Keyboard display height in px
minint0Lowest Key number. Each key is numbered incrementally from this number. If the "min" is not 0 and the modulo 12 is not zero, the keyboard is started from corresponding position (not-C). Note that the specified key should be a 'white-key'.
keysint25Number of keys. 25 means 25 keys keyboard.
colorsstring'#222; #eee;#ccc; #333;#000; #e88;#c44; #c33;#800'semicolon separated 9 keyboard colors. 'border; whitekey-grad-from;whitekey-grad-to; blackkey-grad-from;blackkey-grad-to; active-whitekey-grad-from;active-whitekey-grad-to; active-blackkey-grad-from;active-blackkey-grad-to'. Each key surface can has garadient left to right with 'from' and 'to'.
enable0,11Enable control with the pointing device.

Functions

setValue(value, fire)

webaudio-knob | webaudio-slider | webaudio-switch
description: Each control can be setup and redraw by calling this function from JavaScript. If the fire parameter is undefined or false, this function will not fire 'change' event. Or the change event will be fired.

setNote(state,note)

webaudio-keyboard
description: webaudio-keyboard can be setup pressing state with this function from JavaScript. corresponding key specified by the note is pressed if the state is non-zero otherwise the key is released. This function will NOT fire the 'change' event.


Events

'change'

webaudio-knob | webaudio-slider | webaudio-switch | webaudio-keyboard
description: 'change' event is emitted everytime value changes by user action or setValue() function with fire flag is true. In the event handler of webaudio-knob,webaudio-slider or webaudio-switch, current value can be get with referring event.target.value.

var knobs = document.getElementsByTagName('webaudio-knob');
for (var i = 0; i < knobs.length; i++) {
  var knob = knobs[i];
  knob.addEventListener('change', function(e) {
    console.log(e.target.value);
  });
}

For the webaudio-keyboard, each 'change' event has the property '.note' that contain a array [key-state, key-number]. For example event.note = [1, 60] if the key#60 is on, or event.note = [0, 60] if the key#60 is off.

var keyboard = document.getElementsById('keyboard');
keyboard.addEventListener('change', function(e) {
	if(e.note[0])
		console.log("Note-On:"+e.note[1]);
	else
		console.log("Note-Off:"+e.note[1]);
});

Note: The addEventListener() function is recommended for event handler setup instead of 'onchange=' attribute. 'onchange=' attribute seems not work on Safari.

'click'

webaudio-switch (kick)
description: 'click' event is emitted if the 'kick' type webaudio-switch has clicked.


Creating knob images

webaudio-knob (with sprites is 0 (default)) use a single frame knob image that indicate center position. For example,

This image will be rotated from -135deg to +135deg. This approach will works well if the image is flat designed, but more complex animation (for example, drop-shadowed, highlighted or something elastic) will need pre-rendered frame-by-frame animation as below.

webaudio-knob (with non zero "sprites") use a vertical 'stitched' multi-frames animation image, and webaudio-switch use a vertical 'stitched' two-frames animation image. For example,

This knob example has only 5 frames but it should has more frames for smooth animation. I recommend to use JKnobMan/WebKnobMan for making these stitched images,


Here is a brief instruction to export knob-image from KnobGallery

  • Go to KnobGallery
  • Find your favorite knob design and click 'Open with WebKnobMan'
  • Click on 'Export' to download png file
  • Of course, you can create your own!

Note: comply with license requirements


MIDI support: knobs, sliders and switches have midilearn/midicc support built-in

To enable MIDI related functions, add the following line before the <link> tag that loads webaudio-controls.html

<script>UseWebAudioControlsMidi = 1</script>

Added a midilearn menu to knobs, sliders and switches by @micbuffa and @CellouBalde.

Midilearn right click menu: add a midilearn=true attribute to the <webaudio-knob>, <webaudio-slider> and <webaudio-switch> elements. Then right click on the element in the GUI, a midi learn menu should appear. Then, operate one of your midi controller and it should start actionning the webaudiocontrol widget in the HTML page. You can associate more than one controller to each widget. You can hot plug/unplug midi devices, they will be detected.

Midi Learn Menu

Declarative association between a midi controller and a GUI webaudiocontrol: There is also an HTML midicc="channel.cc#" attribute that works like this: midicc="3.2" means "listen to a cc event on channel 3, cc number 2". If you don't know the channel/cc number of your controller: 1) add a midilearn=true attribute so that a right click on the GUI widget will display the midilearn menu, 2) select "learn" in the menu, 3) operate your knob/slider/switch, normally the midi controller and the GUI object are in sync. 4) look at the devtool console, there is a message indicating the channel and cc number, for example "channel 0, cc 28". Then if you add the attribute midicc="0.28" to the HTML of your knob/slider/switch, the midi mapping between your GUI webaudiocontrol and your midi controller will be automatic. Follow the links at the end of this section and look at the HTML source code to see some examples.

Example: associate a knob with a controller on channel 7, cc number 7:

<webaudio-knob midilearn=true midicc="7.7" ...></webaudio-knob>

External midi event listener (hook): you can also declare in your HTML file your own midi event listener (for example for listening to program changes events): use the webAudioControlsMidiManager object, that comes with an addMidiListener method. Like that you will benefit from the MIDI code included in the webaudiocontrols. Here is an example (also, look at the source code of the Sample1.html demo, and open the devtool console to see midi messages received by the hook at the end of the HTML file).

<script>
// add this to your html page that uses webaudiocontrols
webAudioControlsMidiManager.addMidiListener(function(event) {
    var data = event.data;
    var channel = data[0] & 0xf;
    var controlNumber = data[1];

    console.log("Midi event hook: data:[" + data + "] channel:" + channel + " cc:"+controlNumber);

    // do whatever you want with the event
    // ...
});
</script>

Demo at: https://wasabi.i3s.unice.fr/AmpSimFA/ and at https://wasabi.i3s.unice.fr/AmpSimFA/sample1.html


License

WebAudio-Controls is developped based on:

Copyright (c) 2013 Eiji Kitamura / Ryoya KAWAI / Keisuke Ai / g200kg (Tatsuya Shinyagaito)
Licensed under the Apache License, Version 2.0