Commit b2ca613d authored by Stephanus Volke's avatar Stephanus Volke

fix a lot of bugs in gain node class

parent 24aa5186
......@@ -3,83 +3,84 @@ import {AudioNodeInterface} from './audio-node-interface.js';
export class HTMLAudioGain extends AudioNodeInterface {
/**
* The HTMLAudioSource class is a custom element projecting the
* AudioSource interface of the WebAudio API into an html tag
* `<html-audio-source>`. It handles the playback of a specific audio
* buffer. There is no hard dependenciy to any other element but
* assumes that there also exist an `<html-audio-buffer>` element in
* the same document.
* The HTMLAudioGain class is a custom element projecting the
* AudioGainNode interface of the WebAudio API into an html tag
* `<html-audio-gain>`.
*
* ## Usage
*
* ``` html
* <html-audio-source id="source"
* buffer-id="buffer"
* state="playing"></html-audio-buffer>
* <html-audio-gain value="-20"
* unit="db"></html-audio-gain>
* ```
*
* >! Note: It is required to specify an `id` and a `buffer-id` attribute for this element.
*
* ## API Reference
*
* ### Properties
*
* - **id**{string}: unique identifyer for this element. Access:
* (attr|api) *Required*
*
* - **buffer-id**{string}: identifyer of a `html-audio-buffer`
* element. Access: (attr|api) *Required*
*
*
* - **state**{string}: current state of audio source. Can be
* {waiting, ready, playing, paused, stopped}. This attribute
* controls audio playback. Access: (attr|api)
*
* - **start-pos**{float}: playback start position in seconds. Access: (attr|api)
*
* - **cur-pos**{float}: while playback this attribute shows the
* current playback position. Access: (attr|api) *read only*
*
* - **exclusive**{bool}: If this bool attribute is set, all other
* sources were stopped before playback starts. Access: (attr|api)
*
* ### Methods
*
* - **play()**: Starts playback at `start-pos`.
*
* - **pause()**: Playback pauses at `cur-pos`.
*
* - **stop()**: Playback stopps and `cur-pos` will be set to 0.
*/
constructor() {
super();
this.isSourceNode = false;
this._value = 1;
this._unit = 'lin';
}
static get observedAttributes() {
return ["value", "disabled"];
return super.observedAttributes.concat(["value", "unit"]);
}
onNodeSet() {
this._node.gain.value = this._value;
}
connectedCallback() {
super.connectedCallback();
this.node = this._context.createGain();
this.value = this._value;
}
attributeChangedCallback(attrName, oldVal, newVal) {
super.attributeChangedCallback(attrName, oldVal, newVal);
switch (attrName) {
case 'value':
this._value = newVal;
if (this._node) {
this._node.gain.value = parseFloat(this._value, 10);
if (this._unit == 'lin') {
this._value = parseFloat(parseFloat(newVal, 10).toFixed(4));
} else if (this._unit == 'db') {
this._value = parseFloat((Math.pow(10, parseFloat(newVal, 10)/20).toFixed(4)));
}
this._updateGain();
break;
case 'unit':
if (newVal == 'lin') {
this._unit= 'lin';
this.value = this._value;
} else if (newVal == 'db') {
this._unit= 'db';
if (this._value == 0) {
this.value = -100;
} else {
this.value = parseFloat((Math.log10(this._value)*20).toFixed(4));
}
}
this._updateGain();
break;
}
}
_updateGain() {
if (this._node) {
this._node.gain.value = this._value;
}
}
get value() {
if (this._unit == 'db') {
return 20*Math.log10(this._value);
}
return this._value;
}
......@@ -87,9 +88,13 @@ export class HTMLAudioGain extends AudioNodeInterface {
this.setAttribute('value', v);
}
get unit() {
return this._type;
}
set unit(v) {
this.setAttribute('type', v);
}
}
window.customElements.define('html-audio-gain', HTMLAudioGain);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment