-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximilian.v.0.1.js
171 lines (163 loc) · 5.8 KB
/
maximilian.v.0.1.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
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
161
162
163
164
165
166
167
168
169
170
171
var initAudioEngine = (origin = document.location.origin+"/libs")=>{
return new Promise((resolve, reject)=>{
//Dynamically load modules
import(origin + "/index.mjs").then((semaEngine)=>{
import(origin + "/ringbuf.js").then((RingBuffer)=>{
//Setup sema engine
const Engine = semaEngine.Engine
const Learner = semaEngine.Learner
var maxi = new Engine();
var inputBufferIds = []
//init Engine
maxi.init(origin).then(()=>{
let learner = new Learner();
maxi.addLearner('l1', learner);
maxi.addSample = (name, url)=> {
return maxi.loadSample(name + "----", url, true)
}
let dspCode = "";
//What happens is we do a dacOut on the play function
dspCode = {
setup: `() => {
let q=this.newq();
let createDSPLoop = ()=> {
return ()=>{return 0}
}
q.play = createDSPLoop();
return q;
}`,
loop: `(q, inputs, mem) => {
var sig = q.play(inputs);
if(Array.isArray(sig)) {
for(let i = 0; i < sig.length; i++) {
this.dacOut(sig[i], i);
}
} else {
this.dacOutAll(sig);
}
}`
}
maxi.addEventListener('onSharedBuffer', (e) => {
console.log("onSharedBuffer", e)
let ringbuf = new RingBuffer.default(e.sab, Float64Array);
maxi.sharedArrayBuffers[e.channelID] = {
sab: e.sab,
rb: ringbuf,
blocksize:e.blocksize
};
});
let pollRate = 10;
function sabPrinter() {
try {
//Only check if we need to (e.g. they have defined a callback)
if(maxi.onInput !== undefined) {
for (let v in maxi.sharedArrayBuffers) {
if(!inputBufferIds.includes(v)) {
let avail = maxi.sharedArrayBuffers[v].rb.available_read();
if ( avail > 0 && avail != maxi.sharedArrayBuffers[v].rb.capacity) {
for (let i = 0; i < avail; i += maxi.sharedArrayBuffers[v].blocksize) {
let elements = new Float64Array(maxi.sharedArrayBuffers[v].blocksize);
let val = maxi.sharedArrayBuffers[v].rb.pop(elements);
if(maxi.onInput) {
maxi.onInput(v,elements)
}
}
}
}
}
}
setTimeout(sabPrinter, pollRate);
} catch (error) {
setTimeout(sabPrinter, pollRate);
}
}
sabPrinter()
maxi.send = (id, data)=> {
if(maxi.sharedArrayBuffers[id] === undefined) {
inputBufferIds.push(id)
maxi.createSharedBuffer(id, "ML", data.length);
console.log(maxi.sharedArrayBuffers)
}
maxi.pushDataToSharedBuffer(id, data);
}
async function getCode(location) {
//Try script element
let scriptElement = document.getElementById(location)
if(scriptElement !== null)
{
return scriptElement.innerHTML
}
else
{
//Else try url
console.log("try url")
function isValidURL(url) {
try {
new URL(url);
return true;
} catch (error) {
return false;
}
}
if(isValidURL(location)) {
let response = await fetch(location);
if (response.ok) {
let text = await response.text();
console.log("executing http code")
return text
}
}
else {
//Else use string literal
console.log("executing literal code")
return location
}
}
}
maxi.setAudioCode = async (location,name)=>{
const executeCode = (name, userCode)=> {
console.log(name, typeof(userCode), userCode)
userCode = userCode.replace(/Maximilian/g, "Module");
//Mimic site adds in some stuff for consoles, causes errors, remove it
userCode = userCode.replace(/parent.postMessage\(\[\"console\".*\"\*\"\)/g, "");
dspCode = {}
if(name === undefined) {
dspCode.setup = `()=>{
let q = this.newq();
let createDSPLoop = ()=> {` +
userCode +
` return play;
}
q.play = createDSPLoop();
return q;
}`
dspCode.loop = `(q, inputs, mem) => {
var sig = q.play(inputs);
return sig
}`
} else {
dspCode.loop = {}
dspCode.loop[name] = userCode
}
setTimeout(()=>{
maxi.eval(dspCode)
},50);
}
let code = await getCode(location)
executeCode(name, code)
}
resolve(maxi)
})
})
})
})
}
// const regex = /(let|var|const)\s+play\s*=\s*\([^)]*\)\s*=>\s*{([^}]*)}/g;
// let code = await getCode(location)
// let playFunction = code.match(regex)[0]
// console.log("playFunction",playFunction)
// let otherCode = code.replace(regex,"")
// maxi.setAudioCode("setup", otherCode)
// setTimeout(()=>{
// maxi.setAudioCode("signal", playFunction)
// },100);