-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
111 lines (83 loc) · 2.29 KB
/
main.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
console.log("%c I see you there! ", "background:red; color:cyan");
//LIBRARY THINGIES
document.documentElement.addEventListener("mousedown", function () {
if (Tone.context.state !== 'running')
Tone.context.resume();
})
//SOUND
var ens = new Ensemble([
CreateClickSynth(),
CreateHiHatSynth(),
CreateKickSynth(),
CreateStringSynth(),
]);
//VISUALIZATION
var lastT = 0;
var totalT = 0;
var FPScap = 61;
function draw_loop(t = 0) {
let dt = (t - lastT) / 1000;
lastT = t;
totalT += dt;
if(totalT > 1 / FPScap){
totalT = 0;
draw(ens, Tone.Transport.progress);
}
requestAnimationFrame(draw_loop);
}
draw_loop();
//UI
const playbtn = document.getElementById("playbtn");
playbtn.addEventListener("click", () => {
Tone.Transport.toggle();
var paused = Tone.Transport.state == "stopped";
playbtn.innerHTML = paused ? "PLAY!" : "PAUSE";
});
const segment_list_elem = document.getElementById("segment-list");
function addSegment(canclose){
var seg_ens_id = null;
var [seg, dispose] = createSegment(
function(cache){
ens.updateSegment(seg_ens_id, segment=>{
segment.repeat = cache.repeat;
segment.ratios = cache.ratios;
segment.subdivide = cache.subdivide;
segment.looplength = cache.scale;
});
redraw_favicon();
},
canclose ? function(){ //close
dispose();
segment_list_elem.removeChild(seg);
ens.removeSegment(seg_ens_id);
} : false,
{
ratios:"1:2",
}
);
seg_ens_id = ens.addSegment([1,2])
segment_list_elem.appendChild(seg);
redraw_favicon();
}
function redraw_favicon(){
updateDynamicFavicon(ens.forEach(s=>s.ratios.join(":")).join("\n"));
}
{ // do we have a special url?
var ratios = (new URL(window.location.href)).searchParams.get('ratios')
if(ratios){
//ratios.split("s").map(v=>v.split("v"))
console.warn("special urls not implemented yet!");
}else{
addSegment(false);
}
}
document.getElementById("addbtn").addEventListener("click", e=>{
//var i = Number(document.getElementById("add-indexIN").value);
addSegment(true);
});
function changeBPM(bpm){
Tone.Transport.bpm.value = bpm;
}
document.getElementById("bpmIN").addEventListener("change", ()=>{
changeBPM( Number( document.getElementById("bpmIN").value ) );
})