-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanner.js
More file actions
74 lines (61 loc) · 1.96 KB
/
panner.js
File metadata and controls
74 lines (61 loc) · 1.96 KB
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
import {define, html} from "./hybrids/index.js";
import * as audio from "./audio.js";
import * as element from "./element.js";
import * as ui from "./ui.js";
// this helps to define a reasonable order and provides some defaults for step
const defaults = {
x: {ui: {heading: "cartesian coordinates"}, default: 0, step: 1},
y: {default: 0, step: 1},
z: {default: 0, step: 1},
r: {ui: {heading: "polar coordinates"}, default: 0, step: 0.1},
a_xy: {default: 0, step: 0.1},
a_xz: {default: 0, step: 0.1},
orientationX: {ui: {heading: "orientation"}, default: 0, step: 0.1},
orientationY: {default: 0, step: 0.1},
orientationZ: {default: 0, step: 0.1},
innerAngle: {ui: {heading: "directionality constraints"}, default: 360},
outerAngle: {default: 360},
outerGain: {default: 0},
distanceModel: {ui: {heading: "distance parameters"}, default: "inverse", values: ["inverse", "linear"]},
refDistance: {default: 50},
maxDistance: {default: 10000},
rolloffFactor: {ui: {row: true}, default: 1},
panningModel: {default: "HRTF", type: "list", values: ["HRTF", "equalpower"]},
};
const Panner = element.create("panner", defaults, audio.context.createPanner(), [
["x", "positionX"],
["y", "positionY"],
["z", "positionZ"],
["innerAngle", "coneInnerAngle"],
["outerAngle", "coneOuterAngle"],
["outerGain", "coneOuterGain"],
], {
r: {
observe: (host, value) => {
[host.x, host.y, host.z] = toPolar(host.r, host.a_xy, host.a_xz);
}, // set
connect: element.connect,
}, // r
a_xz: {
connect: element.connect,
observe: (host, value) => {
[host.x, host.y, host.z] = toPolar(host.r, host.a_xy, host.a_xz);
}, // observe
}, // a_xz
a_xy: {
connect: element.connect,
observe: (host, value) => {
[host.x, host.y, host.z] = toPolar(host.r, host.a_xy, host.a_xz);
}, // observe
}, // a_xy
}); // element.create
define ("audio-panner", Panner);
function toPolar (r, theta, phi) {
const cos = Math.cos;
const sin = Math.sin;
return [ // x,y,z
r * cos(theta) * sin(phi),
r * sin(theta) * sin(phi),
r * cos(phi)
];
} // toPolar