-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.js
121 lines (96 loc) · 2.73 KB
/
music.js
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
// plays a positional audio
// todo: add many files, play sequentally, shuffled, or one random (add mode for this).
// idea: add visual body, probably osciallating, activated by checkbox? or is it another component?
export function create( vz, opts ) {
var obj = vz.create_obj( {}, opts );
obj.feature("enabled");
obj.addCmd( "play/stop",function() {
if (starting) return;
if (obj.isplaying())
obj.stop();
else
obj.play();
});
obj.addCheckbox("autoplay",false,function() {
if (obj.getParam("autoplay")) {
setTimeout( function() {
obj.play();
},100 ); // timeout to reflect file change
}
});
obj.addFile("file","//viewlang.ru/assets/sounds/Maxim_Fadeev__Tiho_neset_voda.mp3",function(v) {
});
obj.addSlider( "volume",50,0,100,1,function(v) {
if (snd) snd.setVolume( v/100.0 );
});
// a distance where it starts to decrese sound
obj.addSlider( "refdistance",20,0,110,1,function(v) {
if (snd) snd.setRefDistance( v );
});
obj.addText("coords","0,0,0",function() {
if (snd) {
snd.setCoords( obj.getParam("coords").split(/[\s,;]+/).map(parseFloat) );
}
});
obj.chain("remove", function() {
if (snd) {
snd.stop();
snd=undefined;
}
this.orig();
});
obj.play = function() {
if (obj.isplaying()) return;
if (starting) return;
starting = true;
var pos = obj.getParam("coords").split(/[\s,;]+/).map(parseFloat);
// console.log("Starting music at coords ",pos );
vz.vis.playSound3d( pos, obj.getParam("file"), function(snd1) {
starting=false;
snd = snd1;
snd.setVolume( obj.getParam("volume")/100.0 );
snd.setRefDistance( obj.getParam("refdistance") );
},
function(snd) {
snd=undefined;
starting=false;
});
}
var snd;
var starting;
/* не готово по состояниям
obj.pause = function() {
if (!obj.isplaying()) return;
if (snd) snd.pause();
}
*/
obj.stop = function() {
if (!obj.isplaying()) return;
if (snd) snd.stop();
snd = undefined;
starting = false;
}
obj.isplaying = function() {
if (starting) return true;
if (snd) return true;
return false;
}
obj.isstarting = function() {
return starting;
}
obj.trackParam("enabled",(v) => {
if (v)
obj.play();
else
obj.stop();
});
// разово попробовать запуститься на старте
obj.feature("timers");
obj.setTimeout( () => { obj.signalParam("enabled"); }, 50 );
return obj;
}
export function setup( vz ) {
vz.addItemType( "music_with_coords","Music (coords) (reka)", function( opts ) {
return create( vz, opts );
}, { cat: "music" } );
}