-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio-node.html
160 lines (156 loc) · 3.96 KB
/
audio-node.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-shadow/paper-shadow.html">
<link rel="import" href="../core-tooltip/core-tooltip.html">
<polymer-element name="audio-node">
<template>
<style>
:host{
display:block;
background:inherit;
}
#canvas{
border-radius: 50%;
box-shadow: 0 0 25px black inset;
background:inherit;
}
</style>
<core-tooltip label="{{node.constructor.name}}">
<canvas id="canvas" width="{{size}}" height="{{size}}" style="background:{{color}};"></canvas>
</core-tooltip>
</template>
<script>Polymer((function static(){
const SFX = window["::sfx::"];
const ACCESSORS={
$CTX:{
get:function(){
return this.$.canvas.getContext("2d");
}
},
$INPUTS:{
get:function(){
return this.node.numberOfInputs;
}
},
$OUTPUTS:{
get:function(){
return this.node.numberOfOutputs;
}
},
$_output:{
get:function(){
return this.parentElement;
},
set:function(HTMLElement){
node.appendChild(this);
},
},
$_input:{
get:function(){
return Array.prototype.slice.call(this.children,0,this.$INPUTS);
},
set:function(HTMLElementCollection){
Array.prototype.slice.call(HTMLElementCollection).forEach(function(e){
this.appendChild(e);
});
},
},
$_chanelCount:{
get:function(){
return this.node.chanelCount;
},
set:function(v){
this.node.chanelCount=v;
}
},
$_chanelCountMode:{
get:function(){
return this.node.chanelCountMode;
},
set:function(v){
this.node.chanelCountMode=v;
}
},
$_chanelInterpretation:{
get:function(){
return this.node.chanelInterpretation;
},
set:function(v){
this.node.chanelInterpretation=v;
}
}
};
const METHODS={
connect:{
value:function(target){
if(this.visualize){
this.node.connect(this.analyser);
if(target.visualize){
this.analyser.connect(target.analyser);
target.analyser.connect(target.node);
}else{
this.analyser.connect(target.node);
}
}else this.node.connect(target.node);
return this;
}
},
disconnect:{
value:function(){
this.node.disconnect();
return this;
}
}
};
const PROTOTYPE = {
publish:{
size:75,
visualize:true,
controls:true,
color:"white",
},
observe:{
},
created:function(){
},
ready:function(){
this.startVisualizationLoop();
},
domReady:function(){
},
startVisualizationLoop:function(){
this.analyser = SFX.createAnalyser();
this.timeDomain = new Uint8Array(this.analyser.frequencyBinCount);
const C = this.$CTX;
const A = this.analyser;
const T = this.timeDomain;
const S = this.size;
var i,n;
C.strokeStyle = "white";
C.lineJoin = "round";
C.lineWidth = Math.sqrt(S)/5;
drawLoop();
function drawLoop(){
A.getByteTimeDomainData(T);
C.clearRect(0,0,S,S);
C.beginPath();
C.moveTo(S/2,-S/25);
for(i = 0; i<S; i++){
n = (T.length / S * i)>>0;
C.lineTo(T[n%T.length]/256*S*.5+S/4,i);
}
C.stroke();
requestAnimationFrame(drawLoop);
}
},
attached:function(){
},
detached:function(){
}
};
Object.defineProperties(PROTOTYPE,ACCESSORS);
Object.defineProperties(PROTOTYPE,METHODS);
return PROTOTYPE;
})());
/*
*/</script>
</polymer-element>